1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2017 Alexander Barton <alex@barton.de>
4 script_name="${${(%):-%N}:t}"
5 script_type="$script_name[2,-1]"
7 # Load plugin code of a given type.
9 # - $2: plugin type (optional; defaults to "zshrc")
10 # - $3: cache file (optional)
11 function axzsh_load_plugin {
13 local plugin="$dname:t"
14 [[ -z "$2" ]] && local type="zshrc" || local type="$2"
15 local fname="$dname/$plugin.$type"
18 # Strip repository prefix (like "alexbarton#test-plugin"):
19 [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
21 # "short plugin name": strip ".zsh" suffix:
22 plugin_short=${plugin%.zsh}
24 if [[ ! -d "$dname" ]]; then
26 if [[ -n "$AXZSH_DEBUG" ]]; then
27 # Show error message for all stages in "debug mode":
28 echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
29 elif [[ "$type" == "zshrc" ]]; then
30 # Show error message for the "zshrc" stage:
31 echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
36 if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
37 zsh_themes=("$dname/"*.zsh-theme(NY1))
38 if [[ -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
39 # Native AX-ZSH plugin, but for different stage. Skip it!
41 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
44 fname="$dname/${plugin_short}.plugin.zsh"
45 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
46 # Oh My ZSH plugin with "zsh-" prefix stripped
48 fname="$dname/${plugin_short##zsh-}.plugin.zsh"
49 elif [[ -r "$dname/init.zsh" ]]; then
52 fname="$dname/init.zsh"
53 elif [[ ${#zsh_themes} -gt 0 ]]; then
54 # ZSH "theme plugin", ignore here!
57 echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
58 echo "Contents of \"$dname\":" >&2
64 if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
65 # Add plugin function path when folder exists
66 [[ -n "$AXZSH_DEBUG" ]] \
67 && echo " - $plugin ($type): functions ..."
68 axzsh_fpath+=("$dname/functions")
70 # Add function path to cache file.
71 [[ -n "$cache_file" ]] \
72 && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
75 if [[ -r "$fname" ]]; then
77 [[ -n "$AXZSH_DEBUG" ]] \
78 && echo " - $plugin ($type) ..."
80 # Note for "external" ("repo/*") plugins and "dumb" terminals:
81 # These (modern?) plugins most probably don't expect such an
82 # unusual old terminal configuration and don't behave well
83 # (echo color sequences, for example). Therefore we DON'T load
84 # any external plugins at all when running on such a terminal:
85 # this results in reduced/disabled functionality, but hopefully
86 # in readable output ...
90 axzsh_is_dumb_terminal || source "$fname"
96 if [[ -n "$cache_file" ]]; then
97 # Add plugin data to cache
98 printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
101 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type): \"$fname\" ...'" >>$cache_file
102 echo "axzsh_is_dumb_terminal || source '$fname'" >>$cache_file
105 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type, cached) ...'" >>$cache_file
106 "$cat_cmd" "$fname" >>"$cache_file"
108 printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
112 # It is a success, even if only the plugin directory (and no script!)
113 # exists at all! Rationale: The script could be of an other type ...
117 # Make sure that "my" (=ZSH) directory is in the search path ...
118 if [[ -z "$AXZSH" ]]; then
120 [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
124 # Make sure that "SHELL" variable is set and exported
125 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
127 # Make sure that "AXZSH" variable is set and exported
128 if [[ -z "$AXZSH" ]]; then
129 export AXZSH="$HOME/.axzsh"
130 if [[ -f "$HOME/.axzsh.debug" ]]; then
133 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
134 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
138 [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $script_name:"
141 mkdir -p "$AXZSH/cache"
142 cache_file="$AXZSH/cache/$script_type.cache"
144 cat_cmd=${commands[cat]:-cat}
146 if [[ -r "$cache_file" ]]; then
147 # Cache file exists, use it!
148 # But when in the "zshrc" stage, make sure that the "zprofile" stage
149 # has already been handled (this uses the "01_zprofile" plugin which
150 # is used in the "zshrc.cache" as well, but can't be used successfully
151 # there because it becomes sourced inside of a ZSH function; so we have
152 # to source it here in the global context manually ...):
153 [[ -z "$AXZSH_ZPROFILE_READ" && "$script_type" = "zshrc" ]] \
154 && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
155 [[ -n "$AXZSH_DEBUG" ]] \
156 && echo " - Reading cache file \"$cache_file\" ..."
158 unfunction ax_plugin_init
160 # No cache file available.
161 # Setup list of plugins to load:
162 typeset -U plugin_list
165 "$AXZSH/active_plugins/"*(N)
169 # Create new cache file:
170 if [[ -n "$cache_file" ]]; then
171 [[ -n "$AXZSH_DEBUG" ]] \
172 && echo " (Writing new cache file to \"$cache_file\" ...)"
173 printf "# %s\n\n" "$(LC_ALL=C date)" >"$cache_file"
176 # Read in all the plugins for the current "type":
177 for plugin ($plugin_list); do
178 # Read the "theme file", if any and in "zshrc" stage.
179 # This must be done before 99_cleanup is run!
180 if [[ "$plugin:t" == "99_cleanup" && "$script_type" = "zshrc" ]]; then
181 if [[ -r "$AXZSH_THEME" ]]; then
182 source "$AXZSH_THEME"
183 if [[ -n "$cache_file" ]]; then
184 # Source the theme in the new cache file:
185 echo "# BEGIN Theme" >>"$cache_file"
186 echo 'source "$AXZSH_THEME"' >>"$cache_file"
187 echo "# END Theme" >>"$cache_file"
191 axzsh_load_plugin "$plugin" "$script_type" "$cache_file"
196 unfunction axzsh_load_plugin
197 unset script_name script_type plugin