packadd minpac
call minpac#init()
    " minpac must have {'type': 'opt'} so that it can be loaded with `packadd`.
    call minpac#add('k-takata/minpac', {'type': 'opt'})
    call minpac#add('honza/vim-snippets', {'type': 'start'})
    call minpac#add('SirVer/ultisnips')
    call minpac#add('junegunn/limelight.vim')
    call minpac#add('junegunn/goyo.vim')
    call minpac#add('junegunn/fzf')
    call minpac#add('junegunn/fzf.vim')
    call minpac#add('ambv/black')
    call minpac#add('davidhalter/jedi-vim')
    call minpac#add('vim-airline/vim-airline')
    call minpac#add('ap/vim-css-color')
    call minpac#add('fatih/vim-go')
    call minpac#add('dpelle/vim-Grammalecte')
    call minpac#add('ledger/vim-ledger')
    call minpac#add('tpope/vim-markdown')
    call minpac#add('nfnty/vim-nftables')
    call minpac#add('cespare/vim-toml')
    call minpac#add('pbrisbin/vim-mkdir')
    call minpac#add('tpope/vim-eunuch')
    call minpac#add('lambdalisue/suda.vim')
    call minpac#add('airblade/vim-gitgutter')
    call minpac#add('thaerkh/vim-indentguides')
    call minpac#add('Shougo/deoplete.nvim')

command! PU call minpac#update()
command! PC call minpac#clean()
command! PS call minpac#status()

cmap w!! w suda://%

colorscheme desert-warm-256

let g:deoplete#enable_at_startup = 1
let g:UltiSnipsSnippetDirectories=["UltiSnips", "mysnippets"]
let g:indentguides_ignorelist = ['txt','md','help']
let g:rustfmt_autosave = 1
let g:netrw_dirhistmax = 0
let g:airline#extensions#tabline#enabled = 1
let g:airline_powerline_fonts = 1
let g:limelight_conceal_ctermfg = 'gray'
let g:snips_author = 'Julien Rabier'
let g:name = 'Julien Rabier'
let g:nickname = 'taziden'
let mapleader=","
set completeopt=menuone,noselect,noinsert
set backup
set backupdir=~/.vim/backup
set hidden
set ignorecase
set lcs:tab:>-,trail:.
set list
set modeline
set number
set showmatch
set spelllang=fr
set statusline+=%#warningmsg#
set statusline+=%*
set undofile
set undodir=~/.vim/undo
"set virtualedit=all
"set clipboard=unnamed
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set pastetoggle=<F2>
"set formatoptions=tcrqn2w
"set formatlistpat=^\\s*[\\d\\*-]\\+[\\]:.)}\\t\ ]\\s*
"set dictionary=/usr/share/dict/words

highlight clear SpellBad
highlight SpellBad term=standout ctermfg=1 term=underline cterm=underline
highlight clear SpellCap
highlight SpellCap term=standout ctermfg=3 term=underline cterm=underline
highlight clear SpellRare
highlight SpellRare term=underline cterm=underline
highlight clear SpellLocal
highlight SpellLocal term=underline cterm=underline
highlight RedundantSpaces ctermbg=red guibg=red
match RedundantSpaces /\s\+$\| \+\ze\t\|\t/

" Jump to the last position when reopening a file
if has("autocmd")
 au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

au Filetype yaml silent setlocal foldmethod=marker tw=80 ts=2 sw=2 sts=2 foldlevel=0

"inoremap <silent> <expr> <plug>MyCR 
"    \ mucomplete#ultisnips#expand_snippet("\<cr>")
"imap <cr> <plug>MyCR

nnoremap <C-j> <C-w><C-j>
nnoremap <C-k> <C-w><C-k>
nnoremap <C-h> <C-w><C-h>
nnoremap <C-l> <C-w><C-l>
nmap <F8> :TagbarToggle<CR>
map <F9> <esc>:!hugo server -Dw<CR>
map ,ga :!git add -p<cr>
map ,gA :!git add %<cr>
map ,gf :!git add
map ,gs :!git status<cr>
map ,gc :!git commit -m ""<left>
map ,gC :!git add %<cr>:!git commit -m ""<left>
map ,gd :!git diff<cr>
map ,gD :!git diff --cached<cr>
map ,gp :!git push<cr>
map ,gP :!git pull<cr>
map ,gb :!git branch<cr>
map ,gB :!git branch
map ,gh :!git checkout
map ,gi :!git init<cr>
map ,gt :!tig<cr>
map <A-Left> :bp<cr>  " Alt + Left arrow goes to the previous buffer
map <A-Right> :bn<cr>  " Alt + Right arrow goes to the next buffer
nnoremap <silent> <c-s> :NV<CR>

