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