2014-03-02 09:35:00 -05:00
|
|
|
" commentary.vim - Comment stuff out
|
|
|
|
" Maintainer: Tim Pope <http://tpo.pe/>
|
2014-03-11 16:10:50 -04:00
|
|
|
" Version: 1.2
|
2014-03-02 09:35:00 -05:00
|
|
|
" GetLatestVimScripts: 3695 1 :AutoInstall: commentary.vim
|
|
|
|
|
|
|
|
if exists("g:loaded_commentary") || &cp || v:version < 700
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_commentary = 1
|
|
|
|
|
2014-03-11 16:10:50 -04:00
|
|
|
function! s:surroundings() abort
|
|
|
|
return split(substitute(substitute(
|
|
|
|
\ get(b:, 'commentary_format', &commentstring)
|
|
|
|
\ ,'\S\zs%s',' %s','') ,'%s\ze\S', '%s ', ''), '%s', 1)
|
|
|
|
endfunction
|
|
|
|
|
2014-03-02 09:35:00 -05:00
|
|
|
function! s:go(type,...) abort
|
|
|
|
if a:0
|
|
|
|
let [lnum1, lnum2] = [a:type, a:1]
|
|
|
|
else
|
|
|
|
let [lnum1, lnum2] = [line("'["), line("']")]
|
|
|
|
endif
|
|
|
|
|
2014-03-11 16:10:50 -04:00
|
|
|
let [l, r] = s:surroundings()
|
2014-03-02 09:35:00 -05:00
|
|
|
let uncomment = 2
|
|
|
|
for lnum in range(lnum1,lnum2)
|
|
|
|
let line = matchstr(getline(lnum),'\S.*\s\@<!')
|
|
|
|
if line != '' && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
|
|
|
|
let uncomment = 0
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
for lnum in range(lnum1,lnum2)
|
|
|
|
let line = getline(lnum)
|
|
|
|
if strlen(r) > 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\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
|
|
|
|
else
|
|
|
|
let line = substitute(line,'^\%('.matchstr(getline(lnum1),'^\s*').'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
|
|
|
|
endif
|
|
|
|
call setline(lnum,line)
|
|
|
|
endfor
|
2014-04-18 08:58:02 -04:00
|
|
|
let modelines = &modelines
|
|
|
|
try
|
|
|
|
set modelines=0
|
|
|
|
silent doautocmd User CommentaryPost
|
|
|
|
finally
|
|
|
|
let &modelines = modelines
|
|
|
|
endtry
|
2014-03-02 09:35:00 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-04-18 08:58:02 -04:00
|
|
|
function! s:textobject(inner) abort
|
2014-03-11 16:10:50 -04:00
|
|
|
let [l, r] = s:surroundings()
|
2014-03-02 09:35:00 -05:00
|
|
|
let lnums = [line('.')+1, line('.')-2]
|
|
|
|
for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']]
|
|
|
|
while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
|
|
|
|
let lnums[index] += dir
|
|
|
|
let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!')
|
|
|
|
endwhile
|
|
|
|
endfor
|
2014-04-18 08:58:02 -04:00
|
|
|
while (a:inner || lnums[1] != line('$')) && empty(getline(lnums[0]))
|
|
|
|
let lnums[0] += 1
|
|
|
|
endwhile
|
|
|
|
while a:inner && empty(getline(lnums[1]))
|
|
|
|
let lnums[1] -= 1
|
|
|
|
endwhile
|
|
|
|
if lnums[0] <= lnums[1]
|
|
|
|
execute 'normal! 'lnums[0].'GV'.lnums[1].'G'
|
|
|
|
endif
|
2014-03-02 09:35:00 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>
|
|
|
|
nnoremap <silent> <Plug>Commentary :<C-U>set opfunc=<SID>go<CR>g@
|
|
|
|
nnoremap <silent> <Plug>CommentaryLine :<C-U>set opfunc=<SID>go<Bar>exe 'norm! 'v:count1.'g@_'<CR>
|
2014-04-18 08:58:02 -04:00
|
|
|
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(0)<CR>
|
|
|
|
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
|
|
|
|
nmap <silent> <Plug>CommentaryUndo <Plug>Commentary<Plug>Commentary
|
2014-07-02 07:18:18 -04:00
|
|
|
command! -range -bar Commentary call s:go(<line1>,<line2>)
|
2014-03-02 09:35:00 -05:00
|
|
|
|
2014-04-18 08:58:02 -04:00
|
|
|
if 1 || !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
|
2014-03-02 09:35:00 -05:00
|
|
|
xmap gc <Plug>Commentary
|
|
|
|
nmap gc <Plug>Commentary
|
2014-04-18 08:58:02 -04:00
|
|
|
omap gc <Plug>Commentary
|
2014-03-02 09:35:00 -05:00
|
|
|
nmap gcc <Plug>CommentaryLine
|
2014-04-18 08:58:02 -04:00
|
|
|
nmap cgc <Plug>ChangeCommentary
|
|
|
|
nmap gcu <Plug>Commentary<Plug>Commentary
|
2014-03-02 09:35:00 -05:00
|
|
|
endif
|
|
|
|
|
2014-03-11 16:10:50 -04:00
|
|
|
if maparg('\\','n') ==# '' && maparg('\','n') ==# '' && get(g:, 'commentary_map_backslash', 1)
|
2014-04-18 08:58:02 -04:00
|
|
|
xmap \\ <Plug>Commentary:echomsg '\\ is deprecated. Use gc'<CR>
|
|
|
|
nmap \\ :echomsg '\\ is deprecated. Use gc'<CR><Plug>Commentary
|
|
|
|
nmap \\\ <Plug>CommentaryLine:echomsg '\\ is deprecated. Use gc'<CR>
|
|
|
|
nmap \\u <Plug>CommentaryUndo:echomsg '\\ is deprecated. Use gc'<CR>
|
2014-03-02 09:35:00 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
" vim:set et sw=2:
|