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