-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter-select
435 lines (356 loc) · 12.7 KB
/
filter-select
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#
# filter-select
#
# using filter-select, you can incrementaly filter candidate
# and select one with ^N/^P keys.
#
# press enter for filter-select to update $reply and return 0,
# press meta (alt) + enter to update $reply but return 1,
# and press ^C or ^G not to update $reply and return 1.
#
# you can customize keybinds using bindkey command.
# first, you call::
#
# autoload -U filter-select; filter-select -i
#
# to initialize `filterselect` keymap and then do like::
#
# bindkey -M filterselect '^E' accept-search
#
#
# usage:
# filter-select [-A assoc-array-name] [-r] [--] [arg]...
# filter-select -i
#
# default key binds in filterselect:
# enter: accept-line (update $reply and return)
# meta + enter: accept-search (update $reply but return 1)
# ^G: send-break (return 0)
# ^H, backspace: backward-delete-char
# ^F, right key: forward-char
# ^B, left key: backward-char
# ^A: beginning-of-line
# ^E: end-of-line
# ^W: backward-kill-word
# ^K: kill-line
# ^U: kill-whole-line
# ^N, down key: down-line-or-history (select next item)
# ^P, up key: up-line-or-history (select previous item)
# ^V, page up key: forward-word (page down)
# ^[V, page down key: backward-word (page up)
# ^[<, home key: beginning-of-history (select first item)
# ^[>, end key: end-of-history (select last item)
#
# available zstyles:
# ':filter-select:highlight' selected
# ':filter-select:highlight' matched
# ':filter-select' max-lines
#
# example:
# zstyle ':filter-select:highlight' matched fg=yellow,standout
# zstyle ':filter-select' max-lines 10 # use 10 lines for filter-select
# zstyle ':filter-select' max-lines -10 # use $LINES - 10 for filter-select
function filter-select() {
emulate -L zsh
setopt local_options extended_glob
# save ZLE related variables
local orig_lbuffer="${LBUFFER}"
local orig_rbuffer="${RBUFFER}"
local orig_predisplay="${PREDISPLAY}"
local orig_postdisplay="${POSTDISPLAY}"
local -a orig_region_highlight
orig_region_highlight=("${region_highlight[@]}")
local key cand lines selected cand_disp buffer_pre_zle last_buffer='' opt pattern msg unused
local -a displays matched_cand_keys match mbegin mend outs
local -A candidates
integer i bottom_lines cursor_line=1 display_head_line=1 cand_num disp_num offset display_bottom_line selected_num rev=0 ret=0
local hi_selected hi_matched
zstyle -s ':filter-select:highlight' selected hi_selected || hi_selected='standout'
zstyle -s ':filter-select:highlight' matched hi_matched || hi_matched='fg=magenta,underline'
integer max_lines
zstyle -s ':filter-select' max-lines max_lines || max_lines=0
_filter-select-init-keybind
candidates=()
while getopts 'A:ri' opt; do
case "${opt}" in
A)
# copy input assc array
candidates=("${(@kvP)${OPTARG}}")
;;
r)
# reverse ordering
rev=1
;;
i)
# do nothing. only keybinds are initialized
return
esac
done
if (( OPTIND > 1 )); then
shift $(( OPTIND - 1 ))
fi
integer i=0
for cand in "$@"; do
(( i++ ))
candidates+=( $i "${cand}" )
done
cand_num="${#candidates}"
matched_cand_keys=("${(onk@)candidates}")
if (( rev )); then
matched_cand_keys=("${(Oa@)matched_cand_keys}")
fi
key=''
bounds=''
_filter-select-reset
while [[ "${bounds}" != (accept-line|accept-search|send-break) ]]; do
case "${bounds}" in
*down-line-or-history)
(( cursor_line++ ))
;;
*up-line-or-history)
(( cursor_line-- ))
;;
*forward-word)
(( cursor_line += bottom_lines ))
;;
*backward-word)
(( cursor_line -= bottom_lines ))
;;
beginning-of-history)
(( cursor_line = 1 ))
(( display_head_line = 1 ))
;;
end-of-history)
(( cursor_line = cand_num ))
;;
self-insert|undefined-key)
LBUFFER="${LBUFFER}${key}"
_filter-select-reset
;;
'')
# empty, initial state
;;
*)
buffer_pre_zle="${BUFFER}"
zle "${bounds}"
if [[ "${BUFFER}" != "${buffer_pre_zle}" ]]; then
_filter-select-reset
fi
esac
if (( cursor_line < 1 )); then
(( display_head_line -= 1 - cursor_line ))
if (( display_head_line < 1 )); then
(( display_head_line = 1 ))
fi
(( cursor_line = 1 ))
elif (( bottom_lines == 0 )); then
(( display_head_line = 1 ))
(( cursor_line = 1 ))
elif (( cursor_line > bottom_lines )); then
(( display_head_line += cursor_line - bottom_lines ))
if (( display_head_line > cand_num - bottom_lines + 1 )); then
(( display_head_line = cand_num - bottom_lines + 1 ))
fi
(( cursor_line = bottom_lines ))
fi
if (( ! PENDING )); then
region_highlight=("${orig_region_highlight[@]}")
displays=()
offset="${#BUFFER}"
selected=""
selected_num=0
if [[ "${BUFFER}" != "${last_buffer}" ]]; then
if [[ -n "${BUFFER}" ]]; then
# escape "(", ")", "[", "]" and "#" to avoid crash
pattern="${BUFFER//(#m)[()[\]#]/\\${MATCH}}"
matched_cand_keys=("${(onk@)candidates[(R)*${pattern}*]}")
else
matched_cand_keys=("${(onk@)candidates}")
fi
if (( rev )); then
matched_cand_keys=("${(Oa@)matched_cand_keys}")
fi
last_buffer="${BUFFER}"
fi
# nums pattern matched
cand_num="${#matched_cand_keys}"
# nums displayed
disp_num=0
_filter-select-update-bottom-lines
display_bottom_line=$(( display_head_line + bottom_lines))
for i in "${(@)matched_cand_keys[${display_head_line},$(( display_bottom_line - 1 ))]}"; do
(( disp_num++ ))
cand="${candidates[$i]}"
cand_disp="${cand}"
# escape \r\n\t\
cand_disp="${cand_disp//\\/\\\\}"
cand_disp="${cand_disp//$'\n'/\\n}"
cand_disp="${cand_disp//$'\r'/\\r}"
cand_disp="${cand_disp//$'\t'/\\t}"
if (( ${(m)#cand_disp} > COLUMNS - 1 )); then
# strip long line
cand_disp="${(mr:$(( COLUMNS - 6 )):::::)cand_disp} ..."
fi
displays+="${cand_disp}"
if [[ -n "${BUFFER}" && "${cand_disp}" == (#b)*(${~pattern})* ]]; then
# highlight matched words
region_highlight+="$(( offset + mbegin[1] )) $(( offset + mend[1] + 1 )) ${hi_matched}"
fi
if (( disp_num == cursor_line )); then
region_highlight+="${offset} $(( offset + ${#cand_disp} + 1 )) ${hi_selected}"
selected="${cand}"
(( selected_num = display_head_line + disp_num - 1 ))
fi
(( offset += ${#cand_disp} + 1 )) # +1 -> \n
done
if (( ${#displays} == 0 )); then
msg='pattern not found'
displays=( "${msg}" )
region_highlight+="${offset} $(( offset + ${#msg} + 1 )) fg=white,bg=red"
fi
POSTDISPLAY=$'\n'"${(F)displays}"$'\n'"[${selected_num}/${cand_num}]"
zle -R
fi
_filter-select-read-keys
if [[ $? != 0 ]]; then
# maybe ^C
key=''
bounds=''
break
else
key="${reply}"
# TODO: key sequence
outs=("${(z)$( bindkey -M filterselect -- "${key}" )}")
# XXX: will $outs contains more than two values?
bounds="${outs[2]}"
fi
done
if [[ "${bounds}" == accept-line ]]; then
reply="${selected}"
ret=0
elif [[ "${bounds}" == accept-search ]]; then
reply="${selected}"
ret=1
elif [[ "${bounds}" == send-break ]]; then
reply=''
ret=1
fi
LBUFFER="${orig_lbuffer}"
RBUFFER="${orig_rbuffer}"
PREDISPLAY="${orig_predisplay}"
POSTDISPLAY="${orig_postdisplay}"
region_highlight=("${orig_region_highlight[@]}")
zle -Rc
zle reset-prompt
return $ret
}
function _filter-select-update-lines() {
# XXX: this function override ${lines}
# that define as local in filter-select
local _tmp_postdisplay="${POSTDISPLAY}"
# to re-calculate ${BUFFERLINES}
POSTDISPLAY=""
zle -R
# lines that can be used to display candidates
# -1 for current/total number display area
(( lines = LINES - BUFFERLINES - 1 ))
POSTDISPLAY="${_tmp_postdisplay}"
zle -R
}
function _filter-select-update-bottom-lines() {
# cursor が移動できる一番下の行
# ${max_lines} か ${lines} か ${cand_num} の小さい方を使う
if (( max_lines > 0 && max_lines < lines )); then
(( bottom_lines = max_lines ))
elif (( max_lines < 0 )); then
(( bottom_lines = lines + max_lines ))
else
(( bottom_lines = lines ))
fi
if (( cand_num < bottom_lines )); then
(( bottom_lines = cand_num ))
fi
if (( bottom_lines < 1 )); then
(( bottom_lines = 1 ))
fi
}
function _filter-select-reset() {
display_head_line=1
cursor_line=1
_filter-select-update-lines
_filter-select-update-bottom-lines
}
function _filter-select-init-keybind() {
integer fd ret
# be quiet and check filterselect keybind defined
exec {fd}>&2 2>/dev/null
bindkey -l filterselect > /dev/null
ret=$?
exec 2>&${fd} {fd}>&-
if (( ret != 0 )); then
bindkey -N filterselect
bindkey -M filterselect '^J' accept-line
bindkey -M filterselect '^M' accept-line
bindkey -M filterselect '^[^J' accept-search
bindkey -M filterselect '^[^M' accept-search
bindkey -M filterselect '^[^G' send-break
bindkey -M filterselect '^G' send-break
bindkey -M filterselect '^H' backward-delete-char
bindkey -M filterselect '^?' backward-delete-char
bindkey -M filterselect '^F' forward-char
bindkey -M filterselect '^[[C' forward-char
bindkey -M filterselect '^B' backward-char
bindkey -M filterselect '^[[D' backward-char
bindkey -M filterselect '^A' beginning-of-line
bindkey -M filterselect '^E' end-of-line
bindkey -M filterselect '^W' backward-kill-word
bindkey -M filterselect '^K' kill-line
bindkey -M filterselect '^U' kill-whole-line
# move cursor down/up
bindkey -M filterselect '^N' down-line-or-history
bindkey -M filterselect '^[[B' down-line-or-history
bindkey -M filterselect '^P' up-line-or-history
bindkey -M filterselect '^[[A' up-line-or-history
# page down/up
bindkey -M filterselect '^V' forward-word
bindkey -M filterselect '^[[6~' forward-word
bindkey -M filterselect '^[V' backward-word
bindkey -M filterselect '^[v' backward-word
bindkey -M filterselect '^[[5~' backward-word
# home/end
bindkey -M filterselect '^[<' beginning-of-history
bindkey -M filterselect '^[[1~' beginning-of-history
bindkey -M filterselect '^[>' end-of-history
bindkey -M filterselect '^[[4~' end-of-history
fi
}
function _filter-select-read-keys() {
local key key2 key3 key4
integer ret
read -k key
ret=$?
reply="${key}"
if [[ '#key' -eq '#\\e' ]]; then
# M-...
read -t $(( KEYTIMEOUT / 1000 )) -k key2
if [[ "${key2}" == '[' ]]; then
# cursor keys
read -k key3
ret=$?
if [[ "${key3}" == [0-9] ]]; then
# Home, End, PgUp, PgDn ...
read -k key4
ret=$?
reply="${key}${key2}${key3}${key4}"
else
reply="${key}${key2}${key3}"
fi
else
reply="${key}${key2}"
fi
else
reply="${key}"
fi
return $ret
}
filter-select "$@"