zsh-workers
 help / color / mirror / code / Atom feed
* Teardown for prompts
@ 2012-01-05 16:23 Christian Höltje
  2012-01-05 18:44 ` Teardown for prompts [patch - remove all hooks] Christian Höltje
  2012-01-06 16:22 ` Teardown for prompts Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: Christian Höltje @ 2012-01-05 16:23 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1105 bytes --]

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@gerf.org
http://docwhat.org/


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Teardown for prompts [patch - remove all hooks]
  2012-01-05 16:23 Teardown for prompts Christian Höltje
@ 2012-01-05 18:44 ` Christian Höltje
  2012-01-06 15:47   ` Bart Schaefer
  2012-01-06 16:22 ` Teardown for prompts Bart Schaefer
  1 sibling, 1 reply; 4+ messages in thread
From: Christian Höltje @ 2012-01-05 18:44 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 2228 bytes --]

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.'
        fi
  
        # 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
docwhat@gerf.org
http://docwhat.org/


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@gerf.org (mailto:docwhat@gerf.org)
> http://docwhat.org/
>  


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Teardown for prompts [patch - remove all hooks]
  2012-01-05 18:44 ` Teardown for prompts [patch - remove all hooks] Christian Höltje
@ 2012-01-06 15:47   ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2012-01-06 15:47 UTC (permalink / raw)
  To: zsh-workers

On Jan 5,  1:44pm, Christian Holtje wrote:
} 
} +       for hook in chpwd precmd preexec periodic zshaddhistory zshexit; do

I was going to say I'd commit this, but I see PWS already has.

However, I have to say that if one's prompt needs a zshexit hook, one is
probably doing something wrong. :-)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Teardown for prompts
  2012-01-05 16:23 Teardown for prompts Christian Höltje
  2012-01-05 18:44 ` Teardown for prompts [patch - remove all hooks] Christian Höltje
@ 2012-01-06 16:22 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2012-01-06 16:22 UTC (permalink / raw)
  To: zsh-workers

On Jan 5, 11:23am, Christian Holtje wrote:
}
} 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

I was looking again at the patch you sent for this, and for prompts it
seems it might be important to handle the zsh_directory_name hook too.

}      Bonus points for getting the list of hooks from add-zsh-hook itself.

    for hook in ${=${(f)"$(add-zsh-hook)"}[-1]}

would do it, though it might be better to add a -L option to add-zsh-hook
so as not to rely on the usage message and that being printed to stdout.

There's actually a similar issue with prompt_preview_theme, which tries
to make hook lists local to prevent them from changing the world when a
prompt is "temporarily" installed.

}  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.

If added, this should probably use "cleanup" rather than "teardown" to
be consistent with other zsh usages.

There's an example of something like this in prompt_bart_setup, where
you can say "prompt bart off" and it unloads itself.  A comment in there
seems to indicate that at one time there was a theme named "off" so you
could just say "prompt off" and it would unload everything, but I can't
find any other trace of the "prompt_off_setup" function any longer.

}  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 think I'd rather leave this up to the individual cleanup routines.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-01-06 16:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-05 16:23 Teardown for prompts Christian Höltje
2012-01-05 18:44 ` Teardown for prompts [patch - remove all hooks] Christian Höltje
2012-01-06 15:47   ` Bart Schaefer
2012-01-06 16:22 ` Teardown for prompts Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).