let $MYPLUGDIRECTORY = "~/.config/nvim/plugged/"
let $MYNVIMINIT = "~/.config/nvim/init.vim"
let $MYCOCVIM = "~/.config/nvim/coc.vim"

" Plugins {{{ "
" automatic vim-plug install {{{ "
if empty(glob('~/.local/share/nvim/site/autoload/plug.vim'))
    silent !curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs
                \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
    autocmd VimEnter * PlugInstall | source $MYNVIMINIT
endif
" }}} automatic vim-plug install "

call plug#begin($MYPLUGDIRECTORY)
" automatically install all plugins that are not already installed
if !empty(filter(copy(g:plugs), '!isdirectory(v:val.dir)'))
    autocmd VimEnter * PlugInstall | q
endif
Plug 'preservim/nerdtree'                       " file tree
Plug 'Xuyuanp/nerdtree-git-plugin'              " git plugin for nerdtree
Plug 'PhilRunninger/nerdtree-visual-selection'  " file manipulation in nerdtree
Plug 'kshenoy/vim-signature'                    " show marks 
Plug 'ryanoasis/vim-devicons'                   " devicons for nerdtree
Plug 'vim-airline/vim-airline'                  " statusline
Plug 'preservim/nerdcommenter'                  " easier comment management
Plug 'airblade/vim-gitgutter'                   " show git changes
Plug 'neoclide/coc.nvim', {'branch': 'release'} " code completion
Plug 'jiangmiao/auto-pairs'                     " automatic pairs
Plug 'machakann/vim-sandwich'                   " manipulate elements surrounding other elements
Plug 'tpope/vim-fugitive'                       " git master tool
Plug 'vim-scripts/colorizer'                    " colorize color codes
Plug 'honza/vim-snippets'                       " snippets
Plug 'rbonvall/snipmate-snippets-bib'           " bib snippets
Plug 'lervag/vimtex'                            " vimtex
Plug 'morhetz/gruvbox'                          " gruvbox colorscheme
Plug 'Yggdroot/indentLine'                      " ident guides
Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug']}
Plug 'elkowar/yuck.vim', {'for': 'yuck'}        " for yuck (eww) configuration
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}  " We recommend updating the parsers on update
"Plug 'jghauser/mkdir.nvim'                      " automatic mkdir -p for new directories (e.g. for diary)
"Plug 'ray-x/lsp_signature.nvim'                 " display signature of a function
"Plug 'akinsho/toggleterm.nvim'                  " a terminal inside nvim that can be toggled
" fuzzy finders
Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-telescope/telescope.nvim'
" cheat sheet
Plug 'sudormrfbin/cheatsheet.nvim'
Plug 'nvim-lua/popup.nvim'
call plug#end()
" }}} Plugins "

" General Settings {{{ "
let mapleader=" "           " set mapleader to space
let maplocalleader=" "      " set localleader to space
set timeoutlen=1000         " set timeout length
set spell                   " activate spell-checking
setlocal spelllang=de,en_us " enable German and English spell checking
set nocompatible            " disable compatibility to old-time vi
let g:indentLine_setConceal = 0 " disable the conceal 'feature' of indentLine plugin
set showmatch               " show matching brackets.
set mouse=v                 " middle-click paste with mouse
set hlsearch                " highlight search results
set tabstop=4               " number of columns occupied by a tab character
set softtabstop=4           " see multiple spaces as tabstops so <BS> does the right thing
set expandtab               " converts tabs to white space
set shiftwidth=4            " width for autoindents
set autoindent              " indent a new line the same amount as the line just typed
set wildmode=longest,list   " get bash-like tab completions
filetype plugin indent on   " allows auto-indenting depending on file type
syntax on                   " syntax highlighting
set cc=80                   " set an 80 column border for good coding style
" folding
set foldmethod=marker       
set foldtext=gitgutter#fold#foldtext()
"toggle cc when reasonable
augroup cctoggle
    autocmd!
    autocmd BufEnter,FocusGained    * set cc=80
    autocmd BufLeave,FocusLost      * set cc=0
augroup END
"toggle relativenumber when reasonable
set number relativenumber
augroup numbertoggle
    autocmd!
    autocmd BufEnter,FocusGained,InsertLeave    * set relativenumber
    autocmd BufLeave,FocusLost,InsertEnter      * set norelativenumber
augroup END
" sign column
set signcolumn=yes
" }}} General Settings "

" Mappings {{{ "
"disable colorizer mappings
let g:colorizer_nomap = 1
" copy paste to and fro clipboard {{{ "
vnoremap  <leader>y  "+y
nnoremap  <leader>Y  "+yg_
nnoremap  <leader>y  "+y
nnoremap <leader>p "+p
nnoremap <leader>P "+P
vnoremap <leader>p "+p
vnoremap <leader>P "+P
" }}} copy paste to and fro clipboard "

" custom window commands {{{ "
nnoremap <leader>wv <C-W>v
nnoremap <leader>ws <C-W>s
nnoremap <leader>wc <C-W>c
nnoremap <leader>wo <C-W>o
nnoremap <C-h> <C-W>h
nnoremap <C-l> <C-W>l
nnoremap <C-j> <C-W>j
nnoremap <C-k> <C-W>k
nnoremap öw :w<CR>
nnoremap öq :q<CR>
" }}} custom window commands "

" Telescope {{{ "
nnoremap <leader>ff <cmd>Telescope find_files<cr>
nnoremap <leader>fg <cmd>Telescope live_grep<cr>
nnoremap <leader>fb <cmd>Telescope buffers<cr>
nnoremap <leader>fh <cmd>Telescope help_tags<cr>
" }}} Telescope "

"custom tab commands
nnoremap <leader>tn :tabnew<space>
nnoremap <leader>tc :tabclose<CR>
"show buffers
nnoremap <Leader>b :buffers<CR>:buffer<Space>
" cycle buffers
nnoremap <Tab> :bnext<CR>
"reload settings
nnoremap <leader>rl :source ~/.config/nvim/init.vim<CR>
"NERDTree commands
nnoremap <leader>nt :NERDTreeToggle<CR>
nnoremap <leader>f :NERDTreeFind<CR>
"symbol renaming
nnoremap <leader>rn <Plug>(coc-reame)
" }}} Mappings "

" CoC (Conquer of Completion) {{{ "
" coc plugins (will automatically install)
let g:coc_global_extensions = [
            \'coc-texlab',  
            \'coc-snippets',
            \'coc-git',     
            \'coc-vimtex',  
            \'coc-python',  
            \'coc-vimlsp',  
            \'coc-json']    
source $MYCOCVIM
" }}} CoC (Conquer of Completion) "

" Airline {{{ "
set laststatus=2                                            " start in 'normal' mode
let g:airline#extensions#tabline#enabled = 1                " enable tabline for buffers
let g:airline#extensions#tabline#formatter = 'unique_tail'  " set tabline item style
" }}} Airline "

" NERDTree {{{ "
set modifiable
let NERDTreeShowHidden=1                " show hidden files in NERDTree
let NERDTreeAutoDeleteBuffer=1          " automatically delete the buffer of the file deleted with NERDTree
let g:NERDTreeGitStatusUseNerdFonts=1   " enable nerdfonts for git status in nerdtree
let NERDTreeShowLineNumbers=0           " disable line numbers
let g:NERDTreeWinSize=50                " increase the size of the NERDTree window
" Start NERDTree when Vim is started without file arguments.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif
" Exit Vim if NERDTree is the only window left.
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() |
    \ quit | endif
" If another buffer tries to replace NERDTree, put it in the other window, and bring back NERDTree.
autocmd BufEnter * if bufname('#') =~ 'NERD_tree_\d\+' && bufname('%') !~ 'NERD_tree_\d\+' && winnr('$') > 1 |
    \ let buf=bufnr() | buffer# | execute "normal! \<C-W>w" | execute 'buffer'.buf | endif
" }}} NERDTree "

" Color Scheme {{{ "
" set colorscheme
let g:gruvbox_contrast_dark='hard'
colorscheme gruvbox
set background=dark
" extra specified colors
highlight ColorColumn ctermbg=236
highlight clear SignColumn
highlight GitGutterAdd ctermfg=142
highlight GitGutterChange ctermfg=208
highlight GitGutterDelete ctermfg=124
highlight LineNr ctermfg=7
highlight CursorLineNr ctermfg=7
" }}} Color Scheme "

" Tree-Sitter {{{ "
lua <<EOF
require'nvim-treesitter.configs'.setup {
  -- One of "all", "maintained" (parsers with maintainers), or a list of languages
  ensure_installed = {
      "bash", "bibtex", 
      "c", "cmake", "comment", "cpp", "css", 
      "diff", "dockerfile", "dot", 
      "gitattributes", "gitignore", 
      "html", "http", 
      "javascript", "json", "json5",
      "latex", "lua", 
      "make", "markdown", "markdown_inline", 
      "python", 
      "regex", 
      "sql", 
      "toml", "typescript", 
      "vim", 
      "yaml"},

  -- Install languages synchronously (only applied to `ensure_installed`)
  sync_install = false,

  highlight = {
    -- `false` will disable the whole extension
    enable = true,

    -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
    -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
    -- Using this option may slow down your editor, and you may see some duplicate highlights.
    -- Instead of true it can also be a list of languages
    additional_vim_regex_highlighting = false,
  },
}
EOF
" }}} Tree-Sitter "

" VimTeX {{{ "
let g:vimtex_compiler_progname = 'nvr'  " neovim-remote for vimtex
let g:vimtex_view_method = 'zathura'    " set zathura as basic pdf viewer
"open TOC
nnoremap <leader>toc :VimtexTocToggle<CR>
" latexmk parameters
let g:vimtex_compiler_latexmk = {
    \ 'options' : [
    \   '-pdf',
    \   '-shell-escape',
    \   '-verbose',
    \   '-file-line-error',
    \   '-synctex=1',
    \   '-interaction=nonstopmode',
    \ ],
    \}
" }}} VimTeX "
