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