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