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