-
Notifications
You must be signed in to change notification settings - Fork 8
/
buffers.kak
267 lines (235 loc) · 7.75 KB
/
buffers.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
# buflist++: names AND modified bool
# debug buffers (like *debug*, *lint*…) are excluded
declare-option -hidden str-list buffers_info
declare-option int buffers_total
# keys to use for buffer picking
declare-option str buffer_keys "1234567890qwertyuiopasdfghjklzxcvbnm"
# used to handle [+] (modified) symbol in list
define-command -hidden refresh-buffers-info %{
set-option global buffers_info
set-option global buffers_total 0
# iteration over all buffers (except debug ones)
evaluate-commands -no-hooks -buffer * %{
set-option -add global buffers_info "%val{bufname}_%val{modified}"
}
evaluate-commands %sh{
total=$(printf '%s\n' "$kak_opt_buffers_info" | tr ' ' '\n' | wc -l)
printf "set-option global buffers_total $total"
}
}
# used to handle # (alt) symbol in list
declare-option str alt_bufname
declare-option str current_bufname
# adjust this number to display more buffers in info
declare-option int max_list_buffers 42
hook global WinDisplay .* %{
set-option global alt_bufname %opt{current_bufname}
set-option global current_bufname %val{bufname}
}
define-command info-buffers -docstring 'populate an info box with a numbered buffers list' %{
refresh-buffers-info
evaluate-commands %sh{
# info title
printf "info -title '$kak_opt_buffers_total buffers' -- %%^"
index=0
eval "set -- $kak_quoted_opt_buffers_info"
while [ "$1" ]; do
# limit lists too big
index=$((index + 1))
if [ "$index" -gt "$kak_opt_max_list_buffers" ]; then
printf ' …'
break
fi
name=${1%_*}
if [ "$name" = "$kak_bufname" ]; then
printf '>'
elif [ "$name" = "$kak_opt_alt_bufname" ]; then
printf '#'
else
printf ' '
fi
modified=${1##*_}
if [ "$modified" = true ]; then
printf '+ '
else
printf ' '
fi
if [ "$index" -lt 10 ]; then
echo "0$index - $name"
else
echo "$index - $name"
fi
shift
done
printf ^\\n
}
}
declare-user-mode pick-buffers
define-command pick-buffers -docstring 'enter buffer pick mode' %{
refresh-buffers-info
unmap global pick-buffers
evaluate-commands %sh{
docstring() {
if [ "$1" = true ]; then
printf "%s+ %s" "$2" "$3"
else
printf "%s %s" "$2" "$3"
fi
}
index=0
keys=" $kak_opt_buffer_keys"
num_keys=${#kak_opt_buffer_keys}
eval "set -- $kak_quoted_opt_buffers_info"
while [ "$1" ]; do
# limit lists too big
index=$((index + 1))
if [ "$index" -gt "$num_keys" ]; then
break
fi
buf_id=$(echo ${keys} | cut -c${index})
name=${1%_*}
modified=${1##*_}
if [ "$name" = "$kak_bufname" ]; then
printf "map global pick-buffers %s ': buffer-by-index %s<ret>' -docstring '%s'\n" ${buf_id} $index "$(docstring $modified '>' "$name")"
elif [ "$name" = "$kak_opt_alt_bufname" ]; then
printf "map global pick-buffers %s ': buffer-by-index %s<ret>' -docstring '%s'\n" ${buf_id} $index "$(docstring $modified '#' "$name")"
else
printf "map global pick-buffers %s ': buffer-by-index %s<ret>' -docstring '%s'\n" ${buf_id} $index "$(docstring $modified ':' "$name")"
fi
shift
done
}
enter-user-mode pick-buffers
}
define-command buffer-first -docstring 'move to the first buffer in the list' 'buffer-by-index 1'
define-command buffer-last -docstring 'move to the last buffer in the list' %{
buffer-by-index %opt{buffers_total}
}
define-command -hidden -params 1 buffer-by-index %{
refresh-buffers-info
evaluate-commands %sh{
target=$1
index=0
eval "set -- $kak_quoted_opt_buffers_info"
while [ "$1" ]; do
index=$((index+1))
name=${1%_*}
if [ $index = $target ]; then
printf "buffer '$name'"
fi
shift
done
}
}
define-command buffer-first-modified -docstring 'move to the first modified buffer in the list' %{
refresh-buffers-info
evaluate-commands %sh{
eval "set -- $kak_quoted_opt_buffers_info"
while [ "$1" ]; do
name=${1%_*}
modified=${1##*_}
if [ "$modified" = true ]; then
printf "buffer '$name'"
fi
shift
done
}
}
define-command delete-buffers -docstring 'delete all saved buffers' %{
evaluate-commands %sh{
deleted=0
eval "set -- $kak_quoted_buflist"
while [ "$1" ]; do
echo "try %{delete-buffer '$1'}"
echo "echo -markup '{Information}$deleted buffers deleted'"
deleted=$((deleted+1))
shift
done
}
}
define-command buffer-only -docstring 'delete all saved buffers except current one' %{
evaluate-commands %sh{
deleted=0
eval "set -- $kak_quoted_buflist"
while [ "$1" ]; do
if [ "$1" != "$kak_bufname" ]; then
echo "try %{delete-buffer '$1'}"
echo "echo -markup '{Information}$deleted buffers deleted'"
deleted=$((deleted+1))
fi
shift
done
}
}
define-command buffer-only-force -docstring 'delete all buffers except current one' %{
evaluate-commands %sh{
deleted=0
eval "set -- $kak_quoted_buflist"
while [ "$1" ]; do
if [ "$1" != "$kak_bufname" ]; then
echo "delete-buffer! '$1'"
echo "echo -markup '{Information}$deleted buffers deleted'"
deleted=$((deleted+1))
fi
shift
done
}
}
define-command buffer-only-directory -docstring 'delete all saved buffers except the ones in the same current buffer directory' %{
evaluate-commands %sh{
deleted=0
current_buffer_dir=$(dirname "$kak_bufname")
eval "set -- $kak_quoted_buflist"
while [ "$1" ]; do
dir=$(dirname "$1")
if [ $dir != "$current_buffer_dir" ]; then
echo "try %{delete-buffer '$1'}"
echo "echo -markup '{Information}$deleted buffers deleted'"
deleted=$((deleted+1))
fi
shift
done
}
}
define-command edit-kakrc -docstring 'open kakrc in a new buffer' %{
evaluate-commands %sh{
printf "edit $kak_config/kakrc"
}
}
declare-user-mode buffers
map global buffers a 'ga' -docstring 'alternate ↔'
map global buffers b ': info-buffers<ret>' -docstring 'info'
map global buffers c ': edit-kakrc<ret>' -docstring 'config'
map global buffers d ': delete-buffer<ret>' -docstring 'delete'
map global buffers D ': delete-buffers<ret>' -docstring 'delete all'
map global buffers f ': buffer<space>' -docstring 'find'
map global buffers h ': buffer-first<ret>' -docstring 'first ⇐'
map global buffers l ': buffer-last<ret>' -docstring 'last ⇒'
map global buffers m ': buffer-first-modified<ret>' -docstring 'modified'
map global buffers n ': buffer-next<ret>' -docstring 'next →'
map global buffers o ': buffer-only<ret>' -docstring 'only'
map global buffers p ': buffer-previous<ret>' -docstring 'previous ←'
map global buffers r ': rename-buffer ' -docstring 'rename'
map global buffers s ': edit -scratch *scratch*<ret>' -docstring '*scratch*'
map global buffers u ': buffer *debug*<ret>' -docstring '*debug*'
# trick to access count, 3b → display third buffer
define-command -hidden enter-buffers-mode %{
evaluate-commands %sh{
if [ "$kak_count" -eq 0 ]; then
printf 'enter-user-mode buffers'
else
printf "buffer-by-index $kak_count"
fi
}
}
# Suggested hook
#hook global WinDisplay .* info-buffers
# Suggested mappings
#map global user b ':enter-buffers-mode<ret>' -docstring 'buffers…'
#map global user B ':enter-user-mode -lock buffers<ret>' -docstring 'buffers (lock)…'
# Suggested aliases
#alias global bd delete-buffer
#alias global bf buffer-first
#alias global bl buffer-last
#alias global bo buffer-only
#alias global bo! buffer-only-force