Browse Source

Merge pull request #169 from zsh-users/develop

v0.3.3
pull/150/merge
Eric Freese 8 years ago
committed by GitHub
parent
commit
fedc22e9bb
12 changed files with 143 additions and 34 deletions
  1. +7
    -0
      CHANGELOG.md
  2. +9
    -2
      README.md
  3. +1
    -1
      VERSION
  4. +12
    -2
      src/bind.zsh
  5. +13
    -0
      src/config.zsh
  6. +1
    -8
      src/strategies/default.zsh
  7. +3
    -0
      src/strategies/match_prev_cmd.zsh
  8. +1
    -1
      src/suggestion.zsh
  9. +14
    -4
      src/widgets.zsh
  10. +7
    -0
      test/strategies_test.zsh
  11. +30
    -0
      test/widgets/modify_test.zsh
  12. +45
    -16
      zsh-autosuggestions.zsh

+ 7
- 0
CHANGELOG.md View File

@ -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)

+ 9
- 2
README.md 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)).
- `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
@ -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`:

+ 1
- 1
VERSION View File

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

+ 12
- 2
src/bind.zsh View File

@ -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

+ 13
- 0
src/config.zsh View File

@ -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=

+ 1
- 8
src/strategies/default.zsh View File

@ -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
- 0
src/strategies/match_prev_cmd.zsh View File

@ -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"

+ 1
- 1
src/suggestion.zsh 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}"
}

+ 14
- 4
src/widgets.zsh View File

@ -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

+ 7
- 0
test/strategies_test.zsh View File

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

+ 30
- 0
test/widgets/modify_test.zsh View File

@ -9,6 +9,7 @@ oneTimeSetUp() {
setUp() {
BUFFER=''
POSTDISPLAY=''
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=''
}
tearDown() {
@ -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 \

+ 45
- 16
zsh-autosuggestions.zsh View File

@ -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
#
@ -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 #
#--------------------------------------------------------------------#
@ -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
@ -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
@ -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}"
}
#--------------------------------------------------------------------#
@ -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
}
#--------------------------------------------------------------------#
@ -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"

Loading…
Cancel
Save