mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-10-27 16:50:58 +01:00
38850: Simplify indexing scheme to store hooks in the order they are added
Also, better handling of edge cases and of autoloading/sourcing file
This commit is contained in:
parent
ac813cfade
commit
3e61af3ff4
2 changed files with 45 additions and 31 deletions
|
|
@ -1,3 +1,9 @@
|
||||||
|
2016-07-13 Barton E. Schaefer <schaefer@zsh.org>
|
||||||
|
|
||||||
|
* 38850: Functions/Zle/add-zle-hook-widget: Simplify indexing
|
||||||
|
scheme to store hooks strictly in the order they are added;
|
||||||
|
better handling of edge cases and autoloading/sourcing file.
|
||||||
|
|
||||||
2016-07-13 Eric Cook <llua@gmx.com>
|
2016-07-13 Eric Cook <llua@gmx.com>
|
||||||
|
|
||||||
* 38833: Completion/Unix/Command/_iostat
|
* 38833: Completion/Unix/Command/_iostat
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,15 @@
|
||||||
# Add to HOOK the given WIDGET
|
# Add to HOOK the given WIDGET
|
||||||
#
|
#
|
||||||
# HOOK is one of isearch-exit, isearch-update, line-pre-redraw, line-init,
|
# HOOK is one of isearch-exit, isearch-update, line-pre-redraw, line-init,
|
||||||
# line-finish, history-line-set, keymap-select (the zle- prefix is not
|
# line-finish, history-line-set, keymap-select (the zle- prefix is allowed
|
||||||
# required).
|
# but not required). If a widget corresponding to HOOK already exists, it
|
||||||
|
# is preserved and called first in the new set of HOOK widgets.
|
||||||
#
|
#
|
||||||
# WIDGET may be of the form INDEX:NAME in which case the INDEX determines
|
# With -d, remove the WIDGET from the hook instead; deletes the hook
|
||||||
# the order in which the widget executes relative to other hook widgets.
|
# linkage if it is empty.
|
||||||
# Othewise the widget is assigned an index that appends it to the array.
|
|
||||||
#
|
#
|
||||||
# With -d, remove the widget from the hook instead; delete the hook
|
# -D behaves like -d, but pattern characters are active in WIDGET, so
|
||||||
# variable if it is empty.
|
# any matching widget will be deleted from the hook.
|
||||||
#
|
|
||||||
# -D behaves like -d, but pattern characters are active in the
|
|
||||||
# widget name, so any matching widget will be deleted from the hook.
|
|
||||||
#
|
#
|
||||||
# Without -d, if the WIDGET is not already defined, a function having the
|
# Without -d, if the WIDGET is not already defined, a function having the
|
||||||
# same name is marked for autoload; -U is passed down to autoload if that
|
# same name is marked for autoload; -U is passed down to autoload if that
|
||||||
|
|
@ -47,16 +44,15 @@ do
|
||||||
# and we run them in number order
|
# and we run them in number order
|
||||||
zstyle -a $WIDGET widgets hook_widgets
|
zstyle -a $WIDGET widgets hook_widgets
|
||||||
for hook in "${(@)${(@on)hook_widgets[@]}#<->:}"; do
|
for hook in "${(@)${(@on)hook_widgets[@]}#<->:}"; do
|
||||||
zle "$hook" -Nw "$@" || return
|
if [[ "$hook" = user:* ]]; then
|
||||||
|
# Preserve $WIDGET within the renamed widget
|
||||||
|
zle "$hook" -N "$@"
|
||||||
|
else
|
||||||
|
zle "$hook" -Nw "$@"
|
||||||
|
fi || return
|
||||||
done
|
done
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
# Check for an existing widget, add it as the first hook
|
|
||||||
if [[ ${widgets[$hook]} = user:* ]]; then
|
|
||||||
zle -A "$hook" "${widgets[$hook]}"
|
|
||||||
zstyle -- "$hook" widgets 0:"${widgets[$hook]}"
|
|
||||||
zle -N "$hook" azhw:"$hook"
|
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
|
|
||||||
# Redefine ourself with the setup left out
|
# Redefine ourself with the setup left out
|
||||||
|
|
@ -133,18 +129,20 @@ function add-zle-hook-widget {
|
||||||
else
|
else
|
||||||
integer i=${#options[ksharrays]}-2
|
integer i=${#options[ksharrays]}-2
|
||||||
zstyle -g extant_hooks "$hook" widgets
|
zstyle -g extant_hooks "$hook" widgets
|
||||||
if [[ "$fn" != <->:* ]]; then
|
# Check for an existing widget, add it as the first hook
|
||||||
|
if [[ ${widgets[$hook]} != "user:azhw:$hook" ]]; then
|
||||||
|
zle -A "$hook" "${widgets[$hook]}"
|
||||||
|
extant_hooks=(0:"${widgets[$hook]}" "${extant_hooks[@]}")
|
||||||
|
zle -N "$hook" azhw:"$hook"
|
||||||
|
fi
|
||||||
|
# Add new widget only if not already in the hook list
|
||||||
if [[ -z ${(M)extant_hooks[@]:#(<->:|)$fn} ]]; then
|
if [[ -z ${(M)extant_hooks[@]:#(<->:|)$fn} ]]; then
|
||||||
# no index and not already hooked
|
# no index and not already hooked
|
||||||
# assign largest existing index plus 10
|
# assign largest existing index plus 1
|
||||||
i=${${(On@)${(@M)extant_hooks[@]#<->:}%:}[i]}+10
|
i=${${(On@)${(@M)extant_hooks[@]#<->:}%:}[i]}+1
|
||||||
else
|
else
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
else
|
|
||||||
i=${${(M)fn#<->:}%:}
|
|
||||||
fn=${fn#<->:}
|
|
||||||
fi
|
|
||||||
extant_hooks+=("${i}:${fn}")
|
extant_hooks+=("${i}:${fn}")
|
||||||
zstyle -- "$hook" widgets "${extant_hooks[@]}"
|
zstyle -- "$hook" widgets "${extant_hooks[@]}"
|
||||||
if [[ -z "${widgets[$fn]}" ]]; then
|
if [[ -z "${widgets[$fn]}" ]]; then
|
||||||
|
|
@ -157,7 +155,17 @@ function add-zle-hook-widget {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Handle zsh autoloading conventions
|
# Handle zsh autoloading conventions:
|
||||||
if [[ "$zsh_eval_context" = *loadautofunc && ! -o kshautoload ]]; then
|
# - "file" appears last in zsh_eval_context when "source"-ing
|
||||||
add-zle-hook-widget "$@"
|
# - "evalautofunc" appears with kshautoload set or autoload -k
|
||||||
fi
|
# - "loadautofunc" appears with kshautoload unset or autoload -z
|
||||||
|
# - use of autoload +X cannot reliably be detected, use best guess
|
||||||
|
case "$zsh_eval_context" in
|
||||||
|
*file) ;;
|
||||||
|
*evalautofunc) ;;
|
||||||
|
*loadautofunc) add-zle-hook-widget "$@";;
|
||||||
|
*) [[ -o kshautoload ]] || add-zle-hook-widget "$@";;
|
||||||
|
esac
|
||||||
|
# Note fallback here is equivalent to the usual best-guess used by
|
||||||
|
# functions written for zsh before $zsh_eval_context was available
|
||||||
|
# so this case-statement is backward-compatible.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue