-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkak-mode.zsh
415 lines (366 loc) · 8.98 KB
/
kak-mode.zsh
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#emulate -L zsh
#
# kak mode for zsh
bindkey -v
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
# switch to vi (kak) mode
bindkey -v
# bindkey - visual
# don't use visual keymap - only visual mode
bindkey -N visual
# common
kak-remove-visual() {
test 1 -eq ${REGION_ACTIVE} && zle visual-mode
}
zle -N kak-remove-visual
kak-reset-visual() {
# restart, if already active
zle kak-remove-visual
zle visual-mode
}
zle -N kak-reset-visual
kak-accept-line() {
zle kak-remove-visual
zle accept-line
}
zle -N kak-accept-line
# viins
kak-cmd-mode() {
zle vi-cmd-mode
zle kak-reset-visual
}
zle -N kak-cmd-mode
# bindkey - viins
# reuse viins keymap
bindkey -M viins "^J" kak-accept-line
bindkey -M viins "^L" clear-screen
bindkey -M viins "^M" kak-accept-line
bindkey -M viins "^[" kak-cmd-mode
# vicmd
# history (search)
kak-down-line() {
# default is just history, not search
#zle down-line-or-history
zle down-line-or-beginning-search
zle vi-end-of-line
zle kak-reset-visual
}
zle -N kak-down-line
kak-up-line() {
# default is just history, not search
#zle up-line-or-history
zle up-line-or-beginning-search
zle vi-end-of-line
zle kak-reset-visual
}
zle -N kak-up-line
# indent (4 spaces)
kak-indent() {
CS=${CURSOR}
BUFFER=" ${BUFFER}"
CURSOR=$(( ${CS} + 4 ))
zle kak-reset-visual
}
zle -N kak-indent
kak-unindent() {
CS=${CURSOR}
BS=${#BUFFER}
BUFFER="${BUFFER# }"
test "${BS}" = "${#BUFFER}" || CURSOR=$(( ${CS} - 4 ))
zle kak-reset-visual
}
zle -N kak-unindent
# forward direction for selection
kak-mark-point() {
test ${CURSOR} -lt ${MARK} && zle exchange-point-and-mark
}
zle -N kak-mark-point
kak-repeat-find() {
zle kak-reset-visual
zle vi-repeat-find
}
zle -N kak-repeat-find
# swap case
kak-up-case() {
C=${CURSOR}
M=${MARK}
zle vi-up-case
zle kak-reset-visual
MARK=${M}
CURSOR=${C}
}
zle -N kak-up-case
kak-down-case() {
C=${CURSOR}
M=${MARK}
zle vi-down-case
zle kak-reset-visual
MARK=${M}
CURSOR=${C}
}
zle -N kak-down-case
# delete/yank/append/insert/paste
kak-delete() {
zle vi-delete
zle kak-reset-visual
}
zle -N kak-delete
kak-yank() {
M=${MARK}
zle vi-yank
zle kak-reset-visual
MARK=${M}
}
zle -N kak-yank
kak-add-eol() {
zle kak-remove-visual
zle vi-add-eol
}
zle -N kak-add-eol
kak-insert-bol() {
zle kak-remove-visual
zle vi-insert-bol
}
zle -N kak-insert-bol
kak-insert() {
zle kak-remove-visual
zle vi-insert
}
zle -N kak-insert
kak-add-next() {
zle kak-remove-visual
zle vi-add-next
}
zle -N kak-add-next
kak-put-before() {
CS=${CURSOR}
zle vi-put-before
zle kak-reset-visual
MARK=${CS}
}
zle -N kak-put-before
kak-put-after() {
CS=${CURSOR}
zle vi-put-after
zle kak-reset-visual
MARK=$(( ${CS} + 1 ))
}
zle -N kak-put-after
# line beginn/end
kak-beginning-of-line() {
zle vi-beginning-of-line
zle kak-reset-visual
}
zle -N kak-beginning-of-line
kak-first-non-blank() {
zle vi-first-non-blank
zle kak-reset-visual
}
zle -N kak-first-non-blank
kak-end-of-line() {
zle vi-end-of-line
zle kak-reset-visual
}
zle -N kak-end-of-line
# cursor char
kak-backward-char() {
zle vi-backward-char
zle kak-reset-visual
}
zle -N kak-backward-char
kak-forward-char() {
zle vi-forward-char
zle kak-reset-visual
}
zle -N kak-forward-char
# cursor word
kak-backward-word() {
if test 1 -lt ${NUMERIC:-0}; then
zle vi-backward-word -n $(( ${NUMERIC} - 1 ))
fi
zle kak-reset-visual
zle vi-backward-word -n 1
CS=${CURSOR}
MS=${MARK}
zle vi-forward-word -n 1
test ${MS} -eq ${CURSOR} && MARK=$(( ${CURSOR} - 1 )) || MARK=${MS}
CURSOR=${CS}
}
zle -N kak-backward-word
kak-backward-blank-word() {
if test 1 -lt ${NUMERIC:-0}; then
zle vi-backward-blank-word -n $(( ${NUMERIC} - 1 ))
fi
zle kak-reset-visual
zle vi-backward-blank-word -n 1
CS=${CURSOR}
MS=${MARK}
zle vi-forward-blank-word -n 1
test ${MS} -eq ${CURSOR} && MARK=$(( ${CURSOR} - 1 )) || MARK=${MS}
CURSOR=${CS}
}
zle -N kak-backward-blank-word
kak-forward-word-end() {
CS=${CURSOR}
if test 1 -lt ${NUMERIC:-0}; then
zle select-in-word -n 1
if test ${CS} -eq ${CURSOR}; then
zle vi-forward-word-end -n $(( ${NUMERIC} - 1 ))
else
test 2 -lt ${NUMERIC} && zle vi-forward-word-end -n $(( ${NUMERIC} -2 ))
fi
CS=${CURSOR}
fi
zle kak-reset-visual
zle select-in-word -n 1
if test ${CS} -eq ${CURSOR}; then
zle kak-reset-visual
zle vi-forward-word-end -n 1
MARK=$(( ${CS} + 1 ))
else
MARK=${CS}
fi
}
zle -N kak-forward-word-end
kak-forward-blank-word-end() {
CS=${CURSOR}
zle vi-forward-blank-word -n 1
test $(( ${CS} + 2 )) -eq ${CURSOR} && CURSOR=$(( ${CS} + 1 )) || CURSOR=${CS}
if test 1 -lt ${NUMERIC:-0}; then
zle vi-forward-blank-word-end -n $(( ${NUMERIC} - 1 ))
CURSOR=$(( ${CURSOR} + 1 ))
fi
zle kak-reset-visual
zle vi-forward-blank-word-end -n 1
}
zle -N kak-forward-blank-word-end
kak-sel-forward-word() {
CS=${CURSOR}
zle vi-forward-word -n 1
test $(( ${CS} + 1 )) = ${CURSOR} && zle vi-forward-word -n 1
if test 1 -lt ${NUMERIC:-0}; then
zle vi-forward-word -n $(( ${NUMERIC} - 1 ))
fi
zle vi-backward-char -n 1
}
zle -N kak-sel-forward-word
kak-sel-forward-blank-word() {
CS=${CURSOR}
zle vi-forward-blank-word -n 1
test $(( ${CS} + 1 )) = ${CURSOR} && zle vi-forward-blank-word -n 1
if test 1 -lt ${NUMERIC:-0}; then
zle vi-forward-blank-word -n $(( ${NUMERIC} - 1 ))
fi
zle vi-backward-char -n 1
}
zle -N kak-sel-forward-blank-word
kak-forward-word() {
CS=${CURSOR}
zle kak-reset-visual
zle vi-forward-word -n 1
CE=${CURSOR}
if test 1 -lt ${NUMERIC:-0}; then
test 2 -lt ${NUMERIC} && zle vi-forward-word -n $(( ${NUMERIC} - 2 ))
zle kak-reset-visual
zle vi-forward-word -n 1
fi
if test $(( ${CS} + 1 )) = ${CE}; then
zle kak-reset-visual
zle vi-forward-word -n 1
fi
zle vi-backward-char -n 1
}
zle -N kak-forward-word
kak-forward-blank-word() {
CS=${CURSOR}
zle kak-reset-visual
zle vi-forward-blank-word -n 1
CE=${CURSOR}
if test 1 -lt ${NUMERIC:-0}; then
test 2 -lt ${NUMERIC} && zle vi-forward-blank-word -n $(( ${NUMERIC} - 2 ))
zle kak-reset-visual
zle vi-forward-blank-word -n 1
fi
if test $(( ${CS} + 1 )) = ${CE}; then
zle kak-reset-visual
zle vi-forward-blank-word -n 1
fi
zle vi-backward-char -n 1
}
zle -N kak-forward-blank-word
# bindkey - vicmd
bindkey -N vicmd
bindkey -M vicmd -R 0-9 digit-argument
bindkey -M vicmd "^[" vi-cmd-mode
bindkey -M vicmd "^L" clear-screen
bindkey -M vicmd "^M" kak-accept-line
bindkey -M vicmd "^J" kak-accept-line
bindkey -M vicmd "<" kak-unindent
bindkey -M vicmd ">" kak-indent
bindkey -M vicmd ";" kak-reset-visual
bindkey -M vicmd "\e;" exchange-point-and-mark
bindkey -M vicmd "\e:" kak-mark-point
bindkey -M vicmd "." vi-repeat-change
bindkey -M vicmd "\e." kak-repeat-find
bindkey -M vicmd "~" kak-up-case
bindkey -M vicmd "\`" kak-down-case
bindkey -M vicmd "A" kak-add-eol
bindkey -M vicmd "I" kak-insert-bol
bindkey -M vicmd "J" vi-join
bindkey -M vicmd "O" vi-open-line-above
bindkey -M vicmd "P" kak-put-before
bindkey -M vicmd "U" redo
bindkey -M vicmd "X" visual-line-mode
bindkey -M vicmd "a" kak-add-next
bindkey -M vicmd "c" vi-change
bindkey -M vicmd "d" kak-delete
bindkey -M vicmd "f" vi-find-next-char
bindkey -M vicmd "\ef" vi-find-prev-char
bindkey -M vicmd "i" kak-insert
bindkey -M vicmd "o" vi-open-line-below
bindkey -M vicmd "p" kak-put-after
bindkey -M vicmd "r" vi-replace-chars
bindkey -M vicmd "t" vi-find-next-char-skip
bindkey -M vicmd "\et" vi-find-prev-char-skip
bindkey -M vicmd "u" undo
bindkey -M vicmd "x" visual-line-mode
bindkey -M vicmd "y" kak-yank
bindkey -M vicmd "k" kak-up-line
bindkey -M vicmd "j" kak-down-line
bindkey -M vicmd "H" vi-backward-char
bindkey -M vicmd "h" kak-backward-char
bindkey -M vicmd "\eh" vi-beginning-of-line
bindkey -M vicmd "L" vi-forward-char
bindkey -M vicmd "l" kak-forward-char
bindkey -M vicmd "\el" vi-end-of-line
bindkey -M viins "^P" up-line-or-search
bindkey -M viins "^N" down-line-or-search
bindkey -M vicmd "Gh" vi-beginning-of-line
bindkey -M vicmd "gh" kak-beginning-of-line
bindkey -M vicmd "gi" kak-first-non-blank
bindkey -M vicmd "Gl" vi-end-of-line
bindkey -M vicmd "gl" kak-end-of-line
bindkey -M vicmd "q" kak-backward-word
bindkey -M vicmd "Q" vi-backward-word
bindkey -M vicmd "\eq" kak-backward-blank-word
bindkey -M vicmd "\eQ" vi-backward-blank-word
bindkey -M vicmd "e" kak-forward-word-end
bindkey -M vicmd "E" vi-forward-word-end
bindkey -M vicmd "\ee" kak-forward-blank-word-end
bindkey -M vicmd "\eE" vi-forward-blank-word-end
bindkey -M vicmd "w" kak-forward-word
bindkey -M vicmd "W" kak-sel-forward-word
bindkey -M vicmd "\ew" kak-forward-blank-word
bindkey -M vicmd "\eW" kak-sel-forward-blank-word
# edit-command-line
autoload -Uz edit-command-line
zle -N edit-command-line
kak-edit-command-line() {
zle kak-remove-visual
zle edit-command-line
}
zle -N kak-edit-command-line
bindkey -M vicmd '^V' kak-edit-command-line