oh-my-zsh.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. [[ -z "$ZSH" ]] && export ZSH="${${(%):-%x}:a:h}"
  44. # Set ZSH_CACHE_DIR to the path where cache files should be created
  45. # or else we will use the default cache/
  46. if [[ -z "$ZSH_CACHE_DIR" ]]; then
  47. ZSH_CACHE_DIR="$ZSH/cache"
  48. fi
  49. # Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
  50. if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
  51. ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
  52. fi
  53. # Create cache and completions dir and add to $fpath
  54. mkdir -p "$ZSH_CACHE_DIR/completions"
  55. (( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
  56. # Check for updates on initial load...
  57. source "$ZSH/tools/check_for_upgrade.sh"
  58. # Initializes Oh My Zsh
  59. # add a function path
  60. fpath=("$ZSH/functions" "$ZSH/completions" $fpath)
  61. # Load all stock functions (from $fpath files) called below.
  62. autoload -U compaudit compinit zrecompile
  63. # Set ZSH_CUSTOM to the path where your custom config files
  64. # and plugins exists, or else we will use the default custom/
  65. if [[ -z "$ZSH_CUSTOM" ]]; then
  66. ZSH_CUSTOM="$ZSH/custom"
  67. fi
  68. is_plugin() {
  69. local base_dir=$1
  70. local name=$2
  71. builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
  72. || builtin test -f $base_dir/plugins/$name/_$name
  73. }
  74. # Add all defined plugins to fpath. This must be done
  75. # before running compinit.
  76. for plugin ($plugins); do
  77. if is_plugin "$ZSH_CUSTOM" "$plugin"; then
  78. fpath=("$ZSH_CUSTOM/plugins/$plugin" $fpath)
  79. elif is_plugin "$ZSH" "$plugin"; then
  80. fpath=("$ZSH/plugins/$plugin" $fpath)
  81. else
  82. echo "[oh-my-zsh] plugin '$plugin' not found"
  83. fi
  84. done
  85. # Figure out the SHORT hostname
  86. if [[ "$OSTYPE" = darwin* ]]; then
  87. # macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
  88. SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST="${HOST/.*/}"
  89. else
  90. SHORT_HOST="${HOST/.*/}"
  91. fi
  92. # Save the location of the current completion dump file.
  93. if [[ -z "$ZSH_COMPDUMP" ]]; then
  94. ZSH_COMPDUMP="${ZDOTDIR:-$HOME}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
  95. fi
  96. # Construct zcompdump OMZ metadata
  97. zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
  98. zcompdump_fpath="#omz fpath: $fpath"
  99. # Delete the zcompdump file if OMZ zcompdump metadata changed
  100. if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \
  101. || ! command grep -q -Fx "$zcompdump_fpath" "$ZSH_COMPDUMP" 2>/dev/null; then
  102. command rm -f "$ZSH_COMPDUMP"
  103. zcompdump_refresh=1
  104. fi
  105. if [[ "$ZSH_DISABLE_COMPFIX" != true ]]; then
  106. source "$ZSH/lib/compfix.zsh"
  107. # Load only from secure directories
  108. compinit -i -d "$ZSH_COMPDUMP"
  109. # If completion insecurities exist, warn the user
  110. handle_completion_insecurities &|
  111. else
  112. # If the user wants it, load from all found directories
  113. compinit -u -d "$ZSH_COMPDUMP"
  114. fi
  115. # Append zcompdump metadata if missing
  116. if (( $zcompdump_refresh )) \
  117. || ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null; then
  118. # Use `tee` in case the $ZSH_COMPDUMP filename is invalid, to silence the error
  119. # See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a7269#commitcomment-39003489
  120. tee -a "$ZSH_COMPDUMP" &>/dev/null <<EOF
  121. $zcompdump_revision
  122. $zcompdump_fpath
  123. EOF
  124. fi
  125. unset zcompdump_revision zcompdump_fpath zcompdump_refresh
  126. # zcompile the completion dump file if the .zwc is older or missing.
  127. if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then
  128. zrecompile -q -p "$ZSH_COMPDUMP"
  129. command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock"
  130. fi
  131. _omz_source() {
  132. local context filepath="$1"
  133. # Construct zstyle context based on path
  134. case "$filepath" in
  135. lib/*) context="lib:${filepath:t:r}" ;; # :t = lib_name.zsh, :r = lib_name
  136. plugins/*) context="plugins:${filepath:h:t}" ;; # :h = plugins/plugin_name, :t = plugin_name
  137. esac
  138. local disable_aliases=0
  139. zstyle -T ":omz:${context}" aliases || disable_aliases=1
  140. # Back up alias names prior to sourcing
  141. local -A aliases_pre galiases_pre
  142. if (( disable_aliases )); then
  143. aliases_pre=("${(@kv)aliases}")
  144. galiases_pre=("${(@kv)galiases}")
  145. fi
  146. # Source file from $ZSH_CUSTOM if it exists, otherwise from $ZSH
  147. if [[ -f "$ZSH_CUSTOM/$filepath" ]]; then
  148. source "$ZSH_CUSTOM/$filepath"
  149. elif [[ -f "$ZSH/$filepath" ]]; then
  150. source "$ZSH/$filepath"
  151. fi
  152. # Unset all aliases that don't appear in the backed up list of aliases
  153. if (( disable_aliases )); then
  154. if (( #aliases_pre )); then
  155. aliases=("${(@kv)aliases_pre}")
  156. else
  157. (( #aliases )) && unalias "${(@k)aliases}"
  158. fi
  159. if (( #galiases_pre )); then
  160. galiases=("${(@kv)galiases_pre}")
  161. else
  162. (( #galiases )) && unalias "${(@k)galiases}"
  163. fi
  164. fi
  165. }
  166. # Load all of the lib files in ~/oh-my-zsh/lib that end in .zsh
  167. # TIP: Add files you don't want in git to .gitignore
  168. for lib_file ("$ZSH"/lib/*.zsh); do
  169. _omz_source "lib/${lib_file:t}"
  170. done
  171. unset lib_file
  172. # Load all of the plugins that were defined in ~/.zshrc
  173. for plugin ($plugins); do
  174. _omz_source "plugins/$plugin/$plugin.plugin.zsh"
  175. done
  176. unset plugin
  177. # Load all of your custom configurations from custom/
  178. for config_file ("$ZSH_CUSTOM"/*.zsh(N)); do
  179. source "$config_file"
  180. done
  181. unset config_file
  182. # Load the theme
  183. is_theme() {
  184. local base_dir=$1
  185. local name=$2
  186. builtin test -f $base_dir/$name.zsh-theme
  187. }
  188. if [[ -n "$ZSH_THEME" ]]; then
  189. if is_theme "$ZSH_CUSTOM" "$ZSH_THEME"; then
  190. source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
  191. elif is_theme "$ZSH_CUSTOM/themes" "$ZSH_THEME"; then
  192. source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
  193. elif is_theme "$ZSH/themes" "$ZSH_THEME"; then
  194. source "$ZSH/themes/$ZSH_THEME.zsh-theme"
  195. else
  196. echo "[oh-my-zsh] theme '$ZSH_THEME' not found"
  197. fi
  198. fi
  199. # set completion colors to be the same as `ls`, after theme has been loaded
  200. [[ -z "$LS_COLORS" ]] || zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"