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):"
18 mkdir -p "$AXZSH/cache"
19 cache_file="$AXZSH/cache/$type.cache"
21 cat_cmd=${commands[cat]:-cat}
23 if [[ -r "$cache_file" ]]; then
24 # Cache file exists, use it!
25 # But when in the "zshrc" stage, make sure that the "zprofile" stage
26 # has already been handled (this uses the "01_zprofile" plugin which
27 # is used in the "zshrc.cache" as well, but can't be used successfully
28 # there because it becomes sourced inside of a ZSH function; so we have
29 # to source it here in the global context manually ...):
30 [[ -z "$AXZSH_ZPROFILE_READ" && "$type" = "zshrc" ]] \
31 && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
32 [[ -n "$AXZSH_DEBUG" ]] \
33 && echo " - Reading cache file \"$cache_file\" ..."
35 unfunction ax_plugin_init
37 # No cache file available.
38 # Setup list of plugins to load:
39 typeset -U plugin_list
42 "$AXZSH/active_plugins/"*(N)
46 # Create new cache file:
47 if [[ -n "$cache_file" ]]; then
48 [[ -n "$AXZSH_DEBUG" ]] \
49 && echo " (Writing new cache file to \"$cache_file\" ...)"
50 printf "# %s\n\n" "$(LC_ALL=C date)" >"$cache_file"
53 # Read in all the plugins for the current "type":
54 for plugin ($plugin_list); do
55 # Read the "theme file", if any and in "zshrc" stage.
56 # This must be done before 99_cleanup is run!
57 if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
58 if [[ -r "$AXZSH_THEME" ]]; then
60 if [[ -n "$cache_file" ]]; then
61 # Source the theme in the new cache file:
62 echo "# BEGIN Theme" >>"$cache_file"
63 echo 'source "$AXZSH_THEME"' >>"$cache_file"
64 echo "# END Theme" >>"$cache_file"
68 axzsh_load_plugin "$plugin" "$type" "$cache_file"
73 # Load plugin code of a given type.
75 # - $2: plugin type (optional; defaults to "zshrc")
76 # - $3: cache file (optional)
77 function axzsh_load_plugin {
79 local plugin="$dname:t"
80 [[ -z "$2" ]] && local type="zshrc" || local type="$2"
81 local fname="$dname/$plugin.$type"
84 # Strip repository prefix (like "alexbarton#test-plugin"):
85 [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
87 # "short plugin name": strip ".zsh" suffix:
88 plugin_short=${plugin%.zsh}
90 if [[ ! -d "$dname" ]]; then
92 if [[ -n "$AXZSH_DEBUG" ]]; then
93 # Show error message for all stages in "debug mode":
94 echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
95 elif [[ "$type" == "zshrc" ]]; then
96 # Show error message for the "zshrc" stage:
97 echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
102 if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
103 zsh_themes=("$dname/"*.zsh-theme(NY1))
104 if [[ -r "$dname/$plugin.ax-io" || -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
105 # Native AX-ZSH plugin, but for different stage. Skip it!
107 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
110 fname="$dname/${plugin_short}.plugin.zsh"
111 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
112 # Oh My ZSH plugin with "zsh-" prefix stripped
114 fname="$dname/${plugin_short##zsh-}.plugin.zsh"
115 elif [[ -r "$dname/init.zsh" ]]; then
118 fname="$dname/init.zsh"
119 elif [[ ${#zsh_themes} -gt 0 ]]; then
120 # ZSH "theme plugin", ignore here!
123 echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
124 echo "Contents of \"$dname\":" >&2
130 if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
131 # Add plugin function path when folder exists
132 [[ -n "$AXZSH_DEBUG" ]] \
133 && echo " - $plugin ($type): functions ..."
134 axzsh_fpath+=("$dname/functions")
136 # Add function path to cache file.
137 [[ -n "$cache_file" ]] \
138 && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
141 if [[ -r "$fname" ]]; then
143 [[ -n "$AXZSH_DEBUG" ]] \
144 && echo " - $plugin ($type) ..."
146 # Note for "external" ("repo/*") plugins and unusual ("not so
147 # modern") terminals: These (modern?) plugins most probably
148 # don't expect such a terminal configuration and don't behave
149 # well (echo color sequences, for example). Therefore we DON'T
150 # load any external plugins at all in that case: this results in
151 # reduced/disabled functionality, but hopefully in readable
156 axzsh_is_modern_terminal && source "$fname"
162 if [[ -n "$cache_file" ]]; then
163 # Add plugin data to cache
164 printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
167 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type): \"$fname\" ...'" >>$cache_file
168 echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
171 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type, cached) ...'" >>$cache_file
172 "$cat_cmd" "$fname" >>"$cache_file"
174 printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
178 # It is a success, even if only the plugin directory (and no script!)
179 # exists at all! Rationale: The script could be of an other type ...
183 # Make sure that "my" (=ZSH) directory is in the search path ...
184 if [[ -z "$AXZSH" ]]; then
186 [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
190 # Make sure that "SHELL" variable is set and exported
191 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
193 # Make sure that "AXZSH" variable is set and exported
194 if [[ -z "$AXZSH" ]]; then
195 export AXZSH="$HOME/.axzsh"
196 if [[ -f "$HOME/.axzsh.debug" ]]; then
199 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
200 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
204 if [[ "$script_type" = "zprofile" ]]; then
205 # Load all "output" plugins first, that is, before the "zprofile stage":
206 axzsh_handle_stage "$script_name" "ax-io"
209 axzsh_handle_stage "$script_name" "$script_type"
212 unfunction axzsh_handle_stage axzsh_load_plugin
213 unset script_name script_type