oh-my-zsh.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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
  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. # If completion insecurities exist, warn the user
  103. handle_completion_insecurities
  104. # Load only from secure directories
  105. compinit -i -C -d "${ZSH_COMPDUMP}"
  106. else
  107. # If the user wants it, load from all found directories
  108. compinit -u -C -d "${ZSH_COMPDUMP}"
  109. fi
  110. # Append zcompdump metadata if missing
  111. if (( $zcompdump_refresh )); then
  112. # Use `tee` in case the $ZSH_COMPDUMP filename is invalid, to silence the error
  113. # See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a7269#commitcomment-39003489
  114. tee -a "$ZSH_COMPDUMP" &>/dev/null <<EOF
  115. $zcompdump_revision
  116. $zcompdump_fpath
  117. EOF
  118. fi
  119. unset zcompdump_revision zcompdump_fpath zcompdump_refresh
  120. # Load all of the config files in ~/oh-my-zsh that end in .zsh
  121. # TIP: Add files you don't want in git to .gitignore
  122. for config_file ($ZSH/lib/*.zsh); do
  123. custom_config_file="${ZSH_CUSTOM}/lib/${config_file:t}"
  124. [ -f "${custom_config_file}" ] && config_file=${custom_config_file}
  125. source $config_file
  126. done
  127. # Load all of the plugins that were defined in ~/.zshrc
  128. for plugin ($plugins); do
  129. if [ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]; then
  130. source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh
  131. elif [ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]; then
  132. source $ZSH/plugins/$plugin/$plugin.plugin.zsh
  133. fi
  134. done
  135. # Load all of your custom configurations from custom/
  136. for config_file ($ZSH_CUSTOM/*.zsh(N)); do
  137. source $config_file
  138. done
  139. unset config_file
  140. # Load the theme
  141. if [ ! "$ZSH_THEME" = "" ]; then
  142. if [ -f "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" ]; then
  143. source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
  144. elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then
  145. source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
  146. else
  147. source "$ZSH/themes/$ZSH_THEME.zsh-theme"
  148. fi
  149. fi