" XDG Specifications
let rtp=&runtimepath
set runtimepath=~/.config/vim
let &runtimepath.=','.rtp.',~/.config/vim/after'
set directory=~/.config/vim/swap//,.,~/tmp,/var/tmp,/tmp
set backupdir=~/.config/vim/backup//,.,~/tmp,~/
set spellfile=~/.config/vim/spell/en.utf-8.add
set viminfo+=n~/.config/vim/viminfo
set viewdir=~/.config/vim/view/
set undodir=~/.config/vim/undo//,.

" Preferences
set clipboard=unnamedplus
set shellcmdflag=-c
set encoding=utf-8
set relativenumber
filetype plugin on
set shiftwidth=4
set nocompatible
set cursorline
set ignorecase
set smartcase
set expandtab
set tabstop=4
set autoread
set wildmenu
set hlsearch
set swapfile
set undofile
set mouse=a
set showcmd
set number
syntax on

if (empty($TMUX) && getenv('TERM_PROGRAM') != 'Apple_Terminal')
    if (has("nvim"))
        let $NVIM_TUI_ENABLE_TRUE_COLOR=1
    endif
    if (has("termguicolors"))
        set termguicolors
    endif
endif

" Keybinds
noremap <F5> :setlocal spell! spelllang=en<CR>
noremap <F6> :setlocal spell! spelllang=es<CR>
noremap <C-e> !!$SHELL<CR>
noremap <tab> :bn<CR>
noremap <C-c> :bd<CR>
noremap <C-s> :w<CR>
noremap <A-h> 5<C-w><
noremap <A-j> 5<C-w>-
noremap <A-k> 5<C-w>+
noremap <A-l> 5<C-w>>
noremap H :nohl<CR>
noremap <Space> za
noremap N /@@@<CR>

" groff
autocmd FileType groff noremap <buffer> <silent> <F9> :!zathura --fork %:r.pdf<CR> & disown
autocmd FileType groff noremap <buffer> <C-s> :w<CR> :!groffc %<CR><CR>
au BufNewFile,BufRead *.ms set filetype=groff

" Python
autocmd FileType python noremap <buffer> <F9> :!python %<CR>

" Functions

"Open a shell on a vertical split
set splitright
function OpenTerminal()
    execute "normal \<C-l>"
    execute "normal \<C-l>"
    execute "normal \<C-l>"
    execute "normal \<C-l>"

    let bufNum = bufnr("%")
    let bufType = getbufvar(bufNum, "&buftype", "not found")

    if bufType == "terminal"
        execute "q"
    else
        execute "bot sp term://bash"
        execute "resize -8"
        execute "set nonu"
        execute "set nornu"
        silent au BufLeave <buffer> stopinsert!
        silent au BufWinEnter,WinEnter <buffer> startinsert!
        execute "tnoremap <buffer> <C-h> <C-\\><C-n><C-w><C-h>"
        execute "tnoremap <buffer> <C-t> <C-\\><C-n>:q<CR>"
        execute "tnoremap <buffer> <C-\\><C-\\> <C-\\><C-n>"
        execute "IndentLinesDisable"

        startinsert!
    endif
endfunction
nnoremap <C-t> :call OpenTerminal()<CR>

" Custom commands
command SourceVimrc :source ~/.config/vim/vimrc

" Plugins
if empty(glob('~/.config/vim/autoload/plug.vim'))
    silent !curl -fLo ~/.config/vim/autoload/plug.vim --create-dirs
                \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
    autocmd VimEnter * PlugInstall --sync | source '~/.config/vim/vimrc'
endif

call plug#begin()
Plug 'instant-markdown/vim-instant-markdown'
Plug 'vim-airline/vim-airline-themes'
Plug 'norcalli/nvim-colorizer.lua'
Plug 'vim-scripts/AutoComplPop'
Plug 'vim-airline/vim-airline'
Plug 'jiangmiao/auto-pairs'
Plug 'tpope/vim-commentary'
Plug 'Yggdroot/indentLine'
Plug 'preservim/nerdtree'
Plug 'mhinz/vim-signify'
Plug 'ibhagwan/fzf-lua'
Plug 'morhetz/gruvbox'
call plug#end()

" Gruvbox
let g:gruvbox_contrast_dark='hard'
let g:gruvbox_italic=1
set background=dark
colorscheme gruvbox

" AutoComplPop
inoremap <expr> <Tab> pumvisible() ? "<C-y>" : "<Tab>"
set completeopt=menuone,longest
set complete+=kspell

" Airline
let g:airline#extensions#tabline#buffer_min_count = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline_powerline_fonts = 1
set ttimeout ttimeoutlen=0
set laststatus=2
set noshowmode

" NERDTree
nnoremap <C-f> :NERDTreeToggle<CR>

" IndentLine
let g:indentLine_enabled = 1
let g:indentLine_char = '▏'

" fzf-lua
noremap <A-b> :FzfLua git_branches<CR>
noremap <A-c> :FzfLua git_commits<CR>
noremap <A-s> :FzfLua git_status<CR>
noremap <A-f> :FzfLua files<CR>
noremap gb :FzfLua git_branches<CR>
noremap gs :FzfLua git_status<CR>
noremap gf :FzfLua files<CR>

" Signify
let g:signify_disable_by_default = 1
highlight SignColumn ctermbg=NONE
noremap gd :SignifyToggle<CR>

" Instant Markdown
let g:instant_markdown_autostart = 0
autocmd FileType markdown noremap <buffer> <silent> <F9> :InstantMarkdownPreview<CR>
let g:instant_markdown_browser = "firefox --new-window"
let g:instant_markdown_mathjax = 1
let g:instant_markdown_mermaid = 1