3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2020 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 [[ "$1" = "-v" ]] && 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 -s "$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
90 if [[ -h "$f" ]]; then
91 rm "$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" \
116 || ax_error "Failed to clone repository!"
118 # Try to enable a theme in this "foreign module", but ignore
119 # errors: we don't know if this module provides a theme or is
120 # a "regular" plugin ...
121 if SetTheme "$plugin" 2>/dev/null; then
122 ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
123 # A theme was enabled: So assume that this is a theme
124 # and don't enable it as plugin.
127 echo "Trying to enable \"$1\" as plugin ..."
132 "$AXZSH_PLUGIN_D/$plugin"
133 "$ZSH_CUSTOM/$plugin"
134 "$AXZSH/custom_plugins/$plugin"
135 "$AXZSH/repos/$plugin"
136 "$AXZSH/plugins/$plugin"
137 "$AXZSH/default_plugins/$plugin"
138 "$AXZSH/core/$plugin"
140 [[ ! -d "$dname" ]] && continue
144 ln -s "$dname" "$PWD"
146 ax_error "Failed to create link!"
149 ax_msg 0 "Plugin \"$plugin\" enabled."
153 ax_error "Plugin \"$1\" not found!"
157 function DisablePlugin {
158 local plugin=$(NormalizedPluginName "$1")
159 local dir="$AXZSH/active_plugins"
163 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
164 rm "$AXZSH/active_theme"; r=$?
168 if [[ -h "$dir/$plugin" ]]; then
169 rm "$dir/$plugin"; r=$?
172 if [[ $r -eq -1 ]]; then
173 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
177 if [[ "$plugin" = *"#"* ]]; then
178 # Name matches a cloned repository, try to clean up!
179 echo "Cleaning up cloned repository ..."
180 rm -fr "$AXZSH/repos/$plugin"
186 function ListEnabledPlugins {
187 for plugin ($AXZSH/active_plugins/*(N)); do
188 print ${plugin:t:s/#/\//}
193 function ResetPlugins {
194 local dir="$AXZSH/active_plugins"
197 if [[ -e "$dir" ]]; then
198 ax_msg - "Removing all symbolic links in $dir ..."
199 find "$dir" -type l -print -delete; r1=$?
202 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
203 rm -fr "$AXZSH/repos"; r2=$?
205 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
208 function EnableDefaultPlugins {
209 local dir="$AXZSH/active_plugins"
211 ax_msg - "Activating default plugins ..."
215 ln -sf "$AXZSH/default_plugins/"* "$PWD"
221 local link_name="$AXZSH/active_theme"
223 if [[ "$1" = "-" ]]; then
224 rm -f "$link_name" || return 1
225 ax_msg 0 "Theme settings have been reset."
229 if [[ -r "$1" ]]; then
231 elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
232 theme="$AXZSH/custom_themes/$1.axzshtheme"
233 elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
234 theme="$AXZSH/themes/$1.axzshtheme"
236 # Look for theme in specific remote module:
238 "$AXZSH/repos/$1/"*.axzshtheme(N[1])
239 "$AXZSH/repos/$1/"*.zsh-theme(N[1])
241 if [[ -r "$f" ]]; then
247 # Look for theme inside of installed plugins:
249 "$AXZSH/custom_themes"
250 "$AXZSH/custom_plugins/"*(N)
253 if [[ -r "$dname/$1.axzshtheme" ]]; then
254 theme="$dname/$1.axzshtheme"
256 elif [[ -r "$dname/$1.zsh-theme" ]]; then
257 theme="$dname/$1.zsh-theme"
262 if [[ -z "$theme" ]]; then
263 ax_error "Theme \"$1\" not found!"
267 ln -fs "$theme" "$link_name" || return 1
271 function UpgradeAXZSH {
272 if [[ $+commands[git] -eq 0 ]]; then
273 ax_error "The git(1) command is not available!"
276 if [[ ! -d "$AXZSH/.git" ]]; then
277 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
281 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
282 ( cd "$AXZSH" && git pull --ff-only )
285 function UpgradeForeignPlugins {
286 if [[ $+commands[git] -eq 0 ]]; then
287 ax_error "The git(1) command is not available!"
291 for dir ($AXZSH/repos/*(N)); do
292 name=${dir:t:s/#/\//}
293 if [[ -d "$dir/.git" ]]; then
294 ax_msg - "Upgrading \"$name\" [git] ..."
297 git pull --ff-only || ax_error "Pull failed!"
300 ax_error "Unknown repository type!"
305 function CheckPlugins {
309 ax_msg - "Checking plugins ..."
310 for dir ($AXZSH/plugins/*(N)); do
313 # Test if plugin is already enabled
314 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
315 && enabled=" (enabled)" \
319 printf " - \"%s\"%s ... " "$plugin" "$enabled"
321 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
322 [[ -r "$script" ]] || continue
323 AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
324 if [[ $r -eq 0 ]]; then
329 if [[ -n "$new_plugin" ]]; then
330 detected_plugins+=($new_plugin)
331 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
333 elif [[ $r -eq 91 ]]; then
335 elif [[ $r -eq 92 ]]; then
338 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
345 if [[ -n "$missing_plugins" ]]; then
346 ax_msg 1 "Run the following command to enable all missing plugins:"
347 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
351 ax_msg 0 "All detected plugins are already enabled."
354 if [[ -n "$invalid_plugins" ]]; then
355 ax_msg 1 "Run the following command to disable all failed plugins:"
356 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
359 ax_msg 0 "No failed plugins are enabled."
368 [[ $# -gt 0 ]] || Usage
370 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
371 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
372 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
373 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
383 [[ $# -eq 0 ]] || Usage
387 [[ $# -eq 0 ]] || Usage
391 [[ $# -gt 0 ]] || Usage
392 for plugin in "$@"; do
393 EnablePlugin "$plugin"
398 [[ $# -gt 0 ]] || Usage
399 for plugin in "$@"; do
400 DisablePlugin "$plugin"
405 [[ $# -eq 0 ]] || Usage
409 [[ $# -eq 0 ]] || Usage
414 "enable-default-plugins")
415 [[ $# -eq 0 ]] || Usage
416 EnableDefaultPlugins && UpdatePluginCache
419 [[ $# -eq 0 ]] || Usage
423 [[ $# -eq 1 ]] || Usage
427 [[ $# -eq 0 ]] || Usage
429 UpgradeForeignPlugins
433 [[ $# -eq 0 ]] || Usage
440 ax_error "Invalid command \"$cmd\"!"
441 ax_error "Try \"$0 --help\" for more information."