samer@3
|
1 " commentary.vim - Comment stuff out
|
samer@3
|
2 " Maintainer: Tim Pope <http://tpo.pe/>
|
samer@3
|
3 " Version: 1.1
|
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:go(type,...) abort
|
samer@3
|
12 if a:0
|
samer@3
|
13 let [lnum1, lnum2] = [a:type, a:1]
|
samer@3
|
14 else
|
samer@3
|
15 let [lnum1, lnum2] = [line("'["), line("']")]
|
samer@3
|
16 endif
|
samer@3
|
17
|
samer@3
|
18 let [l, r] = split(substitute(substitute(&commentstring,'\S\zs%s',' %s',''),'%s\ze\S','%s ',''),'%s',1)
|
samer@3
|
19 let uncomment = 2
|
samer@3
|
20 for lnum in range(lnum1,lnum2)
|
samer@3
|
21 let line = matchstr(getline(lnum),'\S.*\s\@<!')
|
samer@3
|
22 if line != '' && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
|
samer@3
|
23 let uncomment = 0
|
samer@3
|
24 endif
|
samer@3
|
25 endfor
|
samer@3
|
26
|
samer@3
|
27 for lnum in range(lnum1,lnum2)
|
samer@3
|
28 let line = getline(lnum)
|
samer@3
|
29 if strlen(r) > 2 && l.r !~# '\\'
|
samer@3
|
30 let line = substitute(line,
|
samer@3
|
31 \'\M'.r[0:-2].'\zs\d\*\ze'.r[-1:-1].'\|'.l[0].'\zs\d\*\ze'.l[1:-1],
|
samer@3
|
32 \'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g')
|
samer@3
|
33 endif
|
samer@3
|
34 if uncomment
|
samer@3
|
35 let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
|
samer@3
|
36 else
|
samer@3
|
37 let line = substitute(line,'^\%('.matchstr(getline(lnum1),'^\s*').'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
|
samer@3
|
38 endif
|
samer@3
|
39 call setline(lnum,line)
|
samer@3
|
40 endfor
|
samer@3
|
41 endfunction
|
samer@3
|
42
|
samer@3
|
43 function! s:undo()
|
samer@3
|
44 let [l, r] = split(substitute(substitute(&commentstring,'\S\zs%s',' %s',''),'%s\ze\S','%s ',''),'%s',1)
|
samer@3
|
45 let lnums = [line('.')+1, line('.')-2]
|
samer@3
|
46 for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']]
|
samer@3
|
47 while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
|
samer@3
|
48 let lnums[index] += dir
|
samer@3
|
49 let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!')
|
samer@3
|
50 endwhile
|
samer@3
|
51 endfor
|
samer@3
|
52 call s:go(lnums[0], lnums[1])
|
samer@3
|
53 silent! call repeat#set("\<Plug>CommentaryUndo")
|
samer@3
|
54 endfunction
|
samer@3
|
55
|
samer@3
|
56 xnoremap <silent> <Plug>Commentary :<C-U>call <SID>go(line("'<"),line("'>"))<CR>
|
samer@3
|
57 nnoremap <silent> <Plug>Commentary :<C-U>set opfunc=<SID>go<CR>g@
|
samer@3
|
58 nnoremap <silent> <Plug>CommentaryLine :<C-U>set opfunc=<SID>go<Bar>exe 'norm! 'v:count1.'g@_'<CR>
|
samer@3
|
59 nnoremap <silent> <Plug>CommentaryUndo :<C-U>call <SID>undo()<CR>
|
samer@3
|
60
|
samer@3
|
61 if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
|
samer@3
|
62 xmap gc <Plug>Commentary
|
samer@3
|
63 nmap gc <Plug>Commentary
|
samer@3
|
64 nmap gcc <Plug>CommentaryLine
|
samer@3
|
65 nmap gcu <Plug>CommentaryUndo
|
samer@3
|
66 endif
|
samer@3
|
67
|
samer@3
|
68 if maparg('\\','n') ==# '' && maparg('\','n') ==# ''
|
samer@3
|
69 xmap \\ <Plug>Commentary
|
samer@3
|
70 nmap \\ <Plug>Commentary
|
samer@3
|
71 nmap \\\ <Plug>CommentaryLine
|
samer@3
|
72 nmap \\u <Plug>CommentaryUndo
|
samer@3
|
73 endif
|
samer@3
|
74
|
samer@3
|
75 " vim:set et sw=2:
|