]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
P10k: Read config after enabling instant prompt
[ax-zsh.git] / ax.zsh
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2024 Alexander Barton <alex@barton.de>
3
4 script_name="${${(%):-%N}:t}"
5 script_type="$script_name[2,-1]"
6
7 # Handle "initialization stage", load all plugins of that stage, either from an
8 # existing cache file or individually, optionally creating the cache.
9 # - $1: Script name
10 # - $2: Stage name (ax-io, zprofile, zshrc, zlogin, zlogout)
11 function axzsh_handle_stage {
12         local name="$1"
13         local type="$2"
14
15         [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $name ($type):"
16
17         # Look for some 3rd-party integrations ...
18
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).
28         if [[ \
29                 ( "$type" == "zprofile" ) || \
30                 ( ! -o login && "$type" == "zshrc" && -n "$AXZSH_ZPROFILE_READ" ) \
31         ]]; then
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"
36                 fi
37                 unset p10k_instant_prompt
38         fi
39         # Read in Powerlevel10k configuration file, if not already read:
40         [[ -z "$POWERLEVEL9K_CONFIG_FILE" && -r ~/.p10k.zsh ]] && source ~/.p10k.zsh
41
42         # Initialize cache
43         [[ -d "$AXZSH/cache" ]] || mkdir -p "$AXZSH/cache"
44         local cache_file="$AXZSH/cache/$type.cache"
45
46         local cat_cmd=${commands[cat]:-cat}
47
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\" ..."
59                 source "$cache_file"
60                 (( $+functions[axzsh_plugin_init] )) && unfunction axzsh_plugin_init
61         else
62                 # No cache file available.
63                 local new_cache_file="$cache_file.NEW"
64
65                 # Setup list of plugins to load:
66                 local plugin_list
67                 typeset -U plugin_list
68                 plugin_list=(
69                         "$AXZSH/core/"[0-4]*
70                         "$AXZSH/active_plugins/"*(N)
71                         "$AXZSH/core/"[5-9]*
72                 )
73
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
78                         unset new_cache_file
79                 else
80                         # New cache file successfully created ...
81                         if [[ "$type" = "ax-io" ]]; then
82                                 # AX-IO Stage:
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"
89                         fi
90                 fi
91
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
98                                         source "$AXZSH_THEME"
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"
104                                         fi
105                                 fi
106                         fi
107                         axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
108                 done
109
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"
113                 fi
114         fi
115 }
116
117 # Load plugin code of a given type.
118 # - $1: plugin name
119 # - $2: plugin type (optional; defaults to "zshrc")
120 # - $3: cache file (optional)
121 function axzsh_load_plugin {
122         local dname="$1:A"
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"
127
128         # Strip repository prefix (like "alexbarton#test-plugin"):
129         [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
130
131         # "short plugin name": strip ".zsh" suffix:
132         plugin_short=${plugin%.zsh}
133
134         if [[ ! -d "$dname" ]]; then
135                 # Plugin not found!
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
142                 fi
143                 return 1
144         fi
145
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!
150                         :
151                 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
152                         # Oh My ZSH plugin
153                         type="plugin.zsh"
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
157                         type="plugin.zsh"
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
161                         type="plugin.zsh"
162                         fname="$dname/${plugin}"
163                 elif [[ -r "$dname/init.zsh" ]]; then
164                         # Prezto module
165                         type="init.zsh"
166                         fname="$dname/init.zsh"
167                 elif [[ ${#zsh_themes} -gt 0 ]]; then
168                         # ZSH "theme plugin", ignore here!
169                         :
170                 else
171                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
172                         echo "Contents of \"$dname\":" >&2
173                         ls -lh "$dname/" >&2
174                         return 0
175                 fi
176         fi
177
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")
183
184                 # Add function path to cache file.
185                 [[ -n "$cache_file" ]] \
186                         && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
187         fi
188
189         if [[ -r "$fname" ]]; then
190                 # Read plugin ...
191                 [[ -n "$AXZSH_DEBUG" ]] \
192                         && echo "   - $plugin ($type) ..."
193
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
200                 # output ...
201
202                 case "$fname" in
203                         *"/repos/"*)
204                                 axzsh_is_modern_terminal && source "$fname"
205                                 ;;
206                         *)
207                                 source "$fname"
208                 esac
209
210                 if [[ -n "$cache_file" ]]; then
211                         # Add plugin data to cache
212                         printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$fname" >>"$cache_file"
213                         case "$fname" in
214                                 *"/repos/"*)
215                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
216                                         echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
217                                         ;;
218                                 *)
219                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
220                                         "$cat_cmd" "$fname" >>"$cache_file"
221                         esac
222                         printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
223                 fi
224         fi
225
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 ...
228         return 0
229 }
230
231 # Make sure that "my" (=ZSH) directory is in the search path ...
232 if [[ -z "$AXZSH" ]]; then
233         _p="${0:h}"
234         [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
235         unset _p
236 fi
237
238 # Make sure that "SHELL" variable is set and exported
239 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
240
241 # Make sure that "AXZSH" variable is set and exported
242 [[ -n "$AXZSH" ]] || export AXZSH="${ZDOTDIR:-$HOME}/.axzsh"
243
244 # Check for "debug mode" ...
245 if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
246         export AXZSH_DEBUG=1
247         export POWERLEVEL9K_INSTANT_PROMPT=quiet
248         echo "AXZSH=$AXZSH"
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"
253 fi
254
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"
258 fi
259
260 axzsh_handle_stage "$script_name" "$script_type"
261
262 # Clean up ...
263 unfunction axzsh_handle_stage axzsh_load_plugin
264 unset script_name script_type
265
266 # Hints for external installers:
267 # - iTerm2: DON'T install "iterm2_shell_integration"!