Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Last active September 10, 2017 07:37
Show Gist options
  • Save PeterRincker/cf3c908f3fbaf7f9590850af18515eb5 to your computer and use it in GitHub Desktop.
Save PeterRincker/cf3c908f3fbaf7f9590850af18515eb5 to your computer and use it in GitHub Desktop.
vim-prismo with a twist
"Prismo.vim
" version of vim-prismo I took a crack at reimplimenting
" https://github.com/guywald1/vim-prismo
"
" Options:
" 'commentstring' - See :h 'commentstring'. Can use b:commentstring to override
" g:prismo_dash - padding character defaults to dash
" g:prismo_toupper - transform the title to uppercase (default to 1)
" b:prismo_width - width of header default to 'textwidth' or 80 if 'textwidth' is 0
"
" Command:
" :{range}Prismo - Makes header comments by centering and add padding to comments
"
" Mappings:
" <Plug>(prismo) - Normal mode operator to apply :Prismo over a motion. Also Visual mode mapping.
" <Plug>(prismo-line) - Normal command to apply :Prismo over current line.
"
" Example mappings ("ch" for create heading):
" nmap chh <Plug>(prismo-line)
" nmap ch <Plug>(prismo)
" xmap H <Plug>(prismo)
if exists('g:loaded_prismo') || &cp
finish
endif
let g:loaded_prismo = 1
function! s:prismo() range
let comment = substitute(get(b:, 'commentstring', &commentstring), '\s*\(%s\)\s*', '%s', '')
let pattern = '\V' . printf(escape(comment, '\'), '\s{-1,}\(\S\.\{-}\)\s\=')
let dash = get(b:, 'prismo_dash', get(g:, 'prismo_dash', '-'))
let d = escape(dash, '\')
let width = get(b:, 'prismo_width', &textwidth ? &textwidth : 80)
let spacing = comment =~ '%s$' ? 1 : 2
for lnum in range(a:firstline, a:lastline)
if getline(lnum) =~# pattern
let text = substitute(getline(lnum), pattern, '\1', '')
let text = substitute(text, '^\(\s\|'.d.'\)*', '', '')
let text = substitute(text, '\(\s\|'.d.'\)*$', '', '')
if get(g:, 'prismo_toupper')
let text = toupper(text)
endif
let text = ' '.text.' '
let indent = matchstr(getline(lnum), '^\s*')
let padding = repeat(dash, width - indent(lnum) - len(text) - len(printf(comment, '')) - spacing)
let text = substitute(padding, '^\('.d.'*\)\(\1'.d.'\=\)$', '\1'.escape(text, '~&\').'\2', '')
let text = ' ' . text . (spacing == 2 ? ' ' : '')
call setline(lnum, indent . printf(comment, text))
endif
endfor
endfunction
function! s:prismoOp(...)
'[,']Prismo
endfunction
command! -range -bar Prismo <line1>,<line2>call <SID>prismo()
nnoremap <silent> <Plug>(prismo-line) :<c-u>.,.+<c-r>=v:count<cr>Prismo<cr>
nnoremap <silent> <Plug>(prismo) :<c-u>set opfunc=<SID>prismoOp<cr>g@
xnoremap <silent> <Plug>(prismo) :Prismo<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment