diff --git a/src/bind.zsh b/src/bind.zsh index 030c6cf..b1e68f1 100644 --- a/src/bind.zsh +++ b/src/bind.zsh @@ -71,7 +71,7 @@ _zsh_autosuggest_invoke_original_widget() { # Do nothing unless called with at least one arg [ $# -gt 0 ] || return - local original_widget_name=$1 + local original_widget_name="$1" shift diff --git a/src/strategies/default.zsh b/src/strategies/default.zsh index b409621..d2d1780 100644 --- a/src/strategies/default.zsh +++ b/src/strategies/default.zsh @@ -9,9 +9,10 @@ _zsh_autosuggest_strategy_default() { local prefix="$(_zsh_autosuggest_escape_command "$1")" - # Get the hist number of the most recent history item that matches - local histkey="${${(@k)history[(R)$prefix*]}[1]}" + # Get the keys of the history items that match + local -a histkeys + histkeys=(${(k)history[(r)$prefix*]}) - # Echo the history entry - echo -E "${history[$histkey]}" + # Echo the value of the first key + echo -E "${history[$histkeys[1]]}" } diff --git a/src/strategies/match_prev_cmd.zsh b/src/strategies/match_prev_cmd.zsh index 0516a1e..79d358f 100644 --- a/src/strategies/match_prev_cmd.zsh +++ b/src/strategies/match_prev_cmd.zsh @@ -30,7 +30,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() { # Get the previously executed command local prev_cmd="$(_zsh_autosuggest_prev_command)" - prev_cmd="$(_zsh_autosuggest_escape_command $prev_cmd)" + prev_cmd="$(_zsh_autosuggest_escape_command "$prev_cmd")" # Iterate up to the first 200 history event numbers that match $prefix for key in "${(@)history_match_keys[1,200]}"; do @@ -39,7 +39,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() { # See if the history entry preceding the suggestion matches the # previous command, and use it if it does - if [[ "${history[$((key - 1))]}" == $prev_cmd ]]; then + if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then histkey="$key" break fi diff --git a/src/suggestion.zsh b/src/suggestion.zsh index f5aea2c..16f2798 100644 --- a/src/suggestion.zsh +++ b/src/suggestion.zsh @@ -20,7 +20,7 @@ _zsh_autosuggest_escape_command() { echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}" } -# Get the previously executed command (hookable for testing) +# Get the previously executed command _zsh_autosuggest_prev_command() { echo -E "${history[$((HISTCMD-1))]}" } diff --git a/src/widgets.zsh b/src/widgets.zsh index 925a5e2..3c15788 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -19,12 +19,12 @@ _zsh_autosuggest_modify() { # Get a new suggestion if the buffer is not empty after modification local suggestion if [ $#BUFFER -gt 0 ]; then - suggestion=$(_zsh_autosuggest_suggestion "$BUFFER") + suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" fi # Add the suggestion to the POSTDISPLAY if [ -n "$suggestion" ]; then - POSTDISPLAY=${suggestion#$BUFFER} + POSTDISPLAY="${suggestion#$BUFFER}" else unset POSTDISPLAY fi @@ -63,7 +63,7 @@ _zsh_autosuggest_execute() { # Partially accept the suggestion _zsh_autosuggest_partial_accept() { # Save the contents of the buffer so we can restore later if needed - local original_buffer=$BUFFER + local original_buffer="$BUFFER" # Temporarily accept the suggestion. BUFFER="$BUFFER$POSTDISPLAY" @@ -74,13 +74,13 @@ _zsh_autosuggest_partial_accept() { # If we've moved past the end of the original buffer if [ $CURSOR -gt $#original_buffer ]; then # Set POSTDISPLAY to text right of the cursor - POSTDISPLAY=$RBUFFER + POSTDISPLAY="$RBUFFER" # Clip the buffer at the cursor - BUFFER=$LBUFFER + BUFFER="$LBUFFER" else # Restore the original buffer - BUFFER=$original_buffer + BUFFER="$original_buffer" fi } diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 9baacbf..1366aa2 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -181,7 +181,7 @@ _zsh_autosuggest_invoke_original_widget() { # Do nothing unless called with at least one arg [ $# -gt 0 ] || return - local original_widget_name=$1 + local original_widget_name="$1" shift @@ -232,12 +232,12 @@ _zsh_autosuggest_modify() { # Get a new suggestion if the buffer is not empty after modification local suggestion if [ $#BUFFER -gt 0 ]; then - suggestion=$(_zsh_autosuggest_suggestion "$BUFFER") + suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" fi # Add the suggestion to the POSTDISPLAY if [ -n "$suggestion" ]; then - POSTDISPLAY=${suggestion#$BUFFER} + POSTDISPLAY="${suggestion#$BUFFER}" else unset POSTDISPLAY fi @@ -276,7 +276,7 @@ _zsh_autosuggest_execute() { # Partially accept the suggestion _zsh_autosuggest_partial_accept() { # Save the contents of the buffer so we can restore later if needed - local original_buffer=$BUFFER + local original_buffer="$BUFFER" # Temporarily accept the suggestion. BUFFER="$BUFFER$POSTDISPLAY" @@ -287,13 +287,13 @@ _zsh_autosuggest_partial_accept() { # If we've moved past the end of the original buffer if [ $CURSOR -gt $#original_buffer ]; then # Set POSTDISPLAY to text right of the cursor - POSTDISPLAY=$RBUFFER + POSTDISPLAY="$RBUFFER" # Clip the buffer at the cursor - BUFFER=$LBUFFER + BUFFER="$LBUFFER" else # Restore the original buffer - BUFFER=$original_buffer + BUFFER="$original_buffer" fi } @@ -330,7 +330,7 @@ _zsh_autosuggest_escape_command() { echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}" } -# Get the previously executed command (hookable for testing) +# Get the previously executed command _zsh_autosuggest_prev_command() { echo -E "${history[$((HISTCMD-1))]}" } @@ -345,11 +345,12 @@ _zsh_autosuggest_prev_command() { _zsh_autosuggest_strategy_default() { local prefix="$(_zsh_autosuggest_escape_command "$1")" - # Get the hist number of the most recent history item that matches - local histkey="${${(@k)history[(R)$prefix*]}[1]}" + # Get the keys of the history items that match + local -a histkeys + histkeys=(${(k)history[(r)$prefix*]}) - # Echo the history entry - echo -E "${history[$histkey]}" + # Echo the value of the first key + echo -E "${history[$histkeys[1]]}" } #--------------------------------------------------------------------# @@ -383,7 +384,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() { # Get the previously executed command local prev_cmd="$(_zsh_autosuggest_prev_command)" - prev_cmd="$(_zsh_autosuggest_escape_command $prev_cmd)" + prev_cmd="$(_zsh_autosuggest_escape_command "$prev_cmd")" # Iterate up to the first 200 history event numbers that match $prefix for key in "${(@)history_match_keys[1,200]}"; do @@ -392,7 +393,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() { # See if the history entry preceding the suggestion matches the # previous command, and use it if it does - if [[ "${history[$((key - 1))]}" == $prev_cmd ]]; then + if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then histkey="$key" break fi