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