vim/plugin/functions.vim

113 lines
3.4 KiB
VimL

" Fold Text
set foldtext=FoldText()
function! FoldText()
let l:line = getline(v:foldstart)
return l: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 => l:group
exe 'silent hi ' . a:group
redir END
while l:group =~? 'links to'
let l:index = stridx(l:group, 'links to') + len('links to')
let l:linked_group = strpart(l:group, l:index + 1)
redir => l:group
exe 'silent hi ' . l:linked_group
redir END
endwhile
" Get highlight parameters and existing remove attributes.
let l:parameters = matchlist(l:group, '\<xxx\>\s\+\(.*\)')[1]
let l:parameters = substitute(l:parameters, '\(c\?term\|gui\)=\w\+', '', 'g')
" Create the cloned highlight group with new attributes.
exe 'hi '.a:new_group.' '.l:parameters.
\' term='.a:attributes.' cterm='.a:attributes.' gui='.a:attributes
endfunction
function! s:Synstack()
for l:id in synstack(line('.'), col('.'))
let l:attributes = synIDattr(l:id, 'name')
let l:attr = synIDattr(l:id, 'fg')
if l:attr !=? ''
let l:attributes = l:attributes.' fg='.l:attr
endif
endfor
endfunction
command Synstack :call s:Synstack()
" Stringify
" Make a code block in to a C string literal
function! s: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 s:Stringify()<CR>
" Make a C string literal in to a code block
function! s: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 s: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! s:TabWidth(width)
execute 'setlocal tabstop='.a:width
execute 'setlocal shiftwidth='.a:width
execute 'setlocal softtabstop='.a:width
echo 'Tab width is now '.a:width
endfunction
command! -nargs=1 TabWidth :call s:TabWidth(<f-args>)
" Toggle GitHub style bullet checkbox
function! s: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 s:CheckboxToggle()
nnoremap <leader><CR> :CheckboxToggle<CR>
" Show highlight groups under the cursor
function! s:CursorHighlightGroups()
let l:hi = synIDattr(synID(line('.'),col('.'),1),'name')
let l:trans = synIDattr(synID(line('.'),col('.'),0),'name')
let l:lo = synIDattr(synIDtrans(synID(line('.'),col('.'),1)),'name')
echo 'hi<'.l:hi.'> trans<'.l:trans.'> lo<'.l:lo.'>'
endfunction
command! CursorHighlightGroups :call s:CursorHighlightGroups()
nnoremap <leader>hi :CursorHighlightGroups<CR>