set nocompatible
" setno
set nobackup
set nocursorline
set noruler
set noshowcmd
set noshowmatch
set nospell
set noswapfile
set notitle
set nowritebackup
" general
set autoindent
set autoread
set background=dark
set backspace=indent,eol,start
set belloff=all
set clipboard=unnamedplus
set colorcolumn=80
set complete-=w,b,u,t
set completeopt=menu,menuone,noselect shortmess+=c
set completeopt-=preview
set confirm
set diffopt+=vertical
set encoding=utf-8
scriptencoding utf-8
set expandtab
set fileformats=unix,dos,mac
set foldmethod=marker
set formatoptions-=r
set formatoptions-=o
set grepprg=rg\ --vimgrep\ --smart-case\ $*
set hidden
set history=100
set ignorecase
set laststatus=2
set list listchars=tab:⇥\ ,trail:˽,nbsp:+
set magic
set mouse=
set nrformats-=octal
set number
set omnifunc=syntaxcomplete#Complete
"set pumheight=10
set scrolloff=10
set sessionoptions-=options
set shiftround
set shiftwidth=2
set shortmess+=sI
set showbreak=↪
set signcolumn=yes
set smartcase
set smartindent
set smarttab
set spellcapcheck=
set splitbelow splitright
set synmaxcol=1000
set tabpagemax=5
set tabstop=2
set timeout timeoutlen=500
"set undolevels=500
set viewoptions-=options
set viminfo=
set virtualedit=block
set wildcharm=<C-Z>
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/node_modules/*
set wildignore+=*.swp,*~,._*,*/.DS_Store,.localized,*/log/*,*/tmp/*
set wildignorecase
set wildmenu
set wildmode=full
if has('nvim')
  " set fillchars+=msgsep:\ ,vert:\│
  set inccommand=split
  " set laststatus=3
  set shada='20,<50,s10
  " lua vim.loader.enable()
elseif has('vim9script')
  set complete-=i
  set cpoptions+=J
  set cryptmethod=blowfish2
  " set fillchars=vert:│,fold:-,eob:~
  " set fillchars=fold:\ ,foldopen:,foldclose:,foldsep:\ ,diff:/,eob:\.
  " set formatoptions+=j
  set ttyfast
  " set ttimeout ttimeoutlen=100
  set wildoptions=pum
endif
" cursor {{{
func! CursorShape() abort
  if has('nvim')
    set guicursor=a:ver25-Cursor/lCursor
  else
    let &t_te.="\e[5 q"
  endif
endfunc
" cursor shape in Vim
if !has('nvim')
  let &t_ti.="\e[2 q"
  let &t_SI.="\e[6 q"
  let &t_EI.="\e[2 q"
  let &t_te.="\e[0 q"
" fix italics in Vim
  let &t_ZH="\e[3m"
  let &t_ZR="\e[23m"
endif
augroup custom_cursor
  autocmd!
  autocmd VimLeave * call CursorShape()
augroup END
" }}}

" leader
let mapleader = "\<Space>"
let maplocalleader = "\<Space>"

" unmap-fix
nnoremap q: <Nop>
nnoremap q/ <Nop>
nnoremap q? <Nop>
" fix-typos.
cnoreabbrev W! w!
cnoreabbrev Q! q!
cnoreabbrev wQ wq
cnoreabbrev W w
command! WQ wq
command! Wq wq
command! Q q
" don't enter ex mode when Q pressed.
nnoremap Q <Nop>
" don't show me man pages when K pressed.
nnoremap K <Nop>
" don't open command history - instead quit.
" map q: :q
" map Q: :q
" command! -bang -nargs=* Q q
" don't start rec.
" nnoremap q <Nop>
" <PageUp> - <PageDown> - do silly things in normal mode with fold.
noremap <PageUp> <C-U>
noremap <PageDown> <C-D>

" command
command! CD cd %:p:h
" :SA - show global variables.
command! SA let g: v:
command! VARS let g: v:
" :SC - show scriptname.
command! SC scriptnames
" :SL - show buffer variables.
command! SL let b: t: w:

" fn
" f3 - mouse toggle.
nmap <silent> <F3> <Esc>:exec &mouse!=""? "set mouse=" : "set mouse=nvi"<CR>
vmap <silent> <F3> <Esc>:exec "normal \<F3>"<CR>v
imap <silent> <F3> <Esc>:exec "normal \<F3>"<CR>i
" f8 - colorcolumn width=80.
nmap <silent> <F8> :exec &cc!=""? "set cc=" : "set cc=80"<CR>

" shortcuts
" <Enter>
nnoremap <ENTER> :
" * - star
nnoremap * *``
" <Esc><Esc> - nohlsearch.
nnoremap <Esc> <Cmd>noh<CR><Esc>
" < > - better-indenting. visual shifting (builtin-repeat)
vnoremap < <gv
vnoremap > >gv
" <Tab> <S-Tab> - visual shift.
nnoremap <Tab>   >>
nnoremap <S-Tab> <<
vnoremap <Tab>   >><Esc>gv
vnoremap <S-Tab> <<<Esc>gv
" inoremap <S-Tab> <C-D>
" ` - remap jumps. better mark jumping (line + col)
nnoremap ' `
noremap ` '
" ,. - go to last edited line location with ,.
nnoremap ,. '.
" ,q - quit.
nnoremap ,q :q<CR>
" ,w - update.
nnoremap ,w :update<CR>
" c - when changing text, don't put the replaced text into the default registry.
nnoremap c "_c
vnoremap c "_c
" d - delete without yanking.
nnoremap d "_d
vnoremap d "_d
" ff - fold-unfold.
nnoremap <silent> ff @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
" jj - escape.
inoremap jj <Esc>
cnoremap jj <C-C>
" j - k - better up/down. move through folded lines.
nnoremap <silent><expr> j (v:count == 0 ? 'gj' : 'j')
nnoremap <silent><expr> k (v:count == 0 ? 'gk' : 'k')
" treat long lines as break lines (useful when moving around in them)
" nmap j gj
" nmap k gk
" vmap j gj
" vmap k gk
" n - center cursor vertically when moving during a search.
nnoremap n nzz
nnoremap N Nzz
" o - new line below+exit insert.
nnoremap o o<Esc>
" O - new line above+exit insert.
nnoremap O O<Esc>
" p - automatically jump to end of the pasted text. paste multiple times with simple ppppp.
nnoremap <silent> p p`]
vnoremap <silent> p p`]
" Q - close windows.
nnoremap <silent>Q <Cmd>close<CR>
" S - save
nnoremap S :w<CR>
" U - redo.
nnoremap U <C-R>
" v - quit visual mode.
vnoremap v <Esc>
" vv - to select line.
nnoremap vv ^vg_
" x - X - delete without placing into registry.
nnoremap x "_x
vnoremap x "_x
nmap X "_d
nmap XX "_dd
vmap X "_d
" y - make visual yanks pace the cursor back where started.
vnoremap y ygv<Esc>
" Y - Yank copy line.
nnoremap Y y$

" z-folds
" z+Up/Down/Left/Right to jump between folding nodes
nnoremap z<Left> zk
nnoremap z<Right> zj
nnoremap z<Up> [z
nnoremap z<Down> ]z
" z+hjkl to jump between folding nodes
nnoremap zh zk
nnoremap zl zj
nnoremap zj ]z
nnoremap zk [z
" zO - recursively open top level fold we're in. no matter where cursor is.
nnoremap zO zCzO

" leader
"nnoremap <Space> <Nop>
map <Space>fix :cnoremap % %<CR>
" _cc - close quickfix.
nnoremap <silent><Leader>cc :cclose<CR>
" _cm - shows user commands.
nnoremap <Leader>cm :<C-U>command<CR>
" _J - join lines without space.
nnoremap <silent> <Leader>J mzJ`z
" _ _n - toggles line numbers.
" {{{togglenumber
func! ToggleNumber()
  if(&number == 1)
    set norelativenumber
    set nonumber
  else
    set relativenumber
    set number
  endif
endfunc
" }}}
nnoremap <Leader><Space>n <Cmd>call ToggleNumber()<CR>
" _o - _O - insert line above-below.
nnoremap <Leader>o o<Esc>^Da
nnoremap <Leader>O O<Esc>^Da
" _p - paste from system clipboard.
nmap <silent> <Leader>p "+p
vmap <Leader>p "+p
" _q - quit current window.
nnoremap <Leader>q :q<CR>
" _Q - close quickfix-list location-list preview-window.
nnoremap <silent> <Leader>Q :<C-U>cclose<CR>:lclose<CR>:pclose<CR>
" _y - yank to system clipboard.
nmap <Leader>y "+y
vmap <Leader>y "+y
" w - <Space> w fast save.
nnoremap <Leader>w :w<CR>
" <Space><Space> - enter visual line mode.
nmap <Leader><Space> V

" shift
" shift+HJKL to quickly move cursor
nnoremap K 7<Up>
nnoremap J 7<Down>
nnoremap H 0
nnoremap L $
" shift+HJKL to quickly move cursor.
vnoremap K 5<Up>
vnoremap J 5<Down>
vnoremap H 0
vnoremap L $h
" shift+Up/Down/Left/Right to quickly move cursor.
nnoremap <S-Up> <Esc>7<Up>
nnoremap <S-Down> <Esc>7<Down>
nnoremap <S-Left> <Esc>0
nnoremap <S-Right> <Esc>$
" shift+Up/Down/Left/Right to quickly move cursor
inoremap <silent><expr> <S-up> pumvisible() ? "\<Space>\<Backspace>\<up>\<up>\<up>\<up>\<up>" : "\<up>\<up>\<up>\<up>\<up>"
inoremap <silent><expr> <S-down> pumvisible() ? "\<Space>\<Backspace>\<down>\<down>\<down>\<down>\<down>" : "\<down>\<down>\<down>\<down>\<down>"
inoremap <S-left> <Esc>I
inoremap <S-right> <Esc>A
" shift+HJKL to quickly move cursor.
" vnoremap K 5<Up>
" vnoremap J 5<Down>
vnoremap H 0
vnoremap L $h
" J - K - move selected text up down.
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv
" shift+Up/Down/Left/Right to quickly move cursor.
vnoremap <S-Up> <Up><Up><Up><Up><Up>
vnoremap <S-Down> <Down><Down><Down><Down><Down>
vnoremap <S-Left> 0
if has('nvim')
  vnoremap <S-right> $
else
  vnoremap <S-right> $<Left>
endif
" shift+Up/Down/Left/Right to quickly move cursor.
tnoremap <S-Left> <C-A>
tnoremap <S-Right> <C-E>

" ctrl
" c-A - home.
nnoremap <C-A> 0
inoremap <C-A> <Home>
cnoremap <C-A> <Home>
" c-C - yank.
vmap <C-C> y
xnoremap <C-C> "+y
nnoremap <silent> cp "+y
nnoremap <silent> cpp "+yy
" c-D. delete.
inoremap <C-D> <Delete>
cnoremap <C-D> <Delete>
" c-E. end.
if !has('nvim')
  nnoremap <expr> <C-E> utils#misc#scroll_cursor_popup(v:count1) ? "<Esc>" : "<C-E>"
  nnoremap <expr> <C-Y> utils#misc#scroll_cursor_popup(-v:count1) ? "<Esc>" : "<C-Y>"
endif
nnoremap <C-E> $
inoremap <C-E> <End>
cnoremap <C-E> <End>
" c-H. ret.
inoremap <C-H> <BS>
cnoremap <C-H> <BS>
" c-J. down.
inoremap <C-J> <Down>
cnoremap <C-J> <Down>
" c-K. up.
inoremap <C-K> <Up>
cnoremap <C-K> <Up>
" ctrl + s | save.
nnoremap <silent> <C-S> :<C-U>w<CR>
inoremap <silent> <C-S> <Esc>:w<CR>a
" ctrl+q - fast quit get notified of changes.
noremap <C-Q> :qa<CR>
inoremap <C-Q> <Esc>:qa<CR>
" quit vim terminal.
tnoremap <C-Q> <C-\><C-N>
" ctrl + u | undo insert mode.
inoremap <C-U> <C-G>u<C-U>
" ctrl + v | paste from buffer.
inoremap <C-V> <Space><Backspace><Esc>pa
vmap <C-V> p
" ctrl vimaps
"vmap <C-W> S
"vmap <C-X> d
" ctrl + w | delete only word before + all text before in insert mode.
inoremap <C-W> <C-G>u<C-W>
" c-Y - redo
inoremap <C-Y> <Esc>:redo<CR>
" c-Z - fg-bg.
if has('nvim')
  autocmd TermOpen * startinsert
  autocmd TermOpen * setlocal nonu fdc=0 scl=no
  nmap <C-Z> :te<CR>
else
  nmap <C-Z> :sh<CR>
endif
" c-Z - undo.
"nnoremap <C-Z> :undo<CR>
inoremap <C-Z> <Esc>:undo<CR>

" completion
" multipurpose tab key
" indent if we're at the beginning of a line. else, do completion.
function! InsertTabWrapper()
  let col = col('.') - 1
  if !col
    return "\<Tab>"
  endif
  let char = getline('.')[col - 1]
  if char =~ '\k'
    " there's an identifier before the cursor, so complete the identifier.
    return "\<C-P>"
  else
    return "\<Tab>"
  endif
endfunction
inoremap <expr> <Tab> InsertTabWrapper()
inoremap <S-Tab> <C-N>
"   Space - context aware omni completion (via 'omnifunc' setting)
"   b     - keyword completion from the current buffer (<C-b><C-n> to extend)
"   d     - dictionary completion (via 'dictionary' setting)
"   f     - file path completion
"   l     - line completion (repeat an existing line)
inoremap <C-Space> <C-X><C-O>
inoremap <C-B>     <C-X><C-P>
" inoremap <C-D>     <C-X><C-K>
" inoremap <C-F>     <C-X><C-F>
inoremap <C-L>     <C-X><C-L>
"inoremap <expr> <Tab>   pumvisible() ? "\<C-N>" : "\<Tab>"
"inoremap <expr> <S-Tab> pumvisible() ? "\<C-P>" : "\<S-Tab>"

" command-line
" Alt+X/Alt+Z/Ctrl+Z to enter normal mode
tnoremap <A-x> <C-\><C-n>
tnoremap <A-z> <C-\><C-n>
tnoremap <C-z> <C-\><C-n>
" Alt+HJKL to jump between windows
tnoremap <silent> <A-h> <C-\><C-n>:<C-u>wincmd h<CR>
tnoremap <silent> <A-j> <C-\><C-n>:<C-u>wincmd j<CR>
tnoremap <silent> <A-k> <C-\><C-n>:<C-u>wincmd k<CR>
tnoremap <silent> <A-l> <C-\><C-n>:<C-u>wincmd l<CR>
" Alt+Left/Right to switch tabs
tnoremap <A-left> <C-\><C-n>gT
tnoremap <A-right> <C-\><C-n>gt
" Alt+Up/Down to move tabs
tnoremap <silent> <A-up> <C-\><C-n>:<C-u>tabm -1<CR>
tnoremap <silent> <A-down> <C-\><C-n>:<C-u>tabm +1<CR>

" create files directory before saving, if it doesnt exist. mkdir/saveas.
augroup BWCCreateDir
  autocmd!
  autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END
func! s:MkNonExDir(file, buf)
  if empty(getbufvar(a:buf, '&buftype')) && a:file !~# '\v^\w+\:\/'
    call mkdir(fnamemodify(a:file, ':h'), 'p')
  endif
endfunc

colorscheme slate