diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2023-11-04 18:38:46 -0700 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2023-11-04 18:38:46 -0700 |
commit | 4d908094fdc2a0c0e9a0a072eba213fab7adef43 (patch) | |
tree | 7c17e70bcdeebbe96c84d849bdf17882007480d8 /oh-my-zsh.sh | |
parent | 4b0bbc0b263a150eb9a9b59f196914629be06a9b (diff) | |
parent | 632ed413a9ce62747ded83d7736491b081be4b49 (diff) | |
download | zsh-4d908094fdc2a0c0e9a0a072eba213fab7adef43.tar.gz zsh-4d908094fdc2a0c0e9a0a072eba213fab7adef43.tar.bz2 zsh-4d908094fdc2a0c0e9a0a072eba213fab7adef43.zip |
Diffstat (limited to 'oh-my-zsh.sh')
-rw-r--r-- | oh-my-zsh.sh | 84 |
1 files changed, 64 insertions, 20 deletions
diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 363cfca8b..137ca3b6f 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -1,14 +1,14 @@ +# ANSI formatting function (\033[<code>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[<code>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}" @@ -146,22 +155,57 @@ if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock" fi -# Load all of the config files in ~/oh-my-zsh that end in .zsh +_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:h:t}" ;; # :h = 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=("${(@kv)aliases}") + galiases_pre=("${(@kv)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 + 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 +} + +# 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 - custom_config_file="$ZSH_CUSTOM/lib/${config_file:t}" - [[ -f "$custom_config_file" ]] && config_file="$custom_config_file" - source "$config_file" +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 - 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 |