call plug#begin('~/.vim/plugged')

Plug 'tpope/vim-sensible'
Plug 'tpope/vim-sleuth'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-surround'
Plug 'bling/vim-airline'
Plug 'mhinz/vim-signify'
Plug 'sjl/gundo.vim'
Plug 'ctrlpvim/ctrlp.vim'

if executable('lein')
  Plug 'guns/vim-sexp'
  Plug 'tpope/vim-sexp-mappings-for-regular-people'
  Plug 'tpope/vim-fireplace', {'for': 'clojure'}
endif

if executable('sbt')
  Plug 'derekwyatt/vim-scala'
endif

Plug 'trusktr/seti.vim'

call plug#end()

" ----- plugin settings ------
let g:signify_vcs_list = [ 'git' ]

if executable('ag')
  " Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
  " we cd first into the dir so ag will use .agignore correctly
  let g:ctrlp_user_command = 'cd %s && ag --nocolor --hidden -g ""'
endif

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

" xdg-ish settings
set undodir=~/.cache/vim/undo,.
set backupdir=~/.cache/vim/backup,.
set directory=~/.cache/vim/swap,.

" default indentation
set shiftwidth=2
set tabstop=4
set expandtab

" permanent undo history
set undofile

" allow switching buffers
set hidden

" always scroll
set scrolloff=99

" show line numbers
set number
set relativenumber

" set wordwrap indent
set nowrap
set linebreak
if exists("&breakindent")
  set breakindent
  set breakindentopt=shift:2,sbr
endif

" show whitespace
set list

" make a new copy of the file for backup
" setting to no or auto messes with filewatchers
set backupcopy=yes

" ------ key bindings ------

" ; as :
nnoremap ; :
vnoremap ; :

" allow <ESC> via jj in insert mode
inoremap jj <ESC>

" fix j/k with wraps
nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk

" opening ctrlp
nnoremap <C-O> :CtrlPBuffer<cr>

nnoremap <C-T>h :tabprev<cr>
nnoremap <C-T>l :tabnext<cr>

" ------ nvim specific ------
if has('nvim')
  let g:airline#themes#dark#palette.terminal = copy(g:airline#themes#dark#palette.insert)

  tnoremap <C-j><C-j> <C-\><C-N>
  tmap <C-T> <C-\><C-N><C-T>
  tmap <C-W> <C-\><C-N><C-W>
  tmap <C-P> <C-\><C-N><C-P>
  tmap <C-O> <C-\><C-N><C-O>

  autocmd BufWinEnter,WinEnter term://* startinsert
endif

augroup formatting
  au!
  " strip trailing whitespace
  autocmd BufWritePre * :%s/\s\+$//e
augroup end

colorscheme seti

if filereadable(expand('~/.vimrc.local'))
  source ~/.vimrc.local
endif