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 # Read in Powerlevel10k configuration file, if not already read:
21 [[ -z "$POWERLEVEL9K_CONFIG_FILE" && -r ~/.p10k.zsh ]] && source ~/.p10k.zsh
22 # Enable instant prompt. Should stay close to the top of ~/.zshrc.
23 # Initialization code that may require console input (password prompts,
24 # [y/n] confirmations, etc.) must be executed before this, so all ax-zsh
25 # plugins should do output in their "ax-io" stage only!
26 # Read the initialization script in the "zprofile" stage for login
27 # shells, and in the "zshrc" stage for non-login sub-shells (which have
28 # the profile already read in and therefore will skip the "ax-io" and
29 # "zprofile" stages and not catch up).
31 ( "$type" == "zprofile" ) || \
32 ( ! -o login && "$type" == "zshrc" && -n "$AXZSH_ZPROFILE_READ" ) \
34 p10k_instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
35 [[ -r "$p10k_instant_prompt" ]] && source "$p10k_instant_prompt"
39 [[ -d "$AXZSH/cache" ]] || mkdir -p "$AXZSH/cache"
40 local cache_file="$AXZSH/cache/$type.cache"
42 local cat_cmd=${commands[cat]:-cat}
44 if [[ -r "$cache_file" ]]; then
45 # Cache file exists, use it!
46 # But when in the "zshrc" stage, make sure that the "zprofile" stage
47 # has already been handled (this uses the "01_zprofile" plugin which
48 # is used in the "zshrc.cache" as well, but can't be used successfully
49 # there because it becomes sourced inside of a ZSH function; so we have
50 # to source it here in the global context manually ...):
51 [[ -z "$AXZSH_ZPROFILE_READ" && "$type" = "zshrc" ]] \
52 && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
53 [[ -n "$AXZSH_DEBUG" ]] \
54 && echo " - Reading cache file \"$cache_file\" ..."
56 unfunction axzsh_plugin_init
58 # No cache file available.
59 local new_cache_file="$cache_file.NEW"
61 # Setup list of plugins to load:
63 typeset -U plugin_list
66 "$AXZSH/active_plugins/"*(N)
70 # Create new cache file:
71 [[ -n "$AXZSH_DEBUG" ]] \
72 && echo " (Writing new cache file to \"$new_cache_file\" ...)"
73 if ! printf "# %s\n\n" "$(LC_ALL=C date)" >"$new_cache_file"; then
76 # New cache file successfully created ...
77 if [[ "$type" = "ax-io" ]]; then
79 # Write an initial PATH variable to the cache
80 # file, which becomes overwritten by the path
81 # plugin at the "zprofile" stage later on, but
82 # this way "ax-io" stage plugins have a somewhat
83 # saner PATH to begin with ...
84 printf 'export PATH="%s"\n\n' "$PATH" >>"$new_cache_file"
88 # Read in all the plugins for the current "type":
89 for plugin ($plugin_list); do
90 # Read the "theme file", if any and in "zshrc" stage.
91 # This must be done before 99_cleanup is run!
92 if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
93 if [[ -r "$AXZSH_THEME" ]]; then
95 if [[ -n "$new_cache_file" ]]; then
96 # Source the theme in the new cache file:
97 echo "# BEGIN Theme" >>"$new_cache_file"
98 echo 'source "$AXZSH_THEME"' >>"$new_cache_file"
99 echo "# END Theme" >>"$new_cache_file"
103 axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
106 if [[ -n "$cache_file" && -n "$new_cache_file" && -r "$new_cache_file" ]]; then
107 # Move newly created cache file in place:
108 mv "$new_cache_file" "$cache_file"
113 # Load plugin code of a given type.
115 # - $2: plugin type (optional; defaults to "zshrc")
116 # - $3: cache file (optional)
117 function axzsh_load_plugin {
119 local plugin="$dname:t"
120 [[ -z "$2" ]] && local type="zshrc" || local type="$2"
121 local fname="$dname/$plugin.$type"
122 local cache_file="$3"
124 # Strip repository prefix (like "alexbarton#test-plugin"):
125 [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
127 # "short plugin name": strip ".zsh" suffix:
128 plugin_short=${plugin%.zsh}
130 if [[ ! -d "$dname" ]]; then
132 if [[ -n "$AXZSH_DEBUG" ]]; then
133 # Show error message for all stages in "debug mode":
134 echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
135 elif [[ "$type" == "zshrc" ]]; then
136 # Show error message for the "zshrc" stage:
137 echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
142 if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
143 zsh_themes=("$dname/"*.zsh-theme(NY1))
144 if [[ -r "$dname/$plugin.ax-io" || -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
145 # Native AX-ZSH plugin, but for different stage. Skip it!
147 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
150 fname="$dname/${plugin_short}.plugin.zsh"
151 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
152 # Oh My ZSH plugin with "zsh-" prefix stripped
154 fname="$dname/${plugin_short##zsh-}.plugin.zsh"
155 elif [[ -r "$dname/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
156 # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
158 fname="$dname/${plugin}"
159 elif [[ -r "$dname/init.zsh" ]]; then
162 fname="$dname/init.zsh"
163 elif [[ ${#zsh_themes} -gt 0 ]]; then
164 # ZSH "theme plugin", ignore here!
167 echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
168 echo "Contents of \"$dname\":" >&2
174 if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
175 # Add plugin function path when folder exists
176 [[ -n "$AXZSH_DEBUG" ]] \
177 && echo " - $plugin ($type): functions ..."
178 axzsh_fpath+=("$dname/functions")
180 # Add function path to cache file.
181 [[ -n "$cache_file" ]] \
182 && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
185 if [[ -r "$fname" ]]; then
187 [[ -n "$AXZSH_DEBUG" ]] \
188 && echo " - $plugin ($type) ..."
190 # Note for "external" ("repo/*") plugins and unusual ("not so
191 # modern") terminals: These (modern?) plugins most probably
192 # don't expect such a terminal configuration and don't behave
193 # well (echo color sequences, for example). Therefore we DON'T
194 # load any external plugins at all in that case: this results in
195 # reduced/disabled functionality, but hopefully in readable
200 axzsh_is_modern_terminal && source "$fname"
206 if [[ -n "$cache_file" ]]; then
207 # Add plugin data to cache
208 printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$fname" >>"$cache_file"
211 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type): \"$fname\" ...'" >>$cache_file
212 echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
215 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type, cached) ...'" >>$cache_file
216 "$cat_cmd" "$fname" >>"$cache_file"
218 printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
222 # It is a success, even if only the plugin directory (and no script!)
223 # exists at all! Rationale: The script could be of an other type ...
227 # Make sure that "my" (=ZSH) directory is in the search path ...
228 if [[ -z "$AXZSH" ]]; then
230 [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
234 # Make sure that "SHELL" variable is set and exported
235 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
237 # Make sure that "AXZSH" variable is set and exported
238 [[ -n "$AXZSH" ]] || export AXZSH="${ZDOTDIR:-$HOME}/.axzsh"
240 # Check for "debug mode" ...
241 if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
243 export POWERLEVEL9K_INSTANT_PROMPT=quiet
245 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
246 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
247 echo "AXZSH_ZLOGIN_READ=$AXZSH_ZLOGIN_READ"
248 echo "AXZSH_ZPROFILE_READ=$AXZSH_ZPROFILE_READ"
251 if [[ "$script_type" = "zprofile" ]]; then
252 # Load all "output" plugins first, that is, before the "zprofile stage":
253 axzsh_handle_stage "$script_name" "ax-io"
256 axzsh_handle_stage "$script_name" "$script_type"
259 unfunction axzsh_handle_stage axzsh_load_plugin
260 unset script_name script_type
262 # Hints for external installers:
263 # - iTerm2: DON'T install "iterm2_shell_integration"!