diff --git a/src/strategies/default.zsh b/src/strategies/default.zsh index d2d1780..42da24e 100644 --- a/src/strategies/default.zsh +++ b/src/strategies/default.zsh @@ -7,7 +7,7 @@ # _zsh_autosuggest_strategy_default() { - local prefix="$(_zsh_autosuggest_escape_command "$1")" + local prefix="$1" # Get the keys of the history items that match local -a histkeys diff --git a/src/strategies/match_prev_cmd.zsh b/src/strategies/match_prev_cmd.zsh index c6b4c0c..e71957e 100644 --- a/src/strategies/match_prev_cmd.zsh +++ b/src/strategies/match_prev_cmd.zsh @@ -18,7 +18,7 @@ # _zsh_autosuggest_strategy_match_prev_cmd() { - local prefix="$(_zsh_autosuggest_escape_command "$1")" + local prefix="$1" # Get all history event numbers that correspond to history # entries that match pattern $prefix* @@ -29,8 +29,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() { local histkey="${history_match_keys[1]}" # Get the previously executed command - local prev_cmd="$(_zsh_autosuggest_prev_command)" - prev_cmd="$(_zsh_autosuggest_escape_command "$prev_cmd")" + local prev_cmd="$(_zsh_autosuggest_escape_command "${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/suggestion.zsh b/src/suggestion.zsh index 16f2798..0a7ca1e 100644 --- a/src/suggestion.zsh +++ b/src/suggestion.zsh @@ -5,11 +5,11 @@ # Delegate to the selected strategy to determine a suggestion _zsh_autosuggest_suggestion() { - local prefix="$1" + local escaped_prefix="$(_zsh_autosuggest_escape_command "$1")" local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY" if [ -n "$functions[$strategy_function]" ]; then - echo -E "$($strategy_function "$prefix")" + echo -E "$($strategy_function "$escaped_prefix")" fi } @@ -19,8 +19,3 @@ _zsh_autosuggest_escape_command() { # Escape special chars in the string (requires EXTENDED_GLOB) echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}" } - -# Get the previously executed command -_zsh_autosuggest_prev_command() { - echo -E "${history[$((HISTCMD-1))]}" -} diff --git a/test/strategies_test.zsh b/test/strategies_test.zsh index 0e18585..50d0a24 100644 --- a/test/strategies_test.zsh +++ b/test/strategies_test.zsh @@ -64,6 +64,16 @@ assertSquareBracketsSuggestion() { 'echo "$history[123]"' } +assertHashSuggestion() { + set_history <<-'EOF' + echo "#yolo" + EOF + + assertSuggestion \ + 'echo "#' \ + 'echo "#yolo"' +} + testSpecialCharsForAllStrategies() { local strategies strategies=( diff --git a/test/suggestion_test.zsh b/test/suggestion_test.zsh index 12b732b..fc6330d 100644 --- a/test/suggestion_test.zsh +++ b/test/suggestion_test.zsh @@ -43,30 +43,4 @@ testEscapeCommand() { "$(_zsh_autosuggest_escape_command '?')" } -testPrevCommand() { - set_history <<-'EOF' - ls foo - ls bar - ls baz - EOF - - assertEquals \ - 'Did not output the last command' \ - 'ls baz' \ - "$(_zsh_autosuggest_prev_command)" - - set_history <<-'EOF' - ls foo - ls bar - ls baz - ls quux - ls foobar - EOF - - assertEquals \ - 'Did not output the last command' \ - 'ls foobar' \ - "$(_zsh_autosuggest_prev_command)" -} - run_tests "$0" diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 4437984..ec1d53f 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -345,11 +345,11 @@ zle -N autosuggest-execute _zsh_autosuggest_widget_execute # Delegate to the selected strategy to determine a suggestion _zsh_autosuggest_suggestion() { - local prefix="$1" + local escaped_prefix="$(_zsh_autosuggest_escape_command "$1")" local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY" if [ -n "$functions[$strategy_function]" ]; then - echo -E "$($strategy_function "$prefix")" + echo -E "$($strategy_function "$escaped_prefix")" fi } @@ -360,11 +360,6 @@ _zsh_autosuggest_escape_command() { echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}" } -# Get the previously executed command -_zsh_autosuggest_prev_command() { - echo -E "${history[$((HISTCMD-1))]}" -} - #--------------------------------------------------------------------# # Default Suggestion Strategy # #--------------------------------------------------------------------# @@ -373,7 +368,7 @@ _zsh_autosuggest_prev_command() { # _zsh_autosuggest_strategy_default() { - local prefix="$(_zsh_autosuggest_escape_command "$1")" + local prefix="$1" # Get the keys of the history items that match local -a histkeys @@ -402,7 +397,7 @@ _zsh_autosuggest_strategy_default() { # _zsh_autosuggest_strategy_match_prev_cmd() { - local prefix="$(_zsh_autosuggest_escape_command "$1")" + local prefix="$1" # Get all history event numbers that correspond to history # entries that match pattern $prefix* @@ -413,8 +408,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() { local histkey="${history_match_keys[1]}" # Get the previously executed command - local prev_cmd="$(_zsh_autosuggest_prev_command)" - prev_cmd="$(_zsh_autosuggest_escape_command "$prev_cmd")" + local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" # Iterate up to the first 200 history event numbers that match $prefix for key in "${(@)history_match_keys[1,200]}"; do