]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Initialize Powerlevel10k in non-login shells, too!
[ax-zsh.git] / ax.zsh
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2022 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         # 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 shells.
28         if [[ ( -o login && "$type" == "zprofile" ) || ( ! -o login && "$type" == "zshrc" ) ]]; then
29                 p10k_instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
30                 [[ -r "$p10k_instant_prompt" ]] && source "$p10k_instant_prompt"
31         fi
32
33         # Initialize cache
34         [[ -d "$AXZSH/cache" ]] || mkdir -p "$AXZSH/cache"
35         local cache_file="$AXZSH/cache/$type.cache"
36
37         local cat_cmd=${commands[cat]:-cat}
38
39         if [[ -r "$cache_file" ]]; then
40                 # Cache file exists, use it!
41                 # But when in the "zshrc" stage, make sure that the "zprofile" stage
42                 # has already been handled (this uses the "01_zprofile" plugin which
43                 # is used in the "zshrc.cache" as well, but can't be used successfully
44                 # there because it becomes sourced inside of a ZSH function; so we have
45                 # to source it here in the global context manually ...):
46                 [[ -z "$AXZSH_ZPROFILE_READ" && "$type" = "zshrc" ]] \
47                         && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
48                 [[ -n "$AXZSH_DEBUG" ]] \
49                         && echo "   - Reading cache file \"$cache_file\" ..."
50                 source "$cache_file"
51                 unfunction axzsh_plugin_init
52         else
53                 # No cache file available.
54                 local new_cache_file="$cache_file.NEW"
55
56                 # Setup list of plugins to load:
57                 local plugin_list
58                 typeset -U plugin_list
59                 plugin_list=(
60                         "$AXZSH/core/"[0-4]*
61                         "$AXZSH/active_plugins/"*(N)
62                         "$AXZSH/core/"[5-9]*
63                 )
64
65                 # Create new cache file:
66                 [[ -n "$AXZSH_DEBUG" ]] \
67                         && echo "   (Writing new cache file to \"$new_cache_file\" ...)"
68                 if ! printf "# %s\n\n" "$(LC_ALL=C date)" >"$new_cache_file"; then
69                         unset new_cache_file
70                 else
71                         # New cache file successfully created ...
72                         if [[ "$type" = "ax-io" ]]; then
73                                 # AX-IO Stage:
74                                 # Write an initial PATH variable to the cache
75                                 # file, which becomes overwritten by the path
76                                 # plugin at the "zprofile" stage later on, but
77                                 # this way "ax-io" stage plugins have a somewhat
78                                 # saner PATH to begin with ...
79                                 printf 'export PATH="%s"\n\n' "$PATH" >>"$new_cache_file"
80                         fi
81                 fi
82
83                 # Read in all the plugins for the current "type":
84                 for plugin ($plugin_list); do
85                         # Read the "theme file", if any and in "zshrc" stage.
86                         # This must be done before 99_cleanup is run!
87                         if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
88                                 if [[ -r "$AXZSH_THEME" ]]; then
89                                         source "$AXZSH_THEME"
90                                         if [[ -n "$new_cache_file" ]]; then
91                                                 # Source the theme in the new cache file:
92                                                 echo "# BEGIN Theme" >>"$new_cache_file"
93                                                 echo 'source "$AXZSH_THEME"' >>"$new_cache_file"
94                                                 echo "# END Theme" >>"$new_cache_file"
95                                         fi
96                                 fi
97                         fi
98                         axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
99                 done
100
101                 if [[ -n "$cache_file" && -n "$new_cache_file" && -r "$new_cache_file" ]]; then
102                         # Move newly created cache file in place:
103                         mv "$new_cache_file" "$cache_file"
104                 fi
105         fi
106 }
107
108 # Load plugin code of a given type.
109 # - $1: plugin name
110 # - $2: plugin type (optional; defaults to "zshrc")
111 # - $3: cache file (optional)
112 function axzsh_load_plugin {
113         local dname="$1:A"
114         local plugin="$dname:t"
115         [[ -z "$2" ]] && local type="zshrc" || local type="$2"
116         local fname="$dname/$plugin.$type"
117         local cache_file="$3"
118
119         # Strip repository prefix (like "alexbarton#test-plugin"):
120         [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
121
122         # "short plugin name": strip ".zsh" suffix:
123         plugin_short=${plugin%.zsh}
124
125         if [[ ! -d "$dname" ]]; then
126                 # Plugin not found!
127                 if [[ -n "$AXZSH_DEBUG" ]]; then
128                         # Show error message for all stages in "debug mode":
129                         echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
130                 elif [[ "$type" == "zshrc" ]]; then
131                         # Show error message for the "zshrc" stage:
132                         echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
133                 fi
134                 return 1
135         fi
136
137         if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
138                 zsh_themes=("$dname/"*.zsh-theme(NY1))
139                 if [[ -r "$dname/$plugin.ax-io" || -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
140                         # Native AX-ZSH plugin, but for different stage. Skip it!
141                         :
142                 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
143                         # Oh My ZSH plugin
144                         type="plugin.zsh"
145                         fname="$dname/${plugin_short}.plugin.zsh"
146                 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
147                         # Oh My ZSH plugin with "zsh-" prefix stripped
148                         type="plugin.zsh"
149                         fname="$dname/${plugin_short##zsh-}.plugin.zsh"
150                 elif [[ -r "$dname/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
151                         # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
152                         type="plugin.zsh"
153                         fname="$dname/${plugin}"
154                 elif [[ -r "$dname/init.zsh" ]]; then
155                         # Prezto module
156                         type="init.zsh"
157                         fname="$dname/init.zsh"
158                 elif [[ ${#zsh_themes} -gt 0 ]]; then
159                         # ZSH "theme plugin", ignore here!
160                         :
161                 else
162                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
163                         echo "Contents of \"$dname\":" >&2
164                         ls -lh "$dname/" >&2
165                         return 0
166                 fi
167         fi
168
169         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
170                 # Add plugin function path when folder exists
171                 [[ -n "$AXZSH_DEBUG" ]] \
172                         && echo "   - $plugin ($type): functions ..."
173                 axzsh_fpath+=("$dname/functions")
174
175                 # Add function path to cache file.
176                 [[ -n "$cache_file" ]] \
177                         && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
178         fi
179
180         if [[ -r "$fname" ]]; then
181                 # Read plugin ...
182                 [[ -n "$AXZSH_DEBUG" ]] \
183                         && echo "   - $plugin ($type) ..."
184
185                 # Note for "external" ("repo/*") plugins and unusual ("not so
186                 # modern") terminals: These (modern?) plugins most probably
187                 # don't expect such a terminal configuration and don't behave
188                 # well (echo color sequences, for example). Therefore we DON'T
189                 # load any external plugins at all in that case: this results in
190                 # reduced/disabled functionality, but hopefully in readable
191                 # output ...
192
193                 case "$fname" in
194                         *"/repos/"*)
195                                 axzsh_is_modern_terminal && source "$fname"
196                                 ;;
197                         *)
198                                 source "$fname"
199                 esac
200
201                 if [[ -n "$cache_file" ]]; then
202                         # Add plugin data to cache
203                         printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$fname" >>"$cache_file"
204                         case "$fname" in
205                                 *"/repos/"*)
206                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
207                                         echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
208                                         ;;
209                                 *)
210                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
211                                         "$cat_cmd" "$fname" >>"$cache_file"
212                         esac
213                         printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
214                 fi
215         fi
216
217         # It is a success, even if only the plugin directory (and no script!)
218         # exists at all! Rationale: The script could be of an other type ...
219         return 0
220 }
221
222 # Make sure that "my" (=ZSH) directory is in the search path ...
223 if [[ -z "$AXZSH" ]]; then
224         _p="${0:h}"
225         [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
226         unset _p
227 fi
228
229 # Make sure that "SHELL" variable is set and exported
230 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
231
232 # Make sure that "AXZSH" variable is set and exported
233 [[ -n "$AXZSH" ]] || export AXZSH="${ZDOTDIR:-$HOME}/.axzsh"
234
235 # Check for "debug mode" ...
236 if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
237         export AXZSH_DEBUG=1
238         export POWERLEVEL9K_INSTANT_PROMPT=quiet
239         echo "AXZSH=$AXZSH"
240         echo "AXZSH_DEBUG=$AXZSH_DEBUG"
241         echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
242         echo "AXZSH_ZLOGIN_READ=$AXZSH_ZLOGIN_READ"
243         echo "AXZSH_ZPROFILE_READ=$AXZSH_ZPROFILE_READ"
244 fi
245
246 if [[ "$script_type" = "zprofile" ]]; then
247         # Load all "output" plugins first, that is, before the "zprofile stage":
248         axzsh_handle_stage "$script_name" "ax-io"
249 fi
250
251 axzsh_handle_stage "$script_name" "$script_type"
252
253 # Clean up ...
254 unfunction axzsh_handle_stage axzsh_load_plugin
255 unset script_name script_type
256
257 # Hints for external installers:
258 # - iTerm2: DON'T install "iterm2_shell_integration"!