1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-11-16 08:41:31 +01:00
dotfiles/vim/functions.vim
2024-03-01 11:25:12 +01:00

151 lines
4.2 KiB
VimL

""" Helper Functions
function! WinZoomToggle() abort
if ! exists('w:WinZoomIsZoomed')
let w:WinZoomIsZoomed = 0
endif
if w:WinZoomIsZoomed == 0
let w:WinZoomOldWidth = winwidth(0)
let w:WinZoomOldHeight = winheight(0)
wincmd _
wincmd |
let w:WinZoomIsZoomed = 1
elseif w:WinZoomIsZoomed == 1
execute 'resize ' . w:WinZoomOldHeight
execute 'vertical resize ' . w:WinZoomOldWidth
let w:WinZoomIsZoomed = 0
endif
endfunction
" Highlight all instances of word under cursor, when idle.
" Type z/ to toggle highlighting on/off.
nnoremap z/ :if AutoHighlightToggle()<Bar>set hls<Bar>endif<CR>
function! AutoHighlightToggle()
let @/ = ''
if exists('#auto_highlight')
au! auto_highlight
augroup! auto_highlight
setl updatetime=4000
echo 'Highlight current word: off'
return 0
else
augroup auto_highlight
au!
au CursorHold * let @/ = '\V\<'.escape(expand('<cword>'), '\').'\>'
augroup end
setl updatetime=500
echo 'Highlight current word: ON'
return 1
endif
endfunction
" Don't close window, when deleting a buffer
command! Bclose call <SID>BufcloseCloseIt()
function! <SID>BufcloseCloseIt()
let l:currentBufNum = bufnr('%')
let l:alternateBufNum = bufnr('#')
if buflisted(l:alternateBufNum)
buffer #
else
bnext
endif
if bufnr('%') == l:currentBufNum
new
endif
if buflisted(l:currentBufNum)
execute('bdelete! '.l:currentBufNum)
endif
endfunction
function! CmdLine(str)
call feedkeys(':' . a:str)
endfunction
" Call Git show on the selected text (must be a git ref)
function! GitShowVisual() range
execute 'Git show ' . @*
endfunction
function! VisualSelection(direction, extra_filter) range
let l:saved_reg = @"
execute 'normal! vgvy'
let l:pattern = escape(@', "\\/.*'$^~[]")
let l:pattern = substitute(l:pattern, '\n$', '', '')
if a:direction ==# 'ack'
call CmdLine("Ack '" . l:pattern . "' ")
elseif a:direction ==# 'replace'
call CmdLine('%s' . '/'. l:pattern . '/')
endif
let @/ = l:pattern
let @" = l:saved_reg
endfunction
function! GitStatus()
let [a,m,r] = GitGutterGetHunkSummary()
echo printf('+%d ~%d -%d', a, m, r)
endfunction
command! GitStatus call GitStatus()
" Redirect command into scratch buffer. Stolen from romainl
function! Redir(cmd, rng, start, end)
for win in range(1, winnr('$'))
if getwinvar(win, 'scratch')
execute win . 'windo close'
endif
endfor
if a:cmd =~? '^!'
let cmd = a:cmd =~?' %'
\ ? matchstr(substitute(a:cmd, ' %', ' ' . expand('%:p'), ''), '^!\zs.*')
\ : matchstr(a:cmd, '^!\zs.*')
if a:rng == 0
let output = systemlist(cmd)
else
let joined_lines = join(getline(a:start, a:end), '\n')
let cleaned_lines = substitute(shellescape(joined_lines), "'\\\\''", "\\\\'", 'g')
let output = systemlist(cmd . ' <<< $' . cleaned_lines)
endif
else
redir => output
execute a:cmd
redir END
let output = split(output, "\n")
endif
vnew
let w:scratch = 1
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
call setline(1, output)
endfunction
command! -nargs=1 -complete=command -bar -range Redir silent call Redir(<q-args>, <range>, <line1>, <line2>)
" Gets the man page for the given command
function! Man(cmd)
vnew
let w:scratch=1
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile filetype=man nonumber norelativenumber
let output = system('env COLUMNS=80 man ' . a:cmd)
call setline(1, split(output, '\n'))
endfunction
command! -nargs=1 -complete=shellcmd Vert Man silent call Man(<q-args>)
" Execute the given command and print it in a scratch buffer
function! Cmd(cmd)
let output = system(a:cmd)
vnew
let w:scratch=1
" setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
setlocal buftype=popup bufhidden=wipe nobuflisted noswapfile
setlocal nonumber norelativenumber
call setline(1, split(output, '\n'))
endfunction
command! -nargs=1 -complete=shellcmd Cmd silent call Cmd(<q-args>)