oh-my-zsh.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # ANSI formatting function (\033[<code>m)
  2. # 0: reset, 1: bold, 4: underline, 22: no bold, 24: no underline, 31: red, 33: yellow
  3. omz_f() {
  4. [ $# -gt 0 ] || return
  5. IFS=";" printf "\033[%sm" $*
  6. }
  7. # If stdout is not a terminal ignore all formatting
  8. [ -t 1 ] || omz_f() { :; }
  9. # Protect against non-zsh execution of Oh My Zsh (use POSIX syntax here)
  10. [ -n "$ZSH_VERSION" ] || {
  11. omz_ptree() {
  12. # Get process tree of the current process
  13. pid=$$; pids="$pid"
  14. while [ ${pid-0} -ne 1 ] && ppid=$(ps -e -o pid,ppid | awk "\$1 == $pid { print \$2 }"); do
  15. pids="$pids $pid"; pid=$ppid
  16. done
  17. # Show process tree
  18. case "$(uname)" in
  19. Linux) ps -o ppid,pid,command -f -p $pids 2>/dev/null ;;
  20. Darwin|*) ps -o ppid,pid,command -p $pids 2>/dev/null ;;
  21. esac
  22. # If ps command failed, try Busybox ps
  23. [ $? -eq 0 ] || ps -o ppid,pid,comm | awk "NR == 1 || index(\"$pids\", \$2) != 0"
  24. }
  25. {
  26. shell=$(ps -o pid,comm | awk "\$1 == $$ { print \$2 }")
  27. printf "$(omz_f 1 31)Error:$(omz_f 22) Oh My Zsh can't be loaded from: $(omz_f 1)${shell}$(omz_f 22). "
  28. printf "You need to run $(omz_f 1)zsh$(omz_f 22) instead.$(omz_f 0)\n"
  29. printf "$(omz_f 33)Here's the process tree:$(omz_f 22)\n\n"
  30. omz_ptree
  31. printf "$(omz_f 0)\n"
  32. } >&2
  33. return 1
  34. }
  35. # Check if in emulation mode, if so early return
  36. # https://github.com/ohmyzsh/ohmyzsh/issues/11686
  37. [[ "$(emulate)" = zsh ]] || {
  38. 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
  39. return 1
  40. }
  41. unset -f omz_f
  42. # If ZSH is not defined, use the current script's directory.
  43. [[ -n "$ZSH" ]] || export ZSH="${${(%):-%x}:a:h}"
  44. # Set ZSH_CUSTOM to the path where your custom config files
  45. # and plugins exists, or else we will use the default custom/
  46. [[ -n "$ZSH_CUSTOM" ]] || ZSH_CUSTOM="$ZSH/custom"
  47. # Set ZSH_CACHE_DIR to the path where cache files should be created
  48. # or else we will use the default cache/
  49. [[ -n "$ZSH_CACHE_DIR" ]] || ZSH_CACHE_DIR="$ZSH/cache"
  50. # Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
  51. if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
  52. ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
  53. fi
  54. # Create cache and completions dir and add to $fpath
  55. mkdir -p "$ZSH_CACHE_DIR/completions"
  56. (( ${fpath[(Ie)$ZSH_CACHE_DIR/completions]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
  57. # Check for updates on initial load...
  58. source "$ZSH/tools/check_for_upgrade.sh"
  59. # Initializes Oh My Zsh
  60. # add a function path
  61. fpath=($ZSH/{functions,completions} $ZSH_CUSTOM/{functions,completions} $fpath)
  62. # Load all stock functions (from $fpath files) called below.
  63. autoload -U compaudit compinit zrecompile
  64. is_plugin() {
  65. local base_dir=$1
  66. local name=$2
  67. builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
  68. || builtin test -f $base_dir/plugins/$name/_$name
  69. }
  70. # Add all defined plugins to fpath. This must be done
  71. # before running compinit.
  72. for plugin ($plugins); do
  73. if is_plugin "$ZSH_CUSTOM" "$plugin"; then
  74. fpath=("$ZSH_CUSTOM/plugins/$plugin" $fpath)
  75. elif is_plugin "$ZSH" "$plugin"; then
  76. fpath=("$ZSH/plugins/$plugin" $fpath)
  77. else
  78. echo "[oh-my-zsh] plugin '$plugin' not found"
  79. fi
  80. done
  81. # Figure out the SHORT hostname
  82. if [[ "$OSTYPE" = darwin* ]]; then
  83. # macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
  84. SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST="${HOST/.*/}"
  85. else
  86. SHORT_HOST="${HOST/.*/}"
  87. fi
  88. # Save the location of the current completion dump file.
  89. if [[ -z "$ZSH_COMPDUMP" ]]; then
  90. ZSH_COMPDUMP="${ZDOTDIR:-$HOME}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
  91. fi
  92. # Construct zcompdump OMZ metadata
  93. zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
  94. zcompdump_fpath="#omz fpath: $fpath"
  95. # Delete the zcompdump file if OMZ zcompdump metadata changed
  96. if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \
  97. || ! command grep -q -Fx "$zcompdump_fpath" "$ZSH_COMPDUMP" 2>/dev/null; then
  98. command rm -f "$ZSH_COMPDUMP"
  99. zcompdump_refresh=1
  100. fi
  101. if [[ "$ZSH_DISABLE_COMPFIX" != true ]]; then
  102. source "$ZSH/lib/compfix.zsh"
  103. # Load only from secure directories
  104. compinit -i -d "$ZSH_COMPDUMP"
  105. # If completion insecurities exist, warn the user
  106. handle_completion_insecurities &|
  107. else
  108. # If the user wants it, load from all found directories
  109. compinit -u -d "$ZSH_COMPDUMP"
  110. fi
  111. # Append zcompdump metadata if missing
  112. if (( $zcompdump_refresh )) \
  113. || ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null; then
  114. # Use `tee` in case the $ZSH_COMPDUMP filename is invalid, to silence the error
  115. # See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a7269#commitcomment-39003489
  116. tee -a "$ZSH_COMPDUMP" &>/dev/null <<EOF
  117. $zcompdump_revision
  118. $zcompdump_fpath
  119. EOF
  120. fi
  121. unset zcompdump_revision zcompdump_fpath zcompdump_refresh
  122. # zcompile the completion dump file if the .zwc is older or missing.
  123. if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then
  124. zrecompile -q -p "$ZSH_COMPDUMP"
  125. command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock"
  126. fi
  127. _omz_source() {
  128. local context filepath="$1"
  129. # Construct zstyle context based on path
  130. case "$filepath" in
  131. lib/*) context="lib:${filepath:t:r}" ;; # :t = lib_name.zsh, :r = lib_name
  132. plugins/*) context="plugins:${filepath:h:t}" ;; # :h = plugins/plugin_name, :t = plugin_name
  133. esac
  134. local disable_aliases=0
  135. zstyle -T ":omz:${context}" aliases || disable_aliases=1
  136. # Back up alias names prior to sourcing
  137. local -A aliases_pre galiases_pre
  138. if (( disable_aliases )); then
  139. aliases_pre=("${(@kv)aliases}")
  140. galiases_pre=("${(@kv)galiases}")
  141. fi
  142. # Source file from $ZSH_CUSTOM if it exists, otherwise from $ZSH
  143. if [[ -f "$ZSH_CUSTOM/$filepath" ]]; then
  144. source "$ZSH_CUSTOM/$filepath"
  145. elif [[ -f "$ZSH/$filepath" ]]; then
  146. source "$ZSH/$filepath"
  147. fi
  148. # Unset all aliases that don't appear in the backed up list of aliases
  149. if (( disable_aliases )); then
  150. if (( #aliases_pre )); then
  151. aliases=("${(@kv)aliases_pre}")
  152. else
  153. (( #aliases )) && unalias "${(@k)aliases}"
  154. fi
  155. if (( #galiases_pre )); then
  156. galiases=("${(@kv)galiases_pre}")
  157. else
  158. (( #galiases )) && unalias "${(@k)galiases}"
  159. fi
  160. fi
  161. }
  162. # Load all of the lib files in ~/oh-my-zsh/lib that end in .zsh
  163. # TIP: Add files you don't want in git to .gitignore
  164. for lib_file ("$ZSH"/lib/*.zsh); do
  165. _omz_source "lib/${lib_file:t}"
  166. done
  167. unset lib_file
  168. # Load all of the plugins that were defined in ~/.zshrc
  169. for plugin ($plugins); do
  170. _omz_source "plugins/$plugin/$plugin.plugin.zsh"
  171. done
  172. unset plugin
  173. # Load all of your custom configurations from custom/
  174. for config_file ("$ZSH_CUSTOM"/*.zsh(N)); do
  175. source "$config_file"
  176. done
  177. unset config_file
  178. # Load the theme
  179. is_theme() {
  180. local base_dir=$1
  181. local name=$2
  182. builtin test -f $base_dir/$name.zsh-theme
  183. }
  184. if [[ -n "$ZSH_THEME" ]]; then
  185. if is_theme "$ZSH_CUSTOM" "$ZSH_THEME"; then
  186. source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
  187. elif is_theme "$ZSH_CUSTOM/themes" "$ZSH_THEME"; then
  188. source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
  189. elif is_theme "$ZSH/themes" "$ZSH_THEME"; then
  190. source "$ZSH/themes/$ZSH_THEME.zsh-theme"
  191. else
  192. echo "[oh-my-zsh] theme '$ZSH_THEME' not found"
  193. fi
  194. fi
  195. # set completion colors to be the same as `ls`, after theme has been loaded
  196. [[ -z "$LS_COLORS" ]] || zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"