commentary.vim (3548B)
1 " commentary.vim - Comment stuff out 2 " Maintainer: Tim Pope <http://tpo.pe/> 3 " Version: 1.3 4 " GetLatestVimScripts: 3695 1 :AutoInstall: commentary.vim 5 6 if exists("g:loaded_commentary") || &cp || v:version < 700 7 finish 8 endif 9 let g:loaded_commentary = 1 10 11 function! s:surroundings() abort 12 return split(get(b:, 'commentary_format', substitute(substitute( 13 \ &commentstring, '\S\zs%s',' %s','') ,'%s\ze\S', '%s ', '')), '%s', 1) 14 endfunction 15 16 function! s:strip_white_space(l,r,line) abort 17 let [l, r] = [a:l, a:r] 18 if l[-1:] == ' ' && stridx(a:line,l) == -1 && stridx(a:line,l[0:-2]) == 0 19 let l = l[:-2] 20 endif 21 if r[0] == ' ' && a:line[-strlen(r):] != r && a:line[1-strlen(r):] == r[1:] 22 let r = r[1:] 23 endif 24 return [l, r] 25 endfunction 26 27 function! s:go(type,...) abort 28 if a:0 29 let [lnum1, lnum2] = [a:type, a:1] 30 else 31 let [lnum1, lnum2] = [line("'["), line("']")] 32 endif 33 34 let [l, r] = s:surroundings() 35 let uncomment = 2 36 for lnum in range(lnum1,lnum2) 37 let line = matchstr(getline(lnum),'\S.*\s\@<!') 38 let [l, r] = s:strip_white_space(l,r,line) 39 if line != '' && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r) 40 let uncomment = 0 41 endif 42 endfor 43 44 for lnum in range(lnum1,lnum2) 45 let line = getline(lnum) 46 if strlen(r) > 2 && l.r !~# '\\' 47 let line = substitute(line, 48 \'\M'.r[0:-2].'\zs\d\*\ze'.r[-1:-1].'\|'.l[0].'\zs\d\*\ze'.l[1:-1], 49 \'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g') 50 endif 51 if uncomment 52 let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','') 53 else 54 let line = substitute(line,'^\%('.matchstr(getline(lnum1),'^\s*').'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','') 55 endif 56 call setline(lnum,line) 57 endfor 58 let modelines = &modelines 59 try 60 set modelines=0 61 silent doautocmd User CommentaryPost 62 finally 63 let &modelines = modelines 64 endtry 65 endfunction 66 67 function! s:textobject(inner) abort 68 let [l, r] = s:surroundings() 69 let lnums = [line('.')+1, line('.')-2] 70 for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']] 71 while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r) 72 let lnums[index] += dir 73 let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!') 74 let [l, r] = s:strip_white_space(l,r,line) 75 endwhile 76 endfor 77 while (a:inner || lnums[1] != line('$')) && empty(getline(lnums[0])) 78 let lnums[0] += 1 79 endwhile 80 while a:inner && empty(getline(lnums[1])) 81 let lnums[1] -= 1 82 endwhile 83 if lnums[0] <= lnums[1] 84 execute 'normal! 'lnums[0].'GV'.lnums[1].'G' 85 endif 86 endfunction 87 88 xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR> 89 nnoremap <silent> <Plug>Commentary :<C-U>set opfunc=<SID>go<CR>g@ 90 nnoremap <silent> <Plug>CommentaryLine :<C-U>set opfunc=<SID>go<Bar>exe 'norm! 'v:count1.'g@_'<CR> 91 onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(0)<CR> 92 nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR> 93 nmap <silent> <Plug>CommentaryUndo <Plug>Commentary<Plug>Commentary 94 command! -range -bar Commentary call s:go(<line1>,<line2>) 95 96 if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# '' 97 xmap gc <Plug>Commentary 98 nmap gc <Plug>Commentary 99 omap gc <Plug>Commentary 100 nmap gcc <Plug>CommentaryLine 101 if maparg('c','n') ==# '' 102 nmap cgc <Plug>ChangeCommentary 103 endif 104 nmap gcu <Plug>Commentary<Plug>Commentary 105 endif 106 107 " vim:set et sw=2: