Commit 1ba0af65 authored by Jacob Tomaw's avatar Jacob Tomaw Committed by Marc Cornellà
Browse files

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: default avatarMarc Cornellà <marc.cornella@live.com>
parent d4f32e9f
...@@ -4,3 +4,4 @@ custom/ ...@@ -4,3 +4,4 @@ custom/
# temp files directories # temp files directories
cache/ cache/
log/ log/
*.swp
...@@ -75,8 +75,9 @@ function omz_termsupport_preexec { ...@@ -75,8 +75,9 @@ function omz_termsupport_preexec {
title '$CMD' '%100>...>$LINE%<<' title '$CMD' '%100>...>$LINE%<<'
} }
precmd_functions+=(omz_termsupport_precmd) autoload -U add-zsh-hook
preexec_functions+=(omz_termsupport_preexec) add-zsh-hook precmd omz_termsupport_precmd
add-zsh-hook preexec omz_termsupport_preexec
# Keep Apple Terminal.app's current working directory updated # Keep Apple Terminal.app's current working directory updated
...@@ -99,7 +100,7 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then ...@@ -99,7 +100,7 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
} }
# Use a precmd hook instead of a chpwd hook to avoid contaminating output # Use a precmd hook instead of a chpwd hook to avoid contaminating output
precmd_functions+=(update_terminalapp_cwd) add-zsh-hook precmd update_terminalapp_cwd
# Run once to get initial cwd set # Run once to get initial cwd set
update_terminalapp_cwd update_terminalapp_cwd
fi fi
...@@ -4,7 +4,7 @@ alias-finder() { ...@@ -4,7 +4,7 @@ alias-finder() {
case $i in case $i in
-e|--exact) exact=true;; -e|--exact) exact=true;;
-l|--longer) longer=true;; -l|--longer) longer=true;;
*) *)
if [[ -z $cmd ]]; then if [[ -z $cmd ]]; then
cmd=$i cmd=$i
else else
...@@ -43,4 +43,5 @@ preexec_alias-finder() { ...@@ -43,4 +43,5 @@ preexec_alias-finder() {
fi fi
} }
preexec_functions+=(preexec_alias-finder) autoload -U add-zsh-hook
add-zsh-hook preexec preexec_alias-finder
...@@ -53,7 +53,8 @@ function push_future() { ...@@ -53,7 +53,8 @@ function push_future() {
} }
# Called by zsh when directory changes # Called by zsh when directory changes
chpwd_functions+=(chpwd_dirhistory) autoload -U add-zsh-hook
add-zsh-hook chpwd chpwd_dirhistory
function chpwd_dirhistory() { function chpwd_dirhistory() {
push_past $PWD push_past $PWD
# If DIRHISTORY_CD is not set... # If DIRHISTORY_CD is not set...
......
...@@ -11,7 +11,8 @@ if [[ -f ${dirstack_file} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then ...@@ -11,7 +11,8 @@ if [[ -f ${dirstack_file} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then
[[ -d $dirstack[1] ]] && cd $dirstack[1] && cd $OLDPWD [[ -d $dirstack[1] ]] && cd $dirstack[1] && cd $OLDPWD
fi fi
chpwd_functions+=(chpwd_dirpersist) autoload -U add-zsh-hook
add-zsh-hook chpwd chpwd_dirpersist
chpwd_dirpersist() { chpwd_dirpersist() {
if (( $DIRSTACKSIZE <= 0 )) || [[ -z $dirstack_file ]]; then return; fi if (( $DIRSTACKSIZE <= 0 )) || [[ -z $dirstack_file ]]; then return; fi
local -ax my_stack local -ax my_stack
......
...@@ -20,9 +20,10 @@ function precmd_update_git_vars() { ...@@ -20,9 +20,10 @@ function precmd_update_git_vars() {
fi fi
} }
chpwd_functions+=(chpwd_update_git_vars) autoload -U add-zsh-hook
precmd_functions+=(precmd_update_git_vars) add-zsh-hook chpwd chpwd_update_git_vars
preexec_functions+=(preexec_update_git_vars) add-zsh-hook precmd precmd_update_git_vars
add-zsh-hook preexec preexec_update_git_vars
## Function definitions ## Function definitions
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
typeset -g ZSH_LAST_WORKING_DIRECTORY typeset -g ZSH_LAST_WORKING_DIRECTORY
# Updates the last directory once directory is changed # Updates the last directory once directory is changed
chpwd_functions+=(chpwd_last_working_dir) autoload -U add-zsh-hook
add-zsh-hook chpwd chpwd_last_working_dir
chpwd_last_working_dir() { chpwd_last_working_dir() {
if [ "$ZSH_SUBSHELL" = 0 ]; then if [ "$ZSH_SUBSHELL" = 0 ]; then
local cache_file="$ZSH_CACHE_DIR/last-working-dir" local cache_file="$ZSH_CACHE_DIR/last-working-dir"
......
...@@ -23,7 +23,8 @@ _togglePipenvShell() { ...@@ -23,7 +23,8 @@ _togglePipenvShell() {
fi fi
fi fi
} }
chpwd_functions+=(_togglePipenvShell) autoload -U add-zsh-hook
add-zsh-hook chpwd _togglePipenvShell
# Aliases # Aliases
alias pch="pipenv check" alias pch="pipenv check"
......
...@@ -25,5 +25,6 @@ __timer_display_timer_precmd() { ...@@ -25,5 +25,6 @@ __timer_display_timer_precmd() {
fi fi
} }
preexec_functions+=(__timer_save_time_preexec) autoload -U add-zsh-hook
precmd_functions+=(__timer_display_timer_precmd) add-zsh-hook preexec __timer_save_time_preexec
add-zsh-hook precmd __timer_display_timer_precmd
...@@ -96,7 +96,6 @@ if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then ...@@ -96,7 +96,6 @@ if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
# Append workon_cwd to the chpwd_functions array, so it will be called on cd # Append workon_cwd to the chpwd_functions array, so it will be called on cd
# http://zsh.sourceforge.net/Doc/Release/Functions.html # http://zsh.sourceforge.net/Doc/Release/Functions.html
if ! (( $chpwd_functions[(I)workon_cwd] )); then autoload -U add-zsh-hook
chpwd_functions+=(workon_cwd) add-zsh-hook chpwd workon_cwd
fi
fi fi
...@@ -28,7 +28,8 @@ prompt_setup_pygmalion(){ ...@@ -28,7 +28,8 @@ prompt_setup_pygmalion(){
base_prompt_nocolor=$(echo "$base_prompt" | perl -pe "s/%\{[^}]+\}//g") base_prompt_nocolor=$(echo "$base_prompt" | perl -pe "s/%\{[^}]+\}//g")
post_prompt_nocolor=$(echo "$post_prompt" | perl -pe "s/%\{[^}]+\}//g") post_prompt_nocolor=$(echo "$post_prompt" | perl -pe "s/%\{[^}]+\}//g")
precmd_functions+=(prompt_pygmalion_precmd) autoload -U add-zsh-hook
add-zsh-hook precmd prompt_pygmalion_precmd
} }
prompt_pygmalion_precmd(){ prompt_pygmalion_precmd(){
...@@ -46,5 +47,3 @@ prompt_pygmalion_precmd(){ ...@@ -46,5 +47,3 @@ prompt_pygmalion_precmd(){
} }
prompt_setup_pygmalion prompt_setup_pygmalion
...@@ -12,7 +12,8 @@ prompt_setup_pygmalion(){ ...@@ -12,7 +12,8 @@ prompt_setup_pygmalion(){
base_prompt_nocolor=$(echo "$base_prompt" | perl -pe "s/%\{[^}]+\}//g") base_prompt_nocolor=$(echo "$base_prompt" | perl -pe "s/%\{[^}]+\}//g")
post_prompt_nocolor=$(echo "$post_prompt" | perl -pe "s/%\{[^}]+\}//g") post_prompt_nocolor=$(echo "$post_prompt" | perl -pe "s/%\{[^}]+\}//g")
precmd_functions+=(prompt_pygmalion_precmd) autoload -U add-zsh-hook
add-zsh-hook precmd prompt_pygmalion_precmd
} }
prompt_pygmalion_precmd(){ prompt_pygmalion_precmd(){
...@@ -30,5 +31,3 @@ prompt_pygmalion_precmd(){ ...@@ -30,5 +31,3 @@ prompt_pygmalion_precmd(){
} }
prompt_setup_pygmalion prompt_setup_pygmalion
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment