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
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
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
2016-02-14 16:54:34 +01:00
user:_zsh_autosuggest_( widget| orig) _*) ; ;
2016-02-05 23:14:08 +01:00
# 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
# Create the bound widget
2016-02-14 16:54:34 +01:00
zle -N $widget _zsh_autosuggest_widget_$autosuggest_action
2016-02-05 23:14:08 +01:00
}
# 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
2016-02-14 08:32:25 +01:00
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
2016-02-14 16:54:34 +01:00
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
2016-02-14 16:54:34 +01:00
_zsh_autosuggest_bind_widget $widget accept
2016-02-05 23:14:08 +01:00
elif [ ${ 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
}
# Given the name of a widget, invoke the original we saved, if it exists
_zsh_autosuggest_invoke_original_widget( ) {
2016-02-14 07:46:34 +01:00
local original_widget_name = " $ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX $WIDGET "
2016-02-05 23:14:08 +01:00
if [ $widgets [ $original_widget_name ] ] ; then
2016-02-14 07:46:34 +01:00
zle $original_widget_name -- $@
2016-02-05 23:14:08 +01:00
fi
}