Skip to content

Commit 71dc4c7

Browse files
committed
Make plugin autoloadable
Similar to tpope#78
1 parent 89f43af commit 71dc4c7

2 files changed

Lines changed: 82 additions & 83 deletions

File tree

autoload/commentary.vim

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
function! s:surroundings() abort
2+
return split(get(b:, 'commentary_format', substitute(substitute(
3+
\ &commentstring, '\S\zs%s',' %s','') ,'%s\ze\S', '%s ', '')), '%s', 1)
4+
endfunction
5+
6+
function! s:strip_white_space(l,r,line) abort
7+
let [l, r] = [a:l, a:r]
8+
if l[-1:] == ' ' && stridx(a:line,l) == -1 && stridx(a:line,l[0:-2]) == 0
9+
let l = l[:-2]
10+
endif
11+
if r[0] == ' ' && a:line[-strlen(r):] != r && a:line[1-strlen(r):] == r[1:]
12+
let r = r[1:]
13+
endif
14+
return [l, r]
15+
endfunction
16+
17+
function! commentary#go(type,...) abort
18+
if a:0
19+
let [lnum1, lnum2] = [a:type, a:1]
20+
else
21+
let [lnum1, lnum2] = [line("'["), line("']")]
22+
endif
23+
24+
let [l, r] = s:surroundings()
25+
let uncomment = 2
26+
for lnum in range(lnum1,lnum2)
27+
let line = matchstr(getline(lnum),'\S.*\s\@<!')
28+
let [l, r] = s:strip_white_space(l,r,line)
29+
if line != '' && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
30+
let uncomment = 0
31+
endif
32+
endfor
33+
34+
for lnum in range(lnum1,lnum2)
35+
let line = getline(lnum)
36+
if strlen(r) > 2 && l.r !~# '\\'
37+
let line = substitute(line,
38+
\'\M'.r[0:-2].'\zs\d\*\ze'.r[-1:-1].'\|'.l[0].'\zs\d\*\ze'.l[1:-1],
39+
\'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g')
40+
endif
41+
if uncomment
42+
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
43+
else
44+
let line = substitute(line,'^\%('.matchstr(getline(lnum1),'^\s*').'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
45+
endif
46+
call setline(lnum,line)
47+
endfor
48+
let modelines = &modelines
49+
try
50+
set modelines=0
51+
silent doautocmd User CommentaryPost
52+
finally
53+
let &modelines = modelines
54+
endtry
55+
endfunction
56+
57+
function! commentary#textobject(inner) abort
58+
let [l, r] = s:surroundings()
59+
let lnums = [line('.')+1, line('.')-2]
60+
for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']]
61+
while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
62+
let lnums[index] += dir
63+
let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!')
64+
let [l, r] = s:strip_white_space(l,r,line)
65+
endwhile
66+
endfor
67+
while (a:inner || lnums[1] != line('$')) && empty(getline(lnums[0]))
68+
let lnums[0] += 1
69+
endwhile
70+
while a:inner && empty(getline(lnums[1]))
71+
let lnums[1] -= 1
72+
endwhile
73+
if lnums[0] <= lnums[1]
74+
execute 'normal! 'lnums[0].'GV'.lnums[1].'G'
75+
endif
76+
endfunction

plugin/commentary.vim

Lines changed: 6 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,90 +8,13 @@ if exists("g:loaded_commentary") || &cp || v:version < 700
88
endif
99
let g:loaded_commentary = 1
1010

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>
11+
xnoremap <silent> <Plug>Commentary :<C-U>call commentary#go(line("'<"),line("'>"))<CR>
12+
nnoremap <silent> <Plug>Commentary :<C-U>set opfunc=commentary#go<CR>g@
13+
nnoremap <silent> <Plug>CommentaryLine :<C-U>set opfunc=commentary#go<Bar>exe 'norm! 'v:count1.'g@_'<CR>
14+
onoremap <silent> <Plug>Commentary :<C-U>call commentary#textobject(0)<CR>
15+
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call commentary#textobject(1)<CR>
9316
nmap <silent> <Plug>CommentaryUndo <Plug>Commentary<Plug>Commentary
94-
command! -range -bar Commentary call s:go(<line1>,<line2>)
17+
command! -range -bar Commentary call commentary#go(<line1>,<line2>)
9518

9619
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
9720
xmap gc <Plug>Commentary

0 commit comments

Comments
 (0)