2014-03-02 09:35:00 -05:00
|
|
|
" commentary.vim - Comment stuff out
|
|
|
|
" Maintainer: Tim Pope <http://tpo.pe/>
|
2016-02-20 08:13:10 -05:00
|
|
|
" Version: 1.3
|
2014-03-02 09:35:00 -05:00
|
|
|
" GetLatestVimScripts: 3695 1 :AutoInstall: commentary.vim
|
|
|
|
|
2022-05-19 09:37:19 -04:00
|
|
|
if exists("g:loaded_commentary") || v:version < 703
|
2014-03-02 09:35:00 -05:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_commentary = 1
|
|
|
|
|
2014-03-11 16:10:50 -04:00
|
|
|
function! s:surroundings() abort
|
2018-07-30 17:18:16 -04:00
|
|
|
return split(get(b:, 'commentary_format', substitute(substitute(substitute(
|
|
|
|
\ &commentstring, '^$', '%s', ''), '\S\zs%s',' %s', '') ,'%s\ze\S', '%s ', '')), '%s', 1)
|
2014-03-11 16:10:50 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
function! s:strip_white_space(l,r,line) abort
|
|
|
|
let [l, r] = [a:l, a:r]
|
2018-06-14 06:31:12 -04:00
|
|
|
if l[-1:] ==# ' ' && stridx(a:line,l) == -1 && stridx(a:line,l[0:-2]) == 0
|
2017-11-24 08:54:40 -05:00
|
|
|
let l = l[:-2]
|
|
|
|
endif
|
2018-06-14 06:31:12 -04:00
|
|
|
if r[0] ==# ' ' && a:line[-strlen(r):] != r && a:line[1-strlen(r):] == r[1:]
|
2017-11-24 08:54:40 -05:00
|
|
|
let r = r[1:]
|
2015-12-08 08:20:04 -05:00
|
|
|
endif
|
|
|
|
return [l, r]
|
|
|
|
endfunction
|
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
function! s:go(...) abort
|
|
|
|
if !a:0
|
|
|
|
let &operatorfunc = matchstr(expand('<sfile>'), '[^. ]*$')
|
|
|
|
return 'g@'
|
|
|
|
elseif a:0 > 1
|
|
|
|
let [lnum1, lnum2] = [a:1, a:2]
|
2014-03-02 09:35:00 -05:00
|
|
|
else
|
|
|
|
let [lnum1, lnum2] = [line("'["), line("']")]
|
|
|
|
endif
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
let [l, r] = s:surroundings()
|
2014-03-02 09:35:00 -05:00
|
|
|
let uncomment = 2
|
2021-10-11 05:30:43 -04:00
|
|
|
let force_uncomment = a:0 > 2 && a:3
|
2014-03-02 09:35:00 -05:00
|
|
|
for lnum in range(lnum1,lnum2)
|
|
|
|
let line = matchstr(getline(lnum),'\S.*\s\@<!')
|
2017-11-24 08:54:40 -05:00
|
|
|
let [l, r] = s:strip_white_space(l,r,line)
|
2018-06-14 06:31:12 -04:00
|
|
|
if len(line) && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
|
2014-03-02 09:35:00 -05:00
|
|
|
let uncomment = 0
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2019-11-16 10:28:42 -05:00
|
|
|
if get(b:, 'commentary_startofline')
|
|
|
|
let indent = '^'
|
|
|
|
else
|
|
|
|
let indent = '^\s*'
|
|
|
|
endif
|
|
|
|
|
2021-05-05 04:25:00 -04:00
|
|
|
let lines = []
|
2014-03-02 09:35:00 -05:00
|
|
|
for lnum in range(lnum1,lnum2)
|
|
|
|
let line = getline(lnum)
|
|
|
|
if strlen(r) > 2 && l.r !~# '\\'
|
|
|
|
let line = substitute(line,
|
2019-11-30 07:06:56 -05:00
|
|
|
\'\M' . substitute(l, '\ze\S\s*$', '\\zs\\d\\*\\ze', '') . '\|' . substitute(r, '\S\zs', '\\zs\\d\\*\\ze', ''),
|
2014-03-02 09:35:00 -05:00
|
|
|
\'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g')
|
|
|
|
endif
|
2021-10-11 05:30:43 -04:00
|
|
|
if force_uncomment
|
|
|
|
if line =~ '^\s*' . l
|
|
|
|
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
|
|
|
|
endif
|
|
|
|
elseif uncomment
|
2014-03-02 09:35:00 -05:00
|
|
|
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
|
|
|
|
else
|
2019-11-16 10:28:42 -05:00
|
|
|
let line = substitute(line,'^\%('.matchstr(getline(lnum1),indent).'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
|
2014-03-02 09:35:00 -05:00
|
|
|
endif
|
2021-05-05 04:25:00 -04:00
|
|
|
call add(lines, line)
|
2014-03-02 09:35:00 -05:00
|
|
|
endfor
|
2021-05-05 04:25:00 -04:00
|
|
|
call setline(lnum1, lines)
|
2014-04-18 08:58:02 -04:00
|
|
|
let modelines = &modelines
|
|
|
|
try
|
|
|
|
set modelines=0
|
|
|
|
silent doautocmd User CommentaryPost
|
|
|
|
finally
|
|
|
|
let &modelines = modelines
|
|
|
|
endtry
|
2018-06-14 06:31:12 -04:00
|
|
|
return ''
|
2014-03-02 09:35:00 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-04-18 08:58:02 -04:00
|
|
|
function! s:textobject(inner) abort
|
2017-11-24 08:54:40 -05: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\@<!')
|
2017-11-24 08:54:40 -05:00
|
|
|
let [l, r] = s:strip_white_space(l,r,line)
|
2014-03-02 09:35:00 -05:00
|
|
|
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
|
|
|
|
|
2021-10-11 05:30:43 -04:00
|
|
|
command! -range -bar -bang Commentary call s:go(<line1>,<line2>,<bang>0)
|
2018-06-14 06:31:12 -04:00
|
|
|
xnoremap <expr> <Plug>Commentary <SID>go()
|
|
|
|
nnoremap <expr> <Plug>Commentary <SID>go()
|
|
|
|
nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_'
|
2018-07-19 08:52:53 -04:00
|
|
|
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR>
|
2014-04-18 08:58:02 -04:00
|
|
|
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
|
2018-06-14 06:31:12 -04:00
|
|
|
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>
|
2014-03-02 09:35:00 -05:00
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
if !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 gcu <Plug>Commentary<Plug>Commentary
|
2014-03-02 09:35:00 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
" vim:set et sw=2:
|