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