Parent directory

oh-my-zsh.sh

7595 bytes
  1# ANSI formatting function (\033[<code>m)
  2# 0: reset, 1: bold, 4: underline, 22: no bold, 24: no underline, 31: red, 33: yellow
  3omz_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
 10# Protect against non-zsh execution of Oh My Zsh (use POSIX syntax here)
 11[ -n "$ZSH_VERSION" ] || {
 12  omz_ptree() {
 13    # Get process tree of the current process
 14    pid=$$; pids="$pid"
 15    while [ ${pid-0} -ne 1 ] && ppid=$(ps -e -o pid,ppid | awk "\$1 == $pid { print \$2 }"); do
 16      pids="$pids $pid"; pid=$ppid
 17    done
 18
 19    # Show process tree
 20    case "$(uname)" in
 21    Linux) ps -o ppid,pid,command -f -p $pids 2>/dev/null ;;
 22    Darwin|*) ps -o ppid,pid,command -p $pids 2>/dev/null ;;
 23    esac
 24
 25    # If ps command failed, try Busybox ps
 26    [ $? -eq 0 ] || ps -o ppid,pid,comm | awk "NR == 1 || index(\"$pids\", \$2) != 0"
 27  }
 28
 29  {
 30    shell=$(ps -o pid,comm | awk "\$1 == $$ { print \$2 }")
 31    printf "$(omz_f 1 31)Error:$(omz_f 22) Oh My Zsh can't be loaded from: $(omz_f 1)${shell}$(omz_f 22). "
 32    printf "You need to run $(omz_f 1)zsh$(omz_f 22) instead.$(omz_f 0)\n"
 33    printf "$(omz_f 33)Here's the process tree:$(omz_f 22)\n\n"
 34    omz_ptree
 35    printf "$(omz_f 0)\n"
 36  } >&2
 37
 38  return 1
 39}
 40
 41# Check if in emulation mode, if so early return
 42# https://github.com/ohmyzsh/ohmyzsh/issues/11686
 43[[ "$(emulate)" = zsh ]] || {
 44  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
 45  return 1
 46}
 47
 48unset -f omz_f
 49
 50# If ZSH is not defined, use the current script's directory.
 51[[ -n "$ZSH" ]] || export ZSH="${${(%):-%x}:a:h}"
 52
 53# Set ZSH_CUSTOM to the path where your custom config files
 54# and plugins exists, or else we will use the default custom/
 55[[ -n "$ZSH_CUSTOM" ]] || ZSH_CUSTOM="$ZSH/custom"
 56
 57# Set ZSH_CACHE_DIR to the path where cache files should be created
 58# or else we will use the default cache/
 59[[ -n "$ZSH_CACHE_DIR" ]] || ZSH_CACHE_DIR="$ZSH/cache"
 60
 61# Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
 62if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
 63  ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
 64fi
 65
 66# Create cache and completions dir and add to $fpath
 67mkdir -p "$ZSH_CACHE_DIR/completions"
 68(( ${fpath[(Ie)$ZSH_CACHE_DIR/completions]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
 69
 70# Check for updates on initial load...
 71source "$ZSH/tools/check_for_upgrade.sh"
 72
 73# Initializes Oh My Zsh
 74
 75# add a function path
 76fpath=($ZSH/{functions,completions} $ZSH_CUSTOM/{functions,completions} $fpath)
 77
 78# Load all stock functions (from $fpath files) called below.
 79autoload -U compaudit compinit zrecompile
 80
 81is_plugin() {
 82  local base_dir=$1
 83  local name=$2
 84  builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
 85    || builtin test -f $base_dir/plugins/$name/_$name
 86}
 87
 88# Add all defined plugins to fpath. This must be done
 89# before running compinit.
 90for plugin ($plugins); do
 91  if is_plugin "$ZSH_CUSTOM" "$plugin"; then
 92    fpath=("$ZSH_CUSTOM/plugins/$plugin" $fpath)
 93  elif is_plugin "$ZSH" "$plugin"; then
 94    fpath=("$ZSH/plugins/$plugin" $fpath)
 95  else
 96    echo "[oh-my-zsh] plugin '$plugin' not found"
 97  fi
 98done
 99
100# Figure out the SHORT hostname
101if [[ "$OSTYPE" = darwin* ]]; then
102  # macOS's $HOST changes with dhcp, etc. Use LocalHostName if possible.
103  SHORT_HOST=$(scutil --get LocalHostName 2>/dev/null) || SHORT_HOST="${HOST/.*/}"
104else
105  SHORT_HOST="${HOST/.*/}"
106fi
107
108# Save the location of the current completion dump file.
109if [[ -z "$ZSH_COMPDUMP" ]]; then
110  ZSH_COMPDUMP="${ZDOTDIR:-$HOME}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
111fi
112
113# Construct zcompdump OMZ metadata
114zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
115zcompdump_fpath="#omz fpath: $fpath"
116
117# Delete the zcompdump file if OMZ zcompdump metadata changed
118if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \
119   || ! command grep -q -Fx "$zcompdump_fpath" "$ZSH_COMPDUMP" 2>/dev/null; then
120  command rm -f "$ZSH_COMPDUMP"
121  zcompdump_refresh=1
122fi
123
124if [[ "$ZSH_DISABLE_COMPFIX" != true ]]; then
125  source "$ZSH/lib/compfix.zsh"
126  # Load only from secure directories
127  # Reset the flag compinit sets when -i excludes insecure entries
128  unset _comp_secure
129  compinit -i -d "$ZSH_COMPDUMP"
130  # If completion insecurities exist, warn the user
131  [[ "$_comp_secure" == yes ]] && handle_completion_insecurities &|
132else
133  # If the user wants it, load from all found directories
134  compinit -u -d "$ZSH_COMPDUMP"
135fi
136
137# Append zcompdump metadata if missing
138if (( $zcompdump_refresh )) \
139  || ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null; then
140  # Use `tee` in case the $ZSH_COMPDUMP filename is invalid, to silence the error
141  # See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a7269#commitcomment-39003489
142  tee -a "$ZSH_COMPDUMP" &>/dev/null <<EOF
143
144$zcompdump_revision
145$zcompdump_fpath
146EOF
147fi
148unset zcompdump_revision zcompdump_fpath zcompdump_refresh
149
150# zcompile the completion dump file if the .zwc is older or missing.
151if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then
152  zrecompile -q -p "$ZSH_COMPDUMP"
153  command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock"
154fi
155
156_omz_source() {
157  local context filepath="$1"
158
159  # Construct zstyle context based on path
160  case "$filepath" in
161  lib/*) context="lib:${filepath:t:r}" ;;         # :t = lib_name.zsh, :r = lib_name
162  plugins/*) context="plugins:${filepath:h:t}" ;; # :h = plugins/plugin_name, :t = plugin_name
163  esac
164
165  local disable_aliases=0
166  zstyle -T ":omz:${context}" aliases || disable_aliases=1
167
168  # Back up alias names prior to sourcing
169  local -A aliases_pre galiases_pre
170  if (( disable_aliases )); then
171    aliases_pre=("${(@kv)aliases}")
172    galiases_pre=("${(@kv)galiases}")
173  fi
174
175  # Source file from $ZSH_CUSTOM if it exists, otherwise from $ZSH
176  if [[ -f "$ZSH_CUSTOM/$filepath" ]]; then
177    source "$ZSH_CUSTOM/$filepath"
178  elif [[ -f "$ZSH/$filepath" ]]; then
179    source "$ZSH/$filepath"
180  fi
181
182  # Unset all aliases that don't appear in the backed up list of aliases
183  if (( disable_aliases )); then
184    if (( #aliases_pre )); then
185      aliases=("${(@kv)aliases_pre}")
186    else
187      (( #aliases )) && unalias "${(@k)aliases}"
188    fi
189    if (( #galiases_pre )); then
190      galiases=("${(@kv)galiases_pre}")
191    else
192      (( #galiases )) && unalias "${(@k)galiases}"
193    fi
194  fi
195}
196
197# Load all of the lib files in ~/.oh-my-zsh/lib that end in .zsh
198# TIP: Add files you don't want in git to .gitignore
199for lib_file ("$ZSH"/lib/*.zsh); do
200  _omz_source "lib/${lib_file:t}"
201done
202unset lib_file
203
204# Load all of the plugins that were defined in ~/.zshrc
205for plugin ($plugins); do
206  _omz_source "plugins/$plugin/$plugin.plugin.zsh"
207done
208unset plugin
209
210# Load all of your custom configurations from custom/
211for config_file ("$ZSH_CUSTOM"/*.zsh(N)); do
212  source "$config_file"
213done
214unset config_file
215
216# Load the theme
217is_theme() {
218  local base_dir=$1
219  local name=$2
220  builtin test -f $base_dir/$name.zsh-theme
221}
222
223if [[ -n "$ZSH_THEME" ]]; then
224  if is_theme "$ZSH_CUSTOM" "$ZSH_THEME"; then
225    source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
226  elif is_theme "$ZSH_CUSTOM/themes" "$ZSH_THEME"; then
227    source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
228  elif is_theme "$ZSH/themes" "$ZSH_THEME"; then
229    source "$ZSH/themes/$ZSH_THEME.zsh-theme"
230  else
231    echo "[oh-my-zsh] theme '$ZSH_THEME' not found"
232  fi
233fi
234
235# set completion colors to be the same as `ls`, after theme has been loaded
236[[ -z "$LS_COLORS" ]] || zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"