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