@@ -8,93 +8,12 @@ if exists("g:loaded_commentary") || v:version < 700
8
8
endif
9
9
let g: loaded_commentary = 1
10
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: 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].' \z s\d\*\z e' .r[-1:-1].' \| ' .l[0].' \z s\d\*\z e'.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
-
92
11
command ! - range - bar Commentary call s: go (<line1> ,<line2> )
93
12
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>
98
17
nmap <silent> <Plug> CommentaryUndo :echoerr "Change your <Plug> CommentaryUndo map to <Plug> Commentary<Plug> Commentary"<CR>
99
18
100
19
if ! hasmapto (' <Plug>Commentary' ) || maparg (' gc' ,' n' ) == # ' '
0 commit comments