mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-18 09:51:06 +01:00
Add an autosuggest widget: autosuggest-execute.
This commit is contained in:
parent
ba029e83d0
commit
606c81a01b
5 changed files with 42 additions and 4 deletions
|
@ -69,6 +69,7 @@ This plugin works by triggering custom behavior when certain [zle widgets](http:
|
|||
|
||||
- `ZSH_AUTOSUGGEST_CLEAR_WIDGETS`: Widgets in this array will clear the suggestion when invoked.
|
||||
- `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.
|
||||
|
||||
Widgets not in any of these lists will update the suggestion when invoked.
|
||||
|
@ -78,10 +79,11 @@ Widgets not in any of these lists will update the suggestion when invoked.
|
|||
|
||||
### Key Bindings
|
||||
|
||||
This plugin provides two widgets that you can use with `bindkey`:
|
||||
This plugin provides three widgets that you can use with `bindkey`:
|
||||
|
||||
1. `autosuggest-accept`: Accepts the current suggestion.
|
||||
2. `autosuggest-clear`: Clears the current suggestion.
|
||||
2. `autosuggest-execute`: Accepts and executes the current suggestion.
|
||||
3. `autosuggest-clear`: Clears the current suggestion.
|
||||
|
||||
For example, this would bind <kbd>ctrl</kbd> + <kbd>space</kbd> to accept the current suggestion.
|
||||
|
||||
|
|
|
@ -55,6 +55,8 @@ _zsh_autosuggest_bind_widgets() {
|
|||
_zsh_autosuggest_bind_widget $widget clear
|
||||
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget accept
|
||||
elif [ ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget execute
|
||||
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget partial_accept
|
||||
else
|
||||
|
|
|
@ -30,6 +30,10 @@ ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
|||
vi-end-of-line
|
||||
)
|
||||
|
||||
# Widgets that accept the entire suggestion and execute it
|
||||
ZSH_AUTOSUGGEST_EXECUTE_WIDGETS=(
|
||||
)
|
||||
|
||||
# Widgets that accept the suggestion as far as the cursor moves
|
||||
ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
|
||||
forward-word
|
||||
|
|
|
@ -47,6 +47,17 @@ _zsh_autosuggest_accept() {
|
|||
_zsh_autosuggest_invoke_original_widget $@
|
||||
}
|
||||
|
||||
# Accept the entire suggestion and execute it
|
||||
_zsh_autosuggest_execute() {
|
||||
# Add the suggestion to the buffer
|
||||
BUFFER="$BUFFER$POSTDISPLAY"
|
||||
|
||||
# Remove the suggestion
|
||||
unset POSTDISPLAY
|
||||
|
||||
zle .accept-line
|
||||
}
|
||||
|
||||
# Partially accept the suggestion
|
||||
_zsh_autosuggest_partial_accept() {
|
||||
# Save the contents of the buffer so we can restore later if needed
|
||||
|
@ -71,7 +82,7 @@ _zsh_autosuggest_partial_accept() {
|
|||
fi
|
||||
}
|
||||
|
||||
for action in clear modify accept partial_accept; do
|
||||
for action in clear modify accept partial_accept execute; do
|
||||
eval "_zsh_autosuggest_widget_$action() {
|
||||
_zsh_autosuggest_highlight_reset
|
||||
_zsh_autosuggest_$action \$@
|
||||
|
@ -81,3 +92,4 @@ done
|
|||
|
||||
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
||||
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
||||
|
|
|
@ -56,6 +56,10 @@ ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
|
|||
vi-end-of-line
|
||||
)
|
||||
|
||||
# Widgets that accept the entire suggestion and execute it
|
||||
ZSH_AUTOSUGGEST_EXECUTE_WIDGETS=(
|
||||
)
|
||||
|
||||
# Widgets that accept the suggestion as far as the cursor moves
|
||||
ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
|
||||
forward-word
|
||||
|
@ -157,6 +161,8 @@ _zsh_autosuggest_bind_widgets() {
|
|||
_zsh_autosuggest_bind_widget $widget clear
|
||||
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget accept
|
||||
elif [ ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget execute
|
||||
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
|
||||
_zsh_autosuggest_bind_widget $widget partial_accept
|
||||
else
|
||||
|
@ -250,6 +256,17 @@ _zsh_autosuggest_accept() {
|
|||
_zsh_autosuggest_invoke_original_widget $@
|
||||
}
|
||||
|
||||
# Accept the entire suggestion and execute it
|
||||
_zsh_autosuggest_execute() {
|
||||
# Add the suggestion to the buffer
|
||||
BUFFER="$BUFFER$POSTDISPLAY"
|
||||
|
||||
# Remove the suggestion
|
||||
unset POSTDISPLAY
|
||||
|
||||
zle .accept-line
|
||||
}
|
||||
|
||||
# Partially accept the suggestion
|
||||
_zsh_autosuggest_partial_accept() {
|
||||
# Save the contents of the buffer so we can restore later if needed
|
||||
|
@ -274,7 +291,7 @@ _zsh_autosuggest_partial_accept() {
|
|||
fi
|
||||
}
|
||||
|
||||
for action in clear modify accept partial_accept; do
|
||||
for action in clear modify accept partial_accept execute; do
|
||||
eval "_zsh_autosuggest_widget_$action() {
|
||||
_zsh_autosuggest_highlight_reset
|
||||
_zsh_autosuggest_$action \$@
|
||||
|
@ -284,6 +301,7 @@ done
|
|||
|
||||
zle -N autosuggest-accept _zsh_autosuggest_widget_accept
|
||||
zle -N autosuggest-clear _zsh_autosuggest_widget_clear
|
||||
zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
||||
|
||||
#--------------------------------------------------------------------#
|
||||
# Suggestion #
|
||||
|
|
Loading…
Reference in a new issue