From 54d7a9a84c88e12f28dd7e441b662088fa25b911 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 15 Dec 2019 06:55:14 -0700 Subject: [PATCH] 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 $@