" commentary.vim - Comment stuff out
" Maintainer: Tim Pope
" Version: 1.3
" GetLatestVimScripts: 3695 1 :AutoInstall: commentary.vim
if exists("g:loaded_commentary") || &cp || v:version < 700
finish
endif
let g:loaded_commentary = 1
function! s:surroundings() abort
return split(get(b:, 'commentary_format', substitute(substitute(
\ &commentstring, '\S\zs%s',' %s','') ,'%s\ze\S', '%s ', '')), '%s', 1)
endfunction
function! s:strip_white_space(l,r,line) abort
let [l, r] = [a:l, a:r]
if l[-1:] == ' ' && stridx(a:line,l) == -1 && stridx(a:line,l[0:-2]) == 0
let l = l[:-2]
endif
if r[0] == ' ' && a:line[-strlen(r):] != r && a:line[1-strlen(r):] == r[1:]
let r = r[1:]
endif
return [l, r]
endfunction
function! s:go(type,...) abort
if a:0
let [lnum1, lnum2] = [a:type, a:1]
else
let [lnum1, lnum2] = [line("'["), line("']")]
endif
let [l, r] = s:surroundings()
let uncomment = 2
for lnum in range(lnum1,lnum2)
let line = matchstr(getline(lnum),'\S.*\s\@ 2 && l.r !~# '\\'
let line = substitute(line,
\'\M'.r[0:-2].'\zs\d\*\ze'.r[-1:-1].'\|'.l[0].'\zs\d\*\ze'.l[1:-1],
\'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g')
endif
if uncomment
let line = substitute(line,'\S.*\s\@ Commentary :call go(line("'<"),line("'>"))
nnoremap Commentary :set opfunc=gog@
nnoremap CommentaryLine :set opfunc=goexe 'norm! 'v:count1.'g@_'
onoremap Commentary :call textobject(0)
nnoremap ChangeCommentary c:call textobject(1)
nmap CommentaryUndo CommentaryCommentary
command! -range -bar Commentary call s:go(,)
if !hasmapto('Commentary') || maparg('gc','n') ==# ''
xmap gc Commentary
nmap gc Commentary
omap gc Commentary
nmap gcc CommentaryLine
if maparg('c','n') ==# ''
nmap cgc ChangeCommentary
endif
nmap gcu CommentaryCommentary
endif
" vim:set et sw=2: