oh-my-zsh.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. _omz_source() {
  125. local context filepath="$1"
  126. # Construct zstyle context based on path
  127. case "$filepath" in
  128. lib/*) context="lib:${filepath:t:r}" ;; # :t = lib_name.zsh, :r = lib_name
  129. plugins/*) context="plugins:${filepath:h:t}" ;; # :h = plugins/plugin_name, :t = plugin_name
  130. esac
  131. local disable_aliases=0
  132. zstyle -T ":omz:${context}" aliases || disable_aliases=1
  133. # Back up alias names prior to sourcing
  134. local -A aliases_pre galiases_pre
  135. if (( disable_aliases )); then
  136. aliases_pre=("${(@kv)aliases}")
  137. galiases_pre=("${(@kv)galiases}")
  138. fi
  139. # Source file from $ZSH_CUSTOM if it exists, otherwise from $ZSH
  140. if [[ -f "$ZSH_CUSTOM/$filepath" ]]; then
  141. source "$ZSH_CUSTOM/$filepath"
  142. elif [[ -f "$ZSH/$filepath" ]]; then
  143. source "$ZSH/$filepath"
  144. fi
  145. # Unset all aliases that don't appear in the backed up list of aliases
  146. if (( disable_aliases )); then
  147. if (( #aliases_pre )); then
  148. aliases=("${(@kv)aliases_pre}")
  149. else
  150. (( #aliases )) && unalias "${(@k)aliases}"
  151. fi
  152. if (( #galiases_pre )); then
  153. galiases=("${(@kv)galiases_pre}")
  154. else
  155. (( #galiases )) && unalias "${(@k)galiases}"
  156. fi
  157. fi
  158. }
  159. # Load all of the lib files in ~/oh-my-zsh/lib that end in .zsh
  160. # TIP: Add files you don't want in git to .gitignore
  161. for lib_file ("$ZSH"/lib/*.zsh); do
  162. _omz_source "lib/${lib_file:t}"
  163. done
  164. unset lib_file
  165. # Load all of the plugins that were defined in ~/.zshrc
  166. for plugin ($plugins); do
  167. _omz_source "plugins/$plugin/$plugin.plugin.zsh"
  168. done
  169. unset plugin
  170. # Load all of your custom configurations from custom/
  171. for config_file ("$ZSH_CUSTOM"/*.zsh(N)); do
  172. source "$config_file"
  173. done
  174. unset config_file
  175. # Load the theme
  176. is_theme() {
  177. local base_dir=$1
  178. local name=$2
  179. builtin test -f $base_dir/$name.zsh-theme
  180. }
  181. if [[ -n "$ZSH_THEME" ]]; then
  182. if is_theme "$ZSH_CUSTOM" "$ZSH_THEME"; then
  183. source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
  184. elif is_theme "$ZSH_CUSTOM/themes" "$ZSH_THEME"; then
  185. source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
  186. elif is_theme "$ZSH/themes" "$ZSH_THEME"; then
  187. source "$ZSH/themes/$ZSH_THEME.zsh-theme"
  188. else
  189. echo "[oh-my-zsh] theme '$ZSH_THEME' not found"
  190. fi
  191. fi
  192. # set completion colors to be the same as `ls`, after theme has been loaded
  193. [[ -z "$LS_COLORS" ]] || zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"