commentary.vim (3781B)
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") || 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(substitute( 13 \ &commentstring, '^$', '%s', ''), '\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(...) abort 28 if !a:0 29 let &operatorfunc = matchstr(expand('<sfile>'), '[^. ]*$') 30 return 'g@' 31 elseif a:0 > 1 32 let [lnum1, lnum2] = [a:1, a:2] 33 else 34 let [lnum1, lnum2] = [line("'["), line("']")] 35 endif 36 37 let [l, r] = s:surroundings() 38 let uncomment = 2 39 for lnum in range(lnum1,lnum2) 40 let line = matchstr(getline(lnum),'\S.*\s\@<!') 41 let [l, r] = s:strip_white_space(l,r,line) 42 if len(line) && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r) 43 let uncomment = 0 44 endif 45 endfor 46 47 if get(b:, 'commentary_startofline') 48 let indent = '^' 49 else 50 let indent = '^\s*' 51 endif 52 53 for lnum in range(lnum1,lnum2) 54 let line = getline(lnum) 55 if strlen(r) > 2 && l.r !~# '\\' 56 let line = substitute(line, 57 \'\M'.r[0:-2].'\zs\d\*\ze'.r[-1:-1].'\|'.l[0].'\zs\d\*\ze'.l[1:-1], 58 \'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g') 59 endif 60 if uncomment 61 let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','') 62 else 63 let line = substitute(line,'^\%('.matchstr(getline(lnum1),indent).'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','') 64 endif 65 call setline(lnum,line) 66 endfor 67 let modelines = &modelines 68 try 69 set modelines=0 70 silent doautocmd User CommentaryPost 71 finally 72 let &modelines = modelines 73 endtry 74 return '' 75 endfunction 76 77 function! s:textobject(inner) abort 78 let [l, r] = s:surroundings() 79 let lnums = [line('.')+1, line('.')-2] 80 for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']] 81 while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r) 82 let lnums[index] += dir 83 let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!') 84 let [l, r] = s:strip_white_space(l,r,line) 85 endwhile 86 endfor 87 while (a:inner || lnums[1] != line('$')) && empty(getline(lnums[0])) 88 let lnums[0] += 1 89 endwhile 90 while a:inner && empty(getline(lnums[1])) 91 let lnums[1] -= 1 92 endwhile 93 if lnums[0] <= lnums[1] 94 execute 'normal! 'lnums[0].'GV'.lnums[1].'G' 95 endif 96 endfunction 97 98 command! -range -bar Commentary call s:go(<line1>,<line2>) 99 xnoremap <expr> <Plug>Commentary <SID>go() 100 nnoremap <expr> <Plug>Commentary <SID>go() 101 nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_' 102 onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR> 103 nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR> 104 nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR> 105 106 if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# '' 107 xmap gc <Plug>Commentary 108 nmap gc <Plug>Commentary 109 omap gc <Plug>Commentary 110 nmap gcc <Plug>CommentaryLine 111 if maparg('c','n') ==# '' && !exists('v:operator') 112 nmap cgc <Plug>ChangeCommentary 113 endif 114 nmap gcu <Plug>Commentary<Plug>Commentary 115 endif 116 117 " vim:set et sw=2: