mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-25 10:01:05 +01:00
Fix various bugs found while testing
This commit is contained in:
parent
e5cdbb6c33
commit
a314a01a6a
6 changed files with 30 additions and 28 deletions
|
@ -71,7 +71,7 @@ _zsh_autosuggest_invoke_original_widget() {
|
||||||
# Do nothing unless called with at least one arg
|
# Do nothing unless called with at least one arg
|
||||||
[ $# -gt 0 ] || return
|
[ $# -gt 0 ] || return
|
||||||
|
|
||||||
local original_widget_name=$1
|
local original_widget_name="$1"
|
||||||
|
|
||||||
shift
|
shift
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,10 @@
|
||||||
_zsh_autosuggest_strategy_default() {
|
_zsh_autosuggest_strategy_default() {
|
||||||
local prefix="$(_zsh_autosuggest_escape_command "$1")"
|
local prefix="$(_zsh_autosuggest_escape_command "$1")"
|
||||||
|
|
||||||
# Get the hist number of the most recent history item that matches
|
# Get the keys of the history items that match
|
||||||
local histkey="${${(@k)history[(R)$prefix*]}[1]}"
|
local -a histkeys
|
||||||
|
histkeys=(${(k)history[(r)$prefix*]})
|
||||||
|
|
||||||
# Echo the history entry
|
# Echo the value of the first key
|
||||||
echo -E "${history[$histkey]}"
|
echo -E "${history[$histkeys[1]]}"
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
|
||||||
|
|
||||||
# Get the previously executed command
|
# Get the previously executed command
|
||||||
local prev_cmd="$(_zsh_autosuggest_prev_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
|
# Iterate up to the first 200 history event numbers that match $prefix
|
||||||
for key in "${(@)history_match_keys[1,200]}"; do
|
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
|
# See if the history entry preceding the suggestion matches the
|
||||||
# previous command, and use it if it does
|
# previous command, and use it if it does
|
||||||
if [[ "${history[$((key - 1))]}" == $prev_cmd ]]; then
|
if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then
|
||||||
histkey="$key"
|
histkey="$key"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -20,7 +20,7 @@ _zsh_autosuggest_escape_command() {
|
||||||
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get the previously executed command (hookable for testing)
|
# Get the previously executed command
|
||||||
_zsh_autosuggest_prev_command() {
|
_zsh_autosuggest_prev_command() {
|
||||||
echo -E "${history[$((HISTCMD-1))]}"
|
echo -E "${history[$((HISTCMD-1))]}"
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,12 +19,12 @@ _zsh_autosuggest_modify() {
|
||||||
# Get a new suggestion if the buffer is not empty after modification
|
# Get a new suggestion if the buffer is not empty after modification
|
||||||
local suggestion
|
local suggestion
|
||||||
if [ $#BUFFER -gt 0 ]; then
|
if [ $#BUFFER -gt 0 ]; then
|
||||||
suggestion=$(_zsh_autosuggest_suggestion "$BUFFER")
|
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add the suggestion to the POSTDISPLAY
|
# Add the suggestion to the POSTDISPLAY
|
||||||
if [ -n "$suggestion" ]; then
|
if [ -n "$suggestion" ]; then
|
||||||
POSTDISPLAY=${suggestion#$BUFFER}
|
POSTDISPLAY="${suggestion#$BUFFER}"
|
||||||
else
|
else
|
||||||
unset POSTDISPLAY
|
unset POSTDISPLAY
|
||||||
fi
|
fi
|
||||||
|
@ -63,7 +63,7 @@ _zsh_autosuggest_execute() {
|
||||||
# Partially accept the suggestion
|
# Partially accept the suggestion
|
||||||
_zsh_autosuggest_partial_accept() {
|
_zsh_autosuggest_partial_accept() {
|
||||||
# Save the contents of the buffer so we can restore later if needed
|
# 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.
|
# Temporarily accept the suggestion.
|
||||||
BUFFER="$BUFFER$POSTDISPLAY"
|
BUFFER="$BUFFER$POSTDISPLAY"
|
||||||
|
@ -74,13 +74,13 @@ _zsh_autosuggest_partial_accept() {
|
||||||
# If we've moved past the end of the original buffer
|
# If we've moved past the end of the original buffer
|
||||||
if [ $CURSOR -gt $#original_buffer ]; then
|
if [ $CURSOR -gt $#original_buffer ]; then
|
||||||
# Set POSTDISPLAY to text right of the cursor
|
# Set POSTDISPLAY to text right of the cursor
|
||||||
POSTDISPLAY=$RBUFFER
|
POSTDISPLAY="$RBUFFER"
|
||||||
|
|
||||||
# Clip the buffer at the cursor
|
# Clip the buffer at the cursor
|
||||||
BUFFER=$LBUFFER
|
BUFFER="$LBUFFER"
|
||||||
else
|
else
|
||||||
# Restore the original buffer
|
# Restore the original buffer
|
||||||
BUFFER=$original_buffer
|
BUFFER="$original_buffer"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -181,7 +181,7 @@ _zsh_autosuggest_invoke_original_widget() {
|
||||||
# Do nothing unless called with at least one arg
|
# Do nothing unless called with at least one arg
|
||||||
[ $# -gt 0 ] || return
|
[ $# -gt 0 ] || return
|
||||||
|
|
||||||
local original_widget_name=$1
|
local original_widget_name="$1"
|
||||||
|
|
||||||
shift
|
shift
|
||||||
|
|
||||||
|
@ -232,12 +232,12 @@ _zsh_autosuggest_modify() {
|
||||||
# Get a new suggestion if the buffer is not empty after modification
|
# Get a new suggestion if the buffer is not empty after modification
|
||||||
local suggestion
|
local suggestion
|
||||||
if [ $#BUFFER -gt 0 ]; then
|
if [ $#BUFFER -gt 0 ]; then
|
||||||
suggestion=$(_zsh_autosuggest_suggestion "$BUFFER")
|
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add the suggestion to the POSTDISPLAY
|
# Add the suggestion to the POSTDISPLAY
|
||||||
if [ -n "$suggestion" ]; then
|
if [ -n "$suggestion" ]; then
|
||||||
POSTDISPLAY=${suggestion#$BUFFER}
|
POSTDISPLAY="${suggestion#$BUFFER}"
|
||||||
else
|
else
|
||||||
unset POSTDISPLAY
|
unset POSTDISPLAY
|
||||||
fi
|
fi
|
||||||
|
@ -276,7 +276,7 @@ _zsh_autosuggest_execute() {
|
||||||
# Partially accept the suggestion
|
# Partially accept the suggestion
|
||||||
_zsh_autosuggest_partial_accept() {
|
_zsh_autosuggest_partial_accept() {
|
||||||
# Save the contents of the buffer so we can restore later if needed
|
# 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.
|
# Temporarily accept the suggestion.
|
||||||
BUFFER="$BUFFER$POSTDISPLAY"
|
BUFFER="$BUFFER$POSTDISPLAY"
|
||||||
|
@ -287,13 +287,13 @@ _zsh_autosuggest_partial_accept() {
|
||||||
# If we've moved past the end of the original buffer
|
# If we've moved past the end of the original buffer
|
||||||
if [ $CURSOR -gt $#original_buffer ]; then
|
if [ $CURSOR -gt $#original_buffer ]; then
|
||||||
# Set POSTDISPLAY to text right of the cursor
|
# Set POSTDISPLAY to text right of the cursor
|
||||||
POSTDISPLAY=$RBUFFER
|
POSTDISPLAY="$RBUFFER"
|
||||||
|
|
||||||
# Clip the buffer at the cursor
|
# Clip the buffer at the cursor
|
||||||
BUFFER=$LBUFFER
|
BUFFER="$LBUFFER"
|
||||||
else
|
else
|
||||||
# Restore the original buffer
|
# Restore the original buffer
|
||||||
BUFFER=$original_buffer
|
BUFFER="$original_buffer"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,7 +330,7 @@ _zsh_autosuggest_escape_command() {
|
||||||
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get the previously executed command (hookable for testing)
|
# Get the previously executed command
|
||||||
_zsh_autosuggest_prev_command() {
|
_zsh_autosuggest_prev_command() {
|
||||||
echo -E "${history[$((HISTCMD-1))]}"
|
echo -E "${history[$((HISTCMD-1))]}"
|
||||||
}
|
}
|
||||||
|
@ -345,11 +345,12 @@ _zsh_autosuggest_prev_command() {
|
||||||
_zsh_autosuggest_strategy_default() {
|
_zsh_autosuggest_strategy_default() {
|
||||||
local prefix="$(_zsh_autosuggest_escape_command "$1")"
|
local prefix="$(_zsh_autosuggest_escape_command "$1")"
|
||||||
|
|
||||||
# Get the hist number of the most recent history item that matches
|
# Get the keys of the history items that match
|
||||||
local histkey="${${(@k)history[(R)$prefix*]}[1]}"
|
local -a histkeys
|
||||||
|
histkeys=(${(k)history[(r)$prefix*]})
|
||||||
|
|
||||||
# Echo the history entry
|
# Echo the value of the first key
|
||||||
echo -E "${history[$histkey]}"
|
echo -E "${history[$histkeys[1]]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
|
@ -383,7 +384,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
|
||||||
|
|
||||||
# Get the previously executed command
|
# Get the previously executed command
|
||||||
local prev_cmd="$(_zsh_autosuggest_prev_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
|
# Iterate up to the first 200 history event numbers that match $prefix
|
||||||
for key in "${(@)history_match_keys[1,200]}"; do
|
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
|
# See if the history entry preceding the suggestion matches the
|
||||||
# previous command, and use it if it does
|
# previous command, and use it if it does
|
||||||
if [[ "${history[$((key - 1))]}" == $prev_cmd ]]; then
|
if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then
|
||||||
histkey="$key"
|
histkey="$key"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue