Compare commits

..

1 Commits

Author SHA1 Message Date
Eric Freese
9fb9675306 Fix #141: Disable suggestions for widgets called from widgets 2016-05-28 08:34:16 -06:00
15 changed files with 58 additions and 161 deletions

View File

@@ -1,12 +1,5 @@
# Changelog
## v0.3.3
- Switch from $history array to fc builtin for better performance with large HISTFILEs (#164)
- Fix tilde handling when extended_glob is set (#168)
- Add config option for maximum buffer length to fetch suggestions for (#178)
- Add config option for list of widgets to ignore (#184)
- Don't fetch a new suggestion unless a modification widget actually modifies the buffer (#183)
## v0.3.2
- Test runner now supports running specific tests and choosing zsh binary
- Return code from original widget is now correctly passed through (#135)

View File

@@ -69,7 +69,7 @@ Set `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` to configure the style that the suggestion
Set `ZSH_AUTOSUGGEST_STRATEGY` to choose the strategy for generating suggestions. There are currently two to choose from:
- `default`: Chooses the most recent match.
- `match_prev_cmd`: 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`.
- `match_prev_cmd`: Chooses the most recent match whose preceding history item matches the most recently executed command ([more info](src/strategies/match_prev_cmd.zsh)).
### Widget Mapping
@@ -80,19 +80,12 @@ This plugin works by triggering custom behavior when certain [zle widgets](http:
- `ZSH_AUTOSUGGEST_ACCEPT_WIDGETS`: Widgets in this array will accept the suggestion when invoked.
- `ZSH_AUTOSUGGEST_EXECUTE_WIDGETS`: Widgets in this array will execute the suggestion when invoked.
- `ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS`: Widgets in this array will partially accept the suggestion when invoked.
- `ZSH_AUTOSUGGEST_IGNORE_WIDGETS`: Widgets in this array will not trigger any custom behavior.
Widgets that modify the buffer and are not found in any of these arrays will fetch a new suggestion after they are invoked.
Widgets not in any of these lists will update the suggestion when invoked.
**Note:** A widget shouldn't belong to more than one of the above arrays.
### Disabling suggestion for large buffers
Set `ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE` to an integer value to disable autosuggestion for large buffers. The default is unset, which means that autosuggestion will be tried for any buffer size. Recommended value is 20.
This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for too long strings.
### Key Bindings
This plugin provides three widgets that you can use with `bindkey`:

View File

@@ -1 +1 @@
v0.3.3
v0.3.2

View File

@@ -47,20 +47,10 @@ _zsh_autosuggest_bind_widget() {
# Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_widgets() {
local widget
local ignore_widgets
ignore_widgets=(
.\*
_\*
zle-line-\*
autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
)
local widget;
# Find every widget we might want to bind and bind it appropriately
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
for widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|autosuggest-*|$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX*|zle-line-*|run-help|which-command|beep|set-local-history|yank)}; do
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then

View File

@@ -47,16 +47,3 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
vi-forward-blank-word
vi-forward-blank-word-end
)
# Widgets that should be ignored (globbing supported but must be escaped)
ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(
orig-\*
beep
run-help
set-local-history
which-command
yank
)
# Max size of buffer to trigger autosuggestion. Leave undefined for no upper bound.
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=

View File

@@ -7,5 +7,12 @@
#
_zsh_autosuggest_strategy_default() {
fc -lnrm "$1*" 1 2>/dev/null | head -n 1
local prefix="$1"
# Get the keys of the history items that match
local -a histkeys
histkeys=(${(k)history[(r)$prefix*]})
# Echo the value of the first key
echo -E "${history[$histkeys[1]]}"
}

View File

@@ -16,9 +16,6 @@
# will be 'ls foo' rather than 'ls bar' because your most recently
# executed command (pwd) was previously followed by 'ls foo'.
#
# 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`.
_zsh_autosuggest_strategy_match_prev_cmd() {
local prefix="$1"

View File

@@ -17,5 +17,5 @@ _zsh_autosuggest_escape_command() {
setopt localoptions EXTENDED_GLOB
# Escape special chars in the string (requires EXTENDED_GLOB)
echo -E "${1//(#m)[\\()\[\]|*?~]/\\$MATCH}"
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
}

View File

@@ -15,34 +15,27 @@ _zsh_autosuggest_clear() {
_zsh_autosuggest_modify() {
local -i retval
# Save the contents of the buffer/postdisplay
local orig_buffer="$BUFFER"
local orig_postdisplay="$POSTDISPLAY"
# Clear suggestion while original widget runs
unset POSTDISPLAY
# Original widget may modify the buffer
# Original widget modifies the buffer
_zsh_autosuggest_invoke_original_widget $@
retval=$?
# Don't fetch a new suggestion if the buffer hasn't changed
if [ "$BUFFER" = "$orig_buffer" ]; then
POSTDISPLAY="$orig_postdisplay"
return $retval
fi
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
# Only fetch suggestions at the first level of widget recursion
if [ -z "${funcstack[(rn:2:)_zsh_autosuggest_widget_*]}" ]; then
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
fi
fi
# Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then
POSTDISPLAY="${suggestion#$BUFFER}"
# Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then
POSTDISPLAY="${suggestion#$BUFFER}"
else
unset POSTDISPLAY
fi
fi
return $retval

View File

@@ -44,12 +44,6 @@ assertTildeSuggestion() {
'cd ~/something'
}
assertTildeSuggestionWithExtendedGlob() {
setopt local_options extended_glob
assertTildeSuggestion
}
assertParenthesesSuggestion() {
set_history <<-'EOF'
echo "$(ls foo)"
@@ -93,7 +87,6 @@ testSpecialCharsForAllStrategies() {
assertBackslashSuggestion
assertDoubleBackslashSuggestion
assertTildeSuggestion
assertTildeSuggestionWithExtendedGlob
assertParenthesesSuggestion
assertSquareBracketsSuggestion
done

View File

@@ -24,7 +24,7 @@ testCursorAtEnd() {
stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_accept 'original-widget'
_zsh_autosuggest_widget_accept 'original-widget'
assertTrue \
'original widget not invoked' \
@@ -48,7 +48,7 @@ testCursorNotAtEnd() {
stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_accept 'original-widget'
_zsh_autosuggest_widget_accept 'original-widget'
assertTrue \
'original widget not invoked' \
@@ -73,7 +73,7 @@ testViCursorAtEnd() {
stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_accept 'original-widget'
_zsh_autosuggest_widget_accept 'original-widget'
assertTrue \
'original widget not invoked' \
@@ -98,7 +98,7 @@ testViCursorNotAtEnd() {
stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_accept 'original-widget'
_zsh_autosuggest_widget_accept 'original-widget'
assertTrue \
'original widget not invoked' \

View File

@@ -19,7 +19,7 @@ testClear() {
BUFFER='ec'
POSTDISPLAY='ho hello'
_zsh_autosuggest_clear 'original-widget'
_zsh_autosuggest_widget_clear 'original-widget'
assertEquals \
'BUFFER was modified' \

View File

@@ -9,7 +9,6 @@ oneTimeSetUp() {
setUp() {
BUFFER=''
POSTDISPLAY=''
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=''
}
tearDown() {
@@ -26,7 +25,7 @@ testModify() {
_zsh_autosuggest_suggestion \
'echo hello'
_zsh_autosuggest_modify 'original-widget'
_zsh_autosuggest_widget_modify 'original-widget'
assertTrue \
'original widget not invoked' \
@@ -43,35 +42,6 @@ testModify() {
"$POSTDISPLAY"
}
testModifyBufferTooLarge() {
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE='20'
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'BUFFER+="012345678901234567890"'
stub_and_echo \
_zsh_autosuggest_suggestion \
'012345678901234567890123456789'
_zsh_autosuggest_modify 'original-widget'
assertTrue \
'original widget not invoked' \
'stub_called _zsh_autosuggest_invoke_original_widget'
assertEquals \
'BUFFER was not modified' \
'012345678901234567890' \
"$BUFFER"
assertEquals \
'POSTDISPLAY does not contain suggestion' \
'' \
"$POSTDISPLAY"
}
testRetval() {
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \

View File

@@ -25,7 +25,7 @@ testCursorMovesOutOfBuffer() {
_zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
_zsh_autosuggest_partial_accept 'original-widget'
_zsh_autosuggest_widget_partial_accept 'original-widget'
assertTrue \
'original widget not invoked' \
@@ -51,7 +51,7 @@ testCursorStaysInBuffer() {
_zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
_zsh_autosuggest_partial_accept 'original-widget'
_zsh_autosuggest_widget_partial_accept 'original-widget'
assertTrue \
'original widget not invoked' \

View File

@@ -1,6 +1,6 @@
# Fish-like fast/unobtrusive autosuggestions for zsh.
# https://github.com/zsh-users/zsh-autosuggestions
# v0.3.3
# v0.3.2
# Copyright (c) 2013 Thiago de Arruda
# Copyright (c) 2016 Eric Freese
#
@@ -74,19 +74,6 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
vi-forward-blank-word-end
)
# Widgets that should be ignored (globbing supported but must be escaped)
ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(
orig-\*
beep
run-help
set-local-history
which-command
yank
)
# Max size of buffer to trigger autosuggestion. Leave undefined for no upper bound.
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=
#--------------------------------------------------------------------#
# Handle Deprecated Variables/Widgets #
#--------------------------------------------------------------------#
@@ -171,20 +158,10 @@ _zsh_autosuggest_bind_widget() {
# Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_widgets() {
local widget
local ignore_widgets
ignore_widgets=(
.\*
_\*
zle-line-\*
autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
)
local widget;
# Find every widget we might want to bind and bind it appropriately
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
for widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|autosuggest-*|$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX*|zle-line-*|run-help|which-command|beep|set-local-history|yank)}; do
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
@@ -256,34 +233,27 @@ _zsh_autosuggest_clear() {
_zsh_autosuggest_modify() {
local -i retval
# Save the contents of the buffer/postdisplay
local orig_buffer="$BUFFER"
local orig_postdisplay="$POSTDISPLAY"
# Clear suggestion while original widget runs
unset POSTDISPLAY
# Original widget may modify the buffer
# Original widget modifies the buffer
_zsh_autosuggest_invoke_original_widget $@
retval=$?
# Don't fetch a new suggestion if the buffer hasn't changed
if [ "$BUFFER" = "$orig_buffer" ]; then
POSTDISPLAY="$orig_postdisplay"
return $retval
fi
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
# Only fetch suggestions at the first level of widget recursion
if [ -z "${funcstack[(rn:2:)_zsh_autosuggest_widget_*]}" ]; then
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
fi
fi
# Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then
POSTDISPLAY="${suggestion#$BUFFER}"
# Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then
POSTDISPLAY="${suggestion#$BUFFER}"
else
unset POSTDISPLAY
fi
fi
return $retval
@@ -393,7 +363,7 @@ _zsh_autosuggest_escape_command() {
setopt localoptions EXTENDED_GLOB
# Escape special chars in the string (requires EXTENDED_GLOB)
echo -E "${1//(#m)[\\()\[\]|*?~]/\\$MATCH}"
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
}
#--------------------------------------------------------------------#
@@ -404,7 +374,14 @@ _zsh_autosuggest_escape_command() {
#
_zsh_autosuggest_strategy_default() {
fc -lnrm "$1*" 1 2>/dev/null | head -n 1
local prefix="$1"
# Get the keys of the history items that match
local -a histkeys
histkeys=(${(k)history[(r)$prefix*]})
# Echo the value of the first key
echo -E "${history[$histkeys[1]]}"
}
#--------------------------------------------------------------------#
@@ -424,9 +401,6 @@ _zsh_autosuggest_strategy_default() {
# will be 'ls foo' rather than 'ls bar' because your most recently
# executed command (pwd) was previously followed by 'ls foo'.
#
# 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`.
_zsh_autosuggest_strategy_match_prev_cmd() {
local prefix="$1"