Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Teardown for prompts [patch - remove all hooks]

This patch will remove all prompt hooks when changing prompts, using the prompt system.

The current code only catches the precmd and preexec hooks, but all the hooks should be checked.

diff --git a/Functions/Prompts/promptinit b/Functions/Prompts/promptinit
index b07eabb..66e73a7 100644
--- a/Functions/Prompts/promptinit
+++ b/Functions/Prompts/promptinit
@@ -155,8 +155,10 @@ Use prompt -h <theme> for help on specific themes.'
        # Reset some commonly altered bits to the default
-       add-zsh-hook -D precmd "prompt_*_precmd"
-       add-zsh-hook -D preexec "prompt_*_preexec"
+       local hook
+       for hook in chpwd precmd preexec periodic zshaddhistory zshexit; do
+         add-zsh-hook -D "${hook}" "prompt_*_${hook}"
+       done
        set -A zle_highlight ${zle_highlight:#default:*}
        (( ${#zle_highlight} )) || unset zle_highlight

Christian Höltje

On Thursday, January 5, 2012 at 11:23 AM, Christian Höltje wrote:

> I have some suggestions for the promptinit stuff....  
> 1) Remove other prompt related hooks.  Currently, it removes preexec and precmd, but it doesn't remove chpwd, periodic, zshexit, nor zshaddhistory.
>       e.g.
> for hook in preexec precmd chpwd periodic zshexit zshaddhistory; do
>           add-zsh-hook -D "${hook}" "prompt_*_${hook}"
>         done
>      Bonus points for getting the list of hooks from add-zsh-hook itself.
>  2) Add a teardown function.  This could be used to unset things, etc. if the prompt changes.  If prompt is switching from the prompt 'adam2', it would look for prompt_adam2_teardown and run it if it exists before switch to the new prompt.
>  3) On switching away from a prompt, look for any and all variables and functions with pattern "prompt_${current_prompt_name}_*" and unset/unfunction them.
> I discovered this when I wrote a prompt that used the chpwd hook to try to minimize certain VCS checks (an optimization) and that function was still being called when I switched to a new prompt.  
> --  
> Christian Höltje
> docwhat@xxxxxxxx (mailto:docwhat@xxxxxxxx)
> http://docwhat.org/

Messages sorted by: Reverse Date, Date, Thread, Author