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 if [[ -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
38 # Native AX-ZSH plugin, but for different stage. Skip it!
40 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
43 fname="$dname/${plugin_short}.plugin.zsh"
44 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
45 # Oh My ZSH plugin with "zsh-" prefix stripped
47 fname="$dname/${plugin_short##zsh-}.plugin.zsh"
48 elif [[ -r "$dname/init.zsh" ]]; then
51 fname="$dname/init.zsh"
53 echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
58 if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
59 # Add plugin function path when folder exists
60 [[ -n "$AXZSH_DEBUG" ]] \
61 && echo " - $plugin ($type): functions ..."
62 axzsh_fpath+=("$dname/functions")
64 # Add function path to cache file.
65 [[ -n "$cache_file" ]] \
66 && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
69 if [[ -r "$fname" ]]; then
71 [[ -n "$AXZSH_DEBUG" ]] \
72 && echo " - $plugin ($type) ..."
74 # Note for "external" ("repo/*") plugins and "dumb" terminals:
75 # These (modern?) plugins most probably don't expect such an
76 # unusual old terminal configuration and don't behave well
77 # (echo color sequences, for example). Therefore we DON'T load
78 # any external plugins at all when running on such a terminal:
79 # this results in reduced/disabled functionality, but hopefully
80 # in readable output ...
84 axzsh_is_dumb_terminal || source "$fname"
90 if [[ -n "$cache_file" ]]; then
91 # Add plugin data to cache
92 printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
95 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type): \"$fname\" ...'" >>$cache_file
96 echo "axzsh_is_dumb_terminal || source '$fname'" >>$cache_file
99 echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo ' - $plugin ($type, cached) ...'" >>$cache_file
100 "$cat_cmd" "$fname" >>"$cache_file"
102 printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
106 # It is a success, even if only the plugin directory (and no script!)
107 # exists at all! Rationale: The script could be of an other type ...
111 # Make sure that "my" (=ZSH) directory is in the search path ...
112 if [[ -z "$AXZSH" ]]; then
114 [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
118 # Make sure that "SHELL" variable is set and exported
119 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
121 # Make sure that "AXZSH" variable is set and exported
122 if [[ -z "$AXZSH" ]]; then
123 export AXZSH="$HOME/.axzsh"
124 if [[ -f "$HOME/.axzsh.debug" ]]; then
127 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
128 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
132 [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $script_name:"
135 mkdir -p "$AXZSH/cache"
136 cache_file="$AXZSH/cache/$script_type.cache"
138 cat_cmd=${commands[cat]:-cat}
140 if [[ -r "$cache_file" ]]; then
141 # Cache file exists, use it!
142 # But when in the "zshrc" stage, make sure that the "zprofile" stage
143 # has already been handled (this uses the "01_zprofile" plugin which
144 # is used in the "zshrc.cache" as well, but can't be used successfully
145 # there because it becomes sourced inside of a ZSH function; so we have
146 # to source it here in the global context manually ...):
147 [[ -z "$AXZSH_ZPROFILE_READ" && "$script_type" = "zshrc" ]] \
148 && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
149 [[ -n "$AXZSH_DEBUG" ]] \
150 && echo " - Reading cache file \"$cache_file\" ..."
152 unfunction ax_plugin_init
154 # No cache file available.
155 # Setup list of plugins to load:
156 typeset -U plugin_list
159 "$AXZSH/active_plugins/"*(N)
163 # Create new cache file:
164 if [[ -n "$cache_file" ]]; then
165 [[ -n "$AXZSH_DEBUG" ]] \
166 && echo " (Writing new cache file to \"$cache_file\" ...)"
167 printf "# %s\n\n" "$(LC_ALL=C date)" >"$cache_file"
170 # Read in all the plugins for the current "type":
171 for plugin ($plugin_list); do
172 axzsh_load_plugin "$plugin" "$script_type" "$cache_file"
177 unfunction axzsh_load_plugin
178 unset script_name script_type plugin