zsh-autosuggestions/src/bind.zsh

93 lines
2.8 KiB
Bash
Raw Normal View History

2016-02-05 23:14:08 +01:00
2016-02-14 08:29:43 +01:00
#--------------------------------------------------------------------#
# Widget Helpers #
#--------------------------------------------------------------------#
2016-02-05 23:14:08 +01:00
# 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
2016-02-05 23:14:08 +01:00
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
# Save a reference to the original widget
2016-02-05 23:14:08 +01:00
case $widgets[$widget] in
# Already bound
2016-02-15 16:31:00 +01:00
user:_zsh_autosuggest_(bound|orig)_*);;
2016-02-05 23:14:08 +01:00
# User-defined widget
user:*)
zle -l "$prefix$widget" && zle -N "$widget" ${widgets[$prefix$widget]#*:}
2016-02-05 23:14:08 +01:00
zle -N $prefix$widget ${widgets[$widget]#*:}
;;
# Built-in widget
builtin)
eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
2016-02-05 23:14:08 +01:00
zle -N $prefix$widget _zsh_autosuggest_orig_$widget
;;
# Completion widget
completion:*)
eval "zle -C $prefix${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}"
2016-02-05 23:14:08 +01:00
;;
esac
2016-02-15 16:31:00 +01:00
# 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_${(q)widget}() {
_zsh_autosuggest_widget_$autosuggest_action $prefix${(q)widget} \$@
2016-02-15 16:31:00 +01:00
}"
2016-02-05 23:14:08 +01:00
# Create the bound widget
2016-02-15 16:31:00 +01:00
zle -N $widget _zsh_autosuggest_bound_$widget
2016-02-05 23:14:08 +01:00
}
# Map all configured widgets to the right autosuggest widgets
_zsh_autosuggest_bind_widgets() {
local widget
local ignore_widgets
ignore_widgets=(
.\*
_\*
zle-\*
autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
)
2016-02-05 23:14:08 +01:00
# Find every widget we might want to bind and bind it appropriately
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
if [ ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget clear
2016-02-05 23:14:08 +01:00
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
2016-02-05 23:14:08 +01:00
elif [ ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]; then
_zsh_autosuggest_bind_widget $widget partial_accept
2016-02-05 23:14:08 +01:00
else
# Assume any unspecified widget might modify the buffer
_zsh_autosuggest_bind_widget $widget modify
2016-02-05 23:14:08 +01:00
fi
done
}
2016-02-15 16:31:00 +01:00
# Given the name of an original widget and args, invoke it, if it exists
2016-02-05 23:14:08 +01:00
_zsh_autosuggest_invoke_original_widget() {
# Do nothing unless called with at least one arg
[ $# -gt 0 ] || return
2016-03-06 05:03:14 +01:00
local original_widget_name="$1"
2016-02-15 16:31:00 +01:00
shift
2016-02-05 23:14:08 +01:00
if [ $widgets[$original_widget_name] ]; then
zle $original_widget_name -- $@
2016-02-05 23:14:08 +01:00
fi
}