set nocompatible
" Syntax highlighting
syntax on
" Line numbering
set number
" Always show the signcolumn
set signcolumn=yes
" highlight
set cursorline
" Encoding
set encoding=UTF-8
" Auto-indentation
set ai
set tabstop=4
set shiftwidth=4
set expandtab
" Matching paren highlighting
set showmatch
" Sane colors
set background=dark
" Show cursor pos
set ruler
" Make the status line always show up
set laststatus=2
set ttimeoutlen=50
" Shut the annoying bell
set novisualbell
" Set the updatetime
set updatetime=500
" Case insensitive when all lowercase
set ignorecase
set smartcase
" Clipboard
"set clipboard^=unnamed,unnamedplus

" Remap arrow keys
noremap <Up> <NOP>
noremap <Down> <NOP>
nnoremap <Left> :bprev<CR>
nnoremap <Right> :bnext<CR>

" write the file when you accidentally opened it without the right (root) privileges
cmap w!! w !sudo tee % > /dev/null

" Remember mouse cursor position
augroup resCur
    autocmd!
    autocmd BufReadPost * call setpos(".", getpos("'\""))
augroup END

" Plugins
" Install vim-plug if not found
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
endif

" Run PlugInstall if there are missing plugins
autocmd VimEnter * if len(filter(values(g:plugs), '!isdirectory(v:val.dir)'))
  \| PlugInstall --sync | source $MYVIMRC
\| endif

call plug#begin()
Plug 'tpope/vim-sensible'

" Colorscheme
Plug 'https://git.sr.ht/~ackyshake/spacegray.vim'

" Interface
Plug 'itchyny/lightline.vim'
Plug 'mgee/lightline-bufferline'
Plug 'ryanoasis/vim-devicons'
Plug 'airblade/vim-gitgutter'
Plug 'scrooloose/nerdtree'
Plug 'Yggdroot/indentLine'

" Movement
Plug 'christoomey/vim-tmux-navigator'
"Plug 'easymotion/vim-easymotion'
Plug 'unblevable/quick-scope'

" Tools / Commands
Plug 'tpope/vim-surround'
Plug 'jiangmiao/auto-pairs'
Plug 'tpope/vim-endwise'
"Plug'chrisbra/unicode.vim'
Plug 'ctrlpvim/ctrlp.vim'
"Plug 'junegunn/fzf.vim'
Plug 'tpope/vim-obsession'

" Linting / Completion
Plug 'w0rp/ale'
Plug 'ervandew/supertab'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
if has('nvim')
  Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
  Plug 'Shougo/deoplete.nvim'
  Plug 'roxma/nvim-yarp'
  Plug 'roxma/vim-hug-neovim-rpc'
endif
Plug 'neoclide/coc-tsserver', {'do': 'yarn install --frozen-lockfile'}
Plug 'neoclide/coc-json', {'do': 'yarn install --frozen-lockfile'}
Plug 'neoclide/coc-yaml', {'do': 'yarn install --frozen-lockfile'}
Plug 'josa42/coc-sh', {'do': 'yarn install --frozen-lockfile'}
Plug 'josa42/coc-go', {'do': 'yarn install --frozen-lockfile'}
Plug 'josa42/coc-docker', {'do': 'yarn install --frozen-lockfile'}
Plug 'yaegassy/coc-pylsp', {'do': 'yarn install --frozen-lockfile'}

" Language
Plug 'sheerun/vim-polyglot'

call plug#end()

filetype plugin indent on
syntax enable

" Color scheme
colorscheme spacegray
" 16 millions colors
"set termguicolors
" Disable background in terminal
hi Normal ctermbg=NONE

