Skip to content

Commit

Permalink
Merge pull request #169 from zsh-users/develop
Browse files Browse the repository at this point in the history
v0.3.3
  • Loading branch information
ericfreese committed Oct 17, 2016
2 parents 62cdba4 + 9cfaf5d commit fedc22e
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# 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)
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
- `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`.


### Widget Mapping
Expand All @@ -80,12 +80,19 @@ 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 not in any of these lists will update the suggestion when invoked.
Widgets that modify the buffer and are not found in any of these arrays will fetch a new suggestion after they are 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`:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.3.2
v0.3.3
14 changes: 12 additions & 2 deletions src/bind.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@ _zsh_autosuggest_bind_widget() {

# Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_widgets() {
local widget;
local widget
local ignore_widgets

ignore_widgets=(
.\*
_\*
zle-line-\*
autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
)

# Find every widget we might want to bind and bind it appropriately
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
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
Expand Down
13 changes: 13 additions & 0 deletions src/config.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@ 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=
9 changes: 1 addition & 8 deletions src/strategies/default.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,5 @@
#

_zsh_autosuggest_strategy_default() {
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]]}"
fc -lnrm "$1*" 1 2>/dev/null | head -n 1
}
3 changes: 3 additions & 0 deletions src/strategies/match_prev_cmd.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# 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"
Expand Down
2 changes: 1 addition & 1 deletion src/suggestion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
18 changes: 14 additions & 4 deletions src/widgets.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@ _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 modifies the buffer
# Original widget may modify 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
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
fi
fi

# Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then
POSTDISPLAY="${suggestion#$BUFFER}"
else
unset POSTDISPLAY
fi

return $retval
Expand Down
7 changes: 7 additions & 0 deletions test/strategies_test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ assertTildeSuggestion() {
'cd ~/something'
}

assertTildeSuggestionWithExtendedGlob() {
setopt local_options extended_glob

assertTildeSuggestion
}

assertParenthesesSuggestion() {
set_history <<-'EOF'
echo "$(ls foo)"
Expand Down Expand Up @@ -87,6 +93,7 @@ testSpecialCharsForAllStrategies() {
assertBackslashSuggestion
assertDoubleBackslashSuggestion
assertTildeSuggestion
assertTildeSuggestionWithExtendedGlob
assertParenthesesSuggestion
assertSquareBracketsSuggestion
done
Expand Down
30 changes: 30 additions & 0 deletions test/widgets/modify_test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ oneTimeSetUp() {
setUp() {
BUFFER=''
POSTDISPLAY=''
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=''
}

tearDown() {
Expand Down Expand Up @@ -42,6 +43,35 @@ 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 \
Expand Down
61 changes: 45 additions & 16 deletions zsh-autosuggestions.zsh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Fish-like fast/unobtrusive autosuggestions for zsh.
# https://github.com/zsh-users/zsh-autosuggestions
# v0.3.2
# v0.3.3
# Copyright (c) 2013 Thiago de Arruda
# Copyright (c) 2016 Eric Freese
#
Expand Down Expand Up @@ -74,6 +74,19 @@ 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 #
#--------------------------------------------------------------------#
Expand Down Expand Up @@ -158,10 +171,20 @@ _zsh_autosuggest_bind_widget() {

# Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_widgets() {
local widget;
local widget
local ignore_widgets

ignore_widgets=(
.\*
_\*
zle-line-\*
autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
)

# Find every widget we might want to bind and bind it appropriately
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
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
Expand Down Expand Up @@ -233,24 +256,34 @@ _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 modifies the buffer
# Original widget may modify 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
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
fi
fi

# Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then
POSTDISPLAY="${suggestion#$BUFFER}"
else
unset POSTDISPLAY
fi

return $retval
Expand Down Expand Up @@ -360,7 +393,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}"
}

#--------------------------------------------------------------------#
Expand All @@ -371,14 +404,7 @@ _zsh_autosuggest_escape_command() {
#

_zsh_autosuggest_strategy_default() {
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]]}"
fc -lnrm "$1*" 1 2>/dev/null | head -n 1
}

#--------------------------------------------------------------------#
Expand All @@ -398,6 +424,9 @@ _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"
Expand Down

0 comments on commit fedc22e

Please sign in to comment.