forked from marlonrichert/zsh-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.autocomplete.async
533 lines (450 loc) · 15.2 KB
/
.autocomplete.async
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/bin/zsh
zmodload -Fa zsh/zpty b:zpty
zmodload -Fa zsh/parameter p:funcstack p:functions p:parameters
zmodload -Fa zsh/system p:sysparams
zmodload -Fa zsh/zselect b:zselect
zmodload -Fa zsh/terminfo b:echoti
zmodload -Fa zsh/zutil b:zparseopts
builtin autoload -Uz add-zle-hook-widget
typeset -gHi _autocomplete__buffer_start_line=1
typeset -g ZSH_AUTOSUGGEST_USE_ASYNC=yes
zle -N history-incremental-search-backward .autocomplete.async.history-incremental-search
zle -N history-incremental-search-forward .autocomplete.async.history-incremental-search
bindkey -s -M menuselect '^R' '^G^R'
bindkey -s -M menuselect '^S' '^G^S'
.autocomplete.async.precmd() {
typeset -gHF _autocomplete__async_avg_duration
zstyle -s :autocomplete: min-delay _autocomplete__async_avg_duration
(( _autocomplete__async_avg_duration += 0.1 ))
zle -N .autocomplete.async.complete.fd-widget
zle -N .autocomplete.async.timeout.fd-widget
# Name starting with `.` avoids getting wrapped by syntax highlighting.
zle -C ._list_choices list-choices .autocomplete.async.list-choices.completion-widget
if [[ -v functions[_zsh_highlight_call_widget] ]]; then
_zsh_highlight_call_widget() {
.autocomplete.zle-flags $WIDGET
builtin zle "$@"
}
fi
if [[ -v functions[_zsh_autosuggest_highlight_apply] ]]; then
local action
for action in clear modify fetch accept partial_accept execute enable disable toggle; do
eval "_zsh_autosuggest_widget_$action() {
.autocomplete.zle-flags \$WIDGET # Set flags according to widget name.
_zsh_autosuggest_$action \"\$@\"
}"
done
_zsh_autosuggest_widget_suggest() {
.autocomplete.zle-flags # Maintain previously set flags.
_zsh_autosuggest_suggest "$@"
}
fi
add-zsh-hook precmd .autocomplete.async.update-location
add-zle-hook-widget line-init .autocomplete.async.reset-context
add-zle-hook-widget line-init .autocomplete.async.complete
add-zle-hook-widget line-pre-redraw .autocomplete.async.complete
add-zle-hook-widget line-finish .autocomplete.async.clear
add-zle-hook-widget isearch-update .autocomplete.async.isearch-update
add-zle-hook-widget isearch-exit .autocomplete.async.isearch-exit
add-zsh-hook zshexit .autocomplete.async.stop
}
.autocomplete.async.max-lines() {
local -i min_lines max_lines lines_below_buffer
zstyle -s ":autocomplete:${curcontext}:" list-lines min_lines || min_lines=16
(( max_lines = LINES - BUFFERLINES ))
(( lines_below_buffer = max_lines - _autocomplete__buffer_start_line ))
return $(( max( min( min_lines, max_lines ), lines_below_buffer ) ))
}
.autocomplete.async.update-location() {
emulate -L zsh; setopt $_autocomplete__options
# Ask the terminal which line the cursor is on.
local -a reply=()
print -nr $'\e[6n'
IFS=$'\e[;' read -Asd R
# Add the number of newlines the prompt will print.
local -i pslines=${#${(%%)PS1}//[^$'\n']}
_autocomplete__buffer_start_line=$(( reply[-2] + pslines ))
}
.autocomplete.async.history-incremental-search() {
if [[ $curcontext == $WIDGET* ]]; then
unset curcontext
else
typeset -gH curcontext=${WIDGET}:::
fi
.autocomplete.async.start
}
.autocomplete.async.reset-context() {
typeset -gH curcontext=''
zstyle -s :autocomplete: default-context curcontext
return 0
}
.autocomplete.async.isearch-update() {
typeset -gHi _autocomplete__isearch=1
}
.autocomplete.async.isearch-exit() {
.autocomplete.zle-flags $LASTWIDGET
unset _autocomplete__isearch
}
.autocomplete.async.complete() {
.autocomplete.zle-flags $LASTWIDGET
(( KEYS_QUEUED_COUNT || PENDING )) &&
return 0
[[ $LASTWIDGET == .autocomplete.async.complete.fd-widget ]] &&
return 0
region_highlight=()
[[ -v functions[_zsh_highlight] ]] &&
_zsh_highlight
typeset -gH _autocomplete__highlight=( "$region_highlight[@]" )
[[ -v functions[_zsh_autosuggest_highlight_apply] ]] &&
_zsh_autosuggest_highlight_apply
if (( REGION_ACTIVE )) ||
[[ -v _autocomplete__isearch && $LASTWIDGET == *(incremental|isearch)* ]]; then
zle -Rc
return 0
fi
if [[ $LASTWIDGET == ('list-expand'|'_complete_help') ||
-n $MENUSELECT && $LASTWIDGET == .autocomplete.async.timeout.fd-widget ]]; then
.autocomplete.async.stop
return 0
fi
[[ $_lastcomp[insert] == *unambiguous ]] &&
zle .auto-suffix-retain # Makes the cursor stay in the right place.
.autocomplete.async.start
return 0
}
.autocomplete.async.stop() {
.autocomplete.async.kill _autocomplete__async_complete_fd _autocomplete__async_complete_pid
.autocomplete.async.kill _autocomplete__async_timeout_fd _autocomplete__async_timeout_pid
return 0
}
.autocomplete.async.clear() {
zle -Rc
unset _autocomplete__isearch
.autocomplete.async.stop
.autocomplete.async.reset-context
return 0
}
.autocomplete.async.kill() {
emulate -L zsh; setopt $_autocomplete__options
local -i fd=${(P)1} pid=${(P)2}
if (( pid )); then
[[ -o MONITOR ]] &&
(( pid *= -1 ))
kill -KILL $pid 2>/dev/null
fi
if (( fd )) && { : <&$fd } 2>/dev/null; then
zle -F $fd 2>/dev/null
exec {fd}<&-
fi
unset $1 $2
}
.autocomplete.async.start() {
setopt $_autocomplete__options
local +h -F SECONDS=0
.autocomplete.async.stop
local -F min_delay; zstyle -s :autocomplete: min-delay min_delay ||
min_delay=0
exec {_autocomplete__async_complete_fd}< <(
print $sysparams[pid]
{
local REPLY
zpty _autocomplete__zpty .autocomplete.async.complete.inner '$LBUFFER' '$RBUFFER'
zpty -w _autocomplete__zpty $'\t'
local line
zpty -r _autocomplete__zpty line '*'$'\0'$'\0'
zpty -r _autocomplete__zpty line '*'$'\0'$'\0'
# `zselect -t` w/out other args wants an int > 0
local -i timeout=$(( 100.0 * (min_delay - SECONDS) ))
(( timeout > 0 )) &&
zselect -t $timeout
} always {
zpty -d _autocomplete__zpty
(( TRY_BLOCK_INTERRUPT > 0 )) ||
print -rNC1 - "$SECONDS" "$line"
}
)
read -u $_autocomplete__async_complete_fd _autocomplete__async_complete_pid
zle -Fw "$_autocomplete__async_complete_fd" .autocomplete.async.complete.fd-widget
exec {_autocomplete__async_timeout_fd}< <(
print $sysparams[pid]
{
local -i timeout=$((
100.0 * (min_delay - SECONDS + 10.0 * _autocomplete__async_avg_duration)
))
(( timeout > 0 )) &&
zselect -t $timeout
} always {
(( TRY_BLOCK_INTERRUPT ))
print $?
}
)
read -u $_autocomplete__async_timeout_fd _autocomplete__async_timeout_pid
zle -Fw "$_autocomplete__async_timeout_fd" .autocomplete.async.timeout.fd-widget
[[ -v functions[_zsh_autosuggest_highlight_apply] && -n $MENUSELECT ]] &&
unset POSTDISPLAY
# There's a weird bug in Zsh < 5.8, where where ^C stops working unless we force a fork.
# See https://github.com/zsh-users/zsh-autosuggestions/issues/364
command true
}
.autocomplete.async.complete.inner() {
setopt $_autocomplete__options
setopt NO_completeinword; [[ -z $curcontext ]] ||
setopt completeinword
setopt promptsubst
PS1=''
PS4=$_autocomplete__x
local hooks=( chpwd periodic precmd preexec zshaddhistory zshexit )
unset ${^hooks}_functions &>/dev/null
$hooks[@] () { : }
.autocomplete.no-op() { : }
local hook; for hook in isearch-exit isearch-update line-pre-redraw line-init line-finish \
history-line-set keymap-select; do
zle -N zle-$hook .autocomplete.no-op
done
typeset -gH lbuffer=$1 rbuffer=$2
complete-word() {
LBUFFER=$lbuffer
RBUFFER=$rbuffer
zle list-choices -w
}
message() {
typeset -gH _message_=$mesg
}
list-choices() {
local curcontext=${curcontext:-${WIDGET}:::}
unset 'compstate[vared]'
local +h -a comppostfuncs=( message )
{
.autocomplete.async.list-choices.complete 2>&$_autocomplete__log_fd
} always {
print -rNC1 - '' '' "$compstate[nmatches]" "${(q+)_message_}" ''
}
compstate[insert]=''
compstate[list]=''
compstate[list_max]=0
}
zle -N complete-word
zle -C list-choices list-choices list-choices
bindkey '^I' complete-word
local __tmp__
vared __tmp__
}
.autocomplete.async.complete.fd-widget() {
.autocomplete.zle-flags ||
return 0
setopt $_autocomplete__options
setopt NO_completeinword; [[ -z $curcontext ]] ||
setopt completeinword
.autocomplete.async.kill _autocomplete__async_timeout_fd _autocomplete__async_timeout_pid
local -F seconds
local -i nmatches
local message rest
{
[[ $2 == (|hup) ]] &&
IFS=$'\0' read -ru $1 seconds nmatches message rest
} always {
.autocomplete.async.kill _autocomplete__async_complete_fd _autocomplete__async_complete_pid
}
(( _autocomplete__async_avg_duration = .1 * seconds + .9 * _autocomplete__async_avg_duration ))
# If a widget can't be called, ZLE always returns true.
# Thus, we return false on purpose, so we can check if our widget got called.
setopt promptsubst
local +h PS4=$_autocomplete__x
if ! zle ._list_choices -w "$nmatches" "${(Q)message}" 2>&$_autocomplete__log_fd; then
region_highlight=( "$_autocomplete__highlight[@]" )
[[ -v functions[_zsh_autosuggest_highlight_apply] ]] &&
_zsh_autosuggest_highlight_apply
# Refresh if and only if our widget got called. Otherwise, Zsh will crash (eventually).
zle -R
else
.autocomplete.async.stop
fi
return 0
}
.autocomplete.async.list-choices.completion-widget() {
local -i nmatches=$1
local message=$2
local min_input; zstyle -s :autocomplete: min-input min_input ||
min_input=0
local ignored; zstyle -s :autocomplete: ignored-input ignored
local +h -a comppostfuncs=( .autocomplete.async.list-choices.post "$comppostfuncs[@]" )
if [[ -n $curcontext ]]; then
_main_complete
elif (( $#words[@] == 1 && $#words[CURRENT] < min_input )) ||
[[ -n $words[CURRENT] && $words[CURRENT] == $~ignored ]]; then
zle -Rc
elif (( nmatches == 0 )); then
if [[ -n $message ]]; then
local msg
zformat -f msg "$message"
compadd -x "$msg"
else
zle -Rc
fi
else
local curcontext=list-choices:::
.autocomplete.async.list-choices.complete
fi
return 2
}
.autocomplete.async.list-choices.post() {
compstate[insert]=
unset MENUSELECT MENUMODE
}
.autocomplete.async.list-choices.complete() {
.autocomplete.async.max-lines
local -i _async_max_lines=$?
{
() {
emulate -L zsh; setopt $_autocomplete__options
functions[compadd]=$functions[.autocomplete.async.compadd]
} "$@"
_main_complete
} always {
() {
emulate -L zsh; setopt $_autocomplete__options
[[ -v functions[compadd] ]] &&
unfunction compadd
[[ -v functions[comptags] ]] &&
unfunction comptags
}
}
}
.autocomplete.async.compadd() {
if [[ $funcstack[2] == _autocomplete.history_lines ]]; then
.autocomplete.compadd "$@"
return
fi
local -a _opts_=()
zparseopts -E -A _opts_ - D: E: x: X:
if [[ -v _opts_[-E] || -v _opts_[-x] ]]; then
.autocomplete.compadd "$@"
return
fi
local -a _matches_=()
local -i header=$+_opts_[-X] footer=1 allinsert=2
local -i lines_available=$((
max(0, _async_max_lines - header - footer - allinsert - compstate[list_lines])
))
local -i ret number_of_new_matches lines_of_new_matches
if [[ -v _opts_[-D] ]]; then
.autocomplete.compadd "$@"; ret=$?
[[ $funcstack[2] == _describe ]] ||
return ret
local array_name=$_opts_[-D]
local -a _matches_=( ${(PA)array_name} )
(( ${_matches_[(I)*:*]} > 0 )) ||
return ret
local -aU uniques=( ${_matches_[@]#*:} )
number_of_new_matches=$#_matches_
lines_of_new_matches=$#uniques[@]
(( lines_available -= _autocomplete__reserved_lines ))
else
_autocomplete__reserved_lines=0
local -i fd list_lines=0 nmatches=0
{
exec {fd}< <(
{
_opts_=()
zparseopts -D -E -A _opts_ - A: D: O: X:
.autocomplete.compadd -O _matches_ "$@"
# Pre-emptively trim all matches that will definitely not fit on screen.
local -i surplus=$(( $#_matches_ - COLUMNS * _async_max_lines / 3 ))
(( surplus > 0 )) &&
shift -p $surplus _matches_
setopt localoptions listtypes
zparseopts -D -E -A _opts_ - a
_autocomplete.compadd_opts_len "$@"
.autocomplete.compadd "${(@)@[1,?]}" -a - _matches_
} always {
print $compstate[list_lines]
print $compstate[nmatches]
print -rNC1 - "$_matches_[@]"
}
)
IFS=$'\0' read -u $fd list_lines
IFS=$'\0' read -u $fd nmatches
IFS=$'\0' read -u $fd -A _matches_
} always {
exec {fd}<&-
}
_matches_=( ${_matches_[@]:#} )
number_of_new_matches=$(( nmatches - compstate[nmatches] ))
lines_of_new_matches=$(( list_lines - compstate[list_lines] ))
fi
if (( lines_of_new_matches <= lines_available )); then
if [[ $funcstack[2] == _describe && -v _opts_[-D] ]]; then
(( _autocomplete__reserved_lines += $lines_of_new_matches ))
return ret
fi
.autocomplete.compadd "$@"
return
fi
local info hint=$'%{\e[02;39m%}' kbd=$'%{\e[22;39m%}' end=$'%{\e[0m%}'
zstyle -s ":autocomplete:${curcontext}:too-many-matches" message info ||
info="${hint}(partial list; press ${kbd}Down$hint to expand)$end"
if [[ -v _opts_[-D] ]]; then
if (( $#uniques > lines_available )); then
builtin compadd -x $info
shift -p $(( $#uniques - max(0, lines_available - 1) )) uniques
set -A $array_name ${(M@)${(PA)array_name}:#*:(${(~j:|:)uniques})}
fi
(( _autocomplete__reserved_lines += $#uniques ))
(( ${(P@)#array_name} > 0 ))
return
fi
zparseopts -D -E -A _opts_ - a d: k l U
local -a dopt=()
if [[ -v _opts_[-d] ]]; then
if [[ -v _opts_[-l] ]]; then
dopt+=( -ld )
else
dopt+=( -d )
fi
dopt+=( "$_opts_[-d]" )
fi
if (( lines_of_new_matches > 0 )); then
local -F matches_per_line=$(( 1.0 * number_of_new_matches / lines_of_new_matches ))
if (( matches_per_line < 1 )); then
dopt[1]=-ld
array_name=$_opts_[-d]
if [[ -z $array_name ]]; then
array_name=displ
local -a displ=( "$_matches_[@]" )
dopt[2]=displ
fi
set -A $array_name ${(r:COLUMNS-1:@)${(P@)array_name}//$'\n'/\n}
matches_per_line=1
fi
if (( $#_matches_ > matches_per_line * lines_available )); then
builtin compadd -x $info
# Round this down, before subtracting.
local -i fit=$(( matches_per_line * max(0, lines_available - 1) ))
shift -p $(( $#_matches_ - fit )) _matches_
(( $#_matches_ > 0 )) ||
comptags() {
[[ $funcstack[3] == _autocomplete.history_lines ]] ||
return 1
builtin comptags "$@"
}
fi
fi
_autocomplete.compadd_opts_len "$@"
.autocomplete.compadd "${(@)@[1,?]}" $dopt -a _matches_
}
.autocomplete.async.timeout.fd-widget() {
.autocomplete.zle-flags ||
return 0
local -i timedout=0
{
[[ $2 == (|hup) ]] &&
read -ru $1 timedout
} always {
.autocomplete.async.kill _autocomplete__async_timeout_fd _autocomplete__async_timeout_pid
}
(( timedout )) ||
return 0
(( _autocomplete__async_avg_duration *= 1.1 ))
.autocomplete.async.kill _autocomplete__async_complete_fd _autocomplete__async_complete_pid
return 0
}