oh-my-zsh.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. source "$ZSH/tools/check_for_upgrade.sh"
  51. # Initializes Oh My Zsh
  52. # add a function path
  53. fpath=("$ZSH/functions" "$ZSH/completions" $fpath)
  54. # Load all stock functions (from $fpath files) called below.
  55. autoload -U compaudit compinit zrecompile
  56. # Set ZSH_CUSTOM to the path where your custom config files
  57. # and plugins exists, or else we will use the default custom/
  58. if [[ -z "$ZSH_CUSTOM" ]]; then
  59. ZSH_CUSTOM="$ZSH/custom"
  60. fi
  61. is_plugin() {
  62. local base_dir=$1
  63. local name=$2
  64. builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
  65. || builtin test -f $base_dir/plugins/$name/_$name
  66. }
  67. # Add all defined plugins to fpath. This must be done
  68. # before running compinit.
  69. for plugin ($plugins); do
  70. if is_plugin "$ZSH_CUSTOM" "$plugin"; then
  71. fpath=("$ZSH_CUSTOM/plugins/$plugin" $fpath)
  72. elif is_plugin "$ZSH" "$plugin"; then
  73. fpath=("$ZSH/plugins/$plugin" $fpath)
  74. else
  75. echo "[oh-my-zsh] plugin '$plugin' not found"
  76. fi
  77. done
  78. # Figure out the SHORT hostname
  79. if [[ "$OSTYPE" = darwin* ]]; then
  80. # macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
  81. SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST="${HOST/.*/}"
  82. else
  83. SHORT_HOST="${HOST/.*/}"
  84. fi
  85. # Save the location of the current completion dump file.
  86. if [[ -z "$ZSH_COMPDUMP" ]]; then
  87. ZSH_COMPDUMP="${ZDOTDIR:-$HOME}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
  88. fi
  89. # Construct zcompdump OMZ metadata
  90. zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
  91. zcompdump_fpath="#omz fpath: $fpath"
  92. # Delete the zcompdump file if OMZ zcompdump metadata changed
  93. if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \
  94. || ! command grep -q -Fx "$zcompdump_fpath" "$ZSH_COMPDUMP" 2>/dev/null; then
  95. command rm -f "$ZSH_COMPDUMP"
  96. zcompdump_refresh=1
  97. fi
  98. if [[ "$ZSH_DISABLE_COMPFIX" != true ]]; then
  99. source "$ZSH/lib/compfix.zsh"
  100. # Load only from secure directories
  101. compinit -i -d "$ZSH_COMPDUMP"
  102. # If completion insecurities exist, warn the user
  103. handle_completion_insecurities &|
  104. else
  105. # If the user wants it, load from all found directories
  106. compinit -u -d "$ZSH_COMPDUMP"
  107. fi
  108. # Append zcompdump metadata if missing
  109. if (( $zcompdump_refresh )) \
  110. || ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null; then
  111. # Use `tee` in case the $ZSH_COMPDUMP filename is invalid, to silence the error
  112. # See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a7269#commitcomment-39003489
  113. tee -a "$ZSH_COMPDUMP" &>/dev/null <<EOF
  114. $zcompdump_revision
  115. $zcompdump_fpath
  116. EOF
  117. fi
  118. unset zcompdump_revision zcompdump_fpath zcompdump_refresh
  119. # zcompile the completion dump file if the .zwc is older or missing.
  120. if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then
  121. zrecompile -q -p "$ZSH_COMPDUMP"
  122. command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock"
  123. fi
  124. # Load all of the config files in ~/oh-my-zsh that end in .zsh
  125. # TIP: Add files you don't want in git to .gitignore
  126. for config_file ("$ZSH"/lib/*.zsh); do
  127. custom_config_file="$ZSH_CUSTOM/lib/${config_file:t}"
  128. [[ -f "$custom_config_file" ]] && config_file="$custom_config_file"
  129. source "$config_file"
  130. done
  131. unset custom_config_file
  132. # Load all of the plugins that were defined in ~/.zshrc
  133. for plugin ($plugins); do
  134. if [[ -f "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh" ]]; then
  135. source "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh"
  136. elif [[ -f "$ZSH/plugins/$plugin/$plugin.plugin.zsh" ]]; then
  137. source "$ZSH/plugins/$plugin/$plugin.plugin.zsh"
  138. fi
  139. done
  140. unset plugin
  141. # Load all of your custom configurations from custom/
  142. for config_file ("$ZSH_CUSTOM"/*.zsh(N)); do
  143. source "$config_file"
  144. done
  145. unset config_file
  146. # Load the theme
  147. is_theme() {
  148. local base_dir=$1
  149. local name=$2
  150. builtin test -f $base_dir/$name.zsh-theme
  151. }
  152. if [[ -n "$ZSH_THEME" ]]; then
  153. if is_theme "$ZSH_CUSTOM" "$ZSH_THEME"; then
  154. source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
  155. elif is_theme "$ZSH_CUSTOM/themes" "$ZSH_THEME"; then
  156. source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
  157. elif is_theme "$ZSH/themes" "$ZSH_THEME"; then
  158. source "$ZSH/themes/$ZSH_THEME.zsh-theme"
  159. else
  160. echo "[oh-my-zsh] theme '$ZSH_THEME' not found"
  161. fi
  162. fi
  163. # set completion colors to be the same as `ls`, after theme has been loaded
  164. [[ -z "$LS_COLORS" ]] || zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"