Skip to content

Commit 3b01e29

Browse files
committed
Make plugin autoloadable
1 parent 54e5676 commit 3b01e29

File tree

2 files changed

+84
-85
lines changed

2 files changed

+84
-85
lines changed

autoload/commentary.vim

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

plugin/commentary.vim

Lines changed: 4 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,12 @@ if exists("g:loaded_commentary") || 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(...) 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-
for lnum in range(lnum1,lnum2)
48-
let line = getline(lnum)
49-
if strlen(r) > 2 && l.r !~# '\\'
50-
let line = substitute(line,
51-
\'\M'.r[0:-2].'\zs\d\*\ze'.r[-1:-1].'\|'.l[0].'\zs\d\*\ze'.l[1:-1],
52-
\'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g')
53-
endif
54-
if uncomment
55-
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
56-
else
57-
let line = substitute(line,'^\%('.matchstr(getline(lnum1),'^\s*').'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
58-
endif
59-
call setline(lnum,line)
60-
endfor
61-
let modelines = &modelines
62-
try
63-
set modelines=0
64-
silent doautocmd User CommentaryPost
65-
finally
66-
let &modelines = modelines
67-
endtry
68-
return ''
69-
endfunction
70-
71-
function! s:textobject(inner) abort
72-
let [l, r] = s:surroundings()
73-
let lnums = [line('.')+1, line('.')-2]
74-
for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']]
75-
while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
76-
let lnums[index] += dir
77-
let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!')
78-
let [l, r] = s:strip_white_space(l,r,line)
79-
endwhile
80-
endfor
81-
while (a:inner || lnums[1] != line('$')) && empty(getline(lnums[0]))
82-
let lnums[0] += 1
83-
endwhile
84-
while a:inner && empty(getline(lnums[1]))
85-
let lnums[1] -= 1
86-
endwhile
87-
if lnums[0] <= lnums[1]
88-
execute 'normal! 'lnums[0].'GV'.lnums[1].'G'
89-
endif
90-
endfunction
91-
9211
command! -range -bar Commentary call s:go(<line1>,<line2>)
9312
xnoremap <silent> <Plug>Commentary :Commentary<CR>
94-
nnoremap <expr> <Plug>Commentary <SID>go()
95-
nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_'
96-
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(0)<CR>
97-
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
13+
nnoremap <expr> <Plug>Commentary commentary#go()
14+
nnoremap <expr> <Plug>CommentaryLine commentary#go() . '_'
15+
onoremap <silent> <Plug>Commentary :<C-U>call commentary#textobject(0)<CR>
16+
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call commentary#textobject(1)<CR>
9817
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>
9918
10019
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''

0 commit comments

Comments
 (0)