1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-12-05 01:01:30 +01:00
dotfiles/vim/functions.vim

Failed to ignore revisions in .git-blame-ignore-revs.

151 lines
4.2 KiB
VimL
Raw Normal View History

2020-12-06 02:29:15 +01:00
""" 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
2023-01-19 17:33:11 +01:00
execute 'resize ' . w:WinZoomOldHeight
execute 'vertical resize ' . w:WinZoomOldWidth
2020-12-06 02:29:15 +01:00
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()
2023-01-19 17:33:11 +01:00
let l:currentBufNum = bufnr('%')
let l:alternateBufNum = bufnr('#')
2020-12-06 02:29:15 +01:00
if buflisted(l:alternateBufNum)
buffer #
else
bnext
endif
2023-01-19 17:33:11 +01:00
if bufnr('%') == l:currentBufNum
2020-12-06 02:29:15 +01:00
new
endif
if buflisted(l:currentBufNum)
2023-01-19 17:33:11 +01:00
execute('bdelete! '.l:currentBufNum)
2020-12-06 02:29:15 +01:00
endif
endfunction
function! CmdLine(str)
2023-01-19 17:33:11 +01:00
call feedkeys(':' . a:str)
2020-12-06 02:29:15 +01:00
endfunction
" Call Git show on the selected text (must be a git ref)
function! GitShowVisual() range
2023-01-19 17:33:11 +01:00
execute 'Git show ' . @*
endfunction
2020-12-06 02:29:15 +01:00
function! VisualSelection(direction, extra_filter) range
let l:saved_reg = @"
2023-01-19 17:33:11 +01:00
execute 'normal! vgvy'
2020-12-06 02:29:15 +01:00
2023-01-19 17:33:11 +01:00
let l:pattern = escape(@', "\\/.*'$^~[]")
let l:pattern = substitute(l:pattern, '\n$', '', '')
2020-12-06 02:29:15 +01:00
2023-01-19 17:33:11 +01:00
if a:direction ==# 'ack'
call CmdLine("Ack '" . l:pattern . "' ")
2023-01-19 17:33:11 +01:00
elseif a:direction ==# 'replace'
call CmdLine('%s' . '/'. l:pattern . '/')
2020-12-06 02:29:15 +01:00
endif
let @/ = l:pattern
let @" = l:saved_reg
endfunction
function! GitStatus()
let [a,m,r] = GitGutterGetHunkSummary()
2021-09-26 15:52:21 +02:00
echo printf('+%d ~%d -%d', a, m, r)
2020-12-06 02:29:15 +01:00
endfunction
2021-09-26 15:52:21 +02:00
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
2023-01-19 17:33:11 +01:00
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')
2023-01-19 17:33:11 +01:00
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
2024-01-16 11:52:12 +01:00
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
2024-01-16 11:52:12 +01:00
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>)