"*****************************************************************************
"" Functions
"*****************************************************************************

function! GetBufferList()
    redir =>buflist
    silent! ls!
    redir END
    return buflist
endfunction

function! ToggleList(bufname, pfx)
    let buflist = GetBufferList()
    for bufnum in map(filter(split(buflist, '\n'), 'v:val =~ "'.a:bufname.'"'), 'str2nr(matchstr(v:val, "\\d\\+"))')
        if bufwinnr(bufnum) != -1
            exec(a:pfx.'close')
            return
        endif
    endfor
    if a:pfx == 'l' && len(getloclist(0)) == 0
        echohl ErrorMsg
        echo "Location List is Empty."
        return
    endif
    let winnr = winnr()
    exec(a:pfx.'open')
    if winnr() != winnr
        wincmd p
    endif
endfunction


"*****************************************************************************
"" Basic Setup
"*****************************************************************************"
" General
let no_buffers_menu=1
syntax on
set ruler
set number
set mousemodel=popup
set t_Co=256
set guioptions=egmrti
set gfn=Monospace\ 10

" TODO: Testing if this works against automatically setting paste mode
" Issue: https://github.com/neovim/neovim/issues/7994
au InsertLeave * set nopaste


set undofile
"maximum number of changes that can be undone
set undolevels=1000000
"maximum number lines to save for undo on a buffer reload
set undoreload=10000000

set backupdir=~/.vim/backup
set directory=~/.vim/tmp
set undodir  =~/.vim/undo

" create Backup/tmp/undo dirs
set backupdir=~/.vim/backup
set directory=~/.vim/tmp

function! InitBackupDir()
  let l:parent = $HOME    . '/.vim/'
  let l:backup = l:parent . 'backup/'
  let l:tmpdir = l:parent . 'tmp/'
  let l:undodir= l:parent . 'undo/'


  if !isdirectory(l:parent)
    call mkdir(l:parent)
  endif
  if !isdirectory(l:backup)
    call mkdir(l:backup)
  endif
  if !isdirectory(l:tmpdir)
    call mkdir(l:tmpdir)
  endif
  if !isdirectory(l:undodir)
    call mkdir(l:undodir)
  endif
endfunction
call InitBackupDir()

augroup Binary
  " edit binaries in xxd-output, xxd is part of vim
  au!
  au BufReadPre  *.bin let &bin=1
  au BufReadPost *.bin if &bin | %!xxd
  au BufReadPost *.bin set ft=xxd | endif
  au BufWritePre *.bin if &bin | %!xxd -r
  au BufWritePre *.bin endif
  au BufWritePost *.bin if &bin | %!xxd
  au BufWritePost *.bin set nomod | endif
augroup END

" Encoding
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=utf-8
set bomb
set binary

" Fix backspace indent
set backspace=indent,eol,start

" Tabs. May be overriten by autocmd rules
set tabstop=4
set softtabstop=0
set shiftwidth=4
set expandtab

" Map leader to ,
let mapleader=','

" Required for operations modifying multiple buffers like rename.
set hidden

" Searching
set hlsearch
set incsearch
set ignorecase
set smartcase

" Directories for swp files
set nobackup
set noswapfile

set fileformats=unix,dos,mac

" File overview
set wildmode=list:longest,list:full
set wildignore+=*.o,*.obj,.git,*.rbc,*.pyc,__pycache__

" Shell to emulate
if exists('$SHELL')
    set shell=$SHELL
else
    set shell=/bin/bash
endif

" Set color scheme
colorscheme molokai

"Show always Status bar
set laststatus=2

" Use modeline overrides
set modeline
set modelines=10

" Set terminal title
set title
set titleold="Terminal"
set titlestring=%F

" search will center on the line it's found in.
nnoremap n nzzzv
nnoremap N Nzzzv



"*****************************************************************************
"" Abbreviations
"*****************************************************************************
" no one is really happy until you have this shortcuts
cnoreabbrev W! w!
cnoreabbrev Q! q!
cnoreabbrev Qall! qall!
cnoreabbrev Wq wq
cnoreabbrev Wa wa
cnoreabbrev wQ wq
cnoreabbrev WQ wq
cnoreabbrev W w
cnoreabbrev Q q
cnoreabbrev Qall qall

" NERDTree configuration
let g:NERDTreeChDirMode=2
let g:NERDTreeIgnore=['\.rbc$', '\~$', '\.pyc$', '\.db$', '\.sqlite$', '__pycache__']
let g:NERDTreeSortOrder=['^__\.py$', '\/$', '*', '\.swp$', '\.bak$', '\~$']
let g:NERDTreeShowBookmarks=1
let g:nerdtree_tabs_focus_on_files=1
let g:NERDTreeMapOpenInTabSilent = '<RightMouse>'
let g:NERDTreeWinSize = 50
set wildignore+=*/tmp/*,*.so,*.swp,*.zip,*.pyc,*.db,*.sqlite
nnoremap <silent> <F1> :NERDTreeFind<CR>
nnoremap <silent> <F2> :NERDTreeToggle<CR>


" open terminal emulation
nnoremap <silent> <leader>sh :terminal<CR>:startinsert<CR>

"*****************************************************************************
"" Autocmd Rules
"*****************************************************************************
"" The PC is fast enough, do syntax highlight syncing from start unless 200 lines
augroup vimrc-sync-fromstart
    autocmd!
    autocmd BufEnter * :syntax sync maxlines=200
augroup END

" Nasm filetype
augroup nasm
    autocmd!
    autocmd BufRead,BufNewFile *.nasm set ft=nasm
augroup END

" Binary filetype
augroup Binary
    au!
    au BufReadPre  *.bin,*.exe,*.elf let &bin=1
    au BufReadPost *.bin,*.exe,*.elf if &bin | %!xxd
    au BufReadPost *.bin,*.exe,*.elf set ft=xxd | endif
    au BufWritePre *.bin,*.exe,*.elf if &bin | %!xxd -r
    au BufWritePre *.bin,*.exe,*.elf endif
    au BufWritePost *.bin,*.exe,*.elf if &bin | %!xxd
    au BufWritePost *.bin,*.exe,*.elf set nomod | endif
augroup END

" Binary filetype
augroup fasm
    au!
    au BufReadPost *.fasm set ft=fasm
augroup END

augroup deoplete-update
    autocmd!
    autocmd VimEnter * UpdateRemotePlugin
augroup END



"" Remember cursor position
augroup vimrc-remember-cursor-position
    autocmd!
    autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
augroup END

"" txt
" augroup vimrc-wrapping
"   autocmd!
"   autocmd BufRead,BufNewFile *.txt call s:setupWrapping()
" augroup END

"" make/cmake
augroup vimrc-make-cmake
    autocmd!
    autocmd FileType make setlocal noexpandtab
    autocmd BufNewFile,BufRead CMakeLists.txt setlocal filetype=cmake
augroup END

set autoread

"*****************************************************************************
"" Mappings
"*****************************************************************************

" Split
noremap <Leader>h :<C-u>split<CR>
noremap <Leader>v :<C-u>vsplit<CR>

" Git
noremap <Leader>ga :Gwrite<CR>
noremap <Leader>gc :Gcommit<CR>
noremap <Leader>gsh :Gpush<CR>
noremap <Leader>gll :Gpull<CR>
noremap <Leader>gs :Gstatus<CR>
noremap <Leader>gb :Gblame<CR>
noremap <Leader>gd :Gvdiff<CR>
noremap <Leader>gr :Gremove<CR>

" Tabs
nnoremap <Tab> gt
nnoremap <S-Tab> gT
nnoremap <silent> <S-t> :tabnew<CR>

" Set working directory
nnoremap <leader>. :lcd %:p:h<CR>

" Opens an edit command with the path of the currently edited file filled in
noremap <Leader>e :e <C-R>=expand("%:p:h") . "/" <CR>

" Opens a tab edit command with the path of the currently edited file filled
noremap <Leader>te :tabe <C-R>=expand("%:p:h") . "/" <CR>

" Tagbar
nmap <silent> <F3> :TagbarToggle<CR>
let g:tagbar_autofocus = 1

" Copy/Paste/Cut
set clipboard^=unnamed,unnamedplus

noremap YY "+y<CR>
noremap <leader>p "+gP<CR>
noremap XX "+x<CR>

" Enable mouse for vim
set mouse=a

" Buffer nav
noremap <leader>z :bp<CR>
noremap <leader>q :bp<CR>
noremap <leader>x :bn<CR>
noremap <leader>w :bn<CR>

" Close buffer
noremap <leader>c :bd<CR>

" Clean search (highlight)
nnoremap <silent> <leader><space> :noh<cr>

" Switching windows
noremap <C-j> <C-w>j
noremap <C-k> <C-w>k
noremap <C-l> <C-w>l
noremap <C-h> <C-w>h

" Vmap for maintain Visual Mode after shifting > and <
vmap < <gv
vmap > >gv

" Move visual block
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv

" Open current line on GitHub
nnoremap <Leader>o :.Gbrowse<CR>


" Save on strg+s if not in paste mode
nmap <c-s> :w<CR>
vmap <c-s> <Esc><c-s>gv
imap <c-s> <Esc><c-s>

" Quit on strg+q in normal mode
nnoremap <c-q> :q<cr>

" Strg+d to replace word under cursor
nnoremap <c-d> :%s/\<<C-r><C-w>\>//g<Left><Left>

" Strg+f ro find word under cursor
nnoremap <c-f> :/<C-r><C-w><Left><Left>

" Remove unneccessary spaces
nnoremap <silent> <F8> :let _s=@/ <Bar> :%s/\s\+$//e <Bar> :let @/=_s <Bar> :nohl <Bar> :unlet _s <CR>

" undotree
nnoremap <F5> :UndotreeToggle<CR>

" Reindent whole file with F6
map <F6> mzgg=G`z

nnoremap <F9> :set invpaste paste?<CR>
set pastetoggle=<F2>
set showmode

" save on focus lost
au FocusLost * :wa

" Toggle location list
nmap <silent> <F4> :call ToggleList("Quickfix List", 'c')<CR>

" Replacing text in visual mode doesn't copy it anymore
xmap p <Plug>ReplaceWithRegisterVisual
xmap <MiddleMouse> <Plug>ReplaceWithRegisterVisual

"" Opens an edit command with the path of the currently edited file filled in
noremap <Leader>e :e <C-R>=expand("%:p:h") . "/" <CR>

" Use tab for navigatin in autocompletion window
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"

" ALE mappings
nmap <Leader>i <Plug>(ale_hover)
nmap <Leader>d <Plug>(ale_go_to_definition_in_tab)
nmap <Leader>rf <Plug>(ale_find_references)
nmap <silent><F7> <Plug>(ale_fix)

"*****************************************************************************
"" Plugin settings
"*****************************************************************************

" vim-airline
set statusline+=%{fugitive#statusline()}
let g:airline_theme = 'powerlineish'
let g:airline#extensions#syntastic#enabled = 1
let g:airline#extensions#branch#enabled = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tagbar#enabled = 1
let g:airline_skip_empty_sections = 1
let g:airline#extensions#ale#enabled = 1

" show indent lines
let g:indent_guides_enable_on_vim_startup = 1
let g:indent_guides_auto_colors = 0
hi IndentGuidesOdd ctermbg=235
hi IndentGuidesEven ctermbg=235
let g:indent_guides_guide_size = 1
let g:indent_guides_start_level = 2

" Enable autocompletion
let g:deoplete#enable_at_startup = 1
set completeopt=noinsert,menuone,noselect
let g:deoplete#sources = {}
let g:deoplete#sources._ = ['ale', 'file', 'omni', 'buffer']

" Ale no preview on hover
let g:ale_close_preview_on_insert = 0
let g:ale_cursor_detail = 0

" Ale skip if file size over 2G
let g:ale_maximum_file_size = "2147483648"
let g:ale_set_quickfix = 1

" Ale language server
let g:ale_linters = {
            \ 'python': ['pyls'],
            \ 'cpp': ['ccls'],
            \ 'c': ['gcc'],
            \ 'xml': ['xmllint'],
            \ 'rust': ['cargo'],
            \ 'go': ['gofmt'],
            \ }

" ALE fixers
let g:ale_fixers = { '*': ['remove_trailing_lines', 'trim_whitespace'] }
let g:ale_fixers.python = ['black']
let g:ale_fixers.go = ['gofmt']
let g:ale_fixers.c = ['clang-format']
let g:ale_fixers.cpp = ['clang-format']
let g:ale_fixers.json = ['jq']
let g:ale_fixers.xml = ['xmllint']

let g:ale_completion_enabled = 1
let g:ale_sign_error = '⤫'
let g:ale_sign_warning = '⚠'
let g:ale_lint_on_insert_leave = 1

"*****************************************************************************
"" Shortcuts overview
"*****************************************************************************
" Shortcuts overview
" F1  --> Filetree find
" F2  --> Filetree toggle
" F3  --> Function overview
" F4  --> Toggle error bar

" F5  --> undotree
" F6  --> Reindent whole file
" F7  --> Format and lint file
" F8  --> Remove trailing whitespaces
" F9  --> toggle paste
" ,i  --> Information about function
" ,d  --> Jump to definition
" ,r  --> Rename in all occurences
" ,rf --> Find references of function/variable
" ,e  --> Change current file
" ,te --> Open file in new tab
" u   --> Undo
" strg+f --> Find current selected word
" strg+d --> Replace current selected word
" strg+s --> Save file
" strg+q --> Close current file
" space+, --> Stop highlighting words after search