1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2024 Alexander Barton <alex@barton.de>
4 script_name="${${(%):-%N}:t}"
5 script_type="$script_name[2,-1]"
7 # Handle "initialization stage", load all plugins of that stage, either from an
8 # existing cache file or individually, optionally creating the cache.
10 # - $2: Stage name (ax-io, zprofile, zshrc, zlogin, zlogout)
11 function axzsh_handle_stage {
15 [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $name ($type):"
17 # Look for some 3rd-party integrations ...
19 # --- Powerlevel10k ---
20 # Enable instant prompt. Should stay close to the top of ~/.zshrc.
21 # Initialization code that may require console input (password prompts,
22 # [y/n] confirmations, etc.) must be executed before this, so all ax-zsh
23 # plugins should do output in their "ax-io" stage only!
24 # Read the initialization script in the "zprofile" stage for login
25 # shells, and in the "zshrc" stage for non-login sub-shells (which have
26 # the profile already read in and therefore will skip the "ax-io" and
27 # "zprofile" stages and not catch up).
29 ( "$type" == "zprofile" ) || \
30 ( ! -o login && "$type" == "zshrc" && -n "$AXZSH_ZPROFILE_READ" ) \
32 p10k_instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
33 if [[ -r "$p10k_instant_prompt" ]]; then
34 [[ -n "$AXZSH_DEBUG" ]] && echo " Reading \"$p10k_instant_prompt\" ..."
35 source "$p10k_instant_prompt"
37 unset p10k_instant_prompt
39 # Read in Powerlevel10k configuration file, if not already read:
40 [[ -z "$POWERLEVEL9K_CONFIG_FILE" && -r ~/.p10k.zsh ]] && source ~/.p10k.zsh
43 [[ -d "$AXZSH/cache" ]] || mkdir -p "$AXZSH/cache"
44 local cache_file="$AXZSH/cache/$type.cache"
46 local cat_cmd=${commands[cat]:-cat}
48 if [[ -r "$cache_file" ]]; then
49 # Cache file exists, use it!
50 # But when in the "zshrc" stage, make sure that the "zprofile" stage
51 # has already been handled (this uses the "01_zprofile" plugin which
52 # is used in the "zshrc.cache" as well, but can't be used successfully
53 # there because it becomes sourced inside of a ZSH function; so we have
54 # to source it here in the global context manually ...):
55 [[ -z "$AXZSH_ZPROFILE_READ" && "$type" = "zshrc" ]] \
56 && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
57 [[ -n "$AXZSH_DEBUG" ]] \
58 && echo " - Reading cache file \"$cache_file\" ..."
60 (( $+functions[axzsh_plugin_init] )) && unfunction axzsh_plugin_init
62 # No cache file available.
63 local new_cache_file="$cache_file.NEW"
65 # Setup list of plugins to load:
67 typeset -U plugin_list
70 "$AXZSH/active_plugins/"*(N)
74 # Create new cache file:
75 [[ -n "$AXZSH_DEBUG" ]] \
76 && echo " (Writing new cache file to \"$new_cache_file\" ...)"
77 if ! printf "# %s\n\n" "$(LC_ALL=C date)" >"$new_cache_file"; then
80 # New cache file successfully created ...
81 if [[ "$type" = "ax-io" ]]; then
83 # Write an initial PATH variable to the cache
84 # file, which becomes overwritten by the path
85 # plugin at the "zprofile" stage later on, but
86 # this way "ax-io" stage plugins have a somewhat
87 # saner PATH to begin with ...
88 printf 'export PATH="%s"\n\n' "$PATH" >>"$new_cache_file"
92 # Read in all the plugins for the current "type":
93 for plugin ($plugin_list); do
94 # Read the "theme file", if any and in "zshrc" stage.
95 # This must be done before 99_cleanup is run!
96 if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
97 if [[ -r "$AXZSH_THEME" ]]; then
99 if [[ -n "$new_cache_file" ]]; then
100 # Source the theme in the new cache file:
101 echo "# BEGIN Theme" >>"$new_cache_file"
102 echo 'source "$AXZSH_THEME"' >>"$new_cache_file"
103 echo "# END Theme" >>"$new_cache_file"
107 axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
110 if [[ -n "$cache_file" && -n "$new_cache_file" && -r "$new_cache_file" ]]; then
111 # Move newly created cache file in place:
112 mv "$new_cache_file" "$cache_file"
117 # Load plugin code of a given type.
119 # - $2: plugin type (optional; defaults to "zshrc")
120 # - $3: cache file (optional)
121 function axzsh_load_plugin {
123 local plugin="$dname:t"
124 [[ -z "$2" ]] && local type="zshrc" || local type="$2"
125 local fname="$dname/$plugin.$type"
126 local cache_file="$3"
128 # Strip repository prefix (like "alexbarton#test-plugin"):
129 [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
131 # "short plugin name": strip ".zsh" suffix:
132 plugin_short=${plugin%.zsh}
134 if [[ ! -d "$dname" ]]; then
136 if [[ -n "$AXZSH_DEBUG" ]]; then
137 # Show error message for all stages in "debug mode":
138 echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
139 elif [[ "$type" == "zshrc" ]]; then
140 # Show error message for the "zshrc" stage:
141 echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
146 if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
147 zsh_themes=("$dname/"*.zsh-theme(NY1))
148 if [[ -r "$dname/$plugin.ax-io" || -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
149 # Native AX-ZSH plugin, but for different stage. Skip it!
151 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
154 fname="$dname/${plugin_short}.plugin.zsh"
155 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
156 # Oh My ZSH plugin with "zsh-" prefix stripped
158 fname="$dname/${plugin_short##zsh-}.plugin.zsh"
159 elif [[ -r "$dname/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
160 # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
162 fname="$dname/${plugin}"
163 elif [[ -r "$dname/init.zsh" ]]; then
166 fname="$dname/init.zsh"
167 elif [[ ${#zsh_themes} -gt 0 ]]; then
168 # ZSH "theme plugin", ignore here!
171 echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
172 echo "Contents of \"$dname\":" >&2
178 if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
179 # Add plugin function path when folder exists
180 [[ -n "$AXZSH_DEBUG" ]] \
181 && echo " - $plugin ($type): functions ..."
182 axzsh_fpath+=("$dname/functions")
184 # Add function path to cache file.
185 [[ -n "$cache_file" ]] \
186 && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
189 if [[ -r "$fname" ]]; then
191 [[ -n "$AXZSH_DEBUG" ]] \
192 && echo " - $plugin ($type) ..."
194 # Note for "external" ("repo/*") plugins and unusual ("not so
195 # modern") terminals: These (modern?) plugins most probably
196 # don't expect such a terminal configuration and don't behave
197 # well (echo color sequences, for example). Therefore we DON'T
198 # load any external plugins at all in that case: this results in
199 # reduced/disabled functionality, but hopefully in readable
204 axzsh_is_modern_terminal && source "$fname"
210 if [[ -n "$cache_file" ]]; then
211 # Add plugin data to cache
212 printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$fname" >>"$cache_file"
215 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type): \"$fname\" ...'" >>$cache_file
216 echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
219 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type, cached) ...'" >>$cache_file
220 "$cat_cmd" "$fname" >>"$cache_file"
222 printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
226 # It is a success, even if only the plugin directory (and no script!)
227 # exists at all! Rationale: The script could be of an other type ...
231 # Make sure that "my" (=ZSH) directory is in the search path ...
232 if [[ -z "$AXZSH" ]]; then
234 [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
238 # Make sure that "SHELL" variable is set and exported
239 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
241 # Make sure that "AXZSH" variable is set and exported
242 [[ -n "$AXZSH" ]] || export AXZSH="${ZDOTDIR:-$HOME}/.axzsh"
244 # Check for "debug mode" ...
245 if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
247 export POWERLEVEL9K_INSTANT_PROMPT=quiet
249 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
250 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
251 echo "AXZSH_ZLOGIN_READ=$AXZSH_ZLOGIN_READ"
252 echo "AXZSH_ZPROFILE_READ=$AXZSH_ZPROFILE_READ"
255 if [[ "$script_type" = "zprofile" ]]; then
256 # Load all "output" plugins first, that is, before the "zprofile stage":
257 axzsh_handle_stage "$script_name" "ax-io"
260 axzsh_handle_stage "$script_name" "$script_type"
263 unfunction axzsh_handle_stage axzsh_load_plugin
264 unset script_name script_type
266 # Hints for external installers:
267 # - iTerm2: DON'T install "iterm2_shell_integration"!