-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapping.kak
219 lines (184 loc) · 6.94 KB
/
mapping.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
# Custom mappings
# ───────────────
map global normal \' \" # quick register mode
map global normal <ret> : # quick command mode
map global normal <tab> '<a-;>' # switch side
map global normal <%> '<c-s>%' # Save position before %
# invert q & b
map global normal q b
map global normal Q B
map global normal <a-q> <a-b>
map global normal <a-Q> <a-B>
map global normal b q
map global normal B Q
# comment with #
map global normal '#' :comment-line<ret>
# format with =
map global normal = '|fmt -w $kak_opt_autowrap_column<ret>'
# clear search buffer
map global user ',' ': set-register slash \<ret><c-l>: execute-keys ", "<ret>' -docstring 'clear search'
# case insensitive search
map global user '/' '/(?i)' -docstring "case insensitive search"
# esc with jj
hook global InsertChar '[jj]' %{
try %{
execute-keys -draft "hH<a-k>%val{hook_param}%val{hook_param}<ret>d"
execute-keys <esc>
}
}
# Always select entire lines
map global normal J "Jx"
map global normal K "Kx"
# tab switch completion
hook global InsertCompletionShow .* %{ map window insert <tab> <c-n>; map window insert <s-tab> <c-p> }
hook global InsertCompletionHide .* %{ unmap window insert <tab> <c-n>; unmap window insert <s-tab> <c-p> }
# split window
define-command split %{
new eval buffer %val{bufname} ';'
}
# map global normal <c-r> ": split<ret>"
# map global normal <c-r> %{: with-option windowing_placement horizontal new<ret>}
map global normal <c-r> %{: new<ret>}
# open term
# map global normal <c-t> ": with-option windowing_placement horizontal kks-connect terminal<ret>"
map global normal <c-t> ": kks-connect terminal<ret>"
# fuzzy
declare-user-mode fuzzy
map global normal <c-p> %{:enter-user-mode fuzzy<ret>} -docstring "fzf commands"
map global fuzzy b ": kks-connect terminal kks-buffers<ret>" -docstring "buffers"
map global fuzzy f ": kks-connect terminal kks-files<ret>" -docstring "files"
map global fuzzy g ": kks-connect terminal kks-grep<ret>" -docstring "grep"
map global fuzzy <space> ": kks-connect terminal broot<ret>" -docstring "broot"
# alt + direction (insert mode)
map global insert <a-h> '<a-;><a-h>'
map global insert <a-j> '<a-;>o'
map global insert <a-k> '<a-;>O'
map global insert <a-l> '<a-;><a-l>'
# restore last selection
hook -group backup-selections global NormalIdle .* %{
set-register b %reg{z}
execute-keys -draft '"zZ'
}
map -docstring 'Restore selections from the backup register' global user z '"bz'
# sort buffer alphabetically
define-command -override sort-buffers %{
eval %sh{
# XXX use uuid to avoid user conflict with root here
rm /tmp/buflist
for bufname in $kak_opt_quickbufs $kak_buflist
do printf "%s\n" "$bufname" >> /tmp/buflist
done
cat /tmp/buflist | awk '!seen[$0]++' | grep -v "/tmp/buflist" | sort | tr '\n' ' ' > /tmp/buflist_tmp
printf "arrange-buffers "
cat /tmp/buflist_tmp
rm /tmp/buflist /tmp/buflist_tmp
}
}
# find file
# define-command find -params 1 -shell-script-candidates %{ find . -type f } %{ edit %arg{1} }
# Safe save
define-command -params 0..1 secure_write %{ evaluate-commands %sh{
if [ -z ${1+x} ]; then
# no param given: usual write
echo "write"
else
bufname=$1
if [ -e "${bufname}" ] && [ "${bufname}" != "${current_bufname}" ]; then
# file already exists and is not the current one
echo "echo 'Use w! to override existing file'"
else
echo "write ${bufname}"
fi
fi
}}
unalias global w write
alias global w secure_write
# Clipboard
map global user 'P' '!xsel --output --clipboard <ret>' -docstring 'paste from clipboard'
map global user 'p' '<a-!>xsel --output --clipboard <ret>' -docstring 'paste from clipboard (after)'
hook global RegisterModified '"' %{ nop %sh{
printf %s "$kak_main_reg_dquote" | xsel --input --clipboard
}}
# Vim like
# ----------
map -docstring 'increment selection' global normal <c-a> ': increment-selection %val{count}<ret>'
map -docstring 'decrement selection' global normal <c-x> ': decrement-selection %val{count}<ret>'
define-command -override increment-selection -params 1 -docstring 'increment-selection <count>: increment selection by count' %{
execute-keys "a+%sh{expr $1 '|' 1}<esc>|{ cat; echo; } | bc<ret>"
}
define-command -override decrement-selection -params 1 -docstring 'decrement-selection <count>: decrement selection by count' %{
execute-keys "a-%sh{expr $1 '|' 1}<esc>|{ cat; echo; } | bc<ret>"
}
# Custom object selections
# ─────────────────────────
# select previous word (bash like)
# useful for snippets
map global insert <c-w> '<a-;>h<a-;><a-B>'
# Current indented paragraph object
define-command -hidden custom-indented-paragraph %{
execute-keys -draft -save-regs '' '<a-i>pZ'
execute-keys '<a-i>i<a-z>i'
}
map -docstring 'Indented paragraph' global object I '<esc>: custom-indented-paragraph<ret>'
# XML tag
map -docstring 'XML tag objet' global object t %{c<lt>([\w.]+)\b[^>]*?(?<lt>!/)>,<lt>/([\w.]+)\b[^>]*?(?<lt>!/)><ret>}
hook global BufCreate .*\.vt.* %{ # VTK file types are XML
set-option buffer filetype xml
}
# Filetype specific
# ─────────────────
# C / CPP: CMake build system
# ,, -> <<
hook global WinSetOption filetype=(c|cpp) %{
hook buffer InsertChar '[,,]' %{
try %{
execute-keys -draft "hH<a-k>%val{hook_param}%val{hook_param}<ret>d"
execute-keys <<
}
}
}
# ;; -> add ; at the end of the declaration
hook global WinSetOption filetype=(c|cpp) %{
hook buffer InsertChar '[;;]' %{
try %{
execute-keys -draft "hH<a-k>%val{hook_param}%val{hook_param}<ret>d"
execute-keys -draft "<esc><a-a>(<c-l>A;"
}
}
}
# header / source
hook global WinSetOption filetype=(cpp) %{
map global user -docstring 'alternate header/source' 'a' ':cpp-alternative-file<ret>'
}
hook global WinSetOption filetype=(c) %{
map global user -docstring 'alternate header/source' 'a' ':c-alternative-file<ret>'
}
# custom function
# ───────────────
define-command -docstring 'Diff the current selections and display result in a new buffer.' \
diff-selections %{
evaluate-commands %sh{
eval set -- "$kak_quoted_selections"
if [ $# -gt 1 ]; then
echo "$1" > /tmp/a.txt
echo "$2" > /tmp/b.txt
diff -uw /tmp/a.txt /tmp/b.txt > /tmp/diff-result.diff
echo 'edit -existing -readonly /tmp/diff-result.diff'
else
echo "echo -debug 'You must have at least 2 selections to compare.'"
fi
}
}
define-command -docstring 'Sum selected values.' \
sum %{
evaluate-commands %sh{
# add new line to each selection
nl_selection=""
for sel in $kak_selections; do
nl_selection="$sel\n$nl_selection"
done
# replace , by . and compute sum
summed_selection=`echo -e "$nl_selection" | sed 's/,/./g' | awk '{s+=$1} END {printf "%.2f\n", s}'`
printf 'echo %%{%s}' "${summed_selection}"
}
}