oh-my-zsh.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # Protect against non-zsh execution of Oh My Zsh (use POSIX syntax here)
  2. [ -n "$ZSH_VERSION" ] || {
  3. # ANSI formatting function (\033[<code>m)
  4. # 0: reset, 1: bold, 4: underline, 22: no bold, 24: no underline, 31: red, 33: yellow
  5. omz_f() {
  6. [ $# -gt 0 ] || return
  7. IFS=";" printf "\033[%sm" $*
  8. }
  9. # If stdout is not a terminal ignore all formatting
  10. [ -t 1 ] || omz_f() { :; }
  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. # If ZSH is not defined, use the current script's directory.
  36. [[ -z "$ZSH" ]] && export ZSH="${${(%):-%x}:a:h}"
  37. # Set ZSH_CACHE_DIR to the path where cache files should be created
  38. # or else we will use the default cache/
  39. if [[ -z "$ZSH_CACHE_DIR" ]]; then
  40. ZSH_CACHE_DIR="$ZSH/cache"
  41. fi
  42. # Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
  43. if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
  44. ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
  45. fi
  46. # Create cache and completions dir and add to $fpath
  47. mkdir -p "$ZSH_CACHE_DIR/completions"
  48. (( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
  49. # Check for updates on initial load...
  50. if [[ "$DISABLE_AUTO_UPDATE" != true ]]; then
  51. source "$ZSH/tools/check_for_upgrade.sh"
  52. fi
  53. # Initializes Oh My Zsh
  54. # add a function path
  55. fpath=("$ZSH/functions" "$ZSH/completions" $fpath)
  56. # Load all stock functions (from $fpath files) called below.
  57. autoload -U compaudit compinit zrecompile
  58. # Set ZSH_CUSTOM to the path where your custom config files
  59. # and plugins exists, or else we will use the default custom/
  60. if [[ -z "$ZSH_CUSTOM" ]]; then
  61. ZSH_CUSTOM="$ZSH/custom"
  62. fi
  63. is_plugin() {
  64. local base_dir=$1
  65. local name=$2
  66. builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
  67. || builtin test -f $base_dir/plugins/$name/_$name
  68. }
  69. # Add all defined plugins to fpath. This must be done
  70. # before running compinit.
  71. for plugin ($plugins); do
  72. if is_plugin "$ZSH_CUSTOM" "$plugin"; then
  73. fpath=("$ZSH_CUSTOM/plugins/$plugin" $fpath)
  74. elif is_plugin "$ZSH" "$plugin"; then
  75. fpath=("$ZSH/plugins/$plugin" $fpath)
  76. else
  77. echo "[oh-my-zsh] plugin '$plugin' not found"
  78. fi
  79. done
  80. # Figure out the SHORT hostname
  81. if [[ "$OSTYPE" = darwin* ]]; then
  82. # macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
  83. SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST="${HOST/.*/}"
  84. else
  85. SHORT_HOST="${HOST/.*/}"
  86. fi
  87. # Save the location of the current completion dump file.
  88. if [[ -z "$ZSH_COMPDUMP" ]]; then
  89. ZSH_COMPDUMP="${ZDOTDIR:-$HOME}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
  90. fi
  91. # Construct zcompdump OMZ metadata
  92. zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
  93. zcompdump_fpath="#omz fpath: $fpath"
  94. # Delete the zcompdump file if OMZ zcompdump metadata changed
  95. if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \
  96. || ! command grep -q -Fx "$zcompdump_fpath" "$ZSH_COMPDUMP" 2>/dev/null; then
  97. command rm -f "$ZSH_COMPDUMP"
  98. zcompdump_refresh=1
  99. fi
  100. if [[ "$ZSH_DISABLE_COMPFIX" != true ]]; then
  101. source "$ZSH/lib/compfix.zsh"
  102. # Load only from secure directories
  103. compinit -i -d "$ZSH_COMPDUMP"
  104. # If completion insecurities exist, warn the user
  105. handle_completion_insecurities &|
  106. else
  107. # If the user wants it, load from all found directories
  108. compinit -u -d "$ZSH_COMPDUMP"
  109. fi
  110. # Append zcompdump metadata if missing
  111. if (( $zcompdump_refresh )) \
  112. || ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null; then
  113. # Use `tee` in case the $ZSH_COMPDUMP filename is invalid, to silence the error
  114. # See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a7269#commitcomment-39003489
  115. tee -a "$ZSH_COMPDUMP" &>/dev/null <<EOF
  116. $zcompdump_revision
  117. $zcompdump_fpath
  118. EOF
  119. fi
  120. unset zcompdump_revision zcompdump_fpath zcompdump_refresh
  121. # zcompile the completion dump file if the .zwc is older or missing.
  122. if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then
  123. zrecompile -q -p "$ZSH_COMPDUMP"
  124. command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock"
  125. fi
  126. # Load all of the config files in ~/oh-my-zsh that end in .zsh
  127. # TIP: Add files you don't want in git to .gitignore
  128. for config_file ("$ZSH"/lib/*.zsh); do
  129. custom_config_file="$ZSH_CUSTOM/lib/${config_file:t}"
  130. [[ -f "$custom_config_file" ]] && config_file="$custom_config_file"
  131. source "$config_file"
  132. done
  133. unset custom_config_file
  134. # Load all of the plugins that were defined in ~/.zshrc
  135. for plugin ($plugins); do
  136. if [[ -f "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh" ]]; then
  137. source "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh"
  138. elif [[ -f "$ZSH/plugins/$plugin/$plugin.plugin.zsh" ]]; then
  139. source "$ZSH/plugins/$plugin/$plugin.plugin.zsh"
  140. fi
  141. done
  142. unset plugin
  143. # Load all of your custom configurations from custom/
  144. for config_file ("$ZSH_CUSTOM"/*.zsh(N)); do
  145. source "$config_file"
  146. done
  147. unset config_file
  148. # Load the theme
  149. is_theme() {
  150. local base_dir=$1
  151. local name=$2
  152. builtin test -f $base_dir/$name.zsh-theme
  153. }
  154. if [[ -n "$ZSH_THEME" ]]; then
  155. if is_theme "$ZSH_CUSTOM" "$ZSH_THEME"; then
  156. source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
  157. elif is_theme "$ZSH_CUSTOM/themes" "$ZSH_THEME"; then
  158. source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
  159. elif is_theme "$ZSH/themes" "$ZSH_THEME"; then
  160. source "$ZSH/themes/$ZSH_THEME.zsh-theme"
  161. else
  162. echo "[oh-my-zsh] theme '$ZSH_THEME' not found"
  163. fi
  164. fi