1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
" Function for commenting out blocks of text.
noremap cd <cmd>set operatorfunc=<sid>comment<cr>g@
nnoremap cdd <cmd>set operatorfunc=<sid>comment<cr>g@_
nnoremap cD <cmd>set operatorfunc=<sid>comment<cr>g@$
" Comment out the rest of the line and put the cursor into insert mode
nnoremap cdC c<Plug><SID>(comment-change-object-i)
" Comment out the line and go into insert mode before the first non-blank
" character.
nnoremap cdcc c<Plug><SID>(comment-change-object-I)
onoremap <Plug><SID>(comment-change-object-i) <cmd>call <sid>change_comment_obj('i')<cr>
onoremap <Plug><SID>(comment-change-object-I) <cmd>call <sid>change_comment_obj('I')<cr>
onoremap i/ <cmd>call <sid>comment_obj(v:operator, 'i')<cr>
vnoremap i/ <cmd>call <sid>comment_obj('', 'i')<cr>
onoremap a/ <cmd>call <sid>comment_obj(v:operator, 'a')<cr>
vnoremap a/ <cmd>call <sid>comment_obj('', 'a')<cr>
noremap czd <cmd>set operatorfunc=<sid>uncomment<cr>g@
nnoremap czdd <cmd>set operatorfunc=<sid>uncomment<cr>g@_
let s:default_style = {
\ 'block_start_regex': '\(^\s*\(\(\S\)\(#\)\@<!.*\|\S\n\)\_s\+\|\%^\)#\+\s*\zs\S\ze',
\ 'block_end_regex': '#.*\zs.\ze\n\_s*\S#\@<!',
\ 'line': '#'
\ }
function! s:regex_combine(s1, s2) abort
if a:s1 == ""
return a:s2
endif
if a:s2 == ""
return a:s1
endif
return printf('\(%s\)\|\(%s\)', a:s1, a:s2)
endfunction
function! s:comment_obj(op, iOrA)
let style = get(s:comment_style, &ft, s:default_style)
let start_regex = s:regex_combine(
\ get(style, 'multi_start_regex', ''),
\ get(style, 'block_start_regex', ''))
let end_regex = s:regex_combine(
\ get(style, 'multi_end_regex', ''),
\ get(style, 'block_end_regex', ''))
\
if a:iOrA == 'i'
let start_regex =
\ substitute(
\ substitute(start_regex, '\V${i\(\[se]\)}', '\\z\1', 'g'),
\ '\V${a\(\[se]\)}', '', 'g')
let end_regex =
\ substitute(
\ substitute(end_regex, '\V${i\(\[se]\)}', '\\z\1', 'g'),
\ '\V${a\(\[se]\)}', '', 'g')
else
let start_regex =
\ substitute(
\ substitute(start_regex, '\V${a\(\[se]\)}', '\\z\1', 'g'),
\ '\V${i\(\[se]\)}', '', 'g')
let end_regex =
\ substitute(
\ substitute(end_regex, '\V${a\(\[se]\)}', '\\z\1', 'g'),
\ '\V${i\(\[se]\)}', '', 'g')
endif
call search(end_regex, 'c')
exec "normal! v\<esc>m>"
call search(start_regex, 'bW')
exec "normal! m<gv"
let [_, sl, sc, _] = getpos("'<")
let [_, el, ec, _] = getpos("'>")
if getline(sl)[:sc - 2] =~ '^\s*$' && a:iOrA == 'a' && ec >= len(getline(el))
" Select line-wise if the comments on on lines.
exec "normal V"
endif
endfunction
function! s:change_comment_obj(mv) abort
if v:operator == 'c'
" This is a cheese to allow the cdcc and cdC commands to be repeated with
" the dot operator. When in insert mode <left><right><up><down> stop
" recording into the redobuff. This is a way to get around that.
let s = get(s:comment_style, &ft, s:default_style)
exec printf("normal! %s %s \<esc>%dhvl", a:mv, s.line, len(s.line) + 3)
else
endif
endfunction
let s:comment_style = {
\ 'java,c,cpp,rust,go': {
\ 'multi_start_regex': '${as}\/\*\([*]\|\_s\)*${is}\S',
\ 'multi_end_regex': '${is}\S\_s*\*\/${as}',
\ 'block_start_regex': '\(^\s*\(\(\S\S\)\(\/\/\)\@<!.*\|\S\n\)\n*${as}\_s\+\|\%^${as}\)\/\/\+\s*${is}\S${ie}',
\ 'block_end_regex': '\/\/.*${is}.${ie}${as}\n\_s*\S\/\@<!',
\ 'line': '//',
\ 'multi_start': '/*',
\ 'multi_cont': ' *',
\ 'multi_end': ' */'
\ },
\ 'vim': {
\ 'block_start_regex': '\(^\s*\(\(\S\)\("\)\@<!.*\|\S\n\)\_s\+\|\%^\)"\s*${is}\S${ie}',
\ 'block_end_regex': '".*${is}.${ie}\n${as}\_s*\S"\@<!',
\ 'line': '"'
\ }
\ }
let s:comment_style_new = {}
for [langs, i] in items(s:comment_style)
for l in split(langs, ',')
let s:comment_style_new[l] = i
endfor
endfor
let s:comment_style = s:comment_style_new
function! s:uncomment(arg, ...) abort
let s = get(s:comment_style, &ft, s:default_style)
let l1 = line("'[")
let l2 = line("']")
if has_key(s, 'multi_start')
silent exec printf(
\ 'silent! %s,%s s/\V\zs%s\s\?\ze//', l1, l2, escape(s.multi_start, '/'))
silent exec printf('silent! %s,%s s/\V\zs\s\?%s\ze//', l1, l2, escape(s.multi_end, '/'))
silent exec printf('silent! %s,%s s/\V\^\s\*\zs%s\s\?\ze//', l1, l2, escape(s.multi_cont, '/'))
endif
silent exec printf('silent! %s,%s s/\V\^\s\*\zs%s\s\?\ze//', l1, l2, escape(s.line, '/'))
endfunction
function! s:comment(arg, ...) abort
let cb = { 'arg': a:arg }
normal! m`
function! cb.operate(ls, t) dict abort
let s = get(s:comment_style, &ft, s:default_style)
let smallest = 100
let ls = a:ls
if self.arg == 'line' || len(ls) > 1
for l in ls
if ! (l =~ '^\s*$')
let sz = match(l, '^\(\s\+\)\zs\S\ze') + 1
if sz <= 0
let sz = 1
endif
if sz < smallest && sz > 0
let smallest = sz
endif
endif
endfor
if self.arg == 'line'
let i = 0
while i < len(ls)
let ls[i] = substitute(ls[i], printf('\%%%dc', smallest), s.line . ' ', '')
let i += 1
endwhile
else
if self.arg != 'line' && !has_key(s, 'multi_start')
throw "Inline comments not allowed"
endif
let ls[0] = substitute(ls[0], '^', s.multi_start . ' ', '')
let ls[-1] = substitute(ls[-1], '$', ' ' . s.multi_end, '')
endif
else
let l = getline('.')
if l[len(l) - len(ls[0]):] ==# ls[0]
let ls[0] = printf("%s %s", s.line, ls[0])
else
if !has_key(s, 'multi_start')
throw "Inline comments not allowed"
endif
let ls[0] = printf("%s %s %s", s.multi_start, ls[0], s.multi_end)
endif
endif
return ls
endfunction
call fieldmarshal#modifytext(a:arg, cb)
norm ``
endfunction
|