mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-18 09:51:06 +01:00
79 lines
2.6 KiB
Bash
79 lines
2.6 KiB
Bash
|
|
#--------------------------------------------------------------------#
|
|
# Widget Helpers #
|
|
#--------------------------------------------------------------------#
|
|
|
|
# Bind a single widget to an autosuggest widget, saving a reference to the original widget
|
|
_zsh_autosuggest_bind_widget() {
|
|
local widget=$1
|
|
local autosuggest_action=$2
|
|
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
|
|
|
|
# Save a reference to the original widget
|
|
case $widgets[$widget] in
|
|
# Already bound
|
|
user:_zsh_autosuggest_(bound|orig)_*);;
|
|
|
|
# User-defined widget
|
|
user:*)
|
|
zle -N $prefix$widget ${widgets[$widget]#*:}
|
|
;;
|
|
|
|
# Built-in widget
|
|
builtin)
|
|
eval "_zsh_autosuggest_orig_$widget() { zle .$widget }"
|
|
zle -N $prefix$widget _zsh_autosuggest_orig_$widget
|
|
;;
|
|
|
|
# Completion widget
|
|
completion:*)
|
|
eval "zle -C $prefix$widget ${${widgets[$widget]#*:}/:/ }"
|
|
;;
|
|
esac
|
|
|
|
# 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_widget_$autosuggest_action $prefix$widget \$@
|
|
}"
|
|
|
|
# Create the bound widget
|
|
zle -N $widget _zsh_autosuggest_bound_$widget
|
|
}
|
|
|
|
# Map all configured widgets to the right autosuggest widgets
|
|
_zsh_autosuggest_bind_widgets() {
|
|
local widget;
|
|
|
|
# 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_CLEAR_WIDGETS[(r)$widget]} ]; then
|
|
_zsh_autosuggest_bind_widget $widget clear
|
|
elif [ ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]; then
|
|
_zsh_autosuggest_bind_widget $widget accept
|
|
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
|
|
_zsh_autosuggest_bind_widget $widget partial_accept
|
|
else
|
|
# Assume any unspecified widget might modify the buffer
|
|
_zsh_autosuggest_bind_widget $widget modify
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Given the name of an original widget and args, invoke it, if it exists
|
|
_zsh_autosuggest_invoke_original_widget() {
|
|
# Do nothing unless called with at least one arg
|
|
[ $# -gt 0 ] || return
|
|
|
|
local original_widget_name=$1
|
|
|
|
shift
|
|
|
|
if [ $widgets[$original_widget_name] ]; then
|
|
zle $original_widget_name -w -- $@
|
|
fi
|
|
}
|