" Italic comments
highlight Comment cterm=italic
set t_ZH=[3m
set t_ZR=[23m

" deoplete
"let g:deoplete#enable_at_startup = 1

" supertab
let g:SuperTabDefaultCompletionType = "<c-n>"

" ALE
let g:ale_sign_error = ''
let g:ale_sign_warning = ''
highlight ALEErrorSign ctermfg=Red
highlight ALEWarningSign ctermfg=Blue
highlight ALEError ctermfg=Red ctermbg=None cterm=underline
highlight ALEWarning ctermfg=Blue ctermbg=None cterm=underline

" nerdtree
map <C-n> :NERDTreeToggle<CR>

" ctrlp
let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlP'

" gitgutter
let g:gitgutter_realtime = 1
let g:gitgutter_eager = 0
let g:gitgutter_override_sign_column_highlight = 0
let g:gitgutter_grep = 'rg'
let g:gitgutter_sign_added = '+▐'
let g:gitgutter_sign_modified = '~▐'
let g:gitgutter_sign_removed = '-▐'
let g:gitgutter_sign_removed_first_line = '-▐'
let g:gitgutter_sign_modified_removed = '~▐'
highlight SignColumn ctermbg=None
highlight GitGutterAdd ctermfg=green
highlight GitGutterChange ctermfg=yellow
highlight GitGutterDelete ctermfg=red
highlight GitGutterChangeDelete ctermfg=red

" indentLine
let g:indentLine_faster = 1
let g:indentLine_color_term = 237
let g:indentLine_leadingSpaceEnabled = 1
let g:indentLine_char = '┆'
let g:indentLine_leadingSpaceChar = '·'

" lightline
set noshowmode
set showtabline=2
let g:lightline = {
    \ 'colorscheme': 'wombat',
    \ 'active': {
        \ 'left': [ [ 'mode', 'paste' ],
        \           [ 'readonly', 'filename' ],
        \           [ 'ctrlpmark' ] ],
        \ 'right': [ [ 'lineinfo' ],
        \            [ 'percent' ],
        \            [ 'linter_errors' ],
        \            [ 'fileformat', 'fileencoding', 'filetype', 'linter_ok', 'linter_warnings' ] ],
    \ },
    \ 'component_expand': {
        \ 'linter_warnings': 'LightlineLinterWarnings',
        \ 'linter_errors': 'LightlineLinterErrors',
        \ 'linter_ok': 'LightlineLinterOK'
    \ },
    \ 'component_type': {
        \ 'readonly': 'error',
        \ 'linter_errors': 'error',
        \ 'linter_warning': 'warning',
    \ },
    \ 'component_function': {
        \ 'filename': 'LightlineFilename',
    \ },
\ }

"lightline-bufferline
let g:lightline#bufferline#show_number=2
let g:lightline#bufferline#number_map = {
    \ 0: '⁰', 1: '¹', 2: '²', 3: '³', 4: '⁴',
    \ 5: '⁵', 6: '⁶', 7: '⁷', 8: '⁸', 9: '⁹'
\ }
let g:lightline#bufferline#enable_devicons=1
let g:lightline#bufferline#min_buffer_count=2
let g:lightline.tabline          = {'left': [['buffers']], 'right': []}
let g:lightline.component_expand = {'buffers': 'lightline#bufferline#buffers'}
let g:lightline.component_type   = {'buffers': 'tabsel'}

function! LightlineFilename()
    let filename = expand('%:t') !=# '' ? expand('%:t') : '[No Name]'
    let modified = &modified ? ' +' : ''
    return filename . modified
endfunction

function! LightlineLinterWarnings() abort
    let l:counts = ale#statusline#Count(bufnr(''))
    let l:all_errors = l:counts.error + l:counts.style_error
    let l:all_non_errors = l:counts.total - l:all_errors
    return l:counts.total == 0 ? '' : printf('%d ', all_non_errors)
endfunction

function! LightlineLinterErrors() abort
    let l:counts = ale#statusline#Count(bufnr(''))
    let l:all_errors = l:counts.error + l:counts.style_error
    let l:all_non_errors = l:counts.total - l:all_errors
    return l:counts.total == 0 ? '' : printf('%d ', all_errors)
endfunction

function! LightlineLinterOK() abort
    let l:counts = ale#statusline#Count(bufnr(''))
    let l:all_errors = l:counts.error + l:counts.style_error
    let l:all_non_errors = l:counts.total - l:all_errors
    return l:counts.total == 0 ? '0 ' : ''
endfunction

autocmd User ALELint call s:MaybeUpdateLightline()

" Update and show lightline but only if it's visible (e.g., not in Goyo)
function! s:MaybeUpdateLightline()
    if exists('#lightline')
        call lightline#update()
    end
endfunction

