From 4ccfdb2435ea4b82983bf22bb2913c0773992cce Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Wed, 29 Jan 2020 21:30:18 -0700 Subject: [PATCH] Fix issue where partial accept duplicates last word When z-sy-h is enabled after autosuggestion widgets have already been bound, partial accepting the last part of a suggestion will result in that string being duplicated. I was able to reproduce using the following versions of the two plugins: - z-sy-h: dde84e1b25f059a298ce3189cddfd0778f998df3 - z-asug: ae315ded4dba10685dbbafbfa2ff3c1aefeb490d and running the following commands interactively one after another: ``` zsh -df % source path/to/zsh-autosuggestions.zsh % source path/to/zsh-syntax-highlighting.zsh % bindkey -e % bindkey # shows 'bindkey -e-e' ``` The order of the `source` statements matters and the issue cannot be reproduced if the two source statements are executed together on one line. The problem is very similar to this one: https://github.com/zsh-users/zsh-autosuggestions/issues/126#issuecomment-217121315 See GitHub issue #483 --- src/widgets.zsh | 7 +++++-- zsh-autosuggestions.zsh | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 33c590c..74e4f90 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -173,11 +173,13 @@ _zsh_autosuggest_execute() { _zsh_autosuggest_partial_accept() { local -i retval cursor_loc - # Save the contents of the buffer so we can restore later if needed + # Save the original buffer/postdisplay so we can restore later if needed local original_buffer="$BUFFER" + local original_postdisplay="$POSTDISPLAY" # Temporarily accept the suggestion. BUFFER="$BUFFER$POSTDISPLAY" + unset POSTDISPLAY # Original widget moves the cursor _zsh_autosuggest_invoke_original_widget $@ @@ -197,8 +199,9 @@ _zsh_autosuggest_partial_accept() { # Clip the buffer at the cursor BUFFER="${BUFFER[1,$cursor_loc]}" else - # Restore the original buffer + # Restore the original buffer/postdisplay BUFFER="$original_buffer" + POSTDISPLAY="$original_postdisplay" fi return $retval diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index ee9d800..4e4ca59 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -435,11 +435,13 @@ _zsh_autosuggest_execute() { _zsh_autosuggest_partial_accept() { local -i retval cursor_loc - # Save the contents of the buffer so we can restore later if needed + # Save the original buffer/postdisplay so we can restore later if needed local original_buffer="$BUFFER" + local original_postdisplay="$POSTDISPLAY" # Temporarily accept the suggestion. BUFFER="$BUFFER$POSTDISPLAY" + unset POSTDISPLAY # Original widget moves the cursor _zsh_autosuggest_invoke_original_widget $@ @@ -459,8 +461,9 @@ _zsh_autosuggest_partial_accept() { # Clip the buffer at the cursor BUFFER="${BUFFER[1,$cursor_loc]}" else - # Restore the original buffer + # Restore the original buffer/postdisplay BUFFER="$original_buffer" + POSTDISPLAY="$original_postdisplay" fi return $retval