set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
" utils
Plugin 'nvim-lua/plenary.nvim'
" LSP, syntax etc.
Plugin 'davidhalter/jedi-vim'
Plugin 'neoclide/coc.nvim'
Plugin 'yaegassy/coc-ansible'
Plugin 'pprovost/vim-ps1'
Plugin 'pearofducks/ansible-vim'
" csv magic
Plugin 'mechatroner/rainbow_csv'
" ChatGPT plugin
Plugin 'CamdenClark/flyboy'
" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" see :h vundle for more details or wiki for FAQ

" general stuff

colorscheme slate
let g:mapleader = ","

syntax on
set ignorecase
set smartcase " Include only uppercase words with uppercase search term
set hlsearch
set tabstop=2
set shiftwidth=2
set softtabstop=4
set expandtab
set scrolloff=5 " Scroll 5 lines from top/bottom
set list " show tabs and EOL
set listchars=tab:>>,trail:-,nbsp:+,eol:$
set number " Show line numbers
set rnu " relative line numbers
set cursorline

" status line
set laststatus=2
" set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ [BUFFER=%n]
hi User1 ctermfg=cyan ctermbg=8
hi User2 ctermfg=green ctermbg=8
hi User3 ctermfg=yellow ctermbg=8
hi User4 ctermfg=white ctermbg=8
hi User5 ctermfg=green ctermbg=8
hi User6 ctermfg=red ctermbg=8

set statusline=
set statusline +=%1*\ %n\ %*            "buffer number
set statusline +=%5*%{&ff}%*            "file format
set statusline +=%3*%y%*                "file type
set statusline +=%4*\ %<%F%*            "full path
set statusline +=%6*%m%*                "modified flag
set statusline +=%1*%=%5l%*             "current line
set statusline +=%2*/%L%*               "total lines
set statusline +=%3*\ %p%%%*            "percentage of file
set statusline +=%1*%4v\ %*             "virtual column number
set statusline +=%2*0x%04B\ %*          "character under cursor

" add underscore to word delimiters
" set iskeyword-=_

" WSL Yank support
 " Copy to clipboard
" set clipboard=unnamedplus

if system('uname -r') =~ "Microsoft"
    augroup Yank
        autocmd!
        autocmd TextYankPost * :call system('/mnt/c/windows/system32/clip.exe ',@")
        augroup END
endif

"map <silent> "=p :r !powershell.exe -Command Get-Clipboard<CR>
"map! <silent> <C-r>= :r !powershell.exe -Command Get-Clipboard<CR>
"noremap "+p :exe 'norm a'.system('/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command Get-Clipboard')<CR>

" Jedi Stuff
let g:jedi#force_py_version = 3

" Remember file position
:au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") && &ft !~# 'commit' |  exe "normal! g`\"" | endif

" autocmd FileType python set makeprg=python3\ %
au FileType python set makeprg=pylint\ --reports=n\ --output-format=parseable\ %:p
au FileType python set efm=%A%f:%l:\ [%t%.%#]\ %m,%Z%p^^,%-C%.%#
nnoremap <C-p> :Files<Cr>

au BufNewFile,BufRead *.ps1 set fileencoding=cp1252

" try fixing italics?
set t_ZH=^[[3m
set t_ZR=^[[23m

" persistent undo
if has('persistent_undo')
    set undofile
endif

" generate pw?
" r! cmd.exe /c 'python C:\Tools\pw.py 10'
" =system("cmd.exe /c 'python C:\\Tools\\pw.py 10' | tr -d '\n' | tr -d '\r'")
let pwcmd = 'cmd.exe /c "python C:\\Tools\\pw.py 10" |tr -d "\\n"|tr -d "\\r"'
:nmap <F8> "=system(pwcmd)<C-M>p
:imap <F8> <C-R>=system(pwcmd)<C-M>

" vim-ansible
au BufRead,BufNewFile */playbooks/*.yml set filetype=yaml.ansible
au BufRead,BufNewFile */playbooks/*.yml set keywordprg=ansible-doc
let g:ansible_name_highlight = 'b'
let g:ansible_extra_keywords_highlight = 1
" coc-ansible
set signcolumn=yes
let g:coc_filetype_map = {
  \ 'yaml.ansible': 'ansible',
  \ }

" Use tab for trigger completion with characters ahead and navigate
" NOTE: There's always complete item selected by default, you may want to enable
" no select by `"suggest.noselect": true` in your configuration file
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config
inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

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

" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

" bind code folding to space (in normal mode)
nnoremap <silent> <Space> @=(foldlevel('.')?'za':"\<Space>")<CR>
vnoremap <Space> zf

set foldmethod=indent
set foldnestmax=2
set foldlevel=1
