"------------------------------------------------------------------------------
" File .vimrc
" jul.io
"------------------------------------------------------------------------------
"
"set nocompatible	      " use vim defaults
syntax on               " use syntax highlighting
set background=dark     " when using black background terminal
" colorscheme ron					" for bash in windows this fixes my colors in vim inside tmux

set nomodeline					" workaround to close a vuln

set number              " line numbers
set relativenumber			" relative line numbers! How cool is that?!

set clipboard=unnamed
set backspace=indent,eol,start	" more powerful backspacing
set autoindent		      " always set autoindenting on
set formatoptions+=t

set nobackup		        " Don't keep a backup file
set viminfo='20,\"50	  " read/write a .viminfo file, don't store more than
			                  " 50 lines of registers
set history=200		      " keep many lines of command line history
set showcmd		          " Show (partial) command in status line.
set showmatch		        " Show matching brackets.
set ignorecase		      " Do case insensitive matching
set smartcase						" When searching with Uppercase Letters be Case-Sensitive, otherwise don't be
set incsearch		        " Incremental search
set hlsearch            " Search highlight
hi Search ctermbg=LightGrey " change color for search hints

set noexpandtab

set tabstop=2
set sw=2
set so=7								"set 7 lines to the cursor when moving vertically with j/k
set foldcolumn=1				"add a bit of extra margin to the left

set smarttab
set smartindent

set enc=utf-8           " default UTF8 encoding
set fileencoding=utf-8  " default UTF8 encoding
set ruler								" show the cursor position all the time

set directory=~/.vim/swap/

set lazyredraw "to speed up vim a bit
set regexpengine=1

" show current file with complete file in statusline
set laststatus=2
set statusline+=%F

filetype plugin on
filetype indent on
filetype on

" With a map leader it's possible to do extra key combinations
" like <leader>w saves the current file
" my mapleader is <spacebar> for easy access with both hands
let mapleader=" "
let g:mapleader=" "

hi LineTooLong cterm=bold ctermbg=red guibg=LightYellow
match LineTooLong /\%>160v.\+/

"useful mappings for managing tabs
map <leader>tn :tabnew<cr>
map <leader>to :tabonly<cr>
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove
map <leader>t<leader> :tabnext<cr>

" Delete trailing white space on save, useful for some filetypes ;)
fun! CleanExtraSpaces()
    let save_cursor = getpos(".")
    let old_query = getreg('/')
    silent! %s/\s\+$//e
    call setpos('.', save_cursor)
    call setreg('/', old_query)
endfun

" grep will sometimes skip displaying the file name
set grepprg=grep\ -nH\ $*

let g:Tex_Folding=0 "I don't like folding.
set iskeyword+=:

" Press Space to turn off highlighting and clear any message already displayed.
nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>

" Incrementing selected numbers (works, when numbers are in a column)
function! Incr()
  let a = line('.') - line("'<")
  let c = virtcol("'<")
  if a > 0
    execute 'normal! '.c.'|'.a."\<C-a>"
  endif
  normal `<
endfunction
vnoremap <C-a> :call Incr()<CR>

highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$\| \+\ze\t/

nnoremap    <F2> :<C-U>%retab!<CR>:set list<CR>:%s/^\(\t\+\) */\1/<CR>

" Press F5 to insert datetime string from normal- and insert mode
nnoremap <F5> "=strftime("%c")<CR>P
inoremap <F5> <C-R>=strftime("%c")<CR>

" YAML and Ansible and so on
augroup ansible_vim_fthosts
  autocmd!
  autocmd BufNewFile,BufRead hosts setfiletype yaml.ansible
augroup END

" as long as vim isn't in diff-mode check the code at opening and closing
" also: special vimdiff-settings
if ! &diff
	let g:syntastic_always_populate_loc_list = 1
	let g:syntastic_auto_loc_list = 1
	let g:syntastic_check_on_open = 1
	let g:syntastic_check_on_wq = 1

  nnoremap <leader>j <C-J>
  nnoremap <leader>k <C-K>
  nnoremap <leader>h <C-H>
  nnoremap <leader>l <C-L>
endif

" NerdTree Settings
nnoremap <leader>n :NERDTreeToggle<CR>
nnoremap <leader>nf :NERDTreeFocus<CR>
nnoremap <leader>nc :NERDTreeClose<CR>

" block-editing workaround for a windows-terminal-wsl situation
nnoremap <Leader>v <c-v>

"tweaking my statusline
function! GitBranch()
	  return system("git rev-parse --abbrev-ref HEAD 2>/dev/null | tr -d '\n'")
	endfunction

function! StatuslineGit()
	  let l:branchname = GitBranch()
		  return strlen(l:branchname) > 0?'  '.l:branchname.' ':''
endfunction

set laststatus=2 "always show statusline

set statusline=
set statusline+=%#PmenuSel#
set statusline+=%{StatuslineGit()}
set statusline+=%#LineNr#
set statusline+=\ %f
set statusline+=%m\
set statusline+=%=
set statusline+=%#CursorColumn#
set statusline+=\ %y
set statusline+=\ %{&fileencoding?&fileencoding:&encoding}
set statusline+=\[%{&fileformat}\]
set statusline+=\ %p%%
set statusline+=\ %l:%c
set statusline+=\

call plug#begin('~/.vim/plugged')

" --- Your Plugin List Goes Here ---
" Example:
Plug 'tpope/vim-surround'
Plug 'preservim/nerdtree'
Plug 'pearofducks/ansible-vim'

" Automatically install plugins if they are not installed
" Call plug#config_commit() is the modern way to replace auto_install, save_state, etc.
" call plug#config_commit()

" Optional: For automatic plugin updates on startup if you want
" autocmd VimEnter *
"   \ PlugUpgrade |
"   \ execute PlugInstall |
"   \ PlugClean

" Initialize plugin system, then load plugins
call plug#end()

