Compare commits

...

24 Commits

Author SHA1 Message Date
Eric Freese
ba029e83d0 bump version v0.2.11 2016-02-17 13:44:52 -07:00
Eric Freese
acc129de6c Fix error when using autosuggest widgets 2016-02-17 13:44:52 -07:00
Eric Freese
aa5ceee256 Make asciinema a bit smaller. 2016-02-16 21:55:56 -07:00
Eric Freese
113ca0ad10 Add asciinema recording to README 2016-02-16 21:35:44 -07:00
Eric Freese
2b449a62f8 bump version v0.2.10 2016-02-16 20:59:31 -07:00
Eric Freese
6d25df6864 Revert usage of fc for suggestions and fix for sh_word_split.
Force field splitting on \0 to support sh_word_split option.
2016-02-16 10:51:01 -07:00
Eric Freese
0faa2b6584 bump version v0.2.9 2016-02-16 09:34:35 -07:00
Eric Freese
a2d8d91196 Actually fix suggestions when sh_split_words option is enabled. 2016-02-16 09:33:26 -07:00
Eric Freese
dd9a8789a7 bump version v0.2.8 2016-02-16 07:59:36 -07:00
Eric Freese
1b98af5b33 Fix suggestions when sh_split_words option is enabled 2016-02-16 07:57:44 -07:00
Eric Freese
45ab49d1f2 bump version v0.2.7 2016-02-15 08:46:23 -07:00
Eric Freese
41f15d5c9f Forgot a pesky backslash 2016-02-15 08:45:52 -07:00
Eric Freese
3ce1adb55d bump version v0.2.6 2016-02-15 08:31:50 -07:00
Eric Freese
2461a98857 Fix segfaults once and for all? 2016-02-15 08:31:00 -07:00
Eric Freese
76f415bf43 bump version v0.2.5 2016-02-14 08:55:20 -07:00
Eric Freese
5e419da326 Remove list of modify widgets and make 'modify' the default behavior. 2016-02-14 08:54:34 -07:00
Eric Freese
cd71081303 bump version v0.2.4 2016-02-14 01:08:21 -07:00
Eric Freese
9788c2ee49 Fix deprecation warnings. 2016-02-14 01:07:05 -07:00
Eric Freese
ebcfc46b72 Comment formatting 2016-02-14 00:29:43 -07:00
Eric Freese
b49d002888 [README] Add example of minimal .zshrc. 2016-02-13 23:53:32 -07:00
Eric Freese
266437c98a bump version v0.2.3 2016-02-13 23:47:24 -07:00
Eric Freese
51b39e210e Bound widgets now handle arguments correctly 2016-02-13 23:46:34 -07:00
Eric Freese
011f5420fc Version bump v0.2.2 2016-02-13 21:15:33 -07:00
Eric Freese
1a38fbf6a5 [README] Add note about workaround for bpm bug. 2016-02-13 21:14:59 -07:00
11 changed files with 151 additions and 228 deletions

View File

@@ -4,6 +4,7 @@ _[Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh._
It suggests commands as you type, based on command history. It suggests commands as you type, based on command history.
<a href="https://asciinema.org/a/36578" target="_blank"><img src="https://asciinema.org/a/36578.png" width="520"/></a>
## Installation ## Installation
@@ -40,8 +41,6 @@ It suggests commands as you type, based on command history.
3. Start a new terminal session. 3. Start a new terminal session.
> **Note:** There is an open issue ([#102](https://github.com/tarruda/zsh-autosuggestions/issues/102)) when using this plugin with `bracketed-paste-magic`, which is enabled by default by Oh My Zsh.
## Usage ## Usage
@@ -69,10 +68,11 @@ Set `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` to configure the style that the suggestion
This plugin works by triggering custom behavior when certain [zle widgets](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets) are invoked. You can add and remove widgets from these arrays to change the behavior of this plugin: This plugin works by triggering custom behavior when certain [zle widgets](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets) are invoked. You can add and remove widgets from these arrays to change the behavior of this plugin:
- `ZSH_AUTOSUGGEST_CLEAR_WIDGETS`: Widgets in this array will clear the suggestion when invoked. - `ZSH_AUTOSUGGEST_CLEAR_WIDGETS`: Widgets in this array will clear the suggestion when invoked.
- `ZSH_AUTOSUGGEST_MODIFY_WIDGETS`: Widgets in this array will modify the buffer and fetch a new suggestion when invoked.
- `ZSH_AUTOSUGGEST_ACCEPT_WIDGETS`: Widgets in this array will accept the suggestion when invoked. - `ZSH_AUTOSUGGEST_ACCEPT_WIDGETS`: Widgets in this array will accept the suggestion when invoked.
- `ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS`: Widgets in this array will partially accept the suggestion when invoked. - `ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS`: Widgets in this array will partially accept the suggestion when 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. **Note:** A widget shouldn't belong to more than one of the above arrays.
@@ -94,32 +94,13 @@ bindkey '^ ' autosuggest-accept
### [`zsh-history-substring-search`](https://github.com/zsh-users/zsh-history-substring-search) ### [`zsh-history-substring-search`](https://github.com/zsh-users/zsh-history-substring-search)
When the buffer is empty and one of the `history-substring-search-up/down` widgets is invoked, it will call the `up/down-line-or-history` widget. If the `up/down-line-or-history` widgets are in `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` (the list of widgets that clear the suggestion), this can create an infinite recursion, crashing the shell session. The `history-substring-search-up` and `history-substring-search-down` widgets are not bound by default. You'll probably want to add them to `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` so that the suggestion will be cleared when you start searching through history:
For best results, you'll want to remove `up-line-or-history` and `down-line-or-history` from `ZSH_AUTOSUGGEST_CLEAR_WIDGETS`:
```
# Remove *-line-or-history widgets from list of widgets that clear the autosuggestion to avoid conflict with history-substring-search-* widgets
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=("${(@)ZSH_AUTOSUGGEST_CLEAR_WIDGETS:#(up|down)-line-or-history}")
```
Additionally, the `history-substring-search-up` and `history-substring-search-down` widgets are not bound by default. You'll probably want to add them to `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` so that the suggestion will be cleared when you start searching through history:
```sh ```sh
# Add history-substring-search-* widgets to list of widgets that clear the autosuggestion # Add history-substring-search-* widgets to list of widgets that clear the autosuggestion
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down) ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down)
``` ```
For example:
```sh
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/Code/zsh-history-substring-search/zsh-history-substring-search.zsh
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=("${(@)ZSH_AUTOSUGGEST_CLEAR_WIDGETS:#(up|down)-line-or-history}")
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down)
```
## Troubleshooting ## Troubleshooting
@@ -132,7 +113,7 @@ Before reporting an issue, please try temporarily disabling sections of your con
When reporting an issue, please include: When reporting an issue, please include:
- The smallest, simplest `.zshrc` configuration that will reproduce the problem - The smallest, simplest `.zshrc` configuration that will reproduce the problem. See [this comment](https://github.com/tarruda/zsh-autosuggestions/issues/102#issuecomment-180944764) for a good example of what this means.
- The version of zsh you're using (`zsh --version`) - The version of zsh you're using (`zsh --version`)
- Which operating system you're running - Which operating system you're running

View File

@@ -1 +1 @@
v0.2.0 v0.2.11

View File

@@ -85,7 +85,7 @@ testWidgetFunctionClear() {
BUFFER="ec" BUFFER="ec"
POSTDISPLAY="ho hello" POSTDISPLAY="ho hello"
_zsh_autosuggest_clear _zsh_autosuggest_clear "original-widget"
assertEquals \ assertEquals \
"BUFFER was modified" \ "BUFFER was modified" \
@@ -109,7 +109,7 @@ testWidgetFunctionModify() {
_zsh_autosuggest_suggestion \ _zsh_autosuggest_suggestion \
"echo hello" "echo hello"
_zsh_autosuggest_modify _zsh_autosuggest_modify "original-widget"
assertTrue \ assertTrue \
"original widget not invoked" \ "original widget not invoked" \
@@ -136,7 +136,7 @@ testWidgetFunctionAcceptCursorAtEnd() {
stub _zsh_autosuggest_invoke_original_widget stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_accept _zsh_autosuggest_accept "original-widget"
assertTrue \ assertTrue \
"original widget not invoked" \ "original widget not invoked" \
@@ -160,7 +160,7 @@ testWidgetFunctionAcceptCursorNotAtEnd() {
stub _zsh_autosuggest_invoke_original_widget stub _zsh_autosuggest_invoke_original_widget
_zsh_autosuggest_accept _zsh_autosuggest_accept "original-widget"
assertTrue \ assertTrue \
"original widget not invoked" \ "original widget not invoked" \
@@ -186,7 +186,7 @@ testWidgetFunctionPartialAcceptCursorMovesOutOfBuffer() {
_zsh_autosuggest_invoke_original_widget \ _zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"' 'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
_zsh_autosuggest_partial_accept _zsh_autosuggest_partial_accept "original-widget"
assertTrue \ assertTrue \
"original widget not invoked" \ "original widget not invoked" \
@@ -212,7 +212,7 @@ testWidgetFunctionPartialAcceptCursorStaysInBuffer() {
_zsh_autosuggest_invoke_original_widget \ _zsh_autosuggest_invoke_original_widget \
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"' 'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
_zsh_autosuggest_partial_accept _zsh_autosuggest_partial_accept "original-widget"
assertTrue \ assertTrue \
"original widget not invoked" \ "original widget not invoked" \
@@ -236,7 +236,7 @@ testWidgetAccept() {
# Call the function pointed to by the widget since we can't call # Call the function pointed to by the widget since we can't call
# the widget itself when zle is not active # the widget itself when zle is not active
${widgets[autosuggest-accept]#*:} ${widgets[autosuggest-accept]#*:} "original-widget"
assertTrue \ assertTrue \
"autosuggest-accept widget does not exist" \ "autosuggest-accept widget does not exist" \
@@ -262,7 +262,7 @@ testWidgetClear() {
# Call the function pointed to by the widget since we can't call # Call the function pointed to by the widget since we can't call
# the widget itself when zle is not active # the widget itself when zle is not active
${widgets[autosuggest-clear]#*:} ${widgets[autosuggest-clear]#*:} "original-widget"
assertTrue \ assertTrue \
"autosuggest-clear widget does not exist" \ "autosuggest-clear widget does not exist" \

View File

@@ -1,15 +1,15 @@
#----------------# #--------------------------------------------------------------------#
# Widget Helpers # # Widget Helpers #
#----------------# #--------------------------------------------------------------------#
# Bind a single widget to an autosuggest widget, saving a reference to the original widget # Bind a single widget to an autosuggest widget, saving a reference to the original widget
_zsh_autosuggest_bind_widget() { _zsh_autosuggest_bind_widget() {
local widget=$1 local widget=$1
local autosuggest_function=$2 local autosuggest_action=$2
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
local action
# Save a reference to the original widget
case $widgets[$widget] in case $widgets[$widget] in
# Already bound # Already bound
user:_zsh_autosuggest_(bound|orig)_*);; user:_zsh_autosuggest_(bound|orig)_*);;
@@ -31,19 +31,14 @@ _zsh_autosuggest_bind_widget() {
;; ;;
esac esac
# Set up widget to call $autosuggest_function if it exists # Pass the original widget's name explicitly into the autosuggest
# Otherwise just call the original widget # function. Use this passed in widget name to call the original
if [ -n "$autosuggest_function" ]; then; # widget instead of relying on the $WIDGET variable being set
action=$autosuggest_function; # correctly. $WIDGET cannot be trusted because other plugins call
else; # zle without the `-w` flag (e.g. `zle self-insert` instead of
action="zle $prefix$widget \$@" # `zle self-insert -w`).
fi
# Create new function for the widget that highlights and calls the action
eval "_zsh_autosuggest_bound_$widget() { eval "_zsh_autosuggest_bound_$widget() {
_zsh_autosuggest_highlight_reset _zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@
$action
_zsh_autosuggest_highlight_apply
}" }"
# Create the bound widget # Create the bound widget
@@ -55,26 +50,30 @@ _zsh_autosuggest_bind_widgets() {
local widget; local widget;
# Find every widget we might want to bind and bind it appropriately # 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*|run-help|which-command|beep|set-local-history|yank)}; 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_MODIFY_WIDGETS[(r)$widget]} ]; then if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_modify _zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_accept _zsh_autosuggest_bind_widget $widget accept
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_partial_accept _zsh_autosuggest_bind_widget $widget partial_accept
else else
_zsh_autosuggest_bind_widget $widget # Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
fi fi
done done
} }
# Given the name of a widget, invoke the original we saved, if it exists # Given the name of an original widget and args, invoke it, if it exists
_zsh_autosuggest_invoke_original_widget() { _zsh_autosuggest_invoke_original_widget() {
local original_widget_name="$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX$1" # Do nothing unless called with at least one arg
[ $# -gt 0 ] || return
local original_widget_name=$1
shift
if [ $widgets[$original_widget_name] ]; then if [ $widgets[$original_widget_name] ]; then
zle $original_widget_name zle $original_widget_name -- $@
fi fi
} }

View File

@@ -1,7 +1,7 @@
#--------------------------------# #--------------------------------------------------------------------#
# Global Configuration Variables # # Global Configuration Variables #
#--------------------------------# #--------------------------------------------------------------------#
# Color to use when highlighting suggestion # Color to use when highlighting suggestion
# Uses format of `region_highlight` # Uses format of `region_highlight`
@@ -22,27 +22,6 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
accept-line accept-line
) )
# Widgets that modify the suggestion
ZSH_AUTOSUGGEST_MODIFY_WIDGETS=(
list-choices
complete-word
menu-complete
menu-expand-or-complete
reverse-menu-complete
expand-or-complete
expand-or-complete-prefix
self-insert
magic-space
bracketed-paste
expand-cmd-path
accept-and-menu-complete
backward-delete-char
vi-backward-delete-char
delete-char
vi-delete-char
delete-char-or-list
)
# Widgets that accept the entire suggestion # Widgets that accept the entire suggestion
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=( ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
forward-char forward-char

View File

@@ -1,9 +1,11 @@
#-------------------------------------# #--------------------------------------------------------------------#
# Handle Deprecated Variables/Widgets # # Handle Deprecated Variables/Widgets #
#-------------------------------------# #--------------------------------------------------------------------#
unset _ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN _zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_check_deprecated_config() { _zsh_autosuggest_check_deprecated_config() {
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then
@@ -23,17 +25,12 @@ _zsh_autosuggest_check_deprecated_config() {
fi fi
} }
_zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_deprecated_start_widget() { _zsh_autosuggest_deprecated_start_widget() {
if [ -z "$_ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN" ]; then _zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. For more info, see the README at https://github.com/tarruda/zsh-autosuggestions."
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. Use the autosuggest_start function instead. For more info, see README at https://github.com/tarruda/zsh-autosuggestions." zle -D autosuggest-start
_ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN=true eval "zle-line-init() {
fi $(echo $functions[${widgets[zle-line-init]#*:}] | sed -e 's/zle autosuggest-start//g')
}"
autosuggest_start
} }
zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget

View File

@@ -1,7 +1,7 @@
#--------------# #--------------------------------------------------------------------#
# Highlighting # # Highlighting #
#--------------# #--------------------------------------------------------------------#
# If there was a highlight, remove it # If there was a highlight, remove it
_zsh_autosuggest_highlight_reset() { _zsh_autosuggest_highlight_reset() {

View File

@@ -1,7 +1,7 @@
#-------# #--------------------------------------------------------------------#
# Start # # Start #
#-------# #--------------------------------------------------------------------#
# Start the autosuggestion widgets # Start the autosuggestion widgets
_zsh_autosuggest_start() { _zsh_autosuggest_start() {

View File

@@ -1,19 +1,19 @@
#------------# #--------------------------------------------------------------------#
# Suggestion # # Suggestion #
#------------# #--------------------------------------------------------------------#
# Get a suggestion from history that matches a given prefix # Get a suggestion from history that matches a given prefix
_zsh_autosuggest_suggestion() { _zsh_autosuggest_suggestion() {
setopt localoptions extendedglob setopt localoptions extendedglob
# Escape the prefix (requires EXTENDED_GLOB) # Escape the prefix (requires EXTENDED_GLOB)
local prefix=${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH} local prefix="${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH}"
# Get all history items (reversed) that match pattern $prefix* # Get all history items (reversed) that match pattern $prefix*
local history_matches local history_matches
history_matches=(${history[(R)$prefix*]}) history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
# Echo the first item that matches # Echo the first item that matches
echo ${history_matches[1]} echo "$history_matches[1]"
} }

View File

@@ -1,25 +1,25 @@
#------------------------------------# #--------------------------------------------------------------------#
# Autosuggest Widget Implementations # # Autosuggest Widget Implementations #
#------------------------------------# #--------------------------------------------------------------------#
# Clear the suggestion # Clear the suggestion
_zsh_autosuggest_clear() { _zsh_autosuggest_clear() {
# Remove the suggestion # Remove the suggestion
unset POSTDISPLAY unset POSTDISPLAY
_zsh_autosuggest_invoke_original_widget $WIDGET _zsh_autosuggest_invoke_original_widget $@
} }
# Modify the buffer and get a new suggestion # Modify the buffer and get a new suggestion
_zsh_autosuggest_modify() { _zsh_autosuggest_modify() {
# Original widget modifies the buffer # Original widget modifies the buffer
_zsh_autosuggest_invoke_original_widget $WIDGET _zsh_autosuggest_invoke_original_widget $@
# 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
@@ -44,7 +44,7 @@ _zsh_autosuggest_accept() {
CURSOR=${#BUFFER} CURSOR=${#BUFFER}
fi fi
_zsh_autosuggest_invoke_original_widget $WIDGET _zsh_autosuggest_invoke_original_widget $@
} }
# Partially accept the suggestion # Partially accept the suggestion
@@ -56,7 +56,7 @@ _zsh_autosuggest_partial_accept() {
BUFFER="$BUFFER$POSTDISPLAY" BUFFER="$BUFFER$POSTDISPLAY"
# Original widget moves the cursor # Original widget moves the cursor
_zsh_autosuggest_invoke_original_widget $WIDGET _zsh_autosuggest_invoke_original_widget $@
# 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
@@ -71,17 +71,13 @@ _zsh_autosuggest_partial_accept() {
fi fi
} }
_zsh_autosuggest_widget_accept() { for action in clear modify accept partial_accept; do
_zsh_autosuggest_highlight_reset eval "_zsh_autosuggest_widget_$action() {
_zsh_autosuggest_accept _zsh_autosuggest_highlight_reset
_zsh_autosuggest_highlight_apply _zsh_autosuggest_$action \$@
} _zsh_autosuggest_highlight_apply
}"
_zsh_autosuggest_widget_clear() { done
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_clear
_zsh_autosuggest_highlight_apply
}
zle -N autosuggest-accept _zsh_autosuggest_widget_accept zle -N autosuggest-accept _zsh_autosuggest_widget_accept
zle -N autosuggest-clear _zsh_autosuggest_widget_clear zle -N autosuggest-clear _zsh_autosuggest_widget_clear

View File

@@ -1,6 +1,6 @@
# Fish-like fast/unobtrusive autosuggestions for zsh. # Fish-like fast/unobtrusive autosuggestions for zsh.
# https://github.com/tarruda/zsh-autosuggestions # https://github.com/tarruda/zsh-autosuggestions
# v0.2.0 # v0.2.11
# Copyright (c) 2013 Thiago de Arruda # Copyright (c) 2013 Thiago de Arruda
# Copyright (c) 2016 Eric Freese # Copyright (c) 2016 Eric Freese
# #
@@ -25,9 +25,9 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE. # OTHER DEALINGS IN THE SOFTWARE.
#--------------------------------# #--------------------------------------------------------------------#
# Global Configuration Variables # # Global Configuration Variables #
#--------------------------------# #--------------------------------------------------------------------#
# Color to use when highlighting suggestion # Color to use when highlighting suggestion
# Uses format of `region_highlight` # Uses format of `region_highlight`
@@ -48,27 +48,6 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
accept-line accept-line
) )
# Widgets that modify the suggestion
ZSH_AUTOSUGGEST_MODIFY_WIDGETS=(
list-choices
complete-word
menu-complete
menu-expand-or-complete
reverse-menu-complete
expand-or-complete
expand-or-complete-prefix
self-insert
magic-space
bracketed-paste
expand-cmd-path
accept-and-menu-complete
backward-delete-char
vi-backward-delete-char
delete-char
vi-delete-char
delete-char-or-list
)
# Widgets that accept the entire suggestion # Widgets that accept the entire suggestion
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=( ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
forward-char forward-char
@@ -86,11 +65,13 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
vi-forward-blank-word-end vi-forward-blank-word-end
) )
#-------------------------------------# #--------------------------------------------------------------------#
# Handle Deprecated Variables/Widgets # # Handle Deprecated Variables/Widgets #
#-------------------------------------# #--------------------------------------------------------------------#
unset _ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN _zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_check_deprecated_config() { _zsh_autosuggest_check_deprecated_config() {
if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then if [ -n "$AUTOSUGGESTION_HIGHLIGHT_COLOR" ]; then
@@ -110,32 +91,27 @@ _zsh_autosuggest_check_deprecated_config() {
fi fi
} }
_zsh_autosuggest_deprecated_warning() {
>&2 echo "zsh-autosuggestions: $@"
}
_zsh_autosuggest_deprecated_start_widget() { _zsh_autosuggest_deprecated_start_widget() {
if [ -z "$_ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN" ]; then _zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. For more info, see the README at https://github.com/tarruda/zsh-autosuggestions."
_zsh_autosuggest_deprecated_warning "The autosuggest-start widget is deprecated. Use the autosuggest_start function instead. For more info, see README at https://github.com/tarruda/zsh-autosuggestions." zle -D autosuggest-start
_ZSH_AUTOSUGGEST_DEPRECATED_START_WIDGET_WARNING_SHOWN=true eval "zle-line-init() {
fi $(echo $functions[${widgets[zle-line-init]#*:}] | sed -e 's/zle autosuggest-start//g')
}"
autosuggest_start
} }
zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget
#----------------# #--------------------------------------------------------------------#
# Widget Helpers # # Widget Helpers #
#----------------# #--------------------------------------------------------------------#
# Bind a single widget to an autosuggest widget, saving a reference to the original widget # Bind a single widget to an autosuggest widget, saving a reference to the original widget
_zsh_autosuggest_bind_widget() { _zsh_autosuggest_bind_widget() {
local widget=$1 local widget=$1
local autosuggest_function=$2 local autosuggest_action=$2
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
local action
# Save a reference to the original widget
case $widgets[$widget] in case $widgets[$widget] in
# Already bound # Already bound
user:_zsh_autosuggest_(bound|orig)_*);; user:_zsh_autosuggest_(bound|orig)_*);;
@@ -157,19 +133,14 @@ _zsh_autosuggest_bind_widget() {
;; ;;
esac esac
# Set up widget to call $autosuggest_function if it exists # Pass the original widget's name explicitly into the autosuggest
# Otherwise just call the original widget # function. Use this passed in widget name to call the original
if [ -n "$autosuggest_function" ]; then; # widget instead of relying on the $WIDGET variable being set
action=$autosuggest_function; # correctly. $WIDGET cannot be trusted because other plugins call
else; # zle without the `-w` flag (e.g. `zle self-insert` instead of
action="zle $prefix$widget \$@" # `zle self-insert -w`).
fi
# Create new function for the widget that highlights and calls the action
eval "_zsh_autosuggest_bound_$widget() { eval "_zsh_autosuggest_bound_$widget() {
_zsh_autosuggest_highlight_reset _zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@
$action
_zsh_autosuggest_highlight_apply
}" }"
# Create the bound widget # Create the bound widget
@@ -181,33 +152,37 @@ _zsh_autosuggest_bind_widgets() {
local widget; local widget;
# Find every widget we might want to bind and bind it appropriately # 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*|run-help|which-command|beep|set-local-history|yank)}; 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_MODIFY_WIDGETS[(r)$widget]} ]; then if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_modify _zsh_autosuggest_bind_widget $widget clear
elif [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_clear
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_accept _zsh_autosuggest_bind_widget $widget accept
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_partial_accept _zsh_autosuggest_bind_widget $widget partial_accept
else else
_zsh_autosuggest_bind_widget $widget # Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
fi fi
done done
} }
# Given the name of a widget, invoke the original we saved, if it exists # Given the name of an original widget and args, invoke it, if it exists
_zsh_autosuggest_invoke_original_widget() { _zsh_autosuggest_invoke_original_widget() {
local original_widget_name="$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX$1" # Do nothing unless called with at least one arg
[ $# -gt 0 ] || return
local original_widget_name=$1
shift
if [ $widgets[$original_widget_name] ]; then if [ $widgets[$original_widget_name] ]; then
zle $original_widget_name zle $original_widget_name -- $@
fi fi
} }
#--------------# #--------------------------------------------------------------------#
# Highlighting # # Highlighting #
#--------------# #--------------------------------------------------------------------#
# If there was a highlight, remove it # If there was a highlight, remove it
_zsh_autosuggest_highlight_reset() { _zsh_autosuggest_highlight_reset() {
@@ -227,27 +202,27 @@ _zsh_autosuggest_highlight_apply() {
fi fi
} }
#------------------------------------# #--------------------------------------------------------------------#
# Autosuggest Widget Implementations # # Autosuggest Widget Implementations #
#------------------------------------# #--------------------------------------------------------------------#
# Clear the suggestion # Clear the suggestion
_zsh_autosuggest_clear() { _zsh_autosuggest_clear() {
# Remove the suggestion # Remove the suggestion
unset POSTDISPLAY unset POSTDISPLAY
_zsh_autosuggest_invoke_original_widget $WIDGET _zsh_autosuggest_invoke_original_widget $@
} }
# Modify the buffer and get a new suggestion # Modify the buffer and get a new suggestion
_zsh_autosuggest_modify() { _zsh_autosuggest_modify() {
# Original widget modifies the buffer # Original widget modifies the buffer
_zsh_autosuggest_invoke_original_widget $WIDGET _zsh_autosuggest_invoke_original_widget $@
# 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
@@ -272,7 +247,7 @@ _zsh_autosuggest_accept() {
CURSOR=${#BUFFER} CURSOR=${#BUFFER}
fi fi
_zsh_autosuggest_invoke_original_widget $WIDGET _zsh_autosuggest_invoke_original_widget $@
} }
# Partially accept the suggestion # Partially accept the suggestion
@@ -284,7 +259,7 @@ _zsh_autosuggest_partial_accept() {
BUFFER="$BUFFER$POSTDISPLAY" BUFFER="$BUFFER$POSTDISPLAY"
# Original widget moves the cursor # Original widget moves the cursor
_zsh_autosuggest_invoke_original_widget $WIDGET _zsh_autosuggest_invoke_original_widget $@
# 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
@@ -299,43 +274,39 @@ _zsh_autosuggest_partial_accept() {
fi fi
} }
_zsh_autosuggest_widget_accept() { for action in clear modify accept partial_accept; do
_zsh_autosuggest_highlight_reset eval "_zsh_autosuggest_widget_$action() {
_zsh_autosuggest_accept _zsh_autosuggest_highlight_reset
_zsh_autosuggest_highlight_apply _zsh_autosuggest_$action \$@
} _zsh_autosuggest_highlight_apply
}"
_zsh_autosuggest_widget_clear() { done
_zsh_autosuggest_highlight_reset
_zsh_autosuggest_clear
_zsh_autosuggest_highlight_apply
}
zle -N autosuggest-accept _zsh_autosuggest_widget_accept zle -N autosuggest-accept _zsh_autosuggest_widget_accept
zle -N autosuggest-clear _zsh_autosuggest_widget_clear zle -N autosuggest-clear _zsh_autosuggest_widget_clear
#------------# #--------------------------------------------------------------------#
# Suggestion # # Suggestion #
#------------# #--------------------------------------------------------------------#
# Get a suggestion from history that matches a given prefix # Get a suggestion from history that matches a given prefix
_zsh_autosuggest_suggestion() { _zsh_autosuggest_suggestion() {
setopt localoptions extendedglob setopt localoptions extendedglob
# Escape the prefix (requires EXTENDED_GLOB) # Escape the prefix (requires EXTENDED_GLOB)
local prefix=${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH} local prefix="${1//(#m)[\][()|\\*?#<>~^]/\\$MATCH}"
# Get all history items (reversed) that match pattern $prefix* # Get all history items (reversed) that match pattern $prefix*
local history_matches local history_matches
history_matches=(${history[(R)$prefix*]}) history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
# Echo the first item that matches # Echo the first item that matches
echo ${history_matches[1]} echo "$history_matches[1]"
} }
#-------# #--------------------------------------------------------------------#
# Start # # Start #
#-------# #--------------------------------------------------------------------#
# Start the autosuggestion widgets # Start the autosuggestion widgets
_zsh_autosuggest_start() { _zsh_autosuggest_start() {