Compare commits

..

6 Commits

Author SHA1 Message Date
Eric Freese
cbf0e24b18 Merge pull request #415 from zsh-users/develop
v0.5.1
2019-04-03 10:58:45 -06:00
Eric Freese
f94e667f59 v0.5.1 2019-04-03 10:52:43 -06:00
Eric Freese
3ee91c731c Update changelog for v0.5.1 release 2019-04-03 10:51:48 -06:00
Eric Freese
0b13f663da Merge pull request #413 from zsh-users/features/faster-prompts
Features/faster prompts
2019-04-02 07:39:55 -06:00
romkatv
4a82ff1ead speed up widget rebinding by removing redundant array subscripts 2019-04-01 14:56:53 -06:00
Eric Freese
f76472272e cleanup: Remove unnecessary braces 2019-04-01 14:36:31 -06:00
11 changed files with 165 additions and 295 deletions

View File

@@ -1,5 +1,10 @@
# Changelog # Changelog
## v0.5.1
- Speed up widget rebinding (#413)
- Clean up global variable creations (#403)
- Respect user's set options when running original widget (#402)
## v0.5.0 ## v0.5.0
- Don't overwrite config with default values (#335) - Don't overwrite config with default values (#335)
- Support fallback strategies by supplying array to suggestion config var - Support fallback strategies by supplying array to suggestion config var

View File

@@ -1 +1 @@
v0.5.0 v0.5.1

View File

@@ -1,24 +1,16 @@
describe 'a wrapped widget' do describe 'a wrapped widget' do
let(:widget) { 'backward-delete-char' } let(:widget) { 'backward-delete-char' }
let(:initialize_widget) do
-> do
session.run_command(<<~ZSH)
if [[ "$widgets[#{widget}]" == "builtin" ]]; then
_orig_#{widget}() { zle .#{widget} }
zle -N orig-#{widget} _orig_#{widget}
else
zle -N orig-#{widget} ${widgets[#{widget}]#*:}
fi
#{widget}-magic() { zle orig-#{widget}; BUFFER+=b }
zle -N #{widget} #{widget}-magic
ZSH
end
end
context 'initialized before sourcing the plugin' do context 'initialized before sourcing the plugin' do
let(:before_sourcing) { initialize_widget } let(:before_sourcing) do
-> do
session.
run_command("_orig_#{widget}() { zle .#{widget} }").
run_command("zle -N orig-#{widget} _orig_#{widget}").
run_command("#{widget}-magic() { zle orig-#{widget}; BUFFER+=b }").
run_command("zle -N #{widget} #{widget}-magic")
end
end
it 'executes the custom behavior and the built-in behavior' do it 'executes the custom behavior and the built-in behavior' do
with_history('foobar', 'foodar') do with_history('foobar', 'foodar') do
@@ -29,7 +21,13 @@ describe 'a wrapped widget' do
end end
context 'initialized after sourcing the plugin' do context 'initialized after sourcing the plugin' do
let(:after_sourcing) { initialize_widget } before do
session.
run_command("zle -N orig-#{widget} ${widgets[#{widget}]#*:}").
run_command("#{widget}-magic() { zle orig-#{widget}; BUFFER+=b }").
run_command("zle -N #{widget} #{widget}-magic").
clear_screen
end
it 'executes the custom behavior and the built-in behavior' do it 'executes the custom behavior and the built-in behavior' do
with_history('foobar', 'foodar') do with_history('foobar', 'foodar') do

View File

@@ -9,9 +9,8 @@ describe 'using `zle -U`' do
let(:options) { ['unset ZSH_AUTOSUGGEST_USE_ASYNC', 'ZSH_AUTOSUGGEST_STRATEGY=test'] } let(:options) { ['unset ZSH_AUTOSUGGEST_USE_ASYNC', 'ZSH_AUTOSUGGEST_STRATEGY=test'] }
it 'does not fetch a suggestion for every inserted character' do # TODO: This is only possible with the $KEYS_QUEUED_COUNT widget parameter, coming soon...
skip 'KEYS_QUEUED_COUNT not supported below v5.4.0' if session.zsh_version < Gem::Version.new('5.4.0') xit 'does not fetch a suggestion for every inserted character' do
session.send_keys('C-b') session.send_keys('C-b')
wait_for { session.content }.to eq('echo hello') wait_for { session.content }.to eq('echo hello')
end end

View File

@@ -41,20 +41,12 @@ describe 'a zle widget' do
end end
end end
context 'that accesses POSTDISPLAY' do context 'when added to ZSH_AUTOSUGGEST_IGNORE_WIDGETS' do
before { session.run_command("#{widget}() { zle -M \"POSTDISPLAY=$POSTDISPLAY\" }") } let(:options) { ["ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(#{widget})"] }
context 'when added to ZSH_AUTOSUGGEST_IGNORE_WIDGETS' do it 'should not be wrapped with an autosuggest widget' do
let(:options) { ["ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(#{widget})"] } session.run_command("echo $widgets[#{widget}]")
wait_for { session.content }.to end_with("\nuser:#{widget}")
it 'gets the correct POSTDISPLAY value' do
with_history('echo hello') do
session.send_string('e')
wait_for { session.content }.to start_with('echo hello')
session.send_keys('C-b')
wait_for { session.content }.to end_with("\nPOSTDISPLAY=cho hello")
end
end
end end
end end

View File

@@ -4,21 +4,8 @@
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
_zsh_autosuggest_incr_bind_count() { _zsh_autosuggest_incr_bind_count() {
if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then typeset -gi bind_count=$((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]+1))
((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]++)) _ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=$bind_count
else
_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=1
fi
typeset -gi bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]
}
_zsh_autosuggest_get_bind_count() {
if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then
typeset -gi bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]
else
typeset -gi bind_count=0
fi
} }
# Bind a single widget to an autosuggest widget, saving a reference to the original widget # Bind a single widget to an autosuggest widget, saving a reference to the original widget
@@ -34,30 +21,30 @@ _zsh_autosuggest_bind_widget() {
# Save a reference to the original widget # Save a reference to the original widget
case $widgets[$widget] in case $widgets[$widget] in
# Already bound # Already bound
user:_zsh_autosuggest_(bound|orig)_*);; user:_zsh_autosuggest_(bound|orig)_*)
bind_count=$((_ZSH_AUTOSUGGEST_BIND_COUNTS[$widget]))
;;
# User-defined widget # User-defined widget
user:*) user:*)
_zsh_autosuggest_incr_bind_count $widget _zsh_autosuggest_incr_bind_count $widget
zle -N $prefix${bind_count}-$widget ${widgets[$widget]#*:} zle -N $prefix$bind_count-$widget ${widgets[$widget]#*:}
;; ;;
# Built-in widget # Built-in widget
builtin) builtin)
_zsh_autosuggest_incr_bind_count $widget _zsh_autosuggest_incr_bind_count $widget
eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }" eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
zle -N $prefix${bind_count}-$widget _zsh_autosuggest_orig_$widget zle -N $prefix$bind_count-$widget _zsh_autosuggest_orig_$widget
;; ;;
# Completion widget # Completion widget
completion:*) completion:*)
_zsh_autosuggest_incr_bind_count $widget _zsh_autosuggest_incr_bind_count $widget
eval "zle -C $prefix${bind_count}-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}" eval "zle -C $prefix$bind_count-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
;; ;;
esac esac
_zsh_autosuggest_get_bind_count $widget
# Pass the original widget's name explicitly into the autosuggest # Pass the original widget's name explicitly into the autosuggest
# function. Use this passed in widget name to call the original # function. Use this passed in widget name to call the original
# widget instead of relying on the $WIDGET variable being set # widget instead of relying on the $WIDGET variable being set
@@ -72,31 +59,11 @@ _zsh_autosuggest_bind_widget() {
zle -N -- $widget _zsh_autosuggest_bound_${bind_count}_$widget zle -N -- $widget _zsh_autosuggest_bound_${bind_count}_$widget
} }
_zsh_autosuggest_bind_autosuggest_widgets() {
local widget
for widget in $ZSH_AUTOSUGGEST_CLEAR_WIDGETS; do
_zsh_autosuggest_bind_widget $widget clear
done
for widget in $ZSH_AUTOSUGGEST_ACCEPT_WIDGETS; do
_zsh_autosuggest_bind_widget $widget accept
done
for widget in $ZSH_AUTOSUGGEST_EXECUTE_WIDGETS; do
_zsh_autosuggest_bind_widget $widget execute
done
for widget in $ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS; do
_zsh_autosuggest_bind_widget $widget partial_accept
done
}
# Map all configured widgets to the right autosuggest widgets # Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_modify_widgets() { _zsh_autosuggest_bind_widgets() {
emulate -L zsh emulate -L zsh
local widget local widget
local ignore_widgets local ignore_widgets
ignore_widgets=( ignore_widgets=(
@@ -106,15 +73,22 @@ _zsh_autosuggest_bind_modify_widgets() {
autosuggest-\* autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\* $ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS $ZSH_AUTOSUGGEST_IGNORE_WIDGETS
$ZSH_AUTOSUGGEST_CLEAR_WIDGETS
$ZSH_AUTOSUGGEST_ACCEPT_WIDGETS
$ZSH_AUTOSUGGEST_EXECUTE_WIDGETS
$ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS
) )
# Assume any widget omitted from the config arrays might modify the buffer # Find every widget we might want to bind and bind it appropriately
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
_zsh_autosuggest_bind_widget $widget modify if [[ -n ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]]; then
_zsh_autosuggest_bind_widget $widget clear
elif [[ -n ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]]; then
_zsh_autosuggest_bind_widget $widget accept
elif [[ -n ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]]; then
_zsh_autosuggest_bind_widget $widget execute
elif [[ -n ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]]; then
_zsh_autosuggest_bind_widget $widget partial_accept
else
# Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
fi
done done
} }

View File

@@ -73,7 +73,6 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
} }
# Widgets that should be ignored (globbing supported but must be escaped) # Widgets that should be ignored (globbing supported but must be escaped)
# Only relevant for zsh versions older than 5.4
(( ! ${+ZSH_AUTOSUGGEST_IGNORE_WIDGETS} )) && { (( ! ${+ZSH_AUTOSUGGEST_IGNORE_WIDGETS} )) && {
typeset -ga ZSH_AUTOSUGGEST_IGNORE_WIDGETS typeset -ga ZSH_AUTOSUGGEST_IGNORE_WIDGETS
ZSH_AUTOSUGGEST_IGNORE_WIDGETS=( ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(

View File

@@ -5,8 +5,6 @@
# Precmd hooks for initializing the library and starting pty's # Precmd hooks for initializing the library and starting pty's
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook
autoload -Uz add-zle-hook-widget
autoload -Uz is-at-least
# Asynchronous suggestions are generated in a pty # Asynchronous suggestions are generated in a pty
zmodload zsh/zpty zmodload zsh/zpty

View File

@@ -7,22 +7,13 @@
_zsh_autosuggest_start() { _zsh_autosuggest_start() {
add-zsh-hook -d precmd _zsh_autosuggest_start add-zsh-hook -d precmd _zsh_autosuggest_start
_zsh_autosuggest_bind_widgets
# Re-bind widgets on every precmd to ensure we wrap other wrappers. # Re-bind widgets on every precmd to ensure we wrap other wrappers.
# Specifically, highlighting breaks if our widgets are wrapped by # Specifically, highlighting breaks if our widgets are wrapped by
# zsh-syntax-highlighting widgets. This also allows modifications # zsh-syntax-highlighting widgets. This also allows modifications
# to the widget list variables to take effect on the next precmd. # to the widget list variables to take effect on the next precmd.
_zsh_autosuggest_bind_autosuggest_widgets add-zsh-hook precmd _zsh_autosuggest_bind_widgets
add-zsh-hook precmd _zsh_autosuggest_bind_autosuggest_widgets
# If available, use a ZLE redraw hook to trigger fetching suggestions.
# Otherwise, we need to wrap all widgets and fetch suggestions after
# running them.
if is-at-least 5.4; then
add-zle-hook-widget line-pre-redraw autosuggest-redraw
else
_zsh_autosuggest_bind_modify_widgets
add-zsh-hook precmd _zsh_autosuggest_bind_modify_widgets
fi
if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then
_zsh_autosuggest_async_start _zsh_autosuggest_async_start

View File

@@ -39,21 +39,56 @@ _zsh_autosuggest_clear() {
_zsh_autosuggest_modify() { _zsh_autosuggest_modify() {
local -i retval local -i retval
# Save the contents of the postdisplay # Only available in zsh >= 5.4
local -i KEYS_QUEUED_COUNT
# Save the contents of the buffer/postdisplay
local orig_buffer="$BUFFER"
local orig_postdisplay="$POSTDISPLAY" local orig_postdisplay="$POSTDISPLAY"
# Clear suggestion while original widget runs # Clear suggestion while waiting for next one
unset POSTDISPLAY unset POSTDISPLAY
# Original widget may modify the buffer # Original widget may modify the buffer
_zsh_autosuggest_invoke_original_widget $@ _zsh_autosuggest_invoke_original_widget $@
retval=$? retval=$?
# Restore postdisplay to be used in redraw emulate -L zsh
POSTDISPLAY="$orig_postdisplay"
# Run redraw to fetch a suggestion if needed # Don't fetch a new suggestion if there's more input to be read immediately
_zsh_autosuggest_redraw if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then
POSTDISPLAY="$orig_postdisplay"
return $retval
fi
# Optimize if manually typing in the suggestion
if (( $#BUFFER > $#orig_buffer )); then
local added=${BUFFER#$orig_buffer}
# If the string added matches the beginning of the postdisplay
if [[ "$added" = "${orig_postdisplay:0:$#added}" ]]; then
POSTDISPLAY="${orig_postdisplay:$#added}"
return $retval
fi
fi
# Don't fetch a new suggestion if the buffer hasn't changed
if [[ "$BUFFER" = "$orig_buffer" ]]; then
POSTDISPLAY="$orig_postdisplay"
return $retval
fi
# Bail out if suggestions are disabled
if [[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]]; then
return $?
fi
# Get a new suggestion if the buffer is not empty after modification
if (( $#BUFFER > 0 )); then
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
_zsh_autosuggest_fetch
fi
fi
return $retval return $retval
} }
@@ -155,67 +190,9 @@ _zsh_autosuggest_partial_accept() {
return $retval return $retval
} }
_zsh_autosuggest_redraw() {
emulate -L zsh
typeset -g _ZSH_AUTOSUGGEST_LAST_BUFFER
# Only available in zsh >= 5.4
local -i KEYS_QUEUED_COUNT
local orig_buffer="$_ZSH_AUTOSUGGEST_LAST_BUFFER"
local widget
# Store the current state of the buffer for next time
_ZSH_AUTOSUGGEST_LAST_BUFFER="$BUFFER"
# Buffer hasn't changed
[[ "$BUFFER" = "$orig_buffer" ]] && return 0
local ignore_widgets
ignore_widgets=(
$ZSH_AUTOSUGGEST_CLEAR_WIDGETS
$ZSH_AUTOSUGGEST_ACCEPT_WIDGETS
$ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
)
# Don't fetch a new suggestion after mapped widgets
for widget in $ignore_widgets; do
[[ "$LASTWIDGET" == "$widget" ]] && return 0
done
# Optimize if manually typing in the suggestion
if (( $#BUFFER > $#orig_buffer )); then
local added=${BUFFER#$orig_buffer}
# If the string added matches the beginning of the postdisplay
if [[ "$added" = "${POSTDISPLAY:0:$#added}" ]]; then
POSTDISPLAY="${POSTDISPLAY:$#added}"
return 0
fi
fi
unset POSTDISPLAY
# Don't fetch a new suggestion if there's more input to be read immediately
(( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )) && return 0
# Buffer is empty
(( ! $#BUFFER )) && return 0
# Buffer longer than max size
[[ -n "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] && (( $#BUFFER > $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )) && return 0
# Suggestions disabled
[[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]] && return 0
_zsh_autosuggest_fetch
}
() { () {
local action local action
for action in clear modify fetch suggest accept partial_accept execute enable disable toggle redraw; do for action in clear modify fetch suggest accept partial_accept execute enable disable toggle; do
eval "_zsh_autosuggest_widget_$action() { eval "_zsh_autosuggest_widget_$action() {
local -i retval local -i retval
@@ -232,7 +209,6 @@ _zsh_autosuggest_redraw() {
}" }"
done done
zle -N autosuggest-redraw _zsh_autosuggest_widget_redraw
zle -N autosuggest-fetch _zsh_autosuggest_widget_fetch zle -N autosuggest-fetch _zsh_autosuggest_widget_fetch
zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest
zle -N autosuggest-accept _zsh_autosuggest_widget_accept zle -N autosuggest-accept _zsh_autosuggest_widget_accept

View File

@@ -31,8 +31,6 @@
# Precmd hooks for initializing the library and starting pty's # Precmd hooks for initializing the library and starting pty's
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook
autoload -Uz add-zle-hook-widget
autoload -Uz is-at-least
# Asynchronous suggestions are generated in a pty # Asynchronous suggestions are generated in a pty
zmodload zsh/zpty zmodload zsh/zpty
@@ -111,7 +109,6 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
} }
# Widgets that should be ignored (globbing supported but must be escaped) # Widgets that should be ignored (globbing supported but must be escaped)
# Only relevant for zsh versions older than 5.4
(( ! ${+ZSH_AUTOSUGGEST_IGNORE_WIDGETS} )) && { (( ! ${+ZSH_AUTOSUGGEST_IGNORE_WIDGETS} )) && {
typeset -ga ZSH_AUTOSUGGEST_IGNORE_WIDGETS typeset -ga ZSH_AUTOSUGGEST_IGNORE_WIDGETS
ZSH_AUTOSUGGEST_IGNORE_WIDGETS=( ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(
@@ -168,21 +165,8 @@ _zsh_autosuggest_feature_detect_zpty_returns_fd() {
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
_zsh_autosuggest_incr_bind_count() { _zsh_autosuggest_incr_bind_count() {
if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then typeset -gi bind_count=$((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]+1))
((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]++)) _ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=$bind_count
else
_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=1
fi
typeset -gi bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]
}
_zsh_autosuggest_get_bind_count() {
if ((${+_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]})); then
typeset -gi bind_count=$_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]
else
typeset -gi bind_count=0
fi
} }
# Bind a single widget to an autosuggest widget, saving a reference to the original widget # Bind a single widget to an autosuggest widget, saving a reference to the original widget
@@ -198,30 +182,30 @@ _zsh_autosuggest_bind_widget() {
# Save a reference to the original widget # Save a reference to the original widget
case $widgets[$widget] in case $widgets[$widget] in
# Already bound # Already bound
user:_zsh_autosuggest_(bound|orig)_*);; user:_zsh_autosuggest_(bound|orig)_*)
bind_count=$((_ZSH_AUTOSUGGEST_BIND_COUNTS[$widget]))
;;
# User-defined widget # User-defined widget
user:*) user:*)
_zsh_autosuggest_incr_bind_count $widget _zsh_autosuggest_incr_bind_count $widget
zle -N $prefix${bind_count}-$widget ${widgets[$widget]#*:} zle -N $prefix$bind_count-$widget ${widgets[$widget]#*:}
;; ;;
# Built-in widget # Built-in widget
builtin) builtin)
_zsh_autosuggest_incr_bind_count $widget _zsh_autosuggest_incr_bind_count $widget
eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }" eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
zle -N $prefix${bind_count}-$widget _zsh_autosuggest_orig_$widget zle -N $prefix$bind_count-$widget _zsh_autosuggest_orig_$widget
;; ;;
# Completion widget # Completion widget
completion:*) completion:*)
_zsh_autosuggest_incr_bind_count $widget _zsh_autosuggest_incr_bind_count $widget
eval "zle -C $prefix${bind_count}-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}" eval "zle -C $prefix$bind_count-${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
;; ;;
esac esac
_zsh_autosuggest_get_bind_count $widget
# Pass the original widget's name explicitly into the autosuggest # Pass the original widget's name explicitly into the autosuggest
# function. Use this passed in widget name to call the original # function. Use this passed in widget name to call the original
# widget instead of relying on the $WIDGET variable being set # widget instead of relying on the $WIDGET variable being set
@@ -236,31 +220,11 @@ _zsh_autosuggest_bind_widget() {
zle -N -- $widget _zsh_autosuggest_bound_${bind_count}_$widget zle -N -- $widget _zsh_autosuggest_bound_${bind_count}_$widget
} }
_zsh_autosuggest_bind_autosuggest_widgets() {
local widget
for widget in $ZSH_AUTOSUGGEST_CLEAR_WIDGETS; do
_zsh_autosuggest_bind_widget $widget clear
done
for widget in $ZSH_AUTOSUGGEST_ACCEPT_WIDGETS; do
_zsh_autosuggest_bind_widget $widget accept
done
for widget in $ZSH_AUTOSUGGEST_EXECUTE_WIDGETS; do
_zsh_autosuggest_bind_widget $widget execute
done
for widget in $ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS; do
_zsh_autosuggest_bind_widget $widget partial_accept
done
}
# Map all configured widgets to the right autosuggest widgets # Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_modify_widgets() { _zsh_autosuggest_bind_widgets() {
emulate -L zsh emulate -L zsh
local widget local widget
local ignore_widgets local ignore_widgets
ignore_widgets=( ignore_widgets=(
@@ -270,15 +234,22 @@ _zsh_autosuggest_bind_modify_widgets() {
autosuggest-\* autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\* $ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS $ZSH_AUTOSUGGEST_IGNORE_WIDGETS
$ZSH_AUTOSUGGEST_CLEAR_WIDGETS
$ZSH_AUTOSUGGEST_ACCEPT_WIDGETS
$ZSH_AUTOSUGGEST_EXECUTE_WIDGETS
$ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS
) )
# Assume any widget omitted from the config arrays might modify the buffer # Find every widget we might want to bind and bind it appropriately
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
_zsh_autosuggest_bind_widget $widget modify if [[ -n ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]]; then
_zsh_autosuggest_bind_widget $widget clear
elif [[ -n ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]]; then
_zsh_autosuggest_bind_widget $widget accept
elif [[ -n ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]]; then
_zsh_autosuggest_bind_widget $widget execute
elif [[ -n ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]]; then
_zsh_autosuggest_bind_widget $widget partial_accept
else
# Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
fi
done done
} }
@@ -362,21 +333,56 @@ _zsh_autosuggest_clear() {
_zsh_autosuggest_modify() { _zsh_autosuggest_modify() {
local -i retval local -i retval
# Save the contents of the postdisplay # Only available in zsh >= 5.4
local -i KEYS_QUEUED_COUNT
# Save the contents of the buffer/postdisplay
local orig_buffer="$BUFFER"
local orig_postdisplay="$POSTDISPLAY" local orig_postdisplay="$POSTDISPLAY"
# Clear suggestion while original widget runs # Clear suggestion while waiting for next one
unset POSTDISPLAY unset POSTDISPLAY
# Original widget may modify the buffer # Original widget may modify the buffer
_zsh_autosuggest_invoke_original_widget $@ _zsh_autosuggest_invoke_original_widget $@
retval=$? retval=$?
# Restore postdisplay to be used in redraw emulate -L zsh
POSTDISPLAY="$orig_postdisplay"
# Run redraw to fetch a suggestion if needed # Don't fetch a new suggestion if there's more input to be read immediately
_zsh_autosuggest_redraw if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then
POSTDISPLAY="$orig_postdisplay"
return $retval
fi
# Optimize if manually typing in the suggestion
if (( $#BUFFER > $#orig_buffer )); then
local added=${BUFFER#$orig_buffer}
# If the string added matches the beginning of the postdisplay
if [[ "$added" = "${orig_postdisplay:0:$#added}" ]]; then
POSTDISPLAY="${orig_postdisplay:$#added}"
return $retval
fi
fi
# Don't fetch a new suggestion if the buffer hasn't changed
if [[ "$BUFFER" = "$orig_buffer" ]]; then
POSTDISPLAY="$orig_postdisplay"
return $retval
fi
# Bail out if suggestions are disabled
if [[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]]; then
return $?
fi
# Get a new suggestion if the buffer is not empty after modification
if (( $#BUFFER > 0 )); then
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
_zsh_autosuggest_fetch
fi
fi
return $retval return $retval
} }
@@ -478,67 +484,9 @@ _zsh_autosuggest_partial_accept() {
return $retval return $retval
} }
_zsh_autosuggest_redraw() {
emulate -L zsh
typeset -g _ZSH_AUTOSUGGEST_LAST_BUFFER
# Only available in zsh >= 5.4
local -i KEYS_QUEUED_COUNT
local orig_buffer="$_ZSH_AUTOSUGGEST_LAST_BUFFER"
local widget
# Store the current state of the buffer for next time
_ZSH_AUTOSUGGEST_LAST_BUFFER="$BUFFER"
# Buffer hasn't changed
[[ "$BUFFER" = "$orig_buffer" ]] && return 0
local ignore_widgets
ignore_widgets=(
$ZSH_AUTOSUGGEST_CLEAR_WIDGETS
$ZSH_AUTOSUGGEST_ACCEPT_WIDGETS
$ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
)
# Don't fetch a new suggestion after mapped widgets
for widget in $ignore_widgets; do
[[ "$LASTWIDGET" == "$widget" ]] && return 0
done
# Optimize if manually typing in the suggestion
if (( $#BUFFER > $#orig_buffer )); then
local added=${BUFFER#$orig_buffer}
# If the string added matches the beginning of the postdisplay
if [[ "$added" = "${POSTDISPLAY:0:$#added}" ]]; then
POSTDISPLAY="${POSTDISPLAY:$#added}"
return 0
fi
fi
unset POSTDISPLAY
# Don't fetch a new suggestion if there's more input to be read immediately
(( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )) && return 0
# Buffer is empty
(( ! $#BUFFER )) && return 0
# Buffer longer than max size
[[ -n "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] && (( $#BUFFER > $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )) && return 0
# Suggestions disabled
[[ -n "${_ZSH_AUTOSUGGEST_DISABLED+x}" ]] && return 0
_zsh_autosuggest_fetch
}
() { () {
local action local action
for action in clear modify fetch suggest accept partial_accept execute enable disable toggle redraw; do for action in clear modify fetch suggest accept partial_accept execute enable disable toggle; do
eval "_zsh_autosuggest_widget_$action() { eval "_zsh_autosuggest_widget_$action() {
local -i retval local -i retval
@@ -555,7 +503,6 @@ _zsh_autosuggest_redraw() {
}" }"
done done
zle -N autosuggest-redraw _zsh_autosuggest_widget_redraw
zle -N autosuggest-fetch _zsh_autosuggest_widget_fetch zle -N autosuggest-fetch _zsh_autosuggest_widget_fetch
zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest zle -N autosuggest-suggest _zsh_autosuggest_widget_suggest
zle -N autosuggest-accept _zsh_autosuggest_widget_accept zle -N autosuggest-accept _zsh_autosuggest_widget_accept
@@ -791,22 +738,13 @@ _zsh_autosuggest_async_start() {
_zsh_autosuggest_start() { _zsh_autosuggest_start() {
add-zsh-hook -d precmd _zsh_autosuggest_start add-zsh-hook -d precmd _zsh_autosuggest_start
_zsh_autosuggest_bind_widgets
# Re-bind widgets on every precmd to ensure we wrap other wrappers. # Re-bind widgets on every precmd to ensure we wrap other wrappers.
# Specifically, highlighting breaks if our widgets are wrapped by # Specifically, highlighting breaks if our widgets are wrapped by
# zsh-syntax-highlighting widgets. This also allows modifications # zsh-syntax-highlighting widgets. This also allows modifications
# to the widget list variables to take effect on the next precmd. # to the widget list variables to take effect on the next precmd.
_zsh_autosuggest_bind_autosuggest_widgets add-zsh-hook precmd _zsh_autosuggest_bind_widgets
add-zsh-hook precmd _zsh_autosuggest_bind_autosuggest_widgets
# If available, use a ZLE redraw hook to trigger fetching suggestions.
# Otherwise, we need to wrap all widgets and fetch suggestions after
# running them.
if is-at-least 5.4; then
add-zle-hook-widget line-pre-redraw autosuggest-redraw
else
_zsh_autosuggest_bind_modify_widgets
add-zsh-hook precmd _zsh_autosuggest_bind_modify_widgets
fi
if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then
_zsh_autosuggest_async_start _zsh_autosuggest_async_start