Skip to content

Commit

Permalink
Fix issues with LASTWIDGET caused by async suggestions
Browse files Browse the repository at this point in the history
There are a number of widgets that rely on LASTWIDGET so that they can
chain their behavior together on subsequent invocations. When a
suggestion was fetched asynchronously, the widget showing the suggestion
would overwrite LASTWIDGET and break these widgets that rely on it.
Relatively recently, a flag was added to the `zle` builtin to opt out of
setting LASTWIDGET when calling a widget. We can use that flag to avoid
setting LASTWIDGET when displaying suggestions that were fetched
asynchronously.
  • Loading branch information
ericfreese committed May 26, 2023
1 parent 700885a commit 77b9916
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/async.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ _zsh_autosuggest_async_response() {
if [[ -z "$2" || "$2" == "hup" ]]; then
# Read everything from the fd and give it as a suggestion
IFS='' read -rd '' -u $1 suggestion
zle autosuggest-suggest -- "$suggestion"
_zsh_autosuggest_async_suggest "$suggestion"

# Close the fd
exec {1}<&-
Expand All @@ -73,3 +73,14 @@ _zsh_autosuggest_async_response() {
# Always remove the handler
zle -F "$1"
}

_zsh_autosuggest_async_suggest() {
# Before 5.9, async suggestions break widgets that rely on LASTWIDGET
# such as copy-earlier-word and {up,down}-line-or-beginning-search. In
# 5.9, a flag was added to `zle` that will skip setting LASTWIDGET so
# that those widgets that depend on it will continue to work
# See https://www.zsh.org/mla/workers/2020/msg00824.html
local nolast
is-at-least 5.9 && nolast=supported
zle autosuggest-suggest ${=nolast:+-f nolast} -- "$1"
}
13 changes: 12 additions & 1 deletion zsh-autosuggestions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ _zsh_autosuggest_async_response() {
if [[ -z "$2" || "$2" == "hup" ]]; then
# Read everything from the fd and give it as a suggestion
IFS='' read -rd '' -u $1 suggestion
zle autosuggest-suggest -- "$suggestion"
_zsh_autosuggest_async_suggest "$suggestion"

# Close the fd
exec {1}<&-
Expand All @@ -829,6 +829,17 @@ _zsh_autosuggest_async_response() {
zle -F "$1"
}

_zsh_autosuggest_async_suggest() {
# Before 5.9, async suggestions break widgets that rely on LASTWIDGET
# such as copy-earlier-word and {up,down}-line-or-beginning-search. In
# 5.9, a flag was added to `zle` that will skip setting LASTWIDGET so
# that those widgets that depend on it will continue to work
# See https://www.zsh.org/mla/workers/2020/msg00824.html
local nolast
is-at-least 5.9 && nolast=supported
zle autosuggest-suggest ${=nolast:+-f nolast} -- "$1"
}

#--------------------------------------------------------------------#
# Start #
#--------------------------------------------------------------------#
Expand Down

0 comments on commit 77b9916

Please sign in to comment.