1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2020 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 # plugings should do output in their "zprofile" stage!
26 if [[ "$type" == "zprofile" ]]; then
27 p10k_instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
28 [[ -r "$p10k_instant_prompt" ]] && source "$p10k_instant_prompt"
32 mkdir -p "$AXZSH/cache"
33 local cache_file="$AXZSH/cache/$type.cache"
35 local cat_cmd=${commands[cat]:-cat}
37 if [[ -r "$cache_file" ]]; then
38 # Cache file exists, use it!
39 # But when in the "zshrc" stage, make sure that the "zprofile" stage
40 # has already been handled (this uses the "01_zprofile" plugin which
41 # is used in the "zshrc.cache" as well, but can't be used successfully
42 # there because it becomes sourced inside of a ZSH function; so we have
43 # to source it here in the global context manually ...):
44 [[ -z "$AXZSH_ZPROFILE_READ" && "$type" = "zshrc" ]] \
45 && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
46 [[ -n "$AXZSH_DEBUG" ]] \
47 && echo " - Reading cache file \"$cache_file\" ..."
49 unfunction ax_plugin_init
51 # No cache file available.
52 # Setup list of plugins to load:
54 typeset -U plugin_list
57 "$AXZSH/active_plugins/"*(N)
61 # Create new cache file:
62 if [[ -n "$cache_file" ]]; then
63 [[ -n "$AXZSH_DEBUG" ]] \
64 && echo " (Writing new cache file to \"$cache_file\" ...)"
65 printf "# %s\n\n" "$(LC_ALL=C date)" >"$cache_file"
68 # Read in all the plugins for the current "type":
69 for plugin ($plugin_list); do
70 # Read the "theme file", if any and in "zshrc" stage.
71 # This must be done before 99_cleanup is run!
72 if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
73 if [[ -r "$AXZSH_THEME" ]]; then
75 if [[ -n "$cache_file" ]]; then
76 # Source the theme in the new cache file:
77 echo "# BEGIN Theme" >>"$cache_file"
78 echo 'source "$AXZSH_THEME"' >>"$cache_file"
79 echo "# END Theme" >>"$cache_file"
83 axzsh_load_plugin "$plugin" "$type" "$cache_file"
88 # Load plugin code of a given type.
90 # - $2: plugin type (optional; defaults to "zshrc")
91 # - $3: cache file (optional)
92 function axzsh_load_plugin {
94 local plugin="$dname:t"
95 [[ -z "$2" ]] && local type="zshrc" || local type="$2"
96 local fname="$dname/$plugin.$type"
99 # Strip repository prefix (like "alexbarton#test-plugin"):
100 [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
102 # "short plugin name": strip ".zsh" suffix:
103 plugin_short=${plugin%.zsh}
105 if [[ ! -d "$dname" ]]; then
107 if [[ -n "$AXZSH_DEBUG" ]]; then
108 # Show error message for all stages in "debug mode":
109 echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
110 elif [[ "$type" == "zshrc" ]]; then
111 # Show error message for the "zshrc" stage:
112 echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
117 if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
118 zsh_themes=("$dname/"*.zsh-theme(NY1))
119 if [[ -r "$dname/$plugin.ax-io" || -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
120 # Native AX-ZSH plugin, but for different stage. Skip it!
122 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
125 fname="$dname/${plugin_short}.plugin.zsh"
126 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
127 # Oh My ZSH plugin with "zsh-" prefix stripped
129 fname="$dname/${plugin_short##zsh-}.plugin.zsh"
130 elif [[ -r "$dname/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
131 # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
133 fname="$dname/${plugin}"
134 elif [[ -r "$dname/init.zsh" ]]; then
137 fname="$dname/init.zsh"
138 elif [[ ${#zsh_themes} -gt 0 ]]; then
139 # ZSH "theme plugin", ignore here!
142 echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
143 echo "Contents of \"$dname\":" >&2
149 if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
150 # Add plugin function path when folder exists
151 [[ -n "$AXZSH_DEBUG" ]] \
152 && echo " - $plugin ($type): functions ..."
153 axzsh_fpath+=("$dname/functions")
155 # Add function path to cache file.
156 [[ -n "$cache_file" ]] \
157 && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
160 if [[ -r "$fname" ]]; then
162 [[ -n "$AXZSH_DEBUG" ]] \
163 && echo " - $plugin ($type) ..."
165 # Note for "external" ("repo/*") plugins and unusual ("not so
166 # modern") terminals: These (modern?) plugins most probably
167 # don't expect such a terminal configuration and don't behave
168 # well (echo color sequences, for example). Therefore we DON'T
169 # load any external plugins at all in that case: this results in
170 # reduced/disabled functionality, but hopefully in readable
175 axzsh_is_modern_terminal && source "$fname"
181 if [[ -n "$cache_file" ]]; then
182 # Add plugin data to cache
183 printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
186 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type): \"$fname\" ...'" >>$cache_file
187 echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
190 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type, cached) ...'" >>$cache_file
191 "$cat_cmd" "$fname" >>"$cache_file"
193 printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
197 # It is a success, even if only the plugin directory (and no script!)
198 # exists at all! Rationale: The script could be of an other type ...
202 # Make sure that "my" (=ZSH) directory is in the search path ...
203 if [[ -z "$AXZSH" ]]; then
205 [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
209 # Make sure that "SHELL" variable is set and exported
210 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
212 # Make sure that "AXZSH" variable is set and exported
213 [[ -n "$AXZSH" ]] || export AXZSH="$HOME/.axzsh"
215 # Check for "debug mode" ...
216 if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
219 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
220 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
221 echo "AXZSH_ZLOGIN_READ=$AXZSH_ZLOGIN_READ"
222 echo "AXZSH_ZPROFILE_READ=$AXZSH_ZPROFILE_READ"
225 if [[ "$script_type" = "zprofile" ]]; then
226 # Load all "output" plugins first, that is, before the "zprofile stage":
227 axzsh_handle_stage "$script_name" "ax-io"
230 axzsh_handle_stage "$script_name" "$script_type"
233 unfunction axzsh_handle_stage axzsh_load_plugin
234 unset script_name script_type
236 # Hints for external installers:
237 # - iTerm2: DON'T install "iterm2_shell_integration"!