From 1ad167dfac325a9f92e0693c70d0ab3f7c4c574b Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 3 Apr 2023 23:14:36 +0200 Subject: feat(init)!: allow turning off aliases for libs and plugins (#11550) BREAKING CHANGE: the previous zstyle setting to disable `lib/directories.zsh` aliases has been changed to the new syntax: `zstyle ':omz:lib:directories' aliases no`. See https://github.com/ohmyzsh/ohmyzsh#skip-aliases to see other ways you can use this setting. Co-authored-by: Carlo Sala --- oh-my-zsh.sh | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 363cfca8b..20d2e354c 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -146,22 +146,51 @@ if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock" fi +_omz_source() { + local context filepath="$1" + + # Construct zstyle context based on path + case "$filepath" in + lib/*) context="lib:${filepath:t:r}" ;; # :t = lib_name.zsh, :r = lib_name + plugins/*) context="plugins:${filepath:h2:t}" ;; # :h2 = plugins/plugin_name, :t = plugin_name + esac + + local disable_aliases=0 + zstyle -T ":omz:${context}" aliases || disable_aliases=1 + + # Back up alias names prior to sourcing + local -a aliases_pre galiases_pre + if (( disable_aliases )); then + aliases_pre=("${(@k)aliases}") + galiases_pre=("${(@k)galiases}") + fi + + # Source file from $ZSH_CUSTOM if it exists, otherwise from $ZSH + if [[ -f "$ZSH_CUSTOM/$filepath" ]]; then + source "$ZSH_CUSTOM/$filepath" + elif [[ -f "$ZSH/$filepath" ]]; then + source "$ZSH/$filepath" + fi + + # Unset all aliases that don't appear in the backed up list of aliases + if (( disable_aliases )); then + local -a disabled + # ${var:|array} gets the list of items in var not in array + disabled=("${(@k)aliases:|aliases_pre}" "${(@k)galiases:|galiases_pre}") + (( $#disabled == 0 )) || unalias "${(@)disabled}" + fi +} + # Load all of the config files in ~/oh-my-zsh that end in .zsh # TIP: Add files you don't want in git to .gitignore for config_file ("$ZSH"/lib/*.zsh); do - custom_config_file="$ZSH_CUSTOM/lib/${config_file:t}" - [[ -f "$custom_config_file" ]] && config_file="$custom_config_file" - source "$config_file" + _omz_source "${config_file:t2}" done unset custom_config_file # Load all of the plugins that were defined in ~/.zshrc for plugin ($plugins); do - if [[ -f "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh" ]]; then - source "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh" - elif [[ -f "$ZSH/plugins/$plugin/$plugin.plugin.zsh" ]]; then - source "$ZSH/plugins/$plugin/$plugin.plugin.zsh" - fi + _omz_source "plugins/$plugin/$plugin.plugin.zsh" done unset plugin -- cgit v1.2.3-70-g09d2 From 9233ef75f28423d147a2f9f64a45a77b1886ee5d Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 3 Apr 2023 23:36:44 +0200 Subject: fix(init): don't use digits in parameter modifiers for compatibility (#11598) Digit modifiers were introduced in zsh 5.7.1 [1]. This commit uses readily available alternatives for backwards compatibility. [1] https://github.com/zsh-users/zsh/commit/b8dc5a7f6d Fixes #11598 --- oh-my-zsh.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 20d2e354c..e047d4834 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -151,8 +151,8 @@ _omz_source() { # Construct zstyle context based on path case "$filepath" in - lib/*) context="lib:${filepath:t:r}" ;; # :t = lib_name.zsh, :r = lib_name - plugins/*) context="plugins:${filepath:h2:t}" ;; # :h2 = plugins/plugin_name, :t = plugin_name + lib/*) context="lib:${filepath:t:r}" ;; # :t = lib_name.zsh, :r = lib_name + plugins/*) context="plugins:${filepath:h:t}" ;; # :h = plugins/plugin_name, :t = plugin_name esac local disable_aliases=0 @@ -184,7 +184,7 @@ _omz_source() { # Load all of the config files in ~/oh-my-zsh that end in .zsh # TIP: Add files you don't want in git to .gitignore for config_file ("$ZSH"/lib/*.zsh); do - _omz_source "${config_file:t2}" + _omz_source "lib/${config_file:t}" done unset custom_config_file -- cgit v1.2.3-70-g09d2 From b22593cf179bae479ab32700c5a7b94d3053762d Mon Sep 17 00:00:00 2001 From: Carlo Sala Date: Mon, 1 May 2023 22:21:01 +0200 Subject: fix(init): avoid overwriting existing aliases Fix regression introduced in #11550. If an existing alias was present in the moment of sourcing, and oh-my-zsh aliases were disabled for that file, it'd be overwritten aswell. See #11658. --- oh-my-zsh.sh | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index e047d4834..a577c1f41 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -159,10 +159,10 @@ _omz_source() { zstyle -T ":omz:${context}" aliases || disable_aliases=1 # Back up alias names prior to sourcing - local -a aliases_pre galiases_pre + local -A aliases_pre galiases_pre if (( disable_aliases )); then - aliases_pre=("${(@k)aliases}") - galiases_pre=("${(@k)galiases}") + aliases_pre=("${(@kv)aliases}") + galiases_pre=("${(@kv)galiases}") fi # Source file from $ZSH_CUSTOM if it exists, otherwise from $ZSH @@ -174,10 +174,16 @@ _omz_source() { # Unset all aliases that don't appear in the backed up list of aliases if (( disable_aliases )); then - local -a disabled - # ${var:|array} gets the list of items in var not in array - disabled=("${(@k)aliases:|aliases_pre}" "${(@k)galiases:|galiases_pre}") - (( $#disabled == 0 )) || unalias "${(@)disabled}" + if (( #aliases_pre )); then + aliases=("${(@kv)aliases_pre}") + else + (( #aliases )) && unalias "${(@k)aliases}" + fi + if (( #galiases_pre )); then + galiases=("${(@kv)galiases_pre}") + else + (( #galiases )) && unalias "${(@k)galiases}" + fi fi } -- cgit v1.2.3-70-g09d2 From dfe2f04de7e839ae0a9757c37a26b9d8710aa372 Mon Sep 17 00:00:00 2001 From: LuckyWindsck Date: Tue, 22 Aug 2023 01:09:02 +0900 Subject: refactor(init): rename variable (#11851) --- oh-my-zsh.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index a577c1f41..40f13f37e 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -187,12 +187,12 @@ _omz_source() { fi } -# Load all of the config files in ~/oh-my-zsh that end in .zsh +# Load all of the lib files in ~/oh-my-zsh/lib that end in .zsh # TIP: Add files you don't want in git to .gitignore -for config_file ("$ZSH"/lib/*.zsh); do - _omz_source "lib/${config_file:t}" +for lib_file ("$ZSH"/lib/*.zsh); do + _omz_source "lib/${lib_file:t}" done -unset custom_config_file +unset lib_file # Load all of the plugins that were defined in ~/.zshrc for plugin ($plugins); do -- cgit v1.2.3-70-g09d2 From 30f0d591881713c4efd1482511943abca5103927 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 4 Sep 2023 19:32:38 +0200 Subject: fix(init): exit gracefully if on non-zsh emulation mode (#11874) Fixes #11686 --- oh-my-zsh.sh | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 40f13f37e..137ca3b6f 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -1,14 +1,14 @@ +# ANSI formatting function (\033[m) +# 0: reset, 1: bold, 4: underline, 22: no bold, 24: no underline, 31: red, 33: yellow +omz_f() { + [ $# -gt 0 ] || return + IFS=";" printf "\033[%sm" $* +} +# If stdout is not a terminal ignore all formatting +[ -t 1 ] || omz_f() { :; } + # Protect against non-zsh execution of Oh My Zsh (use POSIX syntax here) [ -n "$ZSH_VERSION" ] || { - # ANSI formatting function (\033[m) - # 0: reset, 1: bold, 4: underline, 22: no bold, 24: no underline, 31: red, 33: yellow - omz_f() { - [ $# -gt 0 ] || return - IFS=";" printf "\033[%sm" $* - } - # If stdout is not a terminal ignore all formatting - [ -t 1 ] || omz_f() { :; } - omz_ptree() { # Get process tree of the current process pid=$$; pids="$pid" @@ -38,6 +38,15 @@ return 1 } +# Check if in emulation mode, if so early return +# https://github.com/ohmyzsh/ohmyzsh/issues/11686 +[[ "$(emulate)" = zsh ]] || { + printf "$(omz_f 1 31)Error:$(omz_f 22) Oh My Zsh can't be loaded in \`$(emulate)\` emulation mode.$(omz_f 0)\n" >&2 + return 1 +} + +unset -f omz_f + # If ZSH is not defined, use the current script's directory. [[ -z "$ZSH" ]] && export ZSH="${${(%):-%x}:a:h}" -- cgit v1.2.3-70-g09d2