""""""""""""""""""""""
"  General settings  "
""""""""""""""""""""""

filetype plugin indent on       " enable filetype detection, plugin, indent
syntax enable                   " enable syntax highlight
set background=dark             " use a dark background
set cursorline                  " highlight current line
set lazyredraw                  " only redraw when necessary
set autoread                    " reload file when changed outside vim
set backspace=2                 " backspace on indent,eol,start
set showmatch                   " show matching brackets
set scrolloff=3                 " keep some lines from the top and the bottom
set hidden                      " buffer becomes hidden when it is abandoned
set magic                       " more natural regexp
set laststatus=2                " always show the status line
set wrap                        " allow wrap lines
set noshowmode                  " no status info in echo are
set cot=menuone,noselect        " complete options
" Wild command
set wildmenu                    " visual autocomplete for command menu
set wildignorecase              " case insensitive in command line
" Folding
set foldmethod=syntax           " folds defined by syntax
set nofoldenable                " open all folds by default
" Encoding
set encoding=utf8               " set encoding to utf-8
set fileformats=unix,mac,dos    " set end-of-line (EOL) formats
" Line numbers
set number             " enable line numbers
set relativenumber     " show relative line numbers
" Indentation
set tabstop=4          " number of visual spaces per tab
set shiftwidth=4       " number of spaces for each step of autoindent
set softtabstop=4      " number of spaces per tab when editing
set smarttab           " helps with backspacing because of expandtab
set expandtab          " tabs are spaces
set shiftround         " round to multiple of shiftwidth when adjusing indentation
set autoindent         " auto indent on a new line
" Key timeout
set notimeout          " no timeout for mappings
set ttimeout           " allow timeout for keycodes
" Search
set incsearch          " search as characters are enters
set hlsearch           " highlight matches
set ignorecase         " ignore case when searching lowercase
set smartcase          " do not ignore case when searching uppercase
" Backups
set nobackup           " disable backups
set nowritebackup      " disable backup before overwriting
set noswapfile         " disable swapfiles
" Spell check
au Filetype tex setlocal spell
au Filetype markdown setlocal spell

"""""""""""""""""""""""""""""""
"  Some hacks & key mappings  "
"""""""""""""""""""""""""""""""

" Use space as a leader key
let mapleader="\<Space>"

" File explorer
let g:netrw_banner=0
let g:netrw_winsize=24
let g:netrw_liststyle=3
let g:netrw_browse_split=4
noremap <silent> <leader>e :Lex<cr>

" Clipboard sharing
if has("clipboard") | set clipboard=unnamed | set mouse=a | endif

" Treat long lines as break lines
noremap <expr> j v:count?'j':'gj'
noremap <expr> k v:count?'k':'gk'

" Quick highlight removal
noremap <silent> <leader><cr> :nohlsearch<cr>

" Quick save and/or quit
noremap <silent> <leader>w :w<cr>
noremap <silent> <leader>x :x<cr>
noremap <silent> <leader>q :q<cr>

" Quick buffer operations
noremap <leader>bd :bd<cr>
noremap <leader>bn :bn<cr>
noremap <leader>bp :bp<cr>

" Quick tab operations
noremap go :tabnew<cr>
noremap gc :tabclose<cr>

" :W sudo saves the file
command W w !sudo tee % > /dev/null

" Jump to last position everytime
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

" Toggle quickfix window
function! QuickFixToggle()
    let nr1=winnr("$") | cwindow
    let nr2=winnr("$")
    if nr1==nr2 | cclose | endif
endfunction
nnoremap <silent> <leader>k :call QuickFixToggle()<cr>


"""""""""""""""""""""
"  Plugin settings  "
"""""""""""""""""""""

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

call plug#begin('~/.vim/plug')
" ----8<-------------------
Plug 'w0rp/ale'
    let g:ale_completion_enabled=1
    let g:ale_set_highlights=0
    let g:ale_sign_column_always=1
    let g:ale_linters={'python':['pylsp']}
    nnoremap <leader>lj :ALENextWrap<cr>
    nnoremap <leader>lk :ALEPreviousWrap<cr>
    nnoremap <leader>lf :ALEFix<cr>
Plug 'ctrlpvim/ctrlp.vim'
    let g:ctrlp_map='<leader>f'
    let g:ctrlp_cmd='CtrlPBuffer'
Plug 'jiangmiao/auto-pairs'
Plug 'tpope/vim-rsi'
Plug 'scrooloose/nerdcommenter'
Plug 'wellle/targets.vim'
Plug 'tpope/vim-surround'
Plug 'google/vim-searchindex'
Plug 'easymotion/vim-easymotion'
    let g:EasyMotion_do_mapping=0
    let g:EasyMotion_smartcase=1
    map <leader>s <plug>(easymotion-s2)
Plug 'Shougo/neosnippet.vim'
    Plug 'Shougo/neosnippet-snippets'
    imap <expr><tab> neosnippet#expandable_or_jumpable()?
         \ "\<plug>(neosnippet_expand_or_jump)":"\<tab>"
    smap <expr><tab> neosnippet#expandable_or_jumpable()?
         \ "\<plug>(neosnippet_expand_or_jump)":"\<tab>"
    let g:neosnippet#snippets_directory='~/.vim/snippets'
Plug 'joshdick/onedark.vim'
Plug 'itchyny/lightline.vim'
    let g:lightline={'colorscheme':'onedark'}
Plug 'thinca/vim-quickrun'
Plug 'sheerun/vim-polyglot'
    let g:tex_flavor='latex'
" ----8<-------------------
call plug#end()
silent! colo onedark