"
"-----------------------------------------------------------------------
" Vim settings file for Jinks
"
" Most recent update: Wed 8 Feb 02:09:07 CET 2017
"
"-----------------------------------------------------------------------

scriptencoding utf-8
set nocompatible                 " Don't be compatible with vi
if ((&term =~ "xterm") || (&term =~ "rxvt")) && (&termencoding == "")
    set termencoding=utf-8
endif


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

call plug#begin('~/.config/nvim/bundle')

Plug 'tpope/vim-fugitive'
Plug 'cohama/lexima.vim' " Auto-Close parens and quotes
"Plug 'jiangmiao/auto-pairs' " same, but no update since 2019
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'scrooloose/nerdcommenter'
Plug 'majutsushi/tagbar'
Plug 'lervag/vimtex'
Plug 'autozimu/LanguageClient-neovim', { 'branch': 'next', 'do': 'bash install.sh', }
Plug 'ncm2/ncm2'
Plug 'ncm2/ncm2-bufword'
Plug 'ncm2/ncm2-path'
Plug 'ncm2/ncm2-vim' | Plug 'Shougo/neco-vim'
Plug 'ncm2/ncm2-go'
Plug 'roxma/nvim-yarp'
Plug 'junegunn/vim-easy-align'
Plug 'junegunn/fzf'
Plug 'ciaranm/inkpot'
Plug 'airblade/vim-rooter'
Plug 'mbbill/undotree'
"Plug 'will133/vim-dirdiff'
Plug 'ZSaberLv0/ZFVimDirDiff'
Plug 'ZSaberLv0/ZFVimJob'
"Plug 'ZSaberLv0/ZFVimIgnore'
Plug 'ZSaberLv0/ZFVimBackup'
Plug 'vim-airline/vim-airline'
Plug 'cespare/vim-toml'
Plug 'maralla/vim-toml-enhance', {'depends': 'cespare/vim-toml'}
Plug 'vimwiki/vimwiki'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-repeat'

call plug#end()

"-----------------------------------------------------------------------
" plugin configs
"-----------------------------------------------------------------------

" vim-latex
let g:latex_view_general_viewer = "zathura"
let g:vimtex_view_method = "zathura"
let g:vimtex_compiler_progname = "nvr"
let g:tex_flavor = "latex"
let g:vimtex_quickfix_open_on_warning = 0
let g:vimtex_quickfix_mode = 2

" rust.vim
let g:rustfmt_autosave = 1
let g:racer_cmd = "/home/jinks/.cargo/bin/racer"
let g:racer_experimental_completer = 0

let g:LanguageClient_serverCommands = {
    \ 'rust': ['rustup', 'run', 'nightly', 'rls'],
    \ }
let g:LanguageClient_autoStart = 1

nnoremap <silent> K :call LanguageClient_textDocument_hover()<CR>
nnoremap <silent> gd :call LanguageClient_textDocument_definition()<CR>
nnoremap <silent> <F4> :call LanguageClient_textDocument_rename()<CR>

" vim-go
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_fields = 1
let g:go_highlight_types = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
let g:go_fmt_command = "goimports"
"let g:go_snippet_engine = "neosnippet"
let g:go_fmt_fail_silently = 1

" vim-rooter
let g:rooter_silent_chdir = 1

" syntastic
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 = 0
let g:syntastic_go_checkers = ['gometalinter']
"let g:syntastic_mode_map = { 'mode': 'active', 'passive_filetypes': ['go'] }
let g:syntastic_go_gometalinter_args = '--fast --aggregate --disable gas'

" ncm2
autocmd BufEnter * call ncm2#enable_for_buffer()
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<cr>"
autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif

if has('conceal') " For conceal markers.
  set conceallevel=0 concealcursor=niv
endif

" echodoc
let g:echodoc_enable_at_startup = 1

" Toggle Keys
nmap <F2> :NERDTreeToggle<CR>
nmap <silent> <F3> :silent nohlsearch<CR>
nmap <F8> :TagbarToggle<CR>

"-----------------------------------------------------------------------
" settings
"-----------------------------------------------------------------------

set viminfo='1000,f1,:1000,/1000 " Enable a nice big viminfo file
set viminfo+=n~/.config/nvim/viminfo     " And put it in ~/.vim
set history=1000
set backspace=indent,eol,start   " Make backspace delete lots of things
set showcmd                      " Show us the command we're typing
set showmatch                    " Highlight matching parens
set noshowmode                   " Mode is already in the status line
set hlsearch                     " Search options: highlight search
set incsearch                    " Search options: incremental search
set ignorecase
set smartcase                    " Selective case insensitivity
set showfulltag                  " Show full tags when doing search completion
set scrolloff=3                  " Try to show at least three lines and
set sidescrolloff=5              " two columns of context when scrolling
set backup                       " create backups
set whichwrap+=<,>,[,]           " Wrap on these
set wildmenu                     " Use the cool tab complete menu
set wildignore+=*.o,*~,.lo
set suffixes+=.in,.a
set hidden                       " Allow edit buffers to be hidden
set winminheight=1               " 1 height windows
set virtualedit=block,onemore    " enable virtual edit in vblock mode, and one past the end
set laststatus=2                 " always show status line
set completeopt=noinsert,menuone,noselect
set shortmess+=c

function! EnsureDir(adir)
    if !isdirectory(a:adir)
        silent! call mkdir(a:adir, 'p')
    endif
endfunction
set undodir=~/.config/nvim/undo//        " persistent undo
call EnsureDir(&undodir)
set undofile
set directory=~/.config/nvim/swp//       " swap files
call EnsureDir(&directory)

" Enable syntax highlighting
let python_highlight_all = 1
if has("syntax")
    syntax on
endif

if has("mouse")
    set mouse=
endif

" No icky toolbar, menu or scrollbars in the GUI
if has('gui')
"    set guioptions-=m
    set guioptions-=T
    set guioptions-=l
    set guioptions-=L
"    set guioptions-=r
    set guioptions+=b
    set guioptions+=g
    set guioptions-=R
end

" By default, go for an indent of 4
set tabstop=4 shiftwidth=4 expandtab smarttab softtabstop=4

" Do clever indent things. Don't make a # force column zero.
set autoindent
set smartindent
inoremap # X<BS>#

" Syntax when printing
"set printoptions += syntax:y

" Enable filetype settings
if has("eval")
    filetype on
    filetype plugin on
    filetype indent on
endif

" If possible and in gvim, use cursor row highlighting
if has("gui_running") && v:version >= 700 || exists('g:GtkGuiLoaded')
    set cursorline
    autocmd! FocusGained * :redraw!
    "set guifont=SauceCodePro\ Nerd\ Font\ Medium\ 10
    set guifont=xos4\ Terminus\ 10
end
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#enabled = 1

" Try to load a nice colourscheme
if has("eval")
    fun! LoadColourScheme(schemes)
        let l:schemes = a:schemes . ":"
        while l:schemes != ""
            let l:scheme = strpart(l:schemes, 0, stridx(l:schemes, ":"))
            let l:schemes = strpart(l:schemes, stridx(l:schemes, ":") + 1)
            try
                exec "colorscheme" l:scheme
                break
            catch
            endtry
        endwhile
    endfun

    if has('gui_running')
        call LoadColourScheme("inkpot:night:rainbow_night:darkblue:elflord")
    else
        if has("autocmd")
            autocmd VimEnter *
                        \ if &t_Co == 88 || &t_Co == 256 |
                        \     call LoadColourScheme("inkpot:darkblue:elflord") |
                        \ else |
                        \     call LoadColourScheme("darkblue:elflord") |
                        \ endif
        else
            if &t_Co == 88 || &t_Co == 256
                call LoadColourScheme("inkpot:darkblue:elflord")
            else
                call LoadColourScheme("darkblue:elflord")
            endif
        endif
    endif
endif

" Include $HOME in cdpath
if has("file_in_path")
    let &cdpath=','.expand("$HOME").','.expand("$HOME").'/work'
endif

" Show tabs and trailing whitespace visually
if (&termencoding == "utf-8") || has("gui_running") || exists('g:GtkGuiLoaded')
    set list listchars=tab:»·,trail:·,extends:…,nbsp:‗
    "set list listchars=tab:│\ ,trail:·,extends:…,nbsp:‗
else
    "set list listchars=tab:>-,trail:.,extends:>,nbsp:_
    set list listchars=tab:\|\ ,trail:.,extends:>,nbsp:_
endif

set fillchars=fold:-

"-----------------------------------------------------------------------
" autocmds
"-----------------------------------------------------------------------
if has("eval")

    " If we're in a wide window, enable line numbers.
    fun! <SID>WindowWidth()
        if winwidth(0) > 90
            setlocal foldcolumn=2
            setlocal number
        else
            setlocal nonumber
            setlocal foldcolumn=0
        endif
    endfun

    " Force active window to the top of the screen without losing its
    " size.
    fun! <SID>WindowToTop()
        let l:h=winheight(0)
        wincmd K
        execute "resize" l:h
    endfun
endif

if has("autocmd") && has("eval")
    set grepprg=grep\ -nH\ $*
    augroup ciaranm
        autocmd!
        " Automagic line numbers
        autocmd BufEnter * :call <SID>WindowWidth()
        " Always do a full syntax refresh
        autocmd BufEnter * syntax sync fromstart
        "try
            "autocmd Syntax *
                        "\ syn match VimModelineLine /^.\{-1,}vim:[^:]\{-1,}:.*/ contains=VimModeline |
                        "\ syn match VimModeline contained /vim:[^:]\{-1,}:/
            "hi def link VimModelineLine comment
            "hi def link VimModeline     special
        "catch
        "endtry
    augroup END

    augroup jinks
        autocmd!
        " For some files, don't create backups
        autocmd BufRead svn-commit.tmp :setlocal nobackup
        autocmd BufRead /home/jinks/.fvwm/* :setlocal nobackup
        autocmd BufRead /home/jinks/.fvwm-crystal/* :setlocal nobackup
        autocmd FileType go nnoremap <C-i> :GoInfo<CR>
    augroup END

    augroup vimrcEx
        au!
        " When editing a file, always jump to the last known cursor position.
        " Don't do it when the position is invalid or when inside an event handler
        " (happens when dropping a file on gvim).
        " Also don't do it when the mark is in the first line, that is the default
        " position when opening a file.
        autocmd BufReadPost *
          \ if line("'\"") > 1 && line("'\"") <= line("$") |
          \   exe "normal! g`\"" |
          \ endif
    augroup END
endif

"-----------------------------------------------------------------------
" final commands
"-----------------------------------------------------------------------

" turn off any existing search
if has("autocmd")
    autocmd VimEnter * nohls
    autocmd VimEnter * AirlineRefresh
endif

"-----------------------------------------------------------------------
" vim: set shiftwidth=4 softtabstop=4 expandtab tw=120                 :
