-
Notifications
You must be signed in to change notification settings - Fork 719
/
comment.kak
218 lines (183 loc) · 6.93 KB
/
comment.kak
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Line comments
# If the language has no line comments, set to ''
declare-option -docstring "characters inserted at the beginning of a commented line" \
str comment_line '#'
# Block comments
declare-option -docstring "characters inserted before a commented block" \
str comment_block_begin
declare-option -docstring "characters inserted after a commented block" \
str comment_block_end
# Default comments for all languages
hook global BufSetOption filetype=asciidoc %{
set-option buffer comment_line '//'
set-option buffer comment_block_begin '////'
set-option buffer comment_block_end '////'
}
hook global BufSetOption filetype=(c|cpp|dart|gluon|go|java|javascript|objc|odin|php|pony|protobuf|rust|sass|scala|scss|swift|typescript|typst|groovy) %{
set-option buffer comment_line '//'
set-option buffer comment_block_begin '/*'
set-option buffer comment_block_end '*/'
}
hook global BufSetOption filetype=(cabal|haskell|moon|idris|elm|dhall|purescript) %{
set-option buffer comment_line '--'
set-option buffer comment_block_begin '{-'
set-option buffer comment_block_end '-}'
}
hook global BufSetOption filetype=clojure %{
set-option buffer comment_line '#_'
set-option buffer comment_block_begin '(comment '
set-option buffer comment_block_end ')'
}
hook global BufSetOption filetype=janet %{
set-option buffer comment_line '#'
set-option buffer comment_block_begin '(comment '
set-option buffer comment_block_end ')'
}
hook global BufSetOption filetype=coffee %{
set-option buffer comment_block_begin '###'
set-option buffer comment_block_end '###'
}
hook global BufSetOption filetype=conf %{
set-option buffer comment_line '#'
}
hook global BufSetOption filetype=css %{
set-option buffer comment_line ''
set-option buffer comment_block_begin '/*'
set-option buffer comment_block_end '*/'
}
hook global BufSetOption filetype=d %{
set-option buffer comment_line '//'
set-option buffer comment_block_begin '/+'
set-option buffer comment_block_end '+/'
}
hook global BufSetOption filetype=(fennel|gas|ini) %{
set-option buffer comment_line ';'
}
hook global BufSetOption filetype=haml %{
set-option buffer comment_line '-#'
}
hook global BufSetOption filetype=(html|xml) %{
set-option buffer comment_line ''
set-option buffer comment_block_begin '<!--'
set-option buffer comment_block_end '-->'
}
hook global BufSetOption filetype=(erlang|latex|mercury) %{
set-option buffer comment_line '%'
}
hook global BufSetOption filetype=ledger %{
set-option buffer comment_line ';'
}
hook global BufSetOption filetype=(lisp|scheme) %{
set-option buffer comment_line ';'
set-option buffer comment_block_begin '#|'
set-option buffer comment_block_end '|#'
}
hook global BufSetOption filetype=lua %{
set-option buffer comment_line '--'
set-option buffer comment_block_begin '--[['
set-option buffer comment_block_end ']]'
}
hook global BufSetOption filetype=markdown %{
set-option buffer comment_line ''
set-option buffer comment_block_begin '[//]: # "'
set-option buffer comment_block_end '"'
}
hook global BufSetOption filetype=(ocaml|coq) %{
set-option buffer comment_line ''
set-option buffer comment_block_begin '(* '
set-option buffer comment_block_end ' *)'
}
hook global BufSetOption filetype=((free|object)?pascal|delphi) %{
set-option buffer comment_line '//'
set-option buffer comment_block_begin '{'
set-option buffer comment_block_end '}'
}
hook global BufSetOption filetype=perl %{
set-option buffer comment_block_begin '#['
set-option buffer comment_block_end ']'
}
hook global BufSetOption filetype=(pug|zig|cue|hare|gleam) %{
set-option buffer comment_line '//'
}
hook global BufSetOption filetype=python %{
set-option buffer comment_block_begin "'''"
set-option buffer comment_block_end "'''"
}
hook global BufSetOption filetype=r %{
set-option buffer comment_line '#'
}
hook global BufSetOption filetype=ragel %{
set-option buffer comment_line '%%'
set-option buffer comment_block_begin '%%{'
set-option buffer comment_block_end '}%%'
}
hook global BufSetOption filetype=ruby %{
set-option buffer comment_block_begin '^begin='
set-option buffer comment_block_end '^=end'
}
hook global BufSetOption filetype=sql %{
set-option buffer comment_line '--'
set-option buffer comment_block_begin '/*'
set-option buffer comment_block_end '*/'
}
define-command comment-block -docstring '(un)comment selections using block comments' %{
evaluate-commands %sh{
if [ -z "${kak_opt_comment_block_begin}" ] || [ -z "${kak_opt_comment_block_end}" ]; then
echo "fail \"The 'comment_block' options are empty, could not comment the selection\""
fi
}
evaluate-commands -save-regs '"/' -draft %{
# Keep non-empty selections
execute-keys <a-K>\A\s*\z<ret>
try %{
# Assert that the selection has been commented
set-register / "\A\Q%opt{comment_block_begin}\E.*\Q%opt{comment_block_end}\E\n*\z"
execute-keys "s<ret>"
# Uncomment it
set-register / "\A\Q%opt{comment_block_begin}\E|\Q%opt{comment_block_end}\E\n*\z"
execute-keys s<ret>d
} catch %{
# Comment the selection
set-register '"' "%opt{comment_block_begin}"
execute-keys -draft P
set-register '"' "%opt{comment_block_end}"
execute-keys p
}
}
}
define-command comment-line -docstring '(un)comment selected lines using line comments' %{
evaluate-commands %sh{
if [ -z "${kak_opt_comment_line}" ]; then
echo "fail \"The 'comment_line' option is empty, could not comment the line\""
fi
}
evaluate-commands -save-regs '"/' -draft %{
# Select the content of the lines, without indentation
execute-keys <a-s>gi<a-l>
try %{
# Keep non-empty lines
execute-keys <a-K>\A\s*\z<ret>
}
try %{
set-register / "\A\Q%opt{comment_line}\E\h?"
try %{
# See if there are any uncommented lines in the selection
execute-keys -draft <a-K><ret>
# There are uncommented lines, so comment everything
set-register '"' "%opt{comment_line} "
align-selections-left
execute-keys P
} catch %{
# All lines were commented, so uncomment everything
execute-keys s<ret>d
}
}
}
}
define-command align-selections-left -docstring 'extend selections to the left to align with the leftmost selected column' %{
evaluate-commands %sh{
leftmost_column=$(echo "$kak_selections_desc" | tr ' ' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1)
aligned_selections=$(echo "$kak_selections_desc" | sed -E "s/\.[0-9]+,/.$leftmost_column,/g")
echo "select $aligned_selections"
}
}