From 7f8ff2867cef80de7d56e37e1744c27782403e89 Mon Sep 17 00:00:00 2001 From: Stefan Siegel Date: Sun, 10 Sep 2017 04:35:19 +0200 Subject: [PATCH] Simplify escaping of pattern and fix match_prev_cmd strategy Maybe this is also a fix for #247, #248 and #258. Supersedes #267. Testcase: Using match_prev_cmd strategy and with these lines in history: echo '1^' echo '2^' echo '1^' type: echo (unexpected suggestion echo '1^' instead of echo '2^') echo '1^1 (wrong suggestion echo '1^1echo '1^') echo '1^# (error "bad math expression") --- Makefile | 1 - src/strategies/default.zsh | 14 ++------------ src/strategies/match_prev_cmd.zsh | 6 +++--- src/util.zsh | 11 ----------- zsh-autosuggestions.zsh | 31 +++++-------------------------- 5 files changed, 10 insertions(+), 53 deletions(-) delete mode 100644 src/util.zsh diff --git a/Makefile b/Makefile index d5d162c..5402b7f 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,6 @@ SRC_DIR := ./src SRC_FILES := \ $(SRC_DIR)/setup.zsh \ $(SRC_DIR)/config.zsh \ - $(SRC_DIR)/util.zsh \ $(SRC_DIR)/features.zsh \ $(SRC_DIR)/bind.zsh \ $(SRC_DIR)/highlight.zsh \ diff --git a/src/strategies/default.zsh b/src/strategies/default.zsh index 60c0494..80c4842 100644 --- a/src/strategies/default.zsh +++ b/src/strategies/default.zsh @@ -7,19 +7,9 @@ # _zsh_autosuggest_strategy_default() { - # Reset options to defaults and enable LOCAL_OPTIONS - emulate -L zsh - - # Enable globbing flags so that we can use (#m) - setopt EXTENDED_GLOB - - # Escape backslashes and all of the glob operators so we can use - # this string as a pattern to search the $history associative array. - # - (#m) globbing flag enables setting references for match data - local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" + local prefix="$1" # Get the history items that match # - (r) subscript flag makes the pattern match on values - suggestion="${history[(r)$prefix*]}" - + suggestion="${history[(r)${(b)prefix}*]}" } diff --git a/src/strategies/match_prev_cmd.zsh b/src/strategies/match_prev_cmd.zsh index ee26346..e779340 100644 --- a/src/strategies/match_prev_cmd.zsh +++ b/src/strategies/match_prev_cmd.zsh @@ -21,18 +21,18 @@ # `HIST_EXPIRE_DUPS_FIRST`. _zsh_autosuggest_strategy_match_prev_cmd() { - local prefix="${1//(#m)[\\()\[\]|*?~]/\\$MATCH}" + local prefix="$1" # Get all history event numbers that correspond to history # entries that match pattern $prefix* local history_match_keys - history_match_keys=(${(k)history[(R)$prefix*]}) + history_match_keys=(${(k)history[(R)${(b)prefix}*]}) # By default we use the first history number (most recent history entry) local histkey="${history_match_keys[1]}" # Get the previously executed command - local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" + local prev_cmd="${history[$((HISTCMD-1))]}" # Iterate up to the first 200 history event numbers that match $prefix for key in "${(@)history_match_keys[1,200]}"; do diff --git a/src/util.zsh b/src/util.zsh deleted file mode 100644 index 1f55d36..0000000 --- a/src/util.zsh +++ /dev/null @@ -1,11 +0,0 @@ - -#--------------------------------------------------------------------# -# Utility Functions # -#--------------------------------------------------------------------# - -_zsh_autosuggest_escape_command() { - setopt localoptions EXTENDED_GLOB - - # Escape special chars in the string (requires EXTENDED_GLOB) - echo -E "${1//(#m)[\"\'\\()\[\]|*?~]/\\$MATCH}" -} diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 9ea5960..3c3162b 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -101,17 +101,6 @@ ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE= # Pty name for calculating autosuggestions asynchronously ZSH_AUTOSUGGEST_ASYNC_PTY_NAME=zsh_autosuggest_pty -#--------------------------------------------------------------------# -# Utility Functions # -#--------------------------------------------------------------------# - -_zsh_autosuggest_escape_command() { - setopt localoptions EXTENDED_GLOB - - # Escape special chars in the string (requires EXTENDED_GLOB) - echo -E "${1//(#m)[\"\'\\()\[\]|*?~]/\\$MATCH}" -} - #--------------------------------------------------------------------# # Feature Detection # #--------------------------------------------------------------------# @@ -489,21 +478,11 @@ zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle # _zsh_autosuggest_strategy_default() { - # Reset options to defaults and enable LOCAL_OPTIONS - emulate -L zsh - - # Enable globbing flags so that we can use (#m) - setopt EXTENDED_GLOB - - # Escape backslashes and all of the glob operators so we can use - # this string as a pattern to search the $history associative array. - # - (#m) globbing flag enables setting references for match data - local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" + local prefix="$1" # Get the history items that match # - (r) subscript flag makes the pattern match on values - suggestion="${history[(r)$prefix*]}" - + suggestion="${history[(r)${(b)prefix}*]}" } #--------------------------------------------------------------------# @@ -528,18 +507,18 @@ _zsh_autosuggest_strategy_default() { # `HIST_EXPIRE_DUPS_FIRST`. _zsh_autosuggest_strategy_match_prev_cmd() { - local prefix="${1//(#m)[\\()\[\]|*?~]/\\$MATCH}" + local prefix="$1" # Get all history event numbers that correspond to history # entries that match pattern $prefix* local history_match_keys - history_match_keys=(${(k)history[(R)$prefix*]}) + history_match_keys=(${(k)history[(R)${(b)prefix}*]}) # By default we use the first history number (most recent history entry) local histkey="${history_match_keys[1]}" # Get the previously executed command - local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" + local prev_cmd="${history[$((HISTCMD-1))]}" # Iterate up to the first 200 history event numbers that match $prefix for key in "${(@)history_match_keys[1,200]}"; do