let $FAVHOME="/home/root/favconf"
let $MYVIMRC="/home/root/.vimrc"
let $COLORFILE="/usr/share/vim/darktooth.vim"
let $ZSHFILE="/home/root/.bashrc"
let $GITCONFIG="/home/root/.gitconfig"
nnoremap <leader>v :tabe $MYVIMRC<CR>
nnoremap <leader>z :tabe $ZSHFILE<CR>
nnoremap <leader>g :tabe $GITCONFIG<CR>
nnoremap <leader>a :tabe $FAVHOME/ag/agignore"<CR>
nnoremap <leader>x :tabe $FAVHOME/xbk/xbindkeysrc"<CR>

"" Fast vimrc update
augroup vimrc" {
    autocmd!
    autocmd BufWritePost $MYVIMRC source $MYVIMRC
    " autocmd BufWritePost $MYVIMRC source $COLORFILE
augroup END " }

" ============================================= PLUGINS

silent! call plug#begin('~/.vim/bundle')


"" General
Plug 'drmikehenry/vim-fixkey'

"" Text & movement
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-unimpaired'
Plug 'tomtom/tcomment_vim'
Plug 'terryma/vim-expand-region'
Plug 'bkad/CamelCaseMotion'
Plug 'junegunn/vim-easy-align', { 'on': ['<Plug>(EasyAlign)', 'EasyAlign'] }
Plug 'junegunn/vim-peekaboo'


"" Buffers
" Plug 'derekwyatt/vim-fswitch'
" Plug 'majutsushi/tagbar' ", { 'on': 'TagbarToggle' }
Plug 'mkitt/tabline.vim'
Plug 'Valloric/ListToggle'
Plug 'justinmk/vim-dirvish'


"" Formats & colors
" Plug 'davidhalter/jedi-vim'
" Plug 'bronson/vim-trailing-whitespace'
" Plug 'justinmk/vim-syntax-extra'
" Plug 'peterhoeg/vim-qml'
" Plug 'artoj/qmake-syntax-vim'
Plug 'vim-scripts/AnsiEsc.vim'

call plug#end()

" ============================================= PLUGIN CONFIGURATION

"" T-comment
map -- gcc<Esc>
" let g:tcommentMapLeaderOp2
" let g:tcommentMapLeaderOp2
au BufRead,BufNewFile, *.pro   set filetype=qmake
au BufRead,BufNewFile, *.pri   set filetype=qmake
call tcomment#DefineType('qmake','# %s')


"" vim-expand-region
vmap v <Plug>(expand_region_expand)
vmap <C-v> <Plug>(expand_region_shrink)


"" CamelCaseMotion
" map <silent> W <Plug>CamelCaseMotion_w
map <silent> <M-b> <Plug>CamelCaseMotion_b
map <silent> <M-e> <Plug>CamelCaseMotion_e
imap <silent> <M-Left> <C-o><Plug>CamelCaseMotion_b
imap <silent> <M-Right> <C-o><Plug>CamelCaseMotion_e
"map <silent> ge <Plug>CamelCaseMotion_ge


"" FSwitch
noremap <F2> :FSHere<CR>
noremap <S-F2> :FSSplitLeft<CR>

"" TagBar
" map <F3> :TagbarToggle<CR>
" autocmd filetype cpp nested TagbarOpen


"" Dirvish
map <F4> <Plug>(dirvish_up)
map <F6> :let $VIM_DIR=expand('%:p:h')<CR>:terminal<CR>cd $VIM_DIR; reset<CR>
autocmd FileType dirvish
        \  nnoremap <silent><buffer> <CR> gf
        \ |xnoremap <silent><buffer> <CR> gf

"" Easy Align
" Start interactive EasyAlign in visual mode (e.g. vipga)
xmap ga <Plug>(EasyAlign)
" Start interactive EasyAlign for a motion/text object (e.g. gaip)
nmap ga <Plug>(EasyAlign)

