From ef657cb38cc197171c39caf2a93984176092f1ba Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 15 Dec 2019 07:01:12 -0700 Subject: [PATCH 1/4] cleanup: Combine two arithmetic conditionals --- src/widgets.zsh | 2 +- zsh-autosuggestions.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 242d502..9d92816 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -56,7 +56,7 @@ _zsh_autosuggest_modify() { emulate -L zsh # Don't fetch a new suggestion if there's more input to be read immediately - if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then + if (( $PENDING > 0 || $KEYS_QUEUED_COUNT > 0 )); then POSTDISPLAY="$orig_postdisplay" return $retval fi diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 950cf5b..84be34a 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -318,7 +318,7 @@ _zsh_autosuggest_modify() { emulate -L zsh # Don't fetch a new suggestion if there's more input to be read immediately - if (( $PENDING > 0 )) || (( $KEYS_QUEUED_COUNT > 0 )); then + if (( $PENDING > 0 || $KEYS_QUEUED_COUNT > 0 )); then POSTDISPLAY="$orig_postdisplay" return $retval fi From 5217ed52697157f5ccc961d9b6b1c7967abc418b Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 15 Dec 2019 06:49:50 -0700 Subject: [PATCH 2/4] cleanup: Switch to arithmetic comparison --- src/widgets.zsh | 2 +- zsh-autosuggestions.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 9d92816..164c751 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -128,7 +128,7 @@ _zsh_autosuggest_accept() { fi # Only accept if the cursor is at the end of the buffer - if [[ $CURSOR = $max_cursor_pos ]]; then + if (( $CURSOR == $max_cursor_pos )); then # Add the suggestion to the buffer BUFFER="$BUFFER$POSTDISPLAY" diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 84be34a..ffd5533 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -390,7 +390,7 @@ _zsh_autosuggest_accept() { fi # Only accept if the cursor is at the end of the buffer - if [[ $CURSOR = $max_cursor_pos ]]; then + if (( $CURSOR == $max_cursor_pos )); then # Add the suggestion to the buffer BUFFER="$BUFFER$POSTDISPLAY" From 54d7a9a84c88e12f28dd7e441b662088fa25b911 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 15 Dec 2019 06:55:14 -0700 Subject: [PATCH 3/4] cleanup: Switch to guard clause in accept widget handler --- src/widgets.zsh | 25 ++++++++++++++----------- zsh-autosuggestions.zsh | 25 ++++++++++++++----------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 164c751..c6f1137 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -127,20 +127,23 @@ _zsh_autosuggest_accept() { max_cursor_pos=$((max_cursor_pos - 1)) fi + if (( $CURSOR != $max_cursor_pos )); then + _zsh_autosuggest_invoke_original_widget $@ + return + fi + # Only accept if the cursor is at the end of the buffer - if (( $CURSOR == $max_cursor_pos )); then - # Add the suggestion to the buffer - BUFFER="$BUFFER$POSTDISPLAY" + # Add the suggestion to the buffer + BUFFER="$BUFFER$POSTDISPLAY" - # Remove the suggestion - unset POSTDISPLAY + # Remove the suggestion + unset POSTDISPLAY - # Move the cursor to the end of the buffer - if [[ "$KEYMAP" = "vicmd" ]]; then - CURSOR=$(($#BUFFER - 1)) - else - CURSOR=$#BUFFER - fi + # Move the cursor to the end of the buffer + if [[ "$KEYMAP" = "vicmd" ]]; then + CURSOR=$(($#BUFFER - 1)) + else + CURSOR=$#BUFFER fi _zsh_autosuggest_invoke_original_widget $@ diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index ffd5533..899dcf3 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -389,20 +389,23 @@ _zsh_autosuggest_accept() { max_cursor_pos=$((max_cursor_pos - 1)) fi + if (( $CURSOR != $max_cursor_pos )); then + _zsh_autosuggest_invoke_original_widget $@ + return + fi + # Only accept if the cursor is at the end of the buffer - if (( $CURSOR == $max_cursor_pos )); then - # Add the suggestion to the buffer - BUFFER="$BUFFER$POSTDISPLAY" + # Add the suggestion to the buffer + BUFFER="$BUFFER$POSTDISPLAY" - # Remove the suggestion - unset POSTDISPLAY + # Remove the suggestion + unset POSTDISPLAY - # Move the cursor to the end of the buffer - if [[ "$KEYMAP" = "vicmd" ]]; then - CURSOR=$(($#BUFFER - 1)) - else - CURSOR=$#BUFFER - fi + # Move the cursor to the end of the buffer + if [[ "$KEYMAP" = "vicmd" ]]; then + CURSOR=$(($#BUFFER - 1)) + else + CURSOR=$#BUFFER fi _zsh_autosuggest_invoke_original_widget $@ From 6ec95379fa32e4d55360738616ae78240587dcc2 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 15 Dec 2019 06:56:10 -0700 Subject: [PATCH 4/4] Call original widget before moving cursor when accepting suggestion The check on length of `$POSTDISPLAY` is in support of the test for `vi-delete` on the last char of the buffer with `dl`. Fixes issue #482. --- src/widgets.zsh | 13 ++++++++++--- zsh-autosuggestions.zsh | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index c6f1137..8f09792 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -119,7 +119,7 @@ _zsh_autosuggest_suggest() { # Accept the entire suggestion _zsh_autosuggest_accept() { - local -i max_cursor_pos=$#BUFFER + local -i retval max_cursor_pos=$#BUFFER # When vicmd keymap is active, the cursor can't move all the way # to the end of the buffer @@ -127,7 +127,9 @@ _zsh_autosuggest_accept() { max_cursor_pos=$((max_cursor_pos - 1)) fi - if (( $CURSOR != $max_cursor_pos )); then + # If we're not in a valid state to accept a suggestion, just run the + # original widget and bail out + if (( $CURSOR != $max_cursor_pos || !$#POSTDISPLAY )); then _zsh_autosuggest_invoke_original_widget $@ return fi @@ -139,6 +141,11 @@ _zsh_autosuggest_accept() { # Remove the suggestion unset POSTDISPLAY + # Run the original widget before manually moving the cursor so that the + # cursor movement doesn't make the widget do something unexpected + _zsh_autosuggest_invoke_original_widget $@ + retval=$? + # Move the cursor to the end of the buffer if [[ "$KEYMAP" = "vicmd" ]]; then CURSOR=$(($#BUFFER - 1)) @@ -146,7 +153,7 @@ _zsh_autosuggest_accept() { CURSOR=$#BUFFER fi - _zsh_autosuggest_invoke_original_widget $@ + return $retval } # Accept the entire suggestion and execute it diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 899dcf3..0e1549a 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -381,7 +381,7 @@ _zsh_autosuggest_suggest() { # Accept the entire suggestion _zsh_autosuggest_accept() { - local -i max_cursor_pos=$#BUFFER + local -i retval max_cursor_pos=$#BUFFER # When vicmd keymap is active, the cursor can't move all the way # to the end of the buffer @@ -389,7 +389,9 @@ _zsh_autosuggest_accept() { max_cursor_pos=$((max_cursor_pos - 1)) fi - if (( $CURSOR != $max_cursor_pos )); then + # If we're not in a valid state to accept a suggestion, just run the + # original widget and bail out + if (( $CURSOR != $max_cursor_pos || !$#POSTDISPLAY )); then _zsh_autosuggest_invoke_original_widget $@ return fi @@ -401,6 +403,11 @@ _zsh_autosuggest_accept() { # Remove the suggestion unset POSTDISPLAY + # Run the original widget before manually moving the cursor so that the + # cursor movement doesn't make the widget do something unexpected + _zsh_autosuggest_invoke_original_widget $@ + retval=$? + # Move the cursor to the end of the buffer if [[ "$KEYMAP" = "vicmd" ]]; then CURSOR=$(($#BUFFER - 1)) @@ -408,7 +415,7 @@ _zsh_autosuggest_accept() { CURSOR=$#BUFFER fi - _zsh_autosuggest_invoke_original_widget $@ + return $retval } # Accept the entire suggestion and execute it