3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2017 Alexander Barton <alex@barton.de>
7 # Include "ax-common.sh", if available:
8 for dir ("$HOME/lib" "$HOME/.ax" /usr/local /opt/ax /usr); do
9 [[ -z "$ax_common_sourced" ]] || break
10 ax_common="${dir}/lib/ax/ax-common.sh"
11 [[ -r "$ax_common" ]] && source "$ax_common"
13 if [[ -z "$ax_common_sourced" ]]; then
19 unset dir ax_common ax_common_sourced
22 echo "Usage: $NAME <command> [...]"
25 echo " Enable AX-ZSH altogether."
27 echo " Disable AX-ZSH altogether."
29 echo " enable-plugin <name|directory> [<name|directory> [...]]"
30 echo " Enable plugin(s)."
31 echo " disable-plugin <name> [<name> [...]]"
32 echo " Disable plugin(s)."
34 echo " List enabled plugins."
37 echo " Reset active plugins to the default set."
38 echo " enable-default-plugins"
39 echo " Enable all default plugins."
41 echo " Detect plugins which are \"useful\" on this system."
43 echo " set-theme <name>|-"
44 echo " Set active theme to <name>, or to the default."
47 echo " Upgrade AX-ZSH installation (requires Git)."
49 echo " Force rebuild of all cache files."
54 function UpdatePluginCache {
55 [[ -r "$AXZSH/cache" ]] || return 0
57 ax_msg - "Updating plugin cache ..."
59 $AXZSH/cache/zlogin.cache \
60 $AXZSH/cache/zlogout.cache \
61 $AXZSH/cache/zprofile.cache \
62 $AXZSH/cache/zshrc.cache \
64 echo "Regenerating cache files ..."
65 zsh -ilc '' >/dev/null
68 function NormalizedPluginName {
69 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
71 elif [[ "$1" =~ "/" ]]; then
78 function EnableAXZSH {
79 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
80 ln -sv "$AXZSH/ax.zsh" "$f" \
81 || ax_msg 2 "Failed to create symbolic link for \"$f\"!"
85 function DisableAXZSH {
86 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
88 rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
89 elif [ -e "$f" ]; then
90 ax_msg 2 "Error: Not removing \"$f\", it is not a symbolic link!"
92 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
97 function EnablePlugin {
98 local plugin=$(NormalizedPluginName "$1")
99 local dir="$AXZSH/active_plugins"
101 if [[ -h "$dir/$plugin" ]]; then
102 ax_msg 1 "Plugin \"$1\" already active!"
106 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
108 mkdir -p "$AXZSH/repos"
109 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
110 ax_msg - "Cloning plugin from GitHub ..."
111 git clone "https://github.com/$1.git" \
112 "$AXZSH/repos/$plugin"
113 # Try to enable a theme in this "foreign" plugin, but
114 # ignore errors: we don't know if this plugin provides
116 SetTheme "${plugin#*#}" 2>/dev/null
122 "$AXZSH_PLUGIN_D/$plugin"
123 "$ZSH_CUSTOM/$plugin"
124 "$AXZSH/custom_plugins/$plugin"
125 "$AXZSH/repos/$plugin"
126 "$AXZSH/plugins/$plugin"
127 "$AXZSH/default_plugins/$plugin"
128 "$AXZSH/core/$plugin"
130 [[ ! -d "$dname" ]] && continue
134 ln -sv "$dname" "$PWD"
139 ax_msg 2 "Plugin \"$1\" not found!"
143 function DisablePlugin {
144 local plugin=$(NormalizedPluginName "$1")
145 local dir="$AXZSH/active_plugins"
147 if [[ ! -h "$dir/$plugin" ]]; then
148 ax_msg 1 "Plugin \"$1\" not active?"
152 rm -v "$dir/$plugin"; r=$?
153 [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
157 function ListEnabledPlugins {
158 for plugin ($AXZSH/active_plugins/*(N)); do
159 print ${plugin:t:s/#/\//}
164 function ResetPlugins {
165 local dir="$AXZSH/active_plugins"
168 if [[ -e "$dir" ]]; then
169 ax_msg - "Removing all symbolic links in $dir ..."
170 find "$dir" -type l -print -delete; r1=$?
173 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
174 rm -fr "$AXZSH/repos"; r2=$?
176 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
179 function EnableDefaultPlugins {
180 local dir="$AXZSH/active_plugins"
182 ax_msg - "Activating (linking) default plugins ..."
186 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
192 local link_name="$AXZSH/active_theme"
194 if [ $# -ne 1 ]; then
195 echo "Usage: axzsh_set_theme <name|->"
199 rm -f "$link_name" || return 1
201 if [ "$1" = "-" ]; then
202 echo "Theme settings have been reset."
208 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
209 theme="$AXZSH/custom_themes/$1.axzshtheme"
210 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
211 theme="$AXZSH/themes/$1.axzshtheme"
213 # Look for theme inside of installed plugins:
215 "$AXZSH/custom_plugins/"*(N)
218 if [[ -r "$dname/$1.axzshtheme" ]]; then
219 theme="$dname/$1.axzshtheme"
221 elif [[ -r "$dname/$1.zsh-theme" ]]; then
222 theme="$dname/$1.zsh-theme"
226 if [[ -z "$theme" ]]; then
227 echo "Theme \"$1\" not found!"
231 ln -sv "$theme" "$link_name" || return 1
235 function UpgradeAXZSH {
236 if [[ $+commands[git] -eq 0 ]]; then
237 ax_msg 2 "The git(1) command is not available!"
240 if [[ ! -d "$AXZSH/.git" ]]; then
241 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
245 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
246 ( cd "$AXZSH" && git pull --ff-only )
249 function UpgradeForeignPlugins {
250 if [[ $+commands[git] -eq 0 ]]; then
251 ax_msg 2 "The git(1) command is not available!"
255 for dir ($AXZSH/repos/*(N)); do
256 name=${dir:t:s/#/\//}
257 if [ -d "$dir/.git" ]; then
258 ax_msg - "Upgrading \"$name\" [git] ..."
261 git pull --ff-only || ax_msg 2 "Pull failed!"
264 ax_msg 2 "Unknown repository type!"
269 function CheckPlugins {
273 ax_msg - "Checking plugins ..."
274 for dir ($AXZSH/plugins/*(N)); do
277 # Test if plugin is already enabled
278 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
279 && enabled=" (enabled)" \
283 printf " - \"%s\"%s ... " "$plugin" "$enabled"
285 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
286 [[ -r "$script" ]] || continue
287 AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
288 if [[ $r -eq 0 ]]; then
293 if [[ -n "$new_plugin" ]]; then
294 detected_plugins+=($new_plugin)
295 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
297 elif [[ $r -eq 91 ]]; then
299 elif [[ $r -eq 92 ]]; then
302 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
309 if [[ -n "$missing_plugins" ]]; then
310 ax_msg 1 "Run the following command to enable all missing plugins:"
311 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
315 ax_msg 0 "All detected plugins are already enabled."
318 if [[ -n "$invalid_plugins" ]]; then
319 ax_msg 1 "Run the following command to disable all failed plugins:"
320 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
323 ax_msg 0 "No failed plugins are enabled."
332 [[ $# -gt 0 ]] || Usage
334 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
335 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
336 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
337 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
347 [[ $# -eq 0 ]] || Usage
351 [[ $# -eq 0 ]] || Usage
355 [[ $# -gt 0 ]] || Usage
356 for plugin in "$@"; do
357 EnablePlugin "$plugin"
362 [[ $# -gt 0 ]] || Usage
363 for plugin in "$@"; do
364 DisablePlugin "$plugin"
369 [[ $# -eq 0 ]] || Usage
373 [[ $# -eq 0 ]] || Usage
378 "enable-default-plugins")
379 [[ $# -eq 0 ]] || Usage
380 EnableDefaultPlugins && UpdatePluginCache
383 [[ $# -eq 0 ]] || Usage
387 [[ $# -eq 1 ]] || Usage
388 SetTheme "$1" && UpdatePluginCache
391 [[ $# -eq 0 ]] || Usage
393 UpgradeForeignPlugins
397 [[ $# -eq 0 ]] || Usage