function! s:goyo_enter()
  let b:quitting = 0
  let b:quitting_bang = 0
  autocmd QuitPre <buffer> let b:quitting = 1
  cabbrev <buffer> q! let b:quitting_bang = 1 <bar> q!
endfunction

function! s:goyo_leave()
  " Quit Vim if this is the only remaining buffer
  if b:quitting && len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1
    if b:quitting_bang
      qa!
    else
      qa
    endif
  endif
endfunction

autocmd! User GoyoEnter call <SID>goyo_enter()
autocmd! User GoyoLeave call <SID>goyo_leave()

command! -bang -nargs=? -complete=dir Files
    \ call fzf#vim#files(<q-args>, fzf#vim#with_preview(), <bang>0)
nnoremap <c-p> :Files<cr>
nnoremap ,ff :RG<cr>

function! RipgrepFzf(query, fullscreen)
  let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
  let initial_command = printf(command_fmt, shellescape(a:query))
  let reload_command = printf(command_fmt, '{q}')
  let spec = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
  call fzf#vim#grep(initial_command, 1, fzf#vim#with_preview(spec), a:fullscreen)
endfunction

command! -nargs=* -bang RG call RipgrepFzf(<q-args>, <bang>0)

let g:fzf_action = {
      \ 'ctrl-s': 'split',
      \ 'ctrl-v': 'vsplit',
      \ 'ctrl-t': 'edit',
      \ 'enter': 'tabedit',
      \ }

nnoremap ,f :FZF<cr>

{{- if eq .chezmoi.osRelease.prettyName "Debian GNU/Linux 10 (buster)" }}
let g:go_version_warning = 0
{{- end }}

{{- if eq .chezmoi.hostname "taupo" }}
call minpac#add('alok/notational-fzf-vim')
call minpac#add('rust-lang/rust.vim')
call minpac#add('racer-rust/vim-racer')
call minpac#add('dense-analysis/ale')
call minpac#add('mhinz/vim-crates')
call minpac#add('tpope/vim-dispatch')
call minpac#add('paretje/deoplete-notmuch')
call minpac#add('deoplete-plugins/deoplete-dictionary')
call minpac#add('dpelle/vim-Grammalecte')
call minpac#add('itspriddle/vim-shellcheck')

let g:grammalecte_cli_py='/home/taziden/prog/grammalecte/grammalecte-cli.py'
let g:racer_experimental_completer = 1
let g:nv_search_paths = ['./notes', '~/notes', '~/projets/sysnove/notes']
let g:nv_default_extension = '.md'
let g:nv_create_note_window = 'edit'
map <F5> <esc>:!pdflatex %<CR>
map <F6> <esc>:!pinpoint %<CR>
map <F9> <esc>:!hugo server -Dw<CR>
map ,b  :!bash gen.sh<cr><cr>
map ,c  <esc>2GddOTo: contact@sysnove.fr<esc>jddOCc: <esc>
autocmd BufNewFile,BufRead *.hledger,*.ledger,*.journal setfiletype ledger
autocmd BufNewFile,BufRead *.vim.tmpl setfiletype vim
autocmd BufWritePost devis*.md :Start! bash gen.sh
autocmd Filetype ledger set nospell
autocmd CompleteDone * silent! pclose!

"autocmd VimEnter * call deoplete#custom#source('dictionary', 'filetypes', ['markdown','mail','txt'])

" Racer
augroup Racer
    autocmd!
    autocmd FileType rust nmap <buffer> <leader>gd         <Plug>(rust-def)
    autocmd FileType rust nmap <buffer> <leader>gs         <Plug>(rust-def-split)
    autocmd FileType rust nmap <buffer> <leader>gx         <Plug>(rust-def-vertical)
    autocmd FileType rust nmap <buffer> <leader>gt         <Plug>(rust-def-tab)
    autocmd FileType rust nmap <buffer> gd <Plug>(rust-doc-tab)
    autocmd FileType rust nmap <buffer> gD <Plug>(rust-doc)
augroup END

let g:ale_linters = {'rust': ['analyzer']}

autocmd BufRead Cargo.toml call crates#toggle()
{{- end }}
