oh-my-zsh.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Protect against non-zsh execution of Oh My Zsh (use POSIX syntax here)
  2. [ -n "$ZSH_VERSION" ] || {
  3. # ANSI (\033[<code>m): 0: reset, 1: bold, 4: underline, 22: no bold, 24: no underline, 31: red
  4. printf "\033[1;31mERROR:\033[22m Oh My Zsh can't be loaded from: \033[1m${0#-}\033[22m. " >&2
  5. printf "You need to run \033[1;4mzsh\033[22;24m instead.\033[0m\n" >&2
  6. return 1
  7. }
  8. # If ZSH is not defined, use the current script's directory.
  9. [[ -z "$ZSH" ]] && export ZSH="${${(%):-%x}:a:h}"
  10. # Set ZSH_CACHE_DIR to the path where cache files should be created
  11. # or else we will use the default cache/
  12. if [[ -z "$ZSH_CACHE_DIR" ]]; then
  13. ZSH_CACHE_DIR="$ZSH/cache"
  14. fi
  15. # Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
  16. if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
  17. ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
  18. fi
  19. # Create cache and completions dir and add to $fpath
  20. mkdir -p "$ZSH_CACHE_DIR/completions"
  21. (( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
  22. # Check for updates on initial load...
  23. if [ "$DISABLE_AUTO_UPDATE" != "true" ]; then
  24. source $ZSH/tools/check_for_upgrade.sh
  25. fi
  26. # Initializes Oh My Zsh
  27. # add a function path
  28. fpath=($ZSH/functions $ZSH/completions $fpath)
  29. # Load all stock functions (from $fpath files) called below.
  30. autoload -U compaudit compinit
  31. # Set ZSH_CUSTOM to the path where your custom config files
  32. # and plugins exists, or else we will use the default custom/
  33. if [[ -z "$ZSH_CUSTOM" ]]; then
  34. ZSH_CUSTOM="$ZSH/custom"
  35. fi
  36. is_plugin() {
  37. local base_dir=$1
  38. local name=$2
  39. builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
  40. || builtin test -f $base_dir/plugins/$name/_$name
  41. }
  42. # Add all defined plugins to fpath. This must be done
  43. # before running compinit.
  44. for plugin ($plugins); do
  45. if is_plugin $ZSH_CUSTOM $plugin; then
  46. fpath=($ZSH_CUSTOM/plugins/$plugin $fpath)
  47. elif is_plugin $ZSH $plugin; then
  48. fpath=($ZSH/plugins/$plugin $fpath)
  49. else
  50. echo "[oh-my-zsh] plugin '$plugin' not found"
  51. fi
  52. done
  53. # Figure out the SHORT hostname
  54. if [[ "$OSTYPE" = darwin* ]]; then
  55. # macOS's $HOST changes with dhcp, etc. Use ComputerName if possible.
  56. SHORT_HOST=$(scutil --get ComputerName 2>/dev/null) || SHORT_HOST=${HOST/.*/}
  57. else
  58. SHORT_HOST=${HOST/.*/}
  59. fi
  60. # Save the location of the current completion dump file.
  61. if [ -z "$ZSH_COMPDUMP" ]; then
  62. ZSH_COMPDUMP="${ZDOTDIR:-${HOME}}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
  63. fi
  64. # Construct zcompdump OMZ metadata
  65. zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
  66. zcompdump_fpath="#omz fpath: $fpath"
  67. # Delete the zcompdump file if OMZ zcompdump metadata changed
  68. if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \
  69. || ! command grep -q -Fx "$zcompdump_fpath" "$ZSH_COMPDUMP" 2>/dev/null; then
  70. command rm -f "$ZSH_COMPDUMP"
  71. zcompdump_refresh=1
  72. fi
  73. if [[ $ZSH_DISABLE_COMPFIX != true ]]; then
  74. source $ZSH/lib/compfix.zsh
  75. # If completion insecurities exist, warn the user
  76. handle_completion_insecurities
  77. # Load only from secure directories
  78. compinit -i -C -d "${ZSH_COMPDUMP}"
  79. else
  80. # If the user wants it, load from all found directories
  81. compinit -u -C -d "${ZSH_COMPDUMP}"
  82. fi
  83. # Append zcompdump metadata if missing
  84. if (( $zcompdump_refresh )); then
  85. # Use `tee` in case the $ZSH_COMPDUMP filename is invalid, to silence the error
  86. # See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a7269#commitcomment-39003489
  87. tee -a "$ZSH_COMPDUMP" &>/dev/null <<EOF
  88. $zcompdump_revision
  89. $zcompdump_fpath
  90. EOF
  91. fi
  92. unset zcompdump_revision zcompdump_fpath zcompdump_refresh
  93. # Load all of the config files in ~/oh-my-zsh that end in .zsh
  94. # TIP: Add files you don't want in git to .gitignore
  95. for config_file ($ZSH/lib/*.zsh); do
  96. custom_config_file="${ZSH_CUSTOM}/lib/${config_file:t}"
  97. [ -f "${custom_config_file}" ] && config_file=${custom_config_file}
  98. source $config_file
  99. done
  100. # Load all of the plugins that were defined in ~/.zshrc
  101. for plugin ($plugins); do
  102. if [ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]; then
  103. source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh
  104. elif [ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]; then
  105. source $ZSH/plugins/$plugin/$plugin.plugin.zsh
  106. fi
  107. done
  108. # Load all of your custom configurations from custom/
  109. for config_file ($ZSH_CUSTOM/*.zsh(N)); do
  110. source $config_file
  111. done
  112. unset config_file
  113. # Load the theme
  114. if [ ! "$ZSH_THEME" = "" ]; then
  115. if [ -f "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" ]; then
  116. source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
  117. elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then
  118. source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
  119. else
  120. source "$ZSH/themes/$ZSH_THEME.zsh-theme"
  121. fi
  122. fi