"=============================================  GENERAL


"" Tabbing and indentation
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
filetype plugin indent on


"" Windows & buffers
set autowrite   " Automatically save before commands like :next and :make
set hidden          " Hide buffers when they are abandoned
set sessionoptions=folds,tabpages,winsize "no buffers
set nosplitright
set splitbelow
nnoremap <c-t> :tabe<CR>
nnoremap <leader>d :tabe .<CR>
nnoremap <leader>c :tabe ~/favconf<CR>
" Jump to the last position when reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
autocmd FileType help wincmd H
autocmd BufWinEnter wincmd H
nnoremap <C-M> <C-W>o
nmap <C-C> <C-W>c
nmap <C-N> :vnew<CR>


"" Folding
set foldmethod=syntax
set foldnestmax=20
set foldlevelstart=99
hi Folded ctermbg=none


"" Quickfix
noremap <F5> :AsyncStop<CR>:AsyncRun make run<CR>
noremap <F7> :!pkill python<CR>:YcmRestartServer<CR>
noremap <F8> :wa<CR>:AsyncStop<CR>:AsyncRun qmake -r; make notify<CR>:copen<CR>G<C-w><C-w>
" autocmd QuickfixCmdPost make splint %
autocmd QuickFixCmdPost    l* nested lwindow "
autocmd QuickFixCmdPost [^l]* nested cwindow " open quikfix with errors

augroup QuickfixWindow
    autocmd!
    autocmd FileType qf wincmd J
    au!
    au WinEnter * if winnr('$') == 1 && getbufvar(winbufnr(winnr()), "&buftype") == "quickfix"|q|endif
augroup end
noremap [s :colder<CR>
noremap ]s :cnewer<CR>:QToggle<CR>

"" Clipboard
set clipboard=autoselect "  clipboard
set mouse=a   " Enable mouse usage (all modes)
noremap <Delete> "_d<Right>
inoremap <Backspace> <Left><Delete>
" annihilating
noremap xx "_dd
noremap x "_d
noremap X "_D
" for multiple replaces
vnoremap p p:let @"=@0 <CR>
vnoremap P P:let @"=@0 <CR>
noremap <leader>p :!echo <C-r>*
nnoremap <M-p> "*P


"" Disable Replace mode by second <Insert> or jj
inoremap <Insert> <Esc><Right>
inoremap jj <Esc><Right>


" Unset paste on InsertLeave
au InsertLeave * silent! set nopaste
set pastetoggle=<F9>


"" Line breaks
set nowrap
set textwidth=0
set wrapmargin=0
set formatoptions=cq "t

" Trailing-whitespace
hi ExtraWhitespace ctermbg=DarkGrey
" autocmd FileType c,cpp,java,php,ruby,python autocmd BufWritePre <buffer> :FixWhitespace

"" Colors
" syntax on
set hlsearch " Highlight searched word
set nocursorline " Highlight current line
let base16colorspace=256 " Access colors present in 256 colorspace
set t_Co=256 " 256 color mode
set background=dark
colorscheme pablo


"" visual-at.vim
xnoremap @ :<C-u>call ExecuteMacroOverVisualRange()<CR>
function! ExecuteMacroOverVisualRange()
  echo "@".getcmdline()
  execute ":'<,'>normal @".nr2char(getchar())
endfunction


inoremap \fn <C-R>=@%<CR>
noremap  <leader>f :let @*=@%<CR>
inoremap \fh #include "<C-R>=expand("%:t:r").".h"<CR>"


