1 # Add to HOOK the given FUNCTION.
2 # HOOK is one of chpwd, precmd, preexec, periodic, zshaddhistory,
3 # zshexit (the _functions subscript is not required).
5 # With -d, remove the function from the hook instead; delete the hook
6 # variable if it is empty.
8 # -D behaves like -d, but pattern characters are active in the
9 # function name, so any matching function will be deleted from the hook.
11 # Without -d, the FUNCTION is marked for autoload; -U is passed down to
12 # autoload if that is given, as are -z and -k. (This is harmless if the
13 # function is actually defined inline.)
18 hooktypes=(chpwd precmd preexec periodic zshaddhistory zshexit)
24 while getopts "dDUzk" opt; do
43 shift $(( OPTIND - 1 ))
45 if (( $# != 2 || ${hooktypes[(I)$1]} == 0 )); then
46 print "Usage: $0 hook function\nValid hooks are:\n $hooktypes"
50 local hook="${1}_functions"
54 # delete, if hook is set
55 if (( ${(P)+hook} )); then
56 if (( del == 2 )); then
57 set -A $hook ${(P)hook:#${~fn}}
59 set -A $hook ${(P)hook:#$fn}
61 # unset if no remaining entries --- this can give better
62 # performance in some cases
63 if (( ! ${(P)#hook} )); then
68 if (( ${(P)+hook} )); then
69 if (( ${${(P)hook}[(I)$fn]} == 0 )); then
70 set -A $hook ${(P)hook} $fn
75 autoload $autoopts -- $fn