1ba0af650ac575a7d35b10146d2e7a7b3b2e2ae6

Author
Jacob Tomaw <jacob.tomaw@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Use safer append to hook function arrays (#8406)

Use add-zsh-hook to add functions to hooks. That way they won't be added again
when doing `source ~/.zshrc` multiple times.

Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

  1diff --git a/.gitignore b/.gitignore
  2index 87a79cdae3464823e307182badbf1d1ffc4b5aad..251c9dc9fff1fe5fad54a8379737f541f4ab7ef3 100644
  3--- a/.gitignore
  4+++ b/.gitignore
  5@@ -4,3 +4,4 @@ custom/
  6 # temp files directories
  7 cache/
  8 log/
  9+*.swp
 10diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
 11index aa14f3f07fe4a12979cd9352b8a6ee6d917cadc0..f5e367fcbc8a0391f3ed2b3cf28f6fa36fea91b5 100644
 12--- a/lib/termsupport.zsh
 13+++ b/lib/termsupport.zsh
 14@@ -75,8 +75,9 @@ function omz_termsupport_preexec {
 15   title '$CMD' '%100>...>$LINE%<<'
 16 }
 17 
 18-precmd_functions+=(omz_termsupport_precmd)
 19-preexec_functions+=(omz_termsupport_preexec)
 20+autoload -U add-zsh-hook
 21+add-zsh-hook precmd omz_termsupport_precmd
 22+add-zsh-hook preexec omz_termsupport_preexec
 23 
 24 
 25 # Keep Apple Terminal.app's current working directory updated
 26@@ -99,7 +100,7 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
 27   }
 28 
 29   # Use a precmd hook instead of a chpwd hook to avoid contaminating output
 30-  precmd_functions+=(update_terminalapp_cwd)
 31+  add-zsh-hook precmd update_terminalapp_cwd
 32   # Run once to get initial cwd set
 33   update_terminalapp_cwd
 34 fi
 35diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh
 36index 6b8fa66ce4290e8f40dbbf76edd5f2e6f13be405..caee9b5a3bc43b2092cd6a7ab73e086dc664a8ed 100644
 37--- a/plugins/alias-finder/alias-finder.plugin.zsh
 38+++ b/plugins/alias-finder/alias-finder.plugin.zsh
 39@@ -4,7 +4,7 @@ alias-finder() {
 40     case $i in
 41       -e|--exact) exact=true;;
 42       -l|--longer) longer=true;;
 43-      *) 
 44+      *)
 45         if [[ -z $cmd ]]; then
 46           cmd=$i
 47         else
 48@@ -43,4 +43,5 @@ preexec_alias-finder() {
 49   fi
 50 }
 51 
 52-preexec_functions+=(preexec_alias-finder)
 53+autoload -U add-zsh-hook
 54+add-zsh-hook preexec preexec_alias-finder
 55diff --git a/plugins/dirhistory/dirhistory.plugin.zsh b/plugins/dirhistory/dirhistory.plugin.zsh
 56index 239915e4821fec0264ef8bffb256f1c87c77ae0b..35c43d76ad7f06f273dbad3e4490c12b19a294af 100644
 57--- a/plugins/dirhistory/dirhistory.plugin.zsh
 58+++ b/plugins/dirhistory/dirhistory.plugin.zsh
 59@@ -53,7 +53,8 @@ function push_future() {
 60 }
 61 
 62 # Called by zsh when directory changes
 63-chpwd_functions+=(chpwd_dirhistory)
 64+autoload -U add-zsh-hook
 65+add-zsh-hook chpwd chpwd_dirhistory
 66 function chpwd_dirhistory() {
 67   push_past $PWD
 68   # If DIRHISTORY_CD is not set...
 69diff --git a/plugins/dirpersist/dirpersist.plugin.zsh b/plugins/dirpersist/dirpersist.plugin.zsh
 70index 616e2c3c658710d47073cfdd88aa43aea3782675..daadc3850b35b8e0731e8d69dba4232de587ac90 100644
 71--- a/plugins/dirpersist/dirpersist.plugin.zsh
 72+++ b/plugins/dirpersist/dirpersist.plugin.zsh
 73@@ -11,7 +11,8 @@ if [[ -f ${dirstack_file} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then
 74   [[ -d $dirstack[1] ]] && cd $dirstack[1] && cd $OLDPWD
 75 fi
 76 
 77-chpwd_functions+=(chpwd_dirpersist)
 78+autoload -U add-zsh-hook
 79+add-zsh-hook chpwd chpwd_dirpersist
 80 chpwd_dirpersist() {
 81   if (( $DIRSTACKSIZE <= 0 )) || [[ -z $dirstack_file ]]; then return; fi
 82   local -ax my_stack
 83diff --git a/plugins/git-prompt/git-prompt.plugin.zsh b/plugins/git-prompt/git-prompt.plugin.zsh
 84index 76ac2e62b00fde67fe5f1c967dc15a55304084b1..da674af98db9a167db266fbd579a00e21f1a6a22 100644
 85--- a/plugins/git-prompt/git-prompt.plugin.zsh
 86+++ b/plugins/git-prompt/git-prompt.plugin.zsh
 87@@ -20,9 +20,10 @@ function precmd_update_git_vars() {
 88     fi
 89 }
 90 
 91-chpwd_functions+=(chpwd_update_git_vars)
 92-precmd_functions+=(precmd_update_git_vars)
 93-preexec_functions+=(preexec_update_git_vars)
 94+autoload -U add-zsh-hook
 95+add-zsh-hook chpwd chpwd_update_git_vars
 96+add-zsh-hook precmd precmd_update_git_vars
 97+add-zsh-hook preexec preexec_update_git_vars
 98 
 99 
100 ## Function definitions
101diff --git a/plugins/last-working-dir/last-working-dir.plugin.zsh b/plugins/last-working-dir/last-working-dir.plugin.zsh
102index 53bb19e4687faf85d18c51ae61d265e8c64829c4..fd21705ae2ccdd4a20ab93e639d4aefa4d5a83ca 100644
103--- a/plugins/last-working-dir/last-working-dir.plugin.zsh
104+++ b/plugins/last-working-dir/last-working-dir.plugin.zsh
105@@ -2,7 +2,8 @@
106 typeset -g ZSH_LAST_WORKING_DIRECTORY
107 
108 # Updates the last directory once directory is changed
109-chpwd_functions+=(chpwd_last_working_dir)
110+autoload -U add-zsh-hook
111+add-zsh-hook chpwd chpwd_last_working_dir
112 chpwd_last_working_dir() {
113 	if [ "$ZSH_SUBSHELL" = 0 ]; then
114 		local cache_file="$ZSH_CACHE_DIR/last-working-dir"
115diff --git a/plugins/pipenv/pipenv.plugin.zsh b/plugins/pipenv/pipenv.plugin.zsh
116index 0a5dc56a42b5424ba1b4b52f75cdc7d0a903cd37..ec41c3e02960fad0c9b6428a22219849e29bee48 100644
117--- a/plugins/pipenv/pipenv.plugin.zsh
118+++ b/plugins/pipenv/pipenv.plugin.zsh
119@@ -23,7 +23,8 @@ _togglePipenvShell() {
120     fi
121   fi
122 }
123-chpwd_functions+=(_togglePipenvShell)
124+autoload -U add-zsh-hook
125+add-zsh-hook chpwd _togglePipenvShell
126 
127 # Aliases
128 alias pch="pipenv check"
129diff --git a/plugins/timer/timer.plugin.zsh b/plugins/timer/timer.plugin.zsh
130index 231134e7df60cbc47e3929b5d6a4a5640793875f..728377c5cc5cd99905dd68d8d452182879981ee5 100644
131--- a/plugins/timer/timer.plugin.zsh
132+++ b/plugins/timer/timer.plugin.zsh
133@@ -25,5 +25,6 @@ __timer_display_timer_precmd() {
134   fi
135 }
136 
137-preexec_functions+=(__timer_save_time_preexec)
138-precmd_functions+=(__timer_display_timer_precmd)
139+autoload -U add-zsh-hook
140+add-zsh-hook preexec __timer_save_time_preexec
141+add-zsh-hook precmd __timer_display_timer_precmd
142diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
143index e27c6bb76bda1b012a14a3fdcfda30e7a3ef89a7..2a4b43189518cc55593cf4e787f5d0f9feeb92e4 100644
144--- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
145+++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
146@@ -96,7 +96,6 @@ if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
147 
148   # Append workon_cwd to the chpwd_functions array, so it will be called on cd
149   # http://zsh.sourceforge.net/Doc/Release/Functions.html
150-  if ! (( $chpwd_functions[(I)workon_cwd] )); then
151-    chpwd_functions+=(workon_cwd)
152-  fi
153+  autoload -U add-zsh-hook
154+  add-zsh-hook chpwd workon_cwd
155 fi
156diff --git a/themes/pygmalion-virtualenv.zsh-theme b/themes/pygmalion-virtualenv.zsh-theme
157index ea28e125a18e1475565391b262538d0ba2714eb1..605e3d10ceed956b244ac02e10261a62f3cd5783 100644
158--- a/themes/pygmalion-virtualenv.zsh-theme
159+++ b/themes/pygmalion-virtualenv.zsh-theme
160@@ -28,7 +28,8 @@ prompt_setup_pygmalion(){
161   base_prompt_nocolor=$(echo "$base_prompt" | perl -pe "s/%\{[^}]+\}//g")
162   post_prompt_nocolor=$(echo "$post_prompt" | perl -pe "s/%\{[^}]+\}//g")
163 
164-  precmd_functions+=(prompt_pygmalion_precmd)
165+  autoload -U add-zsh-hook
166+  add-zsh-hook precmd prompt_pygmalion_precmd
167 }
168 
169 prompt_pygmalion_precmd(){
170@@ -46,5 +47,3 @@ prompt_pygmalion_precmd(){
171 }
172 
173 prompt_setup_pygmalion
174-
175-
176diff --git a/themes/pygmalion.zsh-theme b/themes/pygmalion.zsh-theme
177index 5f5fe7f9aea113baa33f7979cb18d505bc6b66ac..cd773e4a4591e0fcc0668e449b11d58f9735069e 100644
178--- a/themes/pygmalion.zsh-theme
179+++ b/themes/pygmalion.zsh-theme
180@@ -12,7 +12,8 @@ prompt_setup_pygmalion(){
181   base_prompt_nocolor=$(echo "$base_prompt" | perl -pe "s/%\{[^}]+\}//g")
182   post_prompt_nocolor=$(echo "$post_prompt" | perl -pe "s/%\{[^}]+\}//g")
183 
184-  precmd_functions+=(prompt_pygmalion_precmd)
185+  autoload -U add-zsh-hook
186+  add-zsh-hook precmd prompt_pygmalion_precmd
187 }
188 
189 prompt_pygmalion_precmd(){
190@@ -30,5 +31,3 @@ prompt_pygmalion_precmd(){
191 }
192 
193 prompt_setup_pygmalion
194-
195-