"" Searching
set  ignorecase   " Do smart case matching
set  smartcase    " Do smart case matching
vnoremap // y/<C-R>"<CR><C-o>     " Search for visual selection
"map ** *:%s///gn<CR>2<C-o>
autocmd BufReadPost quickfix nnoremap <buffer> <CR> :.cc<CR><C-W><C-W>
" search in files word under cursor uses fugitive, vv and **
nmap <M-f> :cclose<CR>viwy**:Ag <c-r>"<CR>
vmap <M-f> y:cclose<CR>:Ag <c-r>"<CR>
nmap <c-f> :cclose<CR>viwy**<c-g><c-r>"<CR><leader>q<leader>q
vmap <c-f> y:cclose<CR><c-g>'<c-r>"'<CR><leader>q<leader>q
" nnoremap cgn cgn<c-r>"<c-left>


" Cursor
let &t_SI = "\<Esc>[6 q"
" let &t_SR = "\<Esc>[4 q"
let &t_EI = "\<Esc>[2 q"


" copy line N to cursor position
nnoremap gp ggyy<c-o>pkJa<CR><Esc>


" for jumps
set path+=$PWD/**
set tags=./tags;/


nnoremap <C-j> <PageDown>
nnoremap <C-k> <PageUp>

" vnoremap <S-Up> <Up>
" vnoremap <S-Down> <Down>

"" Stop that stupid window from popping up
noremap q: :q
command! W w

"" Stop unwanted scrolling during selection
vnoremap <S-up> <up>
vnoremap <S-down> <down>


"" Allow saving of files as sudo when I forgot to start vim using sudo.
cmap ww w !sudo tee > /dev/null %


"" Keep selection after indentation move
vnoremap < <gv
vnoremap > >gv


"" Essential
set nocompatible " be iMproved
set number " Show line numbers
set noswapfile
set encoding=utf-8
set laststatus=2
set history=1024
set viminfo='256
set virtualedit=block
set lazyredraw
set showtabline=2

"" tab-navigation
nnoremap th  :tabfirst<CR>
nnoremap tk  :tabnext<CR>
nnoremap tj  :tabprev<CR>
nnoremap tl  :tablast<CR>
nnoremap tt  :tabedit<Space>
nnoremap tn  :tabnext<Space>
nnoremap tm  :tabm<Space>
nnoremap td  :tabclose<CR>
nnoremap t1 1gt
nnoremap t2 2gt
nnoremap t3 3gt
nnoremap t4 4gt
nnoremap t5 5gt
nnoremap t6 6gt
nnoremap t7 7gt
nnoremap t8 8gt

nnoremap t- :-tabmove<CR>
nnoremap t= :+tabmove<CR>

" <tab> / <s-tab> | Circular windows navigation
" nnoremap <tab>   <c-w>w
nnoremap <S-tab> <c-w>W

" Last inserted text
nnoremap g. :normal! `[v`]<cr><left>

" Make Y behave like other capitals
nnoremap Y y$

" qq to record, Q to replay
nnoremap Q @q

" ----------------------------------------------------------------------------
" Move the current line, or a selected block of lines.
" ----------------------------------------------------------------------------
nnoremap <C-S-J> :m .+1<CR>==
nnoremap <C-S-K> :m .-2<CR>==
inoremap <C-S-J> <Esc>:m .+1<CR>==gi
inoremap <C-S-K> <Esc>:m .-2<CR>==gi
vnoremap <C-S-J> :m '>+1<CR>gv=gv
vnoremap <C-S-K> :m '<-2<CR>gv=gv

" ----------------------------------------------------------------------------
" Open FILENAME:LINE:COL
" ----------------------------------------------------------------------------
function! s:goto_line()
  let tokens = split(expand('%'), ':')
  if len(tokens) <= 1 || !filereadable(tokens[0])
    return
  endif

  let file = tokens[0]
  let rest = map(tokens[1:], 'str2nr(v:val)')
  let line = get(rest, 0, 1)
  let col  = get(rest, 1, 1)
  bd!
  silent execute 'e' file
  execute printf('normal! %dG%d|', line, col)
endfunction

autocmd vimrc BufNewFile * nested call s:goto_line()


silent! call repeat#set("\<Plug>MyWonderfulMap", v:count)

