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
|
|
|
|
2017-02-28 19:14:16 +01:00
|
|
|
_zsh_autosuggest_incr_bind_count() {
|
2019-02-25 12:59:31 +01:00
|
|
|
typeset -gi bind_count=$((_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]+1))
|
|
|
|
_ZSH_AUTOSUGGEST_BIND_COUNTS[$1]=$bind_count
|
2017-02-28 19:14:16 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2017-02-28 19:14:16 +01:00
|
|
|
typeset -gA _ZSH_AUTOSUGGEST_BIND_COUNTS
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
local widget=$1
|
2016-02-14 16:54:34 +01:00
|
|
|
local autosuggest_action=$2
|
2016-02-05 23:14:08 +01:00
|
|
|
local prefix=$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX
|
|
|
|
|
2017-02-28 19:14:16 +01:00
|
|
|
local -i bind_count
|
|
|
|
|
2016-02-14 16:54:34 +01:00
|
|
|
# Save a reference to the original widget
|
2016-02-05 23:14:08 +01:00
|
|
|
case $widgets[$widget] in
|
|
|
|
# Already bound
|
2019-02-25 12:59:31 +01:00
|
|
|
user:_zsh_autosuggest_(bound|orig)_*)
|
|
|
|
bind_count=$((_ZSH_AUTOSUGGEST_BIND_COUNTS[$widget]))
|
|
|
|
;;
|
2016-02-05 23:14:08 +01:00
|
|
|
|
|
|
|
# User-defined widget
|
|
|
|
user:*)
|
2017-02-28 19:14:16 +01:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
2019-04-01 22:36:31 +02:00
|
|
|
zle -N $prefix$bind_count-$widget ${widgets[$widget]#*:}
|
2016-02-05 23:14:08 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
# Built-in widget
|
|
|
|
builtin)
|
2017-02-28 19:14:16 +01:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
2016-04-25 22:23:30 +02:00
|
|
|
eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }"
|
2019-04-01 22:36:31 +02:00
|
|
|
zle -N $prefix$bind_count-$widget _zsh_autosuggest_orig_$widget
|
2016-02-05 23:14:08 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
# Completion widget
|
|
|
|
completion:*)
|
2017-02-28 19:14:16 +01:00
|
|
|
_zsh_autosuggest_incr_bind_count $widget
|
2019-04-01 22:36:31 +02:00
|
|
|
eval "zle -C $prefix$bind_count-${(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`).
|
2017-02-28 19:14:16 +01:00
|
|
|
eval "_zsh_autosuggest_bound_${bind_count}_${(q)widget}() {
|
|
|
|
_zsh_autosuggest_widget_$autosuggest_action $prefix$bind_count-${(q)widget} \$@
|
2016-02-15 16:31:00 +01:00
|
|
|
}"
|
|
|
|
|
2016-02-05 23:14:08 +01:00
|
|
|
# Create the bound widget
|
2018-06-11 06:39:58 +02:00
|
|
|
zle -N -- $widget _zsh_autosuggest_bound_${bind_count}_$widget
|
2016-02-05 23:14:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Map all configured widgets to the right autosuggest widgets
|
|
|
|
_zsh_autosuggest_bind_widgets() {
|
2018-07-14 06:16:53 +02:00
|
|
|
emulate -L zsh
|
|
|
|
|
|
|
|
local widget
|
2016-08-01 03:35:30 +02:00
|
|
|
local ignore_widgets
|
|
|
|
|
|
|
|
ignore_widgets=(
|
|
|
|
.\*
|
|
|
|
_\*
|
2016-11-05 06:40:14 +01:00
|
|
|
zle-\*
|
2016-08-01 03:35:30 +02:00
|
|
|
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
|
2016-08-01 03:35:30 +02:00
|
|
|
for widget in ${${(f)"$(builtin zle -la)"}:#${(j:|:)~ignore_widgets}}; do
|
2017-07-17 22:45:48 +02:00
|
|
|
if [[ -n ${ZSH_AUTOSUGGEST_CLEAR_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_bind_widget $widget clear
|
2017-07-17 22:45:48 +02:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_ACCEPT_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_bind_widget $widget accept
|
2017-07-17 22:45:48 +02:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_EXECUTE_WIDGETS[(r)$widget]} ]]; then
|
2016-02-20 14:52:21 +01:00
|
|
|
_zsh_autosuggest_bind_widget $widget execute
|
2017-07-17 22:45:48 +02:00
|
|
|
elif [[ -n ${ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS[(r)$widget]} ]]; then
|
2016-02-14 16:54:34 +01:00
|
|
|
_zsh_autosuggest_bind_widget $widget partial_accept
|
2016-02-05 23:14:08 +01:00
|
|
|
else
|
2016-02-14 16:54:34 +01:00
|
|
|
# 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() {
|
2016-02-17 21:41:37 +01:00
|
|
|
# Do nothing unless called with at least one arg
|
2018-01-16 21:08:38 +01:00
|
|
|
(( $# )) || return 0
|
2016-02-17 21:41:37 +01:00
|
|
|
|
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
|
|
|
|
2017-07-17 22:45:48 +02:00
|
|
|
if (( ${+widgets[$original_widget_name]} )); then
|
2016-02-24 02:13:03 +01:00
|
|
|
zle $original_widget_name -- $@
|
2016-02-05 23:14:08 +01:00
|
|
|
fi
|
|
|
|
}
|