set nocompatible

" XDG
set undodir=$XDG_CACHE_HOME/vim/undo
set directory=$XDG_CACHE_HOME/vim/swap
set backupdir=$XDG_CACHE_HOME/vim/backup//
set viminfo+='1000,n$XDG_CACHE_HOME/vim/viminfo
set runtimepath=$XDG_CONFIG_HOME/vim,$VIMRUNTIME,$XDG_CONFIG_HOME/vim/after

let g:session_directory=$XDG_CACHE_HOME."/vim/sessions"

for directory in [&undodir, &directory, &backupdir, g:session_directory]
  if !filewritable(expand(directory))
      call mkdir(expand(directory), "p")
  endif
endfor

" VUNDLE
" {
let bundledir=$XDG_CONFIG_HOME."/vim/bundle/"

if filereadable(expand(bundledir."vundle/README.md"))
    filetype off                   " required!

    let &rtp=&rtp.",".bundledir."vundle/"
    call vundle#begin(bundledir)

    " let Vundle manage Vundle
    " required!
    Plugin 'gmarik/vundle'

    Plugin 'xolox/vim-misc'
    Plugin 'xolox/vim-session'
    Plugin 'vim-ruby/vim-ruby'
    Plugin 'tpope/vim-rails'
    Plugin 'tpope/vim-markdown'
    Plugin 'tpope/vim-endwise'

    Plugin 'jiangmiao/auto-pairs'

    Plugin 'tpope/vim-haml'
    Plugin 'slim-template/vim-slim'
    Plugin 'lumiliet/vim-twig'

    Plugin 'Sirver/ultisnips'
    Plugin 'honza/vim-snippets'

    Plugin 'ton/vim-bufsurf'
    Plugin 'bsdelf/bufferhint'

    Plugin 'AndrewRadev/switch.vim'

    Plugin 'airblade/vim-gitgutter'
    Plugin 'tpope/vim-fugitive'
    " Plugin 'scrooloose/syntastic'
    Plugin 'dense-analysis/ale'

    Plugin 'sjl/gundo.vim'
    Plugin 'tpope/vim-commentary'

    Plugin 'moll/vim-bbye'

    Plugin 'VisIncr'

    call vundle#end()
endif
" }

filetype plugin indent on
syntax enable

set t_Co=256

set background=dark
colorscheme desert

set number
set relativenumber
set showmode
set expandtab
set shiftwidth=2
set smarttab

set autoindent
set nowrap
set gdefault

set hidden
set switchbuf=useopen,usetab
set ruler
set showcmd
set fileencoding=utf-8
set fileformats=unix,dos,mac
set cmdheight=2
set noerrorbells
set mouse=a
set nohls
set scrolloff=5
set sidescroll=20
set sidescrolloff=20
set noincsearch
set noshowmatch
set colorcolumn=80

set listchars=nbsp:·,tab:▸\ ,extends:>,precedes:<
set list

set formatoptions+=j

set splitright splitbelow

set wildmenu
set wildmode=longest:full,full
set wildignore=*~,*.o,*.obj,*.bak,*.exe,*.class,*.jpg,*.png,*.gif,*.mp3
set wildignorecase

set laststatus=2
set statusline=%f\ %m\ %Y\|%{&ff}\|%{&fileencoding}%=%vx%l\ [%03L]

set nocursorline
set nocursorcolumn

set pastetoggle=<F2>

set history=100
set undolevels=150

set ttimeoutlen=100

set undofile
set backup

let mapleader = ","

let g:bufferhint_MaxWidth = 200

" FOLDING
" {
set foldenable
set foldmethod=syntax
" Number of columns to the left of the window to display folds
set foldcolumn=0
" Fold level to close when opening a file
set foldlevelstart=100

" Don't open folds when searching or undoing into them
set foldopen-=search
set foldopen-=undo
" }

" MAPPING
" {
map <F1> <ESC>
imap <F1> <ESC>

map <F5> <ESC>:GundoToggle<CR>

map <F11> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
map <F12> ggg?G

nnoremap <silent> gy :Switch<CR>

nnoremap <silent> ,bp :bprevious<CR>
nnoremap <silent> ,bf :bnext<CR>
nnoremap <silent> ,Bp :BufSurfBack<CR>
nnoremap <silent> ,Bf :BufSurfForward<CR>

nnoremap <Leader>- :call bufferhint#Popup()<CR>
nnoremap <Leader>\ :call bufferhint#LoadPrevious()<CR>

" delete into the black hole register
nnoremap <leader>d "_d
" }

" PLUGINS
" {
let g:gundo_prefer_python3 = 1
" gitgutter
let g:gitgutter_realtime = 0
let g:gitgutter_eager = 0
" UltiSnips
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<tab>"
" Session
let g:session_autoload = 'no'
let g:session_autosave = 'no'

autocmd FileType ruby let b:switch_custom_definitions =
    \ [
    \   {
    \     'lambda { |\([^|]\+\)|': '->(\1) {',
    \     '->(\([^)]\+\)) {': 'lambda { |\1|',
    \   },
    \ ]
" }

" FILETYPES
" {
" Open PDF files
" need xpdf and cups-pdf
autocmd BufReadPre *.pdf set ro nowrap
autocmd BufReadPost *.pdf silent %!pdftotext "%" -nopgbrk -layout -q -eol unix -
autocmd BufWritePost *.pdf silent !rm -rf ~/Documents/%
autocmd BufWritePost *.pdf silent !lp -s -d pdffg "%"
autocmd BufWritePost *.pdf silent !until [ -e ~/Documents/% ]; do sleep 1; done
autocmd BufWritePost *.pdf silent !mv ~/Documents/% %:p:h

" PHP
let php_sql_query = 1
let php_htmlInStrings = 1

autocmd FileType php setlocal shiftwidth=4 tabstop=4

" Don't screw up folds when inserting text that might affect them, until
" leaving insert mode. Foldmethod is local to the window.
autocmd InsertEnter * let w:last_fdm=&foldmethod | setlocal foldmethod=manual
autocmd InsertLeave * let &l:foldmethod=w:last_fdm

" chmod +x on the file if it starts with a shebang
function! ModeChange()
    if getline(1) =~ "^#!"
        if getline(1) =~ "/bin/"
            silent !chmod a+x <afile>
        endif
    endif
endfunction

au BufWritePost * call ModeChange()
" }

" FUNCTIONS
" {
" }

" MISC
" {
" Required for vim-gitgutter signcolumn
highlight clear SignColumn
" Every spaces/tabs at the end of line will be highlighted.
au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\s\+$', -1)
" autocmd BufWrite * silent! %s/[\r \t]\+$//

"autocmd WinLeave * :call FocusLost()
"autocmd WinEnter * :call FocusGained()
" }