From b87a4972c8be188af347fb3e29b8c47a923c4f75 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sat, 29 Jun 2019 21:44:29 -0600 Subject: [PATCH 01/11] Allow configuring to ignore history entries matching a pattern Set ZSH_AUTOSUGGEST_HISTORY_IGNORE to a glob pattern to have the history and match_prev_cmd suggestion strategies never make suggestions matching that pattern. For example, set to "cd *" to never suggest any `cd` commands from history (see issues #340 and #425). Or set to "?(#c50,)" to never suggest anything 50 characters or longer (see issue #429). --- README.md | 6 ++++++ spec/strategies/history_spec.rb | 11 ++++++++++ spec/strategies/match_prev_cmd_spec.rb | 29 +++++++++++++++++++------- src/strategies/history.zsh | 13 +++++++++--- src/strategies/match_prev_cmd.zsh | 13 +++++++++--- zsh-autosuggestions.zsh | 26 +++++++++++++++++------ 6 files changed, 78 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index fad9bf5..2f4f973 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,12 @@ As of `v0.4.0`, suggestions can be fetched asynchronously. To enable this behavi Set `ZSH_AUTOSUGGEST_MANUAL_REBIND` (it can be set to anything) to disable automatic widget re-binding on each precmd. This can be a big boost to performance, but you'll need to handle re-binding yourself if any of the widget lists change or if you or another plugin wrap any of the autosuggest widgets. To re-bind widgets, run `_zsh_autosuggest_bind_widgets`. +### Ignoring history suggestions that match a pattern + +Set `ZSH_AUTOSUGGEST_HISTORY_IGNORE` to a glob pattern to prevent offering suggestions for history entries that match the pattern. For example, set it to `"cd *"` to never suggest any `cd` commands from history. Or set to `"?(#c50,)"` to never suggest anything 50 characters or longer. + +**Note:** This only affects the `history` and `match_prev_cmd` suggestion strategies. + ### Key Bindings diff --git a/spec/strategies/history_spec.rb b/spec/strategies/history_spec.rb index f8ae526..eee8efd 100644 --- a/spec/strategies/history_spec.rb +++ b/spec/strategies/history_spec.rb @@ -8,5 +8,16 @@ describe 'the `history` suggestion strategy' do end end + context 'when ZSH_AUTOSUGGEST_HISTORY_IGNORE is set to a pattern' do + let(:options) { ['ZSH_AUTOSUGGEST_HISTORY_IGNORE="* bar"'] } + + it 'does not make suggestions that match the pattern' do + with_history('ls foo', 'ls bar', 'echo baz') do + session.send_string('ls') + wait_for { session.content }.to eq('ls foo') + end + end + end + include_examples 'special characters' end diff --git a/spec/strategies/match_prev_cmd_spec.rb b/spec/strategies/match_prev_cmd_spec.rb index 5a143b8..c435f16 100644 --- a/spec/strategies/match_prev_cmd_spec.rb +++ b/spec/strategies/match_prev_cmd_spec.rb @@ -3,19 +3,32 @@ require 'strategies/special_characters_helper' describe 'the `match_prev_cmd` strategy' do let(:options) { ['ZSH_AUTOSUGGEST_STRATEGY=match_prev_cmd'] } + let(:history) { [ + 'echo what', + 'ls foo', + 'echo what', + 'ls bar', + 'ls baz', + 'echo what' + ] } + it 'suggests the last matching history entry after the previous command' do - with_history( - 'echo what', - 'ls foo', - 'echo what', - 'ls bar', - 'ls baz', - 'echo what' - ) do + with_history(*history) do session.send_string('ls') wait_for { session.content }.to eq('ls bar') end end + context 'when ZSH_AUTOSUGGEST_HISTORY_IGNORE is set to a pattern' do + let(:options) { ['ZSH_AUTOSUGGEST_STRATEGY=match_prev_cmd', 'ZSH_AUTOSUGGEST_HISTORY_IGNORE="* bar"'] } + + it 'does not make suggestions that match the pattern' do + with_history(*history) do + session.send_string('ls') + wait_for { session.content }.to eq('ls foo') + end + end + end + include_examples 'special characters' end diff --git a/src/strategies/history.zsh b/src/strategies/history.zsh index a2755a5..0672a13 100644 --- a/src/strategies/history.zsh +++ b/src/strategies/history.zsh @@ -10,7 +10,7 @@ _zsh_autosuggest_strategy_history() { # Reset options to defaults and enable LOCAL_OPTIONS emulate -L zsh - # Enable globbing flags so that we can use (#m) + # Enable globbing flags so that we can use (#m) and (x~y) glob operator setopt EXTENDED_GLOB # Escape backslashes and all of the glob operators so we can use @@ -19,7 +19,14 @@ _zsh_autosuggest_strategy_history() { # TODO: Use (b) flag when we can drop support for zsh older than v5.0.8 local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" - # Get the history items that match + # Get the history items that match the prefix, excluding those that match + # the ignore pattern + local pattern="$prefix*" + if [[ -n $ZSH_AUTOSUGGEST_HISTORY_IGNORE ]]; then + pattern="($pattern)~($ZSH_AUTOSUGGEST_HISTORY_IGNORE)" + fi + + # Give the first history item matching the pattern as the suggestion # - (r) subscript flag makes the pattern match on values - typeset -g suggestion="${history[(r)${prefix}*]}" + typeset -g suggestion="${history[(r)$pattern]}" } diff --git a/src/strategies/match_prev_cmd.zsh b/src/strategies/match_prev_cmd.zsh index f76d3c1..b709783 100644 --- a/src/strategies/match_prev_cmd.zsh +++ b/src/strategies/match_prev_cmd.zsh @@ -24,16 +24,23 @@ _zsh_autosuggest_strategy_match_prev_cmd() { # Reset options to defaults and enable LOCAL_OPTIONS emulate -L zsh - # Enable globbing flags so that we can use (#m) + # Enable globbing flags so that we can use (#m) and (x~y) glob operator setopt EXTENDED_GLOB # TODO: Use (b) flag when we can drop support for zsh older than v5.0.8 local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" + # Get the history items that match the prefix, excluding those that match + # the ignore pattern + local pattern="$prefix*" + if [[ -n $ZSH_AUTOSUGGEST_HISTORY_IGNORE ]]; then + pattern="($pattern)~($ZSH_AUTOSUGGEST_HISTORY_IGNORE)" + fi + # Get all history event numbers that correspond to history - # entries that match pattern $prefix* + # entries that match the pattern local history_match_keys - history_match_keys=(${(k)history[(R)$prefix*]}) + history_match_keys=(${(k)history[(R)$~pattern]}) # By default we use the first history number (most recent history entry) local histkey="${history_match_keys[1]}" diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 872b647..950cf5b 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -626,7 +626,7 @@ _zsh_autosuggest_strategy_history() { # Reset options to defaults and enable LOCAL_OPTIONS emulate -L zsh - # Enable globbing flags so that we can use (#m) + # Enable globbing flags so that we can use (#m) and (x~y) glob operator setopt EXTENDED_GLOB # Escape backslashes and all of the glob operators so we can use @@ -635,9 +635,16 @@ _zsh_autosuggest_strategy_history() { # TODO: Use (b) flag when we can drop support for zsh older than v5.0.8 local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" - # Get the history items that match + # Get the history items that match the prefix, excluding those that match + # the ignore pattern + local pattern="$prefix*" + if [[ -n $ZSH_AUTOSUGGEST_HISTORY_IGNORE ]]; then + pattern="($pattern)~($ZSH_AUTOSUGGEST_HISTORY_IGNORE)" + fi + + # Give the first history item matching the pattern as the suggestion # - (r) subscript flag makes the pattern match on values - typeset -g suggestion="${history[(r)${prefix}*]}" + typeset -g suggestion="${history[(r)$pattern]}" } #--------------------------------------------------------------------# @@ -665,16 +672,23 @@ _zsh_autosuggest_strategy_match_prev_cmd() { # Reset options to defaults and enable LOCAL_OPTIONS emulate -L zsh - # Enable globbing flags so that we can use (#m) + # Enable globbing flags so that we can use (#m) and (x~y) glob operator setopt EXTENDED_GLOB # TODO: Use (b) flag when we can drop support for zsh older than v5.0.8 local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" + # Get the history items that match the prefix, excluding those that match + # the ignore pattern + local pattern="$prefix*" + if [[ -n $ZSH_AUTOSUGGEST_HISTORY_IGNORE ]]; then + pattern="($pattern)~($ZSH_AUTOSUGGEST_HISTORY_IGNORE)" + fi + # Get all history event numbers that correspond to history - # entries that match pattern $prefix* + # entries that match the pattern local history_match_keys - history_match_keys=(${(k)history[(R)$prefix*]}) + history_match_keys=(${(k)history[(R)$~pattern]}) # By default we use the first history number (most recent history entry) local histkey="${history_match_keys[1]}" From eab0e9b574c205f6a9f43d009a076cfd5fe77c90 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Thu, 26 Sep 2019 09:58:36 -0600 Subject: [PATCH 02/11] Fix circle ci build issues We are getting errors on circle ci builds (e.g. see [1]): ``` CircleCI was unable to run the job runner because we were unable to execute commands in build container. This typically means that the build container entrypoint or command is terminating the container prematurely, or interfering with executed commands. Consider clearing entrypoint/command values and try again. ``` Folks in this thread [2] suggest removing the `command: /sbin/init` line initially added by the v1 => v2 migration script. --- 1: https://circleci.com/gh/zsh-users/zsh-autosuggestions/381 2: https://discuss.circleci.com/t/circleci-was-unable-to-run-the-job-runner/31894/18 --- .circleci/config.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 8de098f..d95fa98 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,7 +5,6 @@ jobs: shell: /bin/bash --login docker: - image: ericfreese/zsh-autosuggestions-test:latest - command: /sbin/init steps: - checkout - run: From 0f0f221180b1fec3254e74d8d657c3a929870b1b Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Wed, 25 Sep 2019 15:36:07 -0600 Subject: [PATCH 03/11] cleanup: Split on null bytes instead of pattern matching Follow-up to technique tried in commit d94475c after I learned about the `@` flag to keep empty strings in the array. --- src/strategies/completion.zsh | 2 +- zsh-autosuggestions.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/strategies/completion.zsh b/src/strategies/completion.zsh index dfbf6eb..646f106 100644 --- a/src/strategies/completion.zsh +++ b/src/strategies/completion.zsh @@ -122,7 +122,7 @@ _zsh_autosuggest_strategy_completion() { # versions of zsh (older than 5.3), we sometimes get extra bytes after # the second null byte, so trim those off the end. # See http://www.zsh.org/mla/workers/2015/msg03290.html - suggestion="${${line#*$'\0'}%$'\0'*}" + suggestion="${${(@0)line}[2]}" } always { # Destroy the pty zpty -d $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 950cf5b..66eae6f 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -608,7 +608,7 @@ _zsh_autosuggest_strategy_completion() { # versions of zsh (older than 5.3), we sometimes get extra bytes after # the second null byte, so trim those off the end. # See http://www.zsh.org/mla/workers/2015/msg03290.html - suggestion="${${line#*$'\0'}%$'\0'*}" + suggestion="${${(@0)line}[2]}" } always { # Destroy the pty zpty -d $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME From ef657cb38cc197171c39caf2a93984176092f1ba Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 15 Dec 2019 07:01:12 -0700 Subject: [PATCH 04/11] 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 05/11] 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 06/11] 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 7afb7364f1ba5cb87eb616516c9a7fa4b86539b6 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 15 Dec 2019 08:07:59 -0700 Subject: [PATCH 07/11] Allow skipping completion suggestions when buffer matches a pattern Set ZSH_AUTOSUGGEST_COMPLETION_IGNORE to a glob pattern to have the completion suggestion strategy never make suggestions when the buffer matches the pattern. This can be helpful when some completion routines you have are particularly expensive and you want to prevent them from running automatically on every keystroke. See GitHub issue #463. --- README.md | 6 ++++++ spec/strategies/completion_spec.rb | 19 ++++++++++++++++++- src/strategies/completion.zsh | 9 +++++++++ zsh-autosuggestions.zsh | 9 +++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b10b4c3..dd567b3 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,12 @@ Set `ZSH_AUTOSUGGEST_HISTORY_IGNORE` to a glob pattern to prevent offering sugge **Note:** This only affects the `history` and `match_prev_cmd` suggestion strategies. +### Skipping completion suggestions for certain cases + +Set `ZSH_AUTOSUGGEST_COMPLETION_IGNORE` to a glob pattern to prevent offering completion suggestions when the buffer matches that pattern. For example, set it to `"git *"` to disable completion suggestions for git subcommands. + +**Note:** This only affects the `completion` suggestion strategy. + ### Key Bindings diff --git a/spec/strategies/completion_spec.rb b/spec/strategies/completion_spec.rb index 2be358a..92794d6 100644 --- a/spec/strategies/completion_spec.rb +++ b/spec/strategies/completion_spec.rb @@ -5,7 +5,9 @@ describe 'the `completion` suggestion strategy' do session. run_command('autoload compinit && compinit'). run_command('_foo() { compadd bar; compadd bat }'). - run_command('compdef _foo baz') + run_command('_num() { compadd two; compadd three }'). + run_command('compdef _foo baz'). + run_command('compdef _num one') end end @@ -37,6 +39,21 @@ describe 'the `completion` suggestion strategy' do end end + context 'when ZSH_AUTOSUGGEST_COMPLETION_IGNORE is set to a pattern' do + let(:options) { ['ZSH_AUTOSUGGEST_STRATEGY=completion', 'ZSH_AUTOSUGGEST_COMPLETION_IGNORE="one *"'] } + + it 'makes suggestions when the buffer does not match the pattern' do + session.send_string('baz ') + wait_for { session.content }.to eq('baz bar') + end + + it 'does not make suggestions when the buffer matches the pattern' do + session.send_string('one t') + sleep 1 + expect(session.content).to eq('one t') + end + end + context 'when async mode is enabled' do let(:options) { ['ZSH_AUTOSUGGEST_USE_ASYNC=true', 'ZSH_AUTOSUGGEST_STRATEGY=completion'] } diff --git a/src/strategies/completion.zsh b/src/strategies/completion.zsh index dfbf6eb..b3d52cc 100644 --- a/src/strategies/completion.zsh +++ b/src/strategies/completion.zsh @@ -96,6 +96,12 @@ _zsh_autosuggest_capture_completion_async() { } _zsh_autosuggest_strategy_completion() { + # Reset options to defaults and enable LOCAL_OPTIONS + emulate -L zsh + + # Enable extended glob for completion ignore pattern + setopt EXTENDED_GLOB + typeset -g suggestion local line REPLY @@ -105,6 +111,9 @@ _zsh_autosuggest_strategy_completion() { # Exit if we don't have zpty zmodload zsh/zpty 2>/dev/null || return + # Exit if our search string matches the ignore pattern + [[ -n "$ZSH_AUTOSUGGEST_COMPLETION_IGNORE" ]] && [[ "$1" == $~ZSH_AUTOSUGGEST_COMPLETION_IGNORE ]] && return + # Zle will be inactive if we are in async mode if zle; then zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_sync diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 950cf5b..65991d6 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -582,6 +582,12 @@ _zsh_autosuggest_capture_completion_async() { } _zsh_autosuggest_strategy_completion() { + # Reset options to defaults and enable LOCAL_OPTIONS + emulate -L zsh + + # Enable extended glob for completion ignore pattern + setopt EXTENDED_GLOB + typeset -g suggestion local line REPLY @@ -591,6 +597,9 @@ _zsh_autosuggest_strategy_completion() { # Exit if we don't have zpty zmodload zsh/zpty 2>/dev/null || return + # Exit if our search string matches the ignore pattern + [[ -n "$ZSH_AUTOSUGGEST_COMPLETION_IGNORE" ]] && [[ "$1" == $~ZSH_AUTOSUGGEST_COMPLETION_IGNORE ]] && return + # Zle will be inactive if we are in async mode if zle; then zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_sync From 6ec95379fa32e4d55360738616ae78240587dcc2 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sun, 15 Dec 2019 06:56:10 -0700 Subject: [PATCH 08/11] 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 From 03f59df502557e1b259005382b6e6813fc16ed9c Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 6 Jan 2020 21:02:48 -0700 Subject: [PATCH 09/11] Update changelog for v0.6.4 release --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37bf991..fc2a1fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v0.6.4 +- Fix `vi-forward-char` triggering a bell when using it to accept a suggestion (#488) +- New configuration option to skip completion suggestions when buffer matches a pattern (#487) +- New configuration option to ignore history entries matching a pattern (#456) + ## v0.6.3 - Fixed bug moving cursor to end of buffer after accepting suggestion (#453) From a5affac6f1a4dfd0e1b86e9f6d628191aacf5360 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 6 Jan 2020 21:06:06 -0700 Subject: [PATCH 10/11] v0.6.4 --- VERSION | 2 +- zsh-autosuggestions.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index e4c57af..2fc7b36 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.6.3 +v0.6.4 diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 81a9c62..a8ef6c4 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -1,6 +1,6 @@ # Fish-like fast/unobtrusive autosuggestions for zsh. # https://github.com/zsh-users/zsh-autosuggestions -# v0.6.3 +# v0.6.4 # Copyright (c) 2013 Thiago de Arruda # Copyright (c) 2016-2019 Eric Freese # From ff298e57c0050023ea0020d1ba16f2cd0fa45df1 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 6 Jan 2020 21:11:31 -0700 Subject: [PATCH 11/11] `completion` suggestion strategy seems pretty stable --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dd567b3..f1cc715 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,8 @@ For more info, read the Character Highlighting section of the zsh manual: `man z `ZSH_AUTOSUGGEST_STRATEGY` is an array that specifies how suggestions should be generated. The strategies in the array are tried successively until a suggestion is found. There are currently three built-in strategies to choose from: - `history`: Chooses the most recent match from history. +- `completion`: Chooses a suggestion based on what tab-completion would suggest. (requires `zpty` module) - `match_prev_cmd`: Like `history`, but chooses the most recent match whose preceding history item matches the most recently executed command ([more info](src/strategies/match_prev_cmd.zsh)). Note that this strategy won't work as expected with ZSH options that don't preserve the history order such as `HIST_IGNORE_ALL_DUPS` or `HIST_EXPIRE_DUPS_FIRST`. -- `completion`: (experimental) Chooses a suggestion based on what tab-completion would suggest. (requires `zpty` module) For example, setting `ZSH_AUTOSUGGEST_STRATEGY=(history completion)` will first try to find a suggestion from your history, but, if it can't find a match, will find a suggestion from the completion engine.