]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
homebrew: Set HOMEBREW_AUTO_UPDATE_SECS to 10 minutes
[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                 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!
40                         :
41                 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
42                         # Oh My ZSH plugin
43                         type="plugin.zsh"
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
47                         type="plugin.zsh"
48                         fname="$dname/${plugin_short##zsh-}.plugin.zsh"
49                 elif [[ -r "$dname/init.zsh" ]]; then
50                         # Prezto module
51                         type="init.zsh"
52                         fname="$dname/init.zsh"
53                 elif [[ ${#zsh_themes} -gt 0 ]]; then
54                         # ZSH "theme plugin", ignore here!
55                         :
56                 else
57                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
58                         echo "Contents of \"$dname\":" >&2
59                         ls -lh "$dname/" >&2
60                         return 0
61                 fi
62         fi
63
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")
69
70                 # Add function path to cache file.
71                 [[ -n "$cache_file" ]] \
72                         && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
73         fi
74
75         if [[ -r "$fname" ]]; then
76                 # Read plugin ...
77                 [[ -n "$AXZSH_DEBUG" ]] \
78                         && echo "   - $plugin ($type) ..."
79
80                 # Note for "external" ("repo/*") plugins and unusual ("not so
81                 # modern") terminals: These (modern?) plugins most probably
82                 # don't expect such a terminal configuration and don't behave
83                 # well (echo color sequences, for example). Therefore we DON'T
84                 # load any external plugins at all in that case: this results in
85                 # reduced/disabled functionality, but hopefully in readable
86                 # output ...
87
88                 case "$fname" in
89                         *"/repos/"*)
90                                 axzsh_is_modern_terminal && source "$fname"
91                                 ;;
92                         *)
93                                 source "$fname"
94                 esac
95
96                 if [[ -n "$cache_file" ]]; then
97                         # Add plugin data to cache
98                         printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
99                         case "$fname" in
100                                 *"/repos/"*)
101                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
102                                         echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
103                                         ;;
104                                 *)
105                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
106                                         "$cat_cmd" "$fname" >>"$cache_file"
107                         esac
108                         printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
109                 fi
110         fi
111
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 ...
114         return 0
115 }
116
117 # Make sure that "my" (=ZSH) directory is in the search path ...
118 if [[ -z "$AXZSH" ]]; then
119         _p="${0:h}"
120         [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
121         unset _p
122 fi
123
124 # Make sure that "SHELL" variable is set and exported
125 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
126
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
131                 export AXZSH_DEBUG=1
132                 echo "AXZSH=$AXZSH"
133                 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
134                 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
135         fi
136 fi
137
138 [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $script_name:"
139
140 # Initialize cache
141 mkdir -p "$AXZSH/cache"
142 cache_file="$AXZSH/cache/$script_type.cache"
143
144 cat_cmd=${commands[cat]:-cat}
145
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\" ..."
157         source "$cache_file"
158         unfunction ax_plugin_init
159 else
160         # No cache file available.
161         # Setup list of plugins to load:
162         typeset -U plugin_list
163         plugin_list=(
164                 "$AXZSH/core/"[0-5]*
165                 "$AXZSH/active_plugins/"*(N)
166                 "$AXZSH/core/"[6-9]*
167         )
168
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"
174         fi
175
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"
188                                 fi
189                         fi
190                 fi
191                 axzsh_load_plugin "$plugin" "$script_type" "$cache_file"
192         done
193 fi
194
195 # Clean up ...
196 unfunction axzsh_load_plugin
197 unset script_name script_type plugin
198 unset plugin_list
199 unset cache_file
200 unset cat_cmd