mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2025-12-06 15:20:40 +01:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0faa2b6584 | ||
|
|
a2d8d91196 | ||
|
|
dd9a8789a7 | ||
|
|
1b98af5b33 | ||
|
|
45ab49d1f2 | ||
|
|
41f15d5c9f | ||
|
|
3ce1adb55d | ||
|
|
2461a98857 | ||
|
|
76f415bf43 | ||
|
|
5e419da326 |
26
README.md
26
README.md
@@ -40,8 +40,6 @@ It suggests commands as you type, based on command history.
|
||||
|
||||
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. See the comments in that issue for a workaround.
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -69,10 +67,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:
|
||||
|
||||
- `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_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.
|
||||
|
||||
|
||||
@@ -94,32 +93,13 @@ bindkey '^ ' autosuggest-accept
|
||||
|
||||
### [`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.
|
||||
|
||||
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:
|
||||
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
|
||||
# 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)
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ testWidgetFunctionClear() {
|
||||
BUFFER="ec"
|
||||
POSTDISPLAY="ho hello"
|
||||
|
||||
_zsh_autosuggest_clear
|
||||
_zsh_autosuggest_clear "original-widget"
|
||||
|
||||
assertEquals \
|
||||
"BUFFER was modified" \
|
||||
@@ -109,7 +109,7 @@ testWidgetFunctionModify() {
|
||||
_zsh_autosuggest_suggestion \
|
||||
"echo hello"
|
||||
|
||||
_zsh_autosuggest_modify
|
||||
_zsh_autosuggest_modify "original-widget"
|
||||
|
||||
assertTrue \
|
||||
"original widget not invoked" \
|
||||
@@ -136,7 +136,7 @@ testWidgetFunctionAcceptCursorAtEnd() {
|
||||
|
||||
stub _zsh_autosuggest_invoke_original_widget
|
||||
|
||||
_zsh_autosuggest_accept
|
||||
_zsh_autosuggest_accept "original-widget"
|
||||
|
||||
assertTrue \
|
||||
"original widget not invoked" \
|
||||
@@ -160,7 +160,7 @@ testWidgetFunctionAcceptCursorNotAtEnd() {
|
||||
|
||||
stub _zsh_autosuggest_invoke_original_widget
|
||||
|
||||
_zsh_autosuggest_accept
|
||||
_zsh_autosuggest_accept "original-widget"
|
||||
|
||||
assertTrue \
|
||||
"original widget not invoked" \
|
||||
@@ -186,7 +186,7 @@ testWidgetFunctionPartialAcceptCursorMovesOutOfBuffer() {
|
||||
_zsh_autosuggest_invoke_original_widget \
|
||||
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
|
||||
|
||||
_zsh_autosuggest_partial_accept
|
||||
_zsh_autosuggest_partial_accept "original-widget"
|
||||
|
||||
assertTrue \
|
||||
"original widget not invoked" \
|
||||
@@ -212,7 +212,7 @@ testWidgetFunctionPartialAcceptCursorStaysInBuffer() {
|
||||
_zsh_autosuggest_invoke_original_widget \
|
||||
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
|
||||
|
||||
_zsh_autosuggest_partial_accept
|
||||
_zsh_autosuggest_partial_accept "original-widget"
|
||||
|
||||
assertTrue \
|
||||
"original widget not invoked" \
|
||||
@@ -236,7 +236,7 @@ testWidgetAccept() {
|
||||
|
||||
# Call the function pointed to by the widget since we can't call
|
||||
# the widget itself when zle is not active
|
||||
${widgets[autosuggest-accept]#*:}
|
||||
${widgets[autosuggest-accept]#*:} "original-widget"
|
||||
|
||||
assertTrue \
|
||||
"autosuggest-accept widget does not exist" \
|
||||
@@ -262,7 +262,7 @@ testWidgetClear() {
|
||||
|
||||
# Call the function pointed to by the widget since we can't call
|
||||
# the widget itself when zle is not active
|
||||
${widgets[autosuggest-clear]#*:}
|
||||
${widgets[autosuggest-clear]#*:} "original-widget"
|
||||
|
||||
assertTrue \
|
||||
"autosuggest-clear widget does not exist" \
|
||||
|
||||
42
src/bind.zsh
42
src/bind.zsh
@@ -6,10 +6,10 @@
|
||||
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
|
||||
_zsh_autosuggest_bind_widget() {
|
||||
local widget=$1
|
||||
local autosuggest_function=$2
|
||||
local autosuggest_action=$2
|
||||
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
|
||||
local action
|
||||
|
||||
# Save a reference to the original widget
|
||||
case $widgets[$widget] in
|
||||
# Already bound
|
||||
user:_zsh_autosuggest_(bound|orig)_*);;
|
||||
@@ -31,19 +31,14 @@ _zsh_autosuggest_bind_widget() {
|
||||
;;
|
||||
esac
|
||||
|
||||
# Set up widget to call $autosuggest_function if it exists
|
||||
# Otherwise just call the original widget
|
||||
if [ -n "$autosuggest_function" ]; then;
|
||||
action="$autosuggest_function \$@";
|
||||
else;
|
||||
action="zle $prefix$widget -- \$@"
|
||||
fi
|
||||
|
||||
# Create new function for the widget that highlights and calls the action
|
||||
# Pass the original widget's name explicitly into the autosuggest
|
||||
# function. Use this passed in widget name to call the original
|
||||
# widget instead of relying on the $WIDGET variable being set
|
||||
# correctly. $WIDGET cannot be trusted because other plugins call
|
||||
# zle without the `-w` flag (e.g. `zle self-insert` instead of
|
||||
# `zle self-insert -w`).
|
||||
eval "_zsh_autosuggest_bound_$widget() {
|
||||
_zsh_autosuggest_highlight_reset
|
||||
$action
|
||||
_zsh_autosuggest_highlight_apply
|
||||
_zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@
|
||||
}"
|
||||
|
||||
# Create the bound widget
|
||||
@@ -56,23 +51,24 @@ _zsh_autosuggest_bind_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
|
||||
if [ ${ZSH_AUTOSUGGEST_MODIFY_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_modify
|
||||
elif [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_clear
|
||||
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget clear
|
||||
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
|
||||
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_partial_accept
|
||||
_zsh_autosuggest_bind_widget $widget partial_accept
|
||||
else
|
||||
_zsh_autosuggest_bind_widget $widget
|
||||
# Assume any unspecified widget might modify the buffer
|
||||
_zsh_autosuggest_bind_widget $widget modify
|
||||
fi
|
||||
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() {
|
||||
local original_widget_name="$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX$WIDGET"
|
||||
local original_widget_name=$1
|
||||
|
||||
shift
|
||||
|
||||
if [ $widgets[$original_widget_name] ]; then
|
||||
zle $original_widget_name -- $@
|
||||
|
||||
@@ -22,27 +22,6 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
||||
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
|
||||
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
||||
forward-char
|
||||
|
||||
@@ -8,12 +8,7 @@ _zsh_autosuggest_suggestion() {
|
||||
setopt localoptions extendedglob
|
||||
|
||||
# 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*
|
||||
local history_matches
|
||||
history_matches=(${history[(R)$prefix*]})
|
||||
|
||||
# Echo the first item that matches
|
||||
echo ${history_matches[1]}
|
||||
fc -ln -m "$prefix*" 2>/dev/null | tail -1
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ _zsh_autosuggest_modify() {
|
||||
# Get a new suggestion if the buffer is not empty after modification
|
||||
local suggestion
|
||||
if [ $#BUFFER -gt 0 ]; then
|
||||
suggestion=$(_zsh_autosuggest_suggestion $BUFFER)
|
||||
suggestion=$(_zsh_autosuggest_suggestion "$BUFFER")
|
||||
fi
|
||||
|
||||
# Add the suggestion to the POSTDISPLAY
|
||||
@@ -71,17 +71,13 @@ _zsh_autosuggest_partial_accept() {
|
||||
fi
|
||||
}
|
||||
|
||||
_zsh_autosuggest_widget_accept() {
|
||||
for action in clear modify accept partial_accept; do
|
||||
eval "_zsh_autosuggest_widget_$action() {
|
||||
_zsh_autosuggest_highlight_reset
|
||||
_zsh_autosuggest_accept $@
|
||||
_zsh_autosuggest_$action \$@
|
||||
_zsh_autosuggest_highlight_apply
|
||||
}
|
||||
|
||||
_zsh_autosuggest_widget_clear() {
|
||||
_zsh_autosuggest_highlight_reset
|
||||
_zsh_autosuggest_clear $@
|
||||
_zsh_autosuggest_highlight_apply
|
||||
}
|
||||
}"
|
||||
done
|
||||
|
||||
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
||||
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Fish-like fast/unobtrusive autosuggestions for zsh.
|
||||
# https://github.com/tarruda/zsh-autosuggestions
|
||||
# v0.2.4
|
||||
# v0.2.9
|
||||
# Copyright (c) 2013 Thiago de Arruda
|
||||
# Copyright (c) 2016 Eric Freese
|
||||
#
|
||||
@@ -48,27 +48,6 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
||||
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
|
||||
ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
||||
forward-char
|
||||
@@ -129,10 +108,10 @@ zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget
|
||||
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
|
||||
_zsh_autosuggest_bind_widget() {
|
||||
local widget=$1
|
||||
local autosuggest_function=$2
|
||||
local autosuggest_action=$2
|
||||
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
|
||||
local action
|
||||
|
||||
# Save a reference to the original widget
|
||||
case $widgets[$widget] in
|
||||
# Already bound
|
||||
user:_zsh_autosuggest_(bound|orig)_*);;
|
||||
@@ -154,19 +133,14 @@ _zsh_autosuggest_bind_widget() {
|
||||
;;
|
||||
esac
|
||||
|
||||
# Set up widget to call $autosuggest_function if it exists
|
||||
# Otherwise just call the original widget
|
||||
if [ -n "$autosuggest_function" ]; then;
|
||||
action="$autosuggest_function \$@";
|
||||
else;
|
||||
action="zle $prefix$widget -- \$@"
|
||||
fi
|
||||
|
||||
# Create new function for the widget that highlights and calls the action
|
||||
# Pass the original widget's name explicitly into the autosuggest
|
||||
# function. Use this passed in widget name to call the original
|
||||
# widget instead of relying on the $WIDGET variable being set
|
||||
# correctly. $WIDGET cannot be trusted because other plugins call
|
||||
# zle without the `-w` flag (e.g. `zle self-insert` instead of
|
||||
# `zle self-insert -w`).
|
||||
eval "_zsh_autosuggest_bound_$widget() {
|
||||
_zsh_autosuggest_highlight_reset
|
||||
$action
|
||||
_zsh_autosuggest_highlight_apply
|
||||
_zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@
|
||||
}"
|
||||
|
||||
# Create the bound widget
|
||||
@@ -179,23 +153,24 @@ _zsh_autosuggest_bind_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
|
||||
if [ ${ZSH_AUTOSUGGEST_MODIFY_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_modify
|
||||
elif [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_clear
|
||||
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget clear
|
||||
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
|
||||
_zsh_autosuggest_bind_widget $widget _zsh_autosuggest_partial_accept
|
||||
_zsh_autosuggest_bind_widget $widget partial_accept
|
||||
else
|
||||
_zsh_autosuggest_bind_widget $widget
|
||||
# Assume any unspecified widget might modify the buffer
|
||||
_zsh_autosuggest_bind_widget $widget modify
|
||||
fi
|
||||
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() {
|
||||
local original_widget_name="$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX$WIDGET"
|
||||
local original_widget_name=$1
|
||||
|
||||
shift
|
||||
|
||||
if [ $widgets[$original_widget_name] ]; then
|
||||
zle $original_widget_name -- $@
|
||||
@@ -244,7 +219,7 @@ _zsh_autosuggest_modify() {
|
||||
# Get a new suggestion if the buffer is not empty after modification
|
||||
local suggestion
|
||||
if [ $#BUFFER -gt 0 ]; then
|
||||
suggestion=$(_zsh_autosuggest_suggestion $BUFFER)
|
||||
suggestion=$(_zsh_autosuggest_suggestion "$BUFFER")
|
||||
fi
|
||||
|
||||
# Add the suggestion to the POSTDISPLAY
|
||||
@@ -296,17 +271,13 @@ _zsh_autosuggest_partial_accept() {
|
||||
fi
|
||||
}
|
||||
|
||||
_zsh_autosuggest_widget_accept() {
|
||||
for action in clear modify accept partial_accept; do
|
||||
eval "_zsh_autosuggest_widget_$action() {
|
||||
_zsh_autosuggest_highlight_reset
|
||||
_zsh_autosuggest_accept $@
|
||||
_zsh_autosuggest_$action \$@
|
||||
_zsh_autosuggest_highlight_apply
|
||||
}
|
||||
|
||||
_zsh_autosuggest_widget_clear() {
|
||||
_zsh_autosuggest_highlight_reset
|
||||
_zsh_autosuggest_clear $@
|
||||
_zsh_autosuggest_highlight_apply
|
||||
}
|
||||
}"
|
||||
done
|
||||
|
||||
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
||||
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||
@@ -320,14 +291,9 @@ _zsh_autosuggest_suggestion() {
|
||||
setopt localoptions extendedglob
|
||||
|
||||
# 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*
|
||||
local history_matches
|
||||
history_matches=(${history[(R)$prefix*]})
|
||||
|
||||
# Echo the first item that matches
|
||||
echo ${history_matches[1]}
|
||||
fc -ln -m "$prefix*" 2>/dev/null | tail -1
|
||||
}
|
||||
|
||||
#--------------------------------------------------------------------#
|
||||
|
||||
Reference in New Issue
Block a user