" set the runtime path to include Vundle and initialize
"call has('python3')
" Start plugins
call plug#begin('~/.vim/plugged')
" Sensible defaults
Plug 'tpope/vim-sensible'

" Javascript
Plug 'heavenshell/vim-jsdoc',           {'for': 'javascript'}
Plug 'sheerun/vim-polyglot'
" C/C++
Plug 'vim-scripts/DoxygenToolkit.vim',  {'for': 'cpp'}
" Rust
Plug 'rust-lang/rust.vim', {'for': 'rust'}
"Plug 'racer-rust/vim-racer', {'for': 'rust'}

" Syntax checking
"Plug 'Valloric/YouCompleteMe' " alternative #1, full-blown
Plug 'w0rp/ale',
Plug 'neoclide/coc.nvim', {'branch': 'release'}

" Useful plugins
Plug 'Raimondi/delimitMate'
Plug 'scrooloose/nerdcommenter'
Plug 'scrooloose/nerdtree'
Plug 'jlanzarotta/bufexplorer'
Plug 'ctrlpvim/ctrlp.vim'
Plug 'ervandew/supertab'
Plug 'reedes/vim-lexical'
Plug 'majutsushi/tagbar'

" Snippets
Plug 'sirver/ultisnips'
Plug 'honza/vim-snippets'

" Themes
"Plug 'Lokaltog/vim-distinguished'
"Plug 'altercation/vim-colors-solarized'
"Plug 'tomasr/molokai'
"Plug 'flazz/vim-colorschemes'
"Plug 'tssm/c64-vim-color-scheme'
"Plug 'morhetz/gruvbox'
"Plug 'chriskempson/base16-vim' " Original but not updated
Plug 'danielwe/base16-vim' " Fork. updated. https://github.com/chriskempson/base16-vim/issues/197
" Airline
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'

"let g:airline_theme='base16_default'
let g:airline_theme='base16_gruvbox_dark_hard'
let g:airline_powerline_fonts = 1

" Initialize plugin system
call plug#end()
filetype plugin indent on

" Magical color setup
syntax on
"set t_Co=256
set termguicolors
set background=dark
"let base16colorspace=256
"colorscheme base16-solarized-dark
colorscheme base16-gruvbox-dark-hard
"colorscheme base16-default-dark


" Other
set encoding=utf-8
set autoindent " Automatically indent
set cindent " Indentation for c
set number " Add line numbers
set hlsearch " Highlight search
set modeline
set modelines=5
"set sessionoptions=blank,buffers,curdir,folds,globals,help,localoptions,options,resize,tabpages,winsize,winpos
set swapfile
set autochdir " Change directory automatically
set backupdir=~/.vim/temp/ " Save temporary backups to temp dir
set directory=~/.vim/temp/ " Save temporary files to temp dir
set dir=~/.vim/temp/ " Save temporary files to temp dir
set hidden " Allow changing buffers eventhough they're not saved
"set autowrite " Automatically save the buffer when changing to another buffers
"set autowriteall " Automatically save ALL buffers when changing to another buffers
"set wildchar=<Tab> wildmenu wildmode=full " Show possible commands by pressing TAB
"set wildcharm=<C-Z> " This will allow you to access bufferlist with wildcard by pressing F10
set colorcolumn=80
" Set GUI parameters
if has("gui_running")
	" GUI options.
	set guioptions-=m  " remove menu bar
	set guioptions-=T  " remove toolbar
	set guioptions-=r  " remove right-hand scroll bar
	set guioptions-=L  " remove left-hand scroll bar
	set guifont=Fira\ Code\ Medium\ 11
	"set guifont=Fura\ Code\ Nerd\ Font\ 11
	set guiheadroom=0
endif

" Keybindings
" Long list following
" Indentation
map <F7> mzgg=G`z
map <F3> %s<cr>

" NerdTree
map <F2> :NERDTreeToggle<cr>
imap <F2> <esc>:NERDTreeToggle<cr>i

" NerdCommenter
map <C-c> <leader>ci
inoremap <F9> <C-O>za
nnoremap <F9> za
onoremap <F9> <C-C>za
vnoremap <F9> zf

" tab navigation like firefox
nnoremap <C-S-tab> :tabprevious<CR>
nnoremap <C-tab>   :tabnext<CR>
nnoremap <C-t>     :tabnew<CR>
inoremap <C-S-tab> <Esc>:tabprevious<CR>i
inoremap <C-tab>   <Esc>:tabnext<CR>i
inoremap <C-t>     <Esc>:tabnew<CR>

" Session parameters
set ssop-=options    " do not store global and local values in a session
set ssop-=folds      " do not store folds

" Toggle number
:nmap <C-N><C-N> :set invnumber<CR>

" Toggle hlsearch
:nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>

" Configure for jsdoc
let g:jsdoc_enable_es6 = 1

" Remove trailing whitespace automatically
autocmd BufWritePre *.js %s/\s\+$//e
autocmd BufWritePre *.cpp %s/\s\+$//e

" Set sane defaults for tabs
autocmd Filetype python setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype cpp setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype javascript setlocal ts=2 sw=2 sts=0 expandtab
autocmd Filetype vue setlocal ts=2 sw=2 sts=0 expandtab
autocmd Filetype json setlocal ts=2 sw=2 sts=0 expandtab
autocmd Filetype lua setlocal ts=2 sw=2 sts=0 expandtab
autocmd Filetype html setlocal ts=4 sw=4 sts=0 expandtab

" ultisnips
let g:UltiSnipsExpandTrigger="<c-tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"

augroup lexical
	autocmd!
	autocmd FileType markdown,mkd call lexical#init()
	autocmd FileType textile call lexical#init()
	autocmd FileType text call lexical#init({ 'spell': 0 })
augroup END

let g:ale_linters = {
			\'javascript': ['standard'],
			\'vue': ['standard --plugin html "**/*.{js,vue}"'],
			\'jsx': ['standard --plugin html "**/*.{js,jsx}"'],
			\'rust': ['cargo']
			\}
let b:ale_fixers = {
			\'*': ['remove_trailing_lines', 'trim_whitespace'],
			\'rust': ['rustfmt']
			\}
let g:ale_fix_on_save = 1
let g:ale_completion_enabled = 1

set completeopt+=preview
