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
22 unset dir ax_common ax_common_sourced
25 echo "Usage: $NAME <command> [...]"
28 echo " Enable AX-ZSH altogether."
30 echo " Disable AX-ZSH altogether."
32 echo " enable-plugin <name|directory> [<name|directory> [...]]"
33 echo " Enable plugin(s)."
34 echo " disable-plugin <name> [<name> [...]]"
35 echo " Disable plugin(s)."
37 echo " List enabled plugins."
40 echo " Reset active plugins to the default set."
41 echo " enable-default-plugins"
42 echo " Enable all default plugins."
44 echo " Detect plugins which are \"useful\" on this system."
46 echo " set-theme <name>|-"
47 echo " Set active theme to <name>, or to the default."
50 echo " Upgrade AX-ZSH installation (requires Git)."
52 echo " Force rebuild of all cache files."
57 function UpdatePluginCache {
58 [[ -r "$AXZSH/cache" ]] || return 0
60 ax_msg - "Updating plugin cache ..."
62 $AXZSH/cache/zlogin.cache \
63 $AXZSH/cache/zlogout.cache \
64 $AXZSH/cache/zprofile.cache \
65 $AXZSH/cache/zshrc.cache \
67 echo "Regenerating cache files ..."
68 zsh -ilc '' >/dev/null
71 function NormalizedPluginName {
72 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
74 elif [[ "$1" =~ "/" ]]; then
81 function EnableAXZSH {
82 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
83 ln -sv "$AXZSH/ax.zsh" "$f" \
84 || ax_error "Failed to create symbolic link for \"$f\"!"
88 function DisableAXZSH {
89 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
91 rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
92 elif [ -e "$f" ]; then
93 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
95 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
100 function EnablePlugin {
101 local plugin=$(NormalizedPluginName "$1")
102 local dir="$AXZSH/active_plugins"
104 if [[ -h "$dir/$plugin" ]]; then
105 ax_msg 1 "Plugin \"$1\" already active!"
109 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
111 mkdir -p "$AXZSH/repos"
112 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
113 ax_msg - "Cloning module from GitHub ..."
114 git clone --depth=1 "https://github.com/$1.git" \
115 "$AXZSH/repos/$plugin"
117 # Try to enable a theme in this "foreign module", but ignore
118 # errors: we don't know if this module provides a theme or is
119 # a "regular" plugin ...
120 if SetTheme "${plugin#*#}" 2>/dev/null; then
121 ax_msg 1 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
122 # A theme was enabled: So assume that this is a theme
123 # and don't enable it as plugin.
126 echo "Module \"$1\" will be enabled as plugin ..."
131 "$AXZSH_PLUGIN_D/$plugin"
132 "$ZSH_CUSTOM/$plugin"
133 "$AXZSH/custom_plugins/$plugin"
134 "$AXZSH/repos/$plugin"
135 "$AXZSH/plugins/$plugin"
136 "$AXZSH/default_plugins/$plugin"
137 "$AXZSH/core/$plugin"
139 [[ ! -d "$dname" ]] && continue
143 ln -sv "$dname" "$PWD"
148 ax_error "Plugin \"$1\" not found!"
152 function DisablePlugin {
153 local plugin=$(NormalizedPluginName "$1")
154 local dir="$AXZSH/active_plugins"
158 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
159 rm "$AXZSH/active_theme"; r=$?
163 if [[ -h "$dir/$plugin" ]]; then
164 rm "$dir/$plugin"; r=$?
167 if [[ $r -eq -1 ]]; then
168 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
172 if [[ "$plugin" = *"#"* ]]; then
173 # Name matches a cloned repository, try to clean up!
174 echo "Cleaning up cloned repository ..."
175 rm -fr "$AXZSH/repos/$plugin"
181 function ListEnabledPlugins {
182 for plugin ($AXZSH/active_plugins/*(N)); do
183 print ${plugin:t:s/#/\//}
188 function ResetPlugins {
189 local dir="$AXZSH/active_plugins"
192 if [[ -e "$dir" ]]; then
193 ax_msg - "Removing all symbolic links in $dir ..."
194 find "$dir" -type l -print -delete; r1=$?
197 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
198 rm -fr "$AXZSH/repos"; r2=$?
200 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
203 function EnableDefaultPlugins {
204 local dir="$AXZSH/active_plugins"
206 ax_msg - "Activating (linking) default plugins ..."
210 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
216 local link_name="$AXZSH/active_theme"
218 if [ "$1" = "-" ]; then
219 rm -f "$link_name" || return 1
220 ax_msg 0 "Theme settings have been reset."
226 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
227 theme="$AXZSH/custom_themes/$1.axzshtheme"
228 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
229 theme="$AXZSH/themes/$1.axzshtheme"
231 # Look for theme inside of installed plugins:
233 "$AXZSH/custom_themes"
234 "$AXZSH/custom_plugins/"*(N)
237 if [[ -r "$dname/$1.axzshtheme" ]]; then
238 theme="$dname/$1.axzshtheme"
240 elif [[ -r "$dname/$1.zsh-theme" ]]; then
241 theme="$dname/$1.zsh-theme"
245 if [[ -z "$theme" ]]; then
246 ax_error "Theme \"$1\" not found!"
250 ln -fsv "$theme" "$link_name" || return 1
254 function UpgradeAXZSH {
255 if [[ $+commands[git] -eq 0 ]]; then
256 ax_error "The git(1) command is not available!"
259 if [[ ! -d "$AXZSH/.git" ]]; then
260 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
264 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
265 ( cd "$AXZSH" && git pull --ff-only )
268 function UpgradeForeignPlugins {
269 if [[ $+commands[git] -eq 0 ]]; then
270 ax_error "The git(1) command is not available!"
274 for dir ($AXZSH/repos/*(N)); do
275 name=${dir:t:s/#/\//}
276 if [ -d "$dir/.git" ]; then
277 ax_msg - "Upgrading \"$name\" [git] ..."
280 git pull --ff-only || ax_error "Pull failed!"
283 ax_error "Unknown repository type!"
288 function CheckPlugins {
292 ax_msg - "Checking plugins ..."
293 for dir ($AXZSH/plugins/*(N)); do
296 # Test if plugin is already enabled
297 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
298 && enabled=" (enabled)" \
302 printf " - \"%s\"%s ... " "$plugin" "$enabled"
304 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
305 [[ -r "$script" ]] || continue
306 AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
307 if [[ $r -eq 0 ]]; then
312 if [[ -n "$new_plugin" ]]; then
313 detected_plugins+=($new_plugin)
314 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
316 elif [[ $r -eq 91 ]]; then
318 elif [[ $r -eq 92 ]]; then
321 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
328 if [[ -n "$missing_plugins" ]]; then
329 ax_msg 1 "Run the following command to enable all missing plugins:"
330 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
334 ax_msg 0 "All detected plugins are already enabled."
337 if [[ -n "$invalid_plugins" ]]; then
338 ax_msg 1 "Run the following command to disable all failed plugins:"
339 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
342 ax_msg 0 "No failed plugins are enabled."
351 [[ $# -gt 0 ]] || Usage
353 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
354 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
355 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
356 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
366 [[ $# -eq 0 ]] || Usage
370 [[ $# -eq 0 ]] || Usage
374 [[ $# -gt 0 ]] || Usage
375 for plugin in "$@"; do
376 EnablePlugin "$plugin"
381 [[ $# -gt 0 ]] || Usage
382 for plugin in "$@"; do
383 DisablePlugin "$plugin"
388 [[ $# -eq 0 ]] || Usage
392 [[ $# -eq 0 ]] || Usage
397 "enable-default-plugins")
398 [[ $# -eq 0 ]] || Usage
399 EnableDefaultPlugins && UpdatePluginCache
402 [[ $# -eq 0 ]] || Usage
406 [[ $# -eq 1 ]] || Usage
410 [[ $# -eq 0 ]] || Usage
412 UpgradeForeignPlugins
416 [[ $# -eq 0 ]] || Usage
423 ax_error "Invalid command \"$cmd\"!"
424 ax_error "Try \"$0 --help\" for more information."