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

[RFC][PATCH] Reset ZLE hooks when changing prompt themes (was Re: [RFC][PATCH] `newuser` prompt theme)



Patch attached, but not yet including Daniel’s suggested change below (since I still have questions about it). It includes an update to the docs, but note that I am not able to build those myself. I would much appreciate it f someone else could do that to test whether I did it correctly.

On 14 Apr 2021, at 15:05, Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
>> +++ b/Functions/Prompts/promptinit
>> @@ -178,8 +177,13 @@ Use prompt -h <theme> for help on specific themes.'
>> 
>>        # Reset some commonly altered bits to the default
>>        local hook
>> -       for hook in chpwd precmd preexec periodic zshaddhistory zshexit; do
>> -         add-zsh-hook -D "${hook}" "prompt_*_${hook}"
>> +       for hook in chpwd precmd preexec periodic zshaddhistory zshexit \
>> +           zsh_directory_name; do
>> +         add-zsh-hook -D "$hook" "prompt_*_$hook"
>> +       done
>> +       for hook in isearch-exit isearch-update line-pre-redraw line-init \
>> +           line-finish history-line-set keymap-select; do
>> +         add-zle-hook-widget -D "$hook" "prompt_*_$hook"
> 
> All these should be documented, like prompt_${foo}_preview is.
> 
> Recommend to name these prompt_${foo}_bar-{isearch-exit,isearch-update,…,keymap-select}
> for some fixed value of «bar» to avoid namespace issues (i.e., name
> collisions between existing prompts and future hooks).

Wouldn’t that be a breaking change in the API, though? I mean, there are already 3rd party prompt themes that use the current prompt_${foo}_{isearch-exit,isearch-update,…,keymap-select} naming scheme. Also, isn’t the prefix prompt_${foo}_ already a namespace of sorts? Why change this?

From b60bfd135ae2631a3b28aaf81074594d0ec1f967 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@xxxxxxxxx>
Date: Thu, 15 Apr 2021 21:50:36 +0300
Subject: [PATCH] Reset ZLE hooks when changing prompt themes

---
 Doc/Zsh/contrib.yo           |  8 ++++----
 Functions/Prompts/promptinit | 14 +++++++++-----
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 3c4fdded0..01809c0c4 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -2032,10 +2032,10 @@ setopts (tt(promptbang), etc.) are turned on, all other prompt-related
 options are turned off.  The tt(prompt_opts) array preserves setopts even
 beyond the scope of tt(localoptions), should your function need that.
 )
-item(Modify precmd and preexec)(
-Use of tt(add-zsh-hook) is recommended.  The tt(precmd) and tt(preexec)
-hooks are automatically adjusted if the prompt theme changes or is
-disabled.
+item(Modify hooks)(
+Use of tt(add-zsh-hook) and tt(add-zle-hook-widget) is recommended.  All hooks
+that follow the naming pattern tt(prompt_<theme>_<hook>) are automatically
+removed when the prompt theme changes or is disabled.
 )
 item(Declare cleanup)(
 If your function makes any other changes that should be undone when the
diff --git a/Functions/Prompts/promptinit b/Functions/Prompts/promptinit
index 37d69f100..b9d833c4b 100644
--- a/Functions/Prompts/promptinit
+++ b/Functions/Prompts/promptinit
@@ -14,6 +14,8 @@ prompt_themes=()
 promptinit () {
   emulate -L zsh
   setopt extendedglob
+  autoload -Uz add-zsh-hook add-zle-hook-widget
+
   local ppath='' name theme
   local -a match mbegin mend
 
@@ -32,9 +34,6 @@ promptinit () {
     fi
   done
 
-  # To manipulate precmd and preexec hooks...
-  autoload -Uz add-zsh-hook
-
   # Variables common to all prompt styles
   prompt_newline=$'\n%{\r%}'
 }
@@ -178,8 +177,13 @@ Use prompt -h <theme> for help on specific themes.'
 
        # Reset some commonly altered bits to the default
        local hook
-       for hook in chpwd precmd preexec periodic zshaddhistory zshexit; do
-         add-zsh-hook -D "${hook}" "prompt_*_${hook}"
+       for hook in chpwd precmd preexec periodic zshaddhistory zshexit \
+           zsh_directory_name; do
+         add-zsh-hook -D "$hook" "prompt_*_$hook"
+       done
+       for hook in isearch-exit isearch-update line-pre-redraw line-init \
+           line-finish history-line-set keymap-select; do
+         add-zle-hook-widget -D "$hook" "prompt_*_$hook"
        done
        typeset -ga zle_highlight=( ${zle_highlight:#default:*} )
        (( ${#zle_highlight} )) || unset zle_highlight
-- 
2.31.1



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