diff --git a/src/async.zsh b/src/async.zsh index e179734..e1f4709 100644 --- a/src/async.zsh +++ b/src/async.zsh @@ -38,8 +38,10 @@ _zsh_autosuggest_async_request() { # Fetch and print the suggestion local suggestion + local new_buffer _zsh_autosuggest_fetch_suggestion "$1" - echo -nE "$suggestion" + echo -E $suggestion + echo -E $new_buffer ) # There's a weird bug here where ^C stops working unless we force a fork @@ -61,11 +63,13 @@ _zsh_autosuggest_async_response() { emulate -L zsh local suggestion + local new_buffer 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" + read -r -u $1 suggestion + read -r -u $1 new_buffer + zle autosuggest-suggest -- "$suggestion" "$new_buffer" # Close the fd builtin exec {1}<&- diff --git a/src/fetch.zsh b/src/fetch.zsh index fef2715..410e2a3 100644 --- a/src/fetch.zsh +++ b/src/fetch.zsh @@ -19,7 +19,10 @@ _zsh_autosuggest_fetch_suggestion() { _zsh_autosuggest_strategy_$strategy "$1" # Ensure the suggestion matches the prefix - [[ "$suggestion" != "$1"* ]] && unset suggestion + if [[ "$suggestion" != "$1"* ]]; then + unset suggestion + unset new_buffer + fi # Break once we've found a valid suggestion [[ -n "$suggestion" ]] && break diff --git a/src/widgets.zsh b/src/widgets.zsh index 7562897..a922856 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -3,6 +3,8 @@ # Autosuggest Widget Implementations # #--------------------------------------------------------------------# +_ZSH_AUTOSUGGEST_NEW_BUFFER= + # Disable suggestions _zsh_autosuggest_disable() { typeset -g _ZSH_AUTOSUGGEST_DISABLED @@ -88,8 +90,9 @@ _zsh_autosuggest_fetch() { _zsh_autosuggest_async_request "$BUFFER" else local suggestion + local new_buffer _zsh_autosuggest_fetch_suggestion "$BUFFER" - _zsh_autosuggest_suggest "$suggestion" + _zsh_autosuggest_suggest "$suggestion" "$new_buffer" fi } @@ -98,11 +101,14 @@ _zsh_autosuggest_suggest() { emulate -L zsh local suggestion="$1" + local new_buffer="$2" if [[ -n "$suggestion" ]] && (( $#BUFFER )); then POSTDISPLAY="${suggestion#$BUFFER}" + _ZSH_AUTOSUGGEST_NEW_BUFFER=$new_buffer else POSTDISPLAY= + _ZSH_AUTOSUGGEST_NEW_BUFFER= fi } @@ -125,10 +131,11 @@ _zsh_autosuggest_accept() { # Only accept if the cursor is at the end of the buffer # Add the suggestion to the buffer - BUFFER="$BUFFER$POSTDISPLAY" + BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}" # Remove the suggestion POSTDISPLAY= + _ZSH_AUTOSUGGEST_NEW_BUFFER= # Run the original widget before manually moving the cursor so that the # cursor movement doesn't make the widget do something unexpected @@ -148,10 +155,11 @@ _zsh_autosuggest_accept() { # Accept the entire suggestion and execute it _zsh_autosuggest_execute() { # Add the suggestion to the buffer - BUFFER="$BUFFER$POSTDISPLAY" + BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}" # Remove the suggestion POSTDISPLAY= + _ZSH_AUTOSUGGEST_NEW_BUFFER= # Call the original `accept-line` to handle syntax highlighting or # other potential custom behavior diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index e780225..b11a1d2 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -267,6 +267,8 @@ _zsh_autosuggest_highlight_apply() { # Autosuggest Widget Implementations # #--------------------------------------------------------------------# +_ZSH_AUTOSUGGEST_NEW_BUFFER= + # Disable suggestions _zsh_autosuggest_disable() { typeset -g _ZSH_AUTOSUGGEST_DISABLED @@ -352,8 +354,9 @@ _zsh_autosuggest_fetch() { _zsh_autosuggest_async_request "$BUFFER" else local suggestion + local new_buffer _zsh_autosuggest_fetch_suggestion "$BUFFER" - _zsh_autosuggest_suggest "$suggestion" + _zsh_autosuggest_suggest "$suggestion" "$new_buffer" fi } @@ -362,11 +365,14 @@ _zsh_autosuggest_suggest() { emulate -L zsh local suggestion="$1" + local new_buffer="$2" if [[ -n "$suggestion" ]] && (( $#BUFFER )); then POSTDISPLAY="${suggestion#$BUFFER}" + _ZSH_AUTOSUGGEST_NEW_BUFFER=$new_buffer else POSTDISPLAY= + _ZSH_AUTOSUGGEST_NEW_BUFFER= fi } @@ -389,10 +395,11 @@ _zsh_autosuggest_accept() { # Only accept if the cursor is at the end of the buffer # Add the suggestion to the buffer - BUFFER="$BUFFER$POSTDISPLAY" + BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}" # Remove the suggestion POSTDISPLAY= + _ZSH_AUTOSUGGEST_NEW_BUFFER= # Run the original widget before manually moving the cursor so that the # cursor movement doesn't make the widget do something unexpected @@ -412,10 +419,11 @@ _zsh_autosuggest_accept() { # Accept the entire suggestion and execute it _zsh_autosuggest_execute() { # Add the suggestion to the buffer - BUFFER="$BUFFER$POSTDISPLAY" + BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}" # Remove the suggestion POSTDISPLAY= + _ZSH_AUTOSUGGEST_NEW_BUFFER= # Call the original `accept-line` to handle syntax highlighting or # other potential custom behavior @@ -749,7 +757,10 @@ _zsh_autosuggest_fetch_suggestion() { _zsh_autosuggest_strategy_$strategy "$1" # Ensure the suggestion matches the prefix - [[ "$suggestion" != "$1"* ]] && unset suggestion + if [[ "$suggestion" != "$1"* ]]; then + unset suggestion + unset new_buffer + fi # Break once we've found a valid suggestion [[ -n "$suggestion" ]] && break @@ -795,8 +806,10 @@ _zsh_autosuggest_async_request() { # Fetch and print the suggestion local suggestion + local new_buffer _zsh_autosuggest_fetch_suggestion "$1" - echo -nE "$suggestion" + echo -E $suggestion + echo -E $new_buffer ) # There's a weird bug here where ^C stops working unless we force a fork @@ -818,11 +831,13 @@ _zsh_autosuggest_async_response() { emulate -L zsh local suggestion + local new_buffer 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" + read -r -u $1 suggestion + read -r -u $1 new_buffer + zle autosuggest-suggest -- "$suggestion" "$new_buffer" # Close the fd builtin exec {1}<&-