This repository has been archived by the owner on Apr 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.kak
285 lines (257 loc) · 8.79 KB
/
table.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
define-command table-select %{
# check if the cursor is on a table
try %{
execute-keys "gi<a-k>\|<ret>"
} catch %{
fail "not a table"
}
evaluate-commands -save-regs '/' %{
set-register / (?:\h*\|[^\n]*\n)+
try %{
# check if the cursor is at the begin of the buffer
execute-keys -draft "<a-C><a-space>"
# check if on the first row of the table
execute-keys -draft "kgi<a-k>\|<ret>"
# jump to begin of the table
execute-keys "<a-n>"
}
execute-keys "<a-n>n"
}
}
define-command -hidden table-strip %{
table-select
# strip minuses
try %{
execute-keys -draft "s^\h*\|-<ret>l<a-l>s[^|]<ret>d"
}
# strip whitespaces
try %{
execute-keys -draft "s^\h*\|<ret>2l<a-l>s\h*\|\h*|\A\h*<ret>s\h<ret>d"
}
}
define-command -hidden table-adjust-number-of-bars %{
table-select
# the last character of every line should be a bar
try %{
execute-keys -draft "s[^|]\n<ret>ha|"
}
# align bars and \n
# indent is specified by the first line
execute-keys -draft "s\|<ret>1<a-&>&<a-x>s\n<ret>&"
# select the longest line
execute-keys "<a-x>s\|\n<ret>&<space>"
# select all bars except the first one
execute-keys "<a-x>s\|<ret>)<a-space>"
# add missing bars
evaluate-commands -itersel -draft -save-regs 'c' %{
execute-keys "h<a-h>"
set-register c %val{selection_length}
table-select
execute-keys "<a-s>gh" %val{reg_c} "lr|"
}
}
define-command table-align %{
# make sure that all the rows have the same number of bars
table-adjust-number-of-bars
# prepare the table
table-strip
execute-keys "<a-s><a-K>^\h*\|-<ret>"
# bars should be surrounded by whitespaces
try %{
execute-keys -draft "s\|(?!\s)<ret>a <esc>"
}
try %{
execute-keys -draft "s(?<!\h)\|" "<ret><a-K>^<ret>i <esc>"
}
# there should be at least three characters between two bars
try %{
execute-keys -draft "s(?<=\|)\h+(?=\|)" "<ret>c <esc>"
}
table-select
# align the bars
execute-keys -draft "s\|<ret>&"
# replace whitespaces with minuses
try %{
execute-keys -draft "<a-x><a-s><a-k>^\h*\|-<ret>s\|[^\n]*<ret>s <ret>r-"
}
}
# jump from cell to cell
define-command table-next-cell %{
evaluate-commands -draft table-align
evaluate-commands -save-regs '/' %{
set-register '/' '\| '
try %{
execute-keys -draft "<a-k>\|<ret>"
execute-keys "<a-n>"
}
execute-keys 'nl'
}
}
define-command table-previous-cell %{
evaluate-commands -draft table-align
evaluate-commands -save-regs '/' %{
set-register '/' '\| '
try %{
execute-keys -draft "<a-?><ret><a-K>\n<ret>"
execute-keys "<a-n>"
}
execute-keys '<a-n>l'
}
}
# add row
define-command table-add-row-below %{
execute-keys "o|<esc>"
evaluate-commands -draft table-align
try %{
execute-keys "<a-k>^.<ret>k"
}
execute-keys "gi2l"
}
define-command table-add-row-above %{
execute-keys -draft "O|<esc>2X<a-s><a-&>"
evaluate-commands -draft table-align
execute-keys "kgi2l"
}
# move columns and rows
define-command table-select-column %{
evaluate-commands -draft table-align
try %{
execute-keys "<a-k>\|<ret>h"
}
try %{
execute-keys -draft "<a-K>\n<ret>"
execute-keys -draft "<a-h><a-k>\|<ret>"
} catch %{
fail "not in a table cell"
}
evaluate-commands -save-regs 'c' %{
set-register c %val{cursor_char_column}
table-select
execute-keys "<a-s>gh" %val{reg_c} "l2h<a-i>|L"
}
}
define-command table-move-column-right %{
try %{
# check if the cursor is inside the table
execute-keys -draft "h<a-h><a-k>\|<ret>"
# check if in the last column
execute-keys -draft "h2F|<a-K>\n<ret>"
evaluate-commands -draft %{
execute-keys "hf|l"
table-select-column
execute-keys "d2<a-f>|;p"
}
}
}
define-command table-move-column-left %{
try %{
# check if in the first column
execute-keys -draft "2<a-F>|<a-K>\n<ret>"
evaluate-commands -draft %{
execute-keys "<a-f>|"
table-select-column
execute-keys "df|p"
}
}
}
define-command table-move-row-up %{
try %{
execute-keys -draft "<a-C><a-space>"
execute-keys -draft "kgi<a-k>\|<ret>"
execute-keys -draft "kxdp"
}
}
define-command table-move-row-down %{
try %{
execute-keys -draft "C<a-space>"
execute-keys -draft "jgi<a-k>\|<ret>"
execute-keys -draft "jxdkP"
}
}
# interactive editing
define-command table-enable %{
hook -group table global NormalIdle .* %{
remove-hooks global table-replace
try %{
execute-keys -draft "gi<a-k>\|<ret>"
# replace mode inside table cells
hook -group table-replace global InsertChar .* %{
try %{
execute-keys -draft "<esc>L<a-K>\|<ret>"
execute-keys -draft "<esc>t|;H<a-K>[^\s]<ret>;d"
}
}
# normal mode mappings
map window normal <tab> ": table-next-cell<ret>"
map window normal <s-tab> ": table-previous-cell<ret>"
map window normal o ": table-add-row-below<ret>i"
map window normal O ": table-add-row-above<ret>i"
map window normal <a-h> ": table-move-column-left<ret>"
map window normal <a-l> ": table-move-column-right<ret>"
map window normal <a-k> ": table-move-row-up<ret>"
map window normal <a-j> ": table-move-row-down<ret>"
# insert mode mappings
map window insert <esc> "<esc>: evaluate-commands -draft table-align<ret>"
map window insert <tab> "<esc>: table-next-cell<ret>i"
map window insert <s-tab> "<esc>: table-previous-cell<ret>i"
map window insert <a-h> ": table-move-column-left<ret>"
map window insert <a-l> ": table-move-column-right<ret>"
map window insert <a-k> ": table-move-row-up<ret>"
map window insert <a-j> ": table-move-row-down<ret>"
} catch %{
table-remove-mappings
}
}
set-option global table_enabled yes
}
define-command table-disable %{
remove-hooks global table
remove-hooks global table-replace
table-remove-mappings
set-option global table_enabled no
}
define-command table-toggle %{
evaluate-commands %sh{
if $kak_opt_table_enabled
then
printf "table-disable"
else
printf "table-enable"
fi
}
}
define-command -hidden table-remove-mappings %{
# normal mode mappings
unmap window normal <tab> ": table-next-cell<ret>"
unmap window normal <s-tab> ": table-previous-cell<ret>"
unmap window normal o ": table-add-row-below<ret>i"
unmap window normal O ": table-add-row-above<ret>i"
unmap window normal <a-h> ": table-move-column-left<ret>"
unmap window normal <a-l> ": table-move-column-right<ret>"
unmap window normal <a-k> ": table-move-row-up<ret>"
unmap window normal <a-j> ": table-move-row-down<ret>"
# insert mode mappings
unmap window insert <esc> "<esc>: evaluate-commands -draft table-align<ret>"
unmap window insert <tab> "<esc>: table-next-cell<ret>i"
unmap window insert <s-tab> "<esc>: table-previous-cell<ret>i"
unmap window insert <a-h> ": table-move-column-left<ret>"
unmap window insert <a-l> ": table-move-column-right<ret>"
unmap window insert <a-k> ": table-move-row-up<ret>"
unmap window insert <a-j> ": table-move-row-down<ret>"
}
# User mode
declare-user-mode table
map global table a ": evaluate-commands -draft table-align<ret>" -docstring "align table"
map global table e ": table-enable<ret>" -docstring "enable table mode"
map global table d ": table-disable<ret>" -docstring "disable table mode"
map global table t ": table-toggle<ret>" -docstring "toggle table mode"
map global table l ": table-next-cell<ret>" -docstring "jump to next cell"
map global table h ": table-previous-cell<ret>" -docstring "jump to previous cell"
map global table <a-k> ": table-move-row-up<ret>" -docstring "move row up"
map global table <a-j> ": table-move-row-down<ret>" -docstring "move row down"
map global table <a-l> ": table-move-column-right<ret>" -docstring "move column to the right"
map global table <a-h> ": table-move-column-left<ret>" -docstring "move column to the left"
map global table s ": table-select<ret>" -docstring "select table"
map global table c ": table-select-column<ret>" -docstring "select column"
# Options
declare-option -hidden bool table_enabled no