From fb676b973f84f5fd1502b5dc39dd9ccd90224fd4 Mon Sep 17 00:00:00 2001 From: Ivan Povalyukhin Date: Wed, 6 May 2015 20:39:44 -0700 Subject: [PATCH] [issue-206] rvmrc should get sourced on cd; over version managers may also override cdpwd hooks, so provide a way to support hooks; fixes #206 --- plugins/available/z_autoenv.plugin.bash | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/plugins/available/z_autoenv.plugin.bash b/plugins/available/z_autoenv.plugin.bash index 553a7ba3..f6506a77 100644 --- a/plugins/available/z_autoenv.plugin.bash +++ b/plugins/available/z_autoenv.plugin.bash @@ -32,14 +32,28 @@ autoenv_init() done } -cd() +# Bash support for Zsh like chpwd hook +[[ -n "${ZSH_VERSION:-}" ]] || { cd() { __zsh_like_cd cd "$@" ; } } + +__zsh_like_cd() { - if builtin cd "$@" + \typeset __zsh_like_cd_hook + if + builtin "$@" then - autoenv_init - return 0 + shift || true # remove the called method + for __zsh_like_cd_hook in chpwd "${chpwd_functions[@]}" + do + if \typeset -f "$__zsh_like_cd_hook" >/dev/null 2>&1 + then "$__zsh_like_cd_hook" "$@" || break # finish on first failed hook + fi + done + true else - echo "else?" return $? fi } + +[[ -n "${chpwd_functions}" ]] || { export -a chpwd_functions; } # define hooks as shell array +[[ " ${chpwd_functions[*]} " == *" autoenv_init "* ]] || # prevent double addition +chpwd_functions+=(autoenv_init) # add hook to the list