]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
30_env.ax-io: Use mktemp(3) to detect default remporary directory
[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/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
131                         # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
132                         type="plugin.zsh"
133                         fname="$dname/${plugin}"
134                 elif [[ -r "$dname/init.zsh" ]]; then
135                         # Prezto module
136                         type="init.zsh"
137                         fname="$dname/init.zsh"
138                 elif [[ ${#zsh_themes} -gt 0 ]]; then
139                         # ZSH "theme plugin", ignore here!
140                         :
141                 else
142                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
143                         echo "Contents of \"$dname\":" >&2
144                         ls -lh "$dname/" >&2
145                         return 0
146                 fi
147         fi
148
149         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
150                 # Add plugin function path when folder exists
151                 [[ -n "$AXZSH_DEBUG" ]] \
152                         && echo "   - $plugin ($type): functions ..."
153                 axzsh_fpath+=("$dname/functions")
154
155                 # Add function path to cache file.
156                 [[ -n "$cache_file" ]] \
157                         && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
158         fi
159
160         if [[ -r "$fname" ]]; then
161                 # Read plugin ...
162                 [[ -n "$AXZSH_DEBUG" ]] \
163                         && echo "   - $plugin ($type) ..."
164
165                 # Note for "external" ("repo/*") plugins and unusual ("not so
166                 # modern") terminals: These (modern?) plugins most probably
167                 # don't expect such a terminal configuration and don't behave
168                 # well (echo color sequences, for example). Therefore we DON'T
169                 # load any external plugins at all in that case: this results in
170                 # reduced/disabled functionality, but hopefully in readable
171                 # output ...
172
173                 case "$fname" in
174                         *"/repos/"*)
175                                 axzsh_is_modern_terminal && source "$fname"
176                                 ;;
177                         *)
178                                 source "$fname"
179                 esac
180
181                 if [[ -n "$cache_file" ]]; then
182                         # Add plugin data to cache
183                         printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
184                         case "$fname" in
185                                 *"/repos/"*)
186                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
187                                         echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
188                                         ;;
189                                 *)
190                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
191                                         "$cat_cmd" "$fname" >>"$cache_file"
192                         esac
193                         printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
194                 fi
195         fi
196
197         # It is a success, even if only the plugin directory (and no script!)
198         # exists at all! Rationale: The script could be of an other type ...
199         return 0
200 }
201
202 # Make sure that "my" (=ZSH) directory is in the search path ...
203 if [[ -z "$AXZSH" ]]; then
204         _p="${0:h}"
205         [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
206         unset _p
207 fi
208
209 # Make sure that "SHELL" variable is set and exported
210 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
211
212 # Make sure that "AXZSH" variable is set and exported
213 [[ -n "$AXZSH" ]] || export AXZSH="$HOME/.axzsh"
214
215 # Check for "debug mode" ...
216 if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
217         export AXZSH_DEBUG=1
218         echo "AXZSH=$AXZSH"
219         echo "AXZSH_DEBUG=$AXZSH_DEBUG"
220         echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
221         echo "AXZSH_ZLOGIN_READ=$AXZSH_ZLOGIN_READ"
222         echo "AXZSH_ZPROFILE_READ=$AXZSH_ZPROFILE_READ"
223 fi
224
225 if [[ "$script_type" = "zprofile" ]]; then
226         # Load all "output" plugins first, that is, before the "zprofile stage":
227         axzsh_handle_stage "$script_name" "ax-io"
228 fi
229
230 axzsh_handle_stage "$script_name" "$script_type"
231
232 # Clean up ...
233 unfunction axzsh_handle_stage axzsh_load_plugin
234 unset script_name script_type
235
236 # Hints for external installers:
237 # - iTerm2: DON'T install "iterm2_shell_integration"!