dot/shell-rc, dot/bashrc, dot/zshrc: Introduce a formal notion of hooks.
[profile] / dot / zshrc
index c6cfa99..62766c0 100644 (file)
--- a/dot/zshrc
+++ b/dot/zshrc
@@ -15,10 +15,32 @@ HISTSIZE=1000
 SAVEHIST=1000
 
 ###--------------------------------------------------------------------------
+### Hook implementation.
+
+__mdw_precmd_hook= __mdw_preexec_hook=
+
+__mdw_run_precmd_hook () { __mdw_runhook __mdw_precmd_hook "$@"; }
+__mdw_run_preexec_hook () { __mdw_runhook __mdw_preexec_hook "$@"; }
+
+precmd_functions+=(__mdw_run_precmd_hook)
+preexec_functions+=(__mdw_run_preexec_hook)
+
+###--------------------------------------------------------------------------
 ### Common shell configuration.
 
 . "$HOME/.shell-rc"
 
+## Fixup: `zsh' has sane quoting rules.
+__mdw_runhook () {
+  local hk=$1 saverc=$? t i; shift
+
+  eval t=\${$hk+t}
+  case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
+
+  eval t=\$$hk
+  for i in ${=t}; do __mdw_setrc $saverc; "$i" "$@"; done
+}
+
 ###--------------------------------------------------------------------------
 ### Prompt hacking.
 
@@ -36,8 +58,6 @@ __mdw_set_prompt_hacks () {
 if [ -t 0 ]; then
   __mdw_source_if_exists /usr/lib/git-core/git-sh-prompt
   __mdw_set_prompt_pieces
-  precmd_functions+=(__mdw_precmd)
-  preexec_functions+=(__mdw_preexec)
 fi
 
 ###--------------------------------------------------------------------------