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