vim/plugin/functions.vim

117 lines
3.3 KiB
VimL

" Fold Text
set foldtext=FoldText()
function! FoldText()
let line = getline(v:foldstart)
return line.' '.string(v:foldend - v:foldstart + 1).' lines '
endfunction
" Derived from http://stackoverflow.com/a/1333025
function! CloneHighlightGroupWithAttributes(group, new_group, attributes)
" Get group details, resolving group links.
redir => group
exe "silent hi " . a:group
redir END
while group =~ "links to"
let index = stridx(group, "links to") + len("links to")
let linked_group = strpart(group, index + 1)
redir => group
exe "silent hi " . linked_group
redir END
endwhile
" Get highlight parameters and existing remove attributes.
let parameters = matchlist(group, '\<xxx\>\s\+\(.*\)')[1]
let parameters = substitute(parameters, '\(c\?term\|gui\)=\w\+', '', 'g')
" Create the cloned highlight group with new attributes.
exe 'hi '.a:new_group.' '.parameters.
\' term='.a:attributes.' cterm='.a:attributes.' gui='.a:attributes
endfunction
function! <SID>Synstack()
for id in synstack(line('.'), col('.'))
let attributes = synIDattr(id, 'name')
let attr = synIDattr(id, 'fg')
if attr != ''
let attributes = attributes.' fg='.attr
endif
endfor
endfunction
command Synstack :call <SID>Synstack()
" Strip trailing whitespace
function! <SID>StripWhitespace()
let l = line(".")
let c = col(".")
%s/\s\+$//e
call cursor(l, c)
endfunction
command! StripWhitespace :call <SID>StripWhitespace()
augroup strip_white_space
" Strip whitespace on buffer write
autocmd!
autocmd BufWritePre * :call <SID>StripWhitespace()
augroup END
" Stringify
" Make a code block in to a C string literal
function! <SID>Stringify()
" Escape existing escape characters
execute 's/\\/\\\\/ge'
" Escape quotes
execute 's/"/\\"/ge'
" Prepend quote
execute 's/^/"/g'
" Append carriage return, quote
execute 's/$/\\n"/g'
noh
endfunction
map <silent> <leader>s :call <SID>Stringify()<CR>
" Make a C string literal in to a code block
function! <SID>Destringify()
" Remove final quote and carriage return
execute 's/\\n"\s*$//ge'
" Remove first quote
execute 's:^\(\s*\)":\1:ge'
" Remove quote escapes
execute 's/\\"/"/ge'
" Remove escapes of escapes characters
execute 's/\\\\/\\/ge'
noh
endfunction
map <silent> <leader>S :call <SID>Destringify()<CR>
" Invoke terminal command without prompt and then redraw.
command! -nargs=+ Silent execute 'silent <args>' | redraw!
" Set the tab width for the current filetype
function! <SID>TabWidth(width)
execute "set tabstop=".a:width
execute "set shiftwidth=".a:width
execute "set softtabstop=".a:width
echo "Tab width is now ".a:width
endfunction
command! -nargs=1 TabWidth :call <SID>TabWidth(<f-args>)
" Toggle GitHub style bullet checkbox
function! <SID>CheckboxToggle()
" Get current line
let l:line = getline('.')
" Get the ' ' or 'x' character from within the task bullet
let l:pattern = '[-\*+] \[\zs[ x]\ze]'
let l:char = matchstr(l:line, l:pattern)
" Toggle the ' ' or 'x' character
if l:char == 'x'
let l:char = ' '
else
let l:char = 'x'
endif
" Replace the current line with a new one
call setline(line('.'), substitute(l:line, l:pattern, l:char, ''))
endfunction
command! CheckboxToggle :call <SID>CheckboxToggle()
nnoremap <leader><CR> :CheckboxToggle<CR>