mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-18 09:51:06 +01:00
Remove list of modify widgets and make 'modify' the default behavior.
This commit is contained in:
parent
cd71081303
commit
5e419da326
5 changed files with 36 additions and 117 deletions
|
@ -69,10 +69,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.
|
||||||
|
|
||||||
|
|
||||||
|
|
36
src/bind.zsh
36
src/bind.zsh
|
@ -6,13 +6,13 @@
|
||||||
# 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_(widget|orig)_*);;
|
||||||
|
|
||||||
# User-defined widget
|
# User-defined widget
|
||||||
user:*)
|
user:*)
|
||||||
|
@ -31,23 +31,8 @@ _zsh_autosuggest_bind_widget() {
|
||||||
;;
|
;;
|
||||||
esac
|
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
|
|
||||||
eval "_zsh_autosuggest_bound_$widget() {
|
|
||||||
_zsh_autosuggest_highlight_reset
|
|
||||||
$action
|
|
||||||
_zsh_autosuggest_highlight_apply
|
|
||||||
}"
|
|
||||||
|
|
||||||
# Create the bound widget
|
# Create the bound widget
|
||||||
zle -N $widget _zsh_autosuggest_bound_$widget
|
zle -N $widget _zsh_autosuggest_widget_$autosuggest_action
|
||||||
}
|
}
|
||||||
|
|
||||||
# Map all configured widgets to the right autosuggest widgets
|
# Map all configured widgets to the right autosuggest widgets
|
||||||
|
@ -56,16 +41,15 @@ _zsh_autosuggest_bind_widgets() {
|
||||||
|
|
||||||
# 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*|zle-line-*|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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -71,17 +71,13 @@ _zsh_autosuggest_partial_accept() {
|
||||||
fi
|
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_highlight_reset
|
||||||
_zsh_autosuggest_accept $@
|
_zsh_autosuggest_$action \$@
|
||||||
_zsh_autosuggest_highlight_apply
|
_zsh_autosuggest_highlight_apply
|
||||||
}
|
}"
|
||||||
|
done
|
||||||
_zsh_autosuggest_widget_clear() {
|
|
||||||
_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
|
||||||
|
|
|
@ -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
|
||||||
|
@ -129,13 +108,13 @@ zle -N autosuggest-start _zsh_autosuggest_deprecated_start_widget
|
||||||
# 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_(widget|orig)_*);;
|
||||||
|
|
||||||
# User-defined widget
|
# User-defined widget
|
||||||
user:*)
|
user:*)
|
||||||
|
@ -154,23 +133,8 @@ _zsh_autosuggest_bind_widget() {
|
||||||
;;
|
;;
|
||||||
esac
|
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
|
|
||||||
eval "_zsh_autosuggest_bound_$widget() {
|
|
||||||
_zsh_autosuggest_highlight_reset
|
|
||||||
$action
|
|
||||||
_zsh_autosuggest_highlight_apply
|
|
||||||
}"
|
|
||||||
|
|
||||||
# Create the bound widget
|
# Create the bound widget
|
||||||
zle -N $widget _zsh_autosuggest_bound_$widget
|
zle -N $widget _zsh_autosuggest_widget_$autosuggest_action
|
||||||
}
|
}
|
||||||
|
|
||||||
# Map all configured widgets to the right autosuggest widgets
|
# Map all configured widgets to the right autosuggest widgets
|
||||||
|
@ -179,16 +143,15 @@ _zsh_autosuggest_bind_widgets() {
|
||||||
|
|
||||||
# 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*|zle-line-*|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
|
||||||
}
|
}
|
||||||
|
@ -296,17 +259,13 @@ _zsh_autosuggest_partial_accept() {
|
||||||
fi
|
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_highlight_reset
|
||||||
_zsh_autosuggest_accept $@
|
_zsh_autosuggest_$action \$@
|
||||||
_zsh_autosuggest_highlight_apply
|
_zsh_autosuggest_highlight_apply
|
||||||
}
|
}"
|
||||||
|
done
|
||||||
_zsh_autosuggest_widget_clear() {
|
|
||||||
_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
|
||||||
|
|
Loading…
Reference in a new issue