" My awesome vimrc

let mapleader =","

if ! filereadable(expand('~/.config/nvim/autoload/plug.vim'))
    echo "Downloading junegunn/vim-plug to manage plugins..."
    silent !mkdir -p ~/.config/nvim/autoload/
    silent !curl "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" > ~/.config/nvim/autoload/plug.vim
    autocmd VimEnter * PlugInstall
endif

function! Cond(cond, ...)
    let opts = get(a:000, 0, {})
    return a:cond ? opts : extend(opts, { 'on': [], 'for': [] })
endfunction

call plug#begin('~/.config/nvim/plugged')

if !exists('g:vscode')
    Plug 'neoclide/coc.nvim', Cond(!exists('g:vscode'))

    Plug 'nvim-lua/popup.nvim', Cond(!exists('g:vscode'))
    Plug 'nvim-lua/plenary.nvim', Cond(!exists('g:vscode'))
    Plug 'nvim-telescope/telescope.nvim', Cond(!exists('g:vscode'))
    Plug 'nvim-telescope/telescope-fzy-native.nvim', Cond(!exists('g:vscode'))

    Plug 'vimwiki/vimwiki', Cond(!exists('g:vscode'))

    Plug 'ap/vim-css-color', Cond(!exists('g:vscode'))

endif

Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
Plug 'darrikonn/vim-gofmt', { 'do': ':GoUpdateBinaries' }

call plug#end()

" Some basics:
set clipboard+=unnamedplus
set nohlsearch
set incsearch
set ignorecase


if !exists('g:vscode')
    set go=a
    set mouse=a
    set smartcase
    set linebreak

    set noswapfile
    set nobackup
    set undodir=~/.config/nvim/undodir
    set undofile

    nnoremap c "_c
    set nocompatible
    filetype plugin on
    syntax on
    set encoding=utf-8
    set number relativenumber

    " Tab 2 spaces
    filetype plugin indent on
    set tabstop=4 softtabstop=4 expandtab shiftwidth=4 smarttab

    " Disables automatic commenting on newline:
    autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o

    " Remember last cursor position
    autocmd BufReadPost * if @% !~# '\.git[\/\\]COMMIT_EDITMSG$' && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif

    " Copy entire document
    noremap <leader>y <Esc>ggyG<C-o>
    map <leader>y <Esc>ggyG<C-o>

    " Set gohtml template files to html syntax
    autocmd BufNewFile,BufRead *.gohtml set syntax=html

    " NETRW

    let g:netrw_liststyle = 3
    let g:netrw_banner = 0
    let g:netrw_browse_split = 4
    let g:netrw_winsize = 20
    let g:netrw_altv = 1

    let g:NetrwIsOpen=0
    function! ToggleNetrw()
        if g:NetrwIsOpen
            let i = bufnr("$")
            while (i >= 1)
                if (getbufvar(i, "&filetype") == "netrw")
                    silent exe "bwipeout " . i 
                endif
                let i-=1
            endwhile
            let g:NetrwIsOpen=0
        else
            let g:NetrwIsOpen=1
            silent Lexplore
        endif
    endfunction

    " Add your own mapping. For example:
    noremap <silent> <C-p> :call ToggleNetrw()<CR>

    " coc
    highlight CocErrorSign ctermfg=Black ctermbg=Magenta

    inoremap <silent><expr> <TAB>
                \ pumvisible() ? "\<C-n>" :
                \ <SID>check_back_space() ? "\<TAB>" :
                \ coc#refresh()
    inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

    function! s:check_back_space() abort
        let col = col('.') - 1
        return !col || getline('.')[col - 1]  =~# '\s'
    endfunction

    nmap <leader>gd <Plug>(coc-definition)
    nmap <leader>gr <Plug>(coc-references)
    nmap <leader>rr <Plug>(coc-rename)
    nnoremap <leader>prw :CocSearch <C-R>=expand("<cword>")<CR><CR>

    inoremap <expr> <C-j> pumvisible() ? "\<Down>" : "\<C-j>"
    inoremap <expr> <C-k> pumvisible() ? "\<Up>" : "\<C-k>"

    try
        nmap <silent> <leader>ej :call CocAction('diagnosticNext')<cr>
        nmap <silent> <leader>ek :call CocAction('diagnosticPrevious')<cr>
    endtry

    " Telescope remaps
    lua require("telescope")

    nnoremap <leader>gs :lua require('telescope.builtin').grep_string({ search = vim.fn.input("Grep For > ")})<CR>
    nnoremap <C-q> :lua require('telescope.builtin').git_files()<CR>
    nnoremap <C-a> :lua require('telescope.builtin').find_files()<CR>

    nnoremap <leader>fb <cmd>lua require('telescope.builtin').buffers()<cr>

    " VimWiki

    let pentest_wiki = {}
    let pentest_wiki.path = '~/Documents/PentestWiki/'
    let pentest_wiki.path_html = '~/Documents/PentestWikiHtml/'
    let pentest_wiki.syntax = 'markdown'
    let pentest_wiki.ext = '.md'
    let pentest_wiki.custom_wiki2html = '~/.local/bin/vimwiki_convert.py'

    let g:vimwiki_list = [pentest_wiki]
    let g:vimwiki_ext2syntax = {'.md': 'markdown', '.markdown': 'markdown', '.mdown': 'markdown'}

    au filetype vimwiki silent! iunmap <buffer> <Tab>

    " Spell-check
    map <leader>o :setlocal spell! spelllang=en_au<CR>

    " Shortcutting split navigation, saving a keypress:
    nnoremap <C-h> <C-w>h
    nnoremap <C-j> <C-w>j
    nnoremap <C-k> <C-w>k
    nnoremap <C-l> <C-w>l

    " Replace all is aliased to S.
    nnoremap <c-s> :%s//g<Left><Left>

    " Navigating with guides
    inoremap <leader><leader> <Esc>/<++><Enter>"_c4l
    vnoremap <leader><leader> <Esc>/<++><Enter>"_c4l
    map <leader><leader> <Esc>/<++><Enter>"_c4l

endif
