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 "ax-zsh -- Modular configuration system for the Z shell (ZSH)"
26 echo "Copyright (c) 2015-2019 Alexander Barton <alex@barton.de>."
27 echo "Licensed under the terms of the MIT license, see LICENSE.md for details."
28 echo "Homepage: <https://github.com/alexbarton/ax-zsh>"
29 if [[ -d "$AXZSH/.git" && -n "$commands[git]" ]]; then
30 echo -n "Version: Git ID "
31 ( cd "$AXZSH" && git describe --always )
38 echo "Usage: $NAME <command> [...]"
41 echo " Enable AX-ZSH altogether."
43 echo " Disable AX-ZSH altogether."
45 echo " enable-plugin <name|directory> [<name|directory> [...]]"
46 echo " Enable plugin(s)."
47 echo " disable-plugin <name> [<name> [...]]"
48 echo " Disable plugin(s)."
50 echo " List enabled plugins."
53 echo " Reset active plugins to the default set."
54 echo " enable-default-plugins"
55 echo " Enable all default plugins."
57 echo " Detect plugins which are \"useful\" on this system."
59 echo " set-theme <name>|-"
60 echo " Set active theme to <name>, or to the default."
63 echo " Upgrade AX-ZSH installation (requires Git)."
65 echo " Force rebuild of all cache files."
70 function UpdatePluginCache {
71 [[ -r "$AXZSH/cache" ]] || return 0
73 [[ "$1" = "-v" ]] && ax_msg - "Updating plugin cache ..."
75 $AXZSH/cache/ax-io.cache \
76 $AXZSH/cache/zlogin.cache \
77 $AXZSH/cache/zlogout.cache \
78 $AXZSH/cache/zprofile.cache \
79 $AXZSH/cache/zshrc.cache \
81 echo "Regenerating cache files ..."
82 AXZSH_PLUGIN_CHECK=1 zsh -ilc '' >/dev/null
85 function NormalizedPluginName {
86 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
88 elif [[ "$1" =~ "/" ]]; then
95 function EnableAXZSH {
96 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
97 ln -s "$AXZSH/ax.zsh" "$f" \
98 || ax_error "Failed to create symbolic link for \"$f\"!"
102 function DisableAXZSH {
103 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
104 if [[ -h "$f" ]]; then
105 rm "$f" || ax_msg 2 "Failed to remove \"$f\"!"
106 elif [[ -e "$f" ]]; then
107 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
109 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
114 function EnablePlugin {
115 local plugin=$(NormalizedPluginName "$1")
116 local dir="$AXZSH/active_plugins"
118 if [[ -h "$dir/$plugin" ]]; then
119 ax_msg 1 "Plugin \"$1\" already active!"
123 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
125 mkdir -p "$AXZSH/repos"
126 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
127 ax_msg - "Cloning module from GitHub ..."
128 git clone --depth=1 "https://github.com/$1.git" \
129 "$AXZSH/repos/$plugin" \
130 || ax_error "Failed to clone repository!"
132 # Try to enable a theme in this "foreign module", but ignore
133 # errors: we don't know if this module provides a theme or is
134 # a "regular" plugin ...
135 if SetTheme "$plugin" 2>/dev/null; then
136 ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
137 # A theme was enabled: So assume that this is a theme
138 # and don't enable it as plugin.
141 echo "Trying to enable \"$1\" as plugin ..."
146 "$AXZSH_PLUGIN_D/$plugin"
147 "$ZSH_CUSTOM/$plugin"
148 "$AXZSH/custom_plugins/$plugin"
149 "$AXZSH/repos/$plugin"
150 "$AXZSH/plugins/$plugin"
151 "$AXZSH/default_plugins/$plugin"
152 "$AXZSH/core/$plugin"
154 [[ ! -d "$dname" ]] && continue
158 ln -s "$dname" "$PWD"
160 ax_error "Failed to create link!"
163 ax_msg 0 "Plugin \"$plugin\" enabled."
167 ax_error "Plugin \"$1\" not found!"
171 function DisablePlugin {
172 local plugin=$(NormalizedPluginName "$1")
173 local dir="$AXZSH/active_plugins"
177 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
178 rm "$AXZSH/active_theme"; r=$?
182 if [[ -h "$dir/$plugin" ]]; then
183 rm "$dir/$plugin"; r=$?
186 if [[ $r -eq -1 ]]; then
187 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
191 if [[ "$plugin" = *"#"* ]]; then
192 # Name matches a cloned repository, try to clean up!
193 echo "Cleaning up cloned repository ..."
194 rm -fr "$AXZSH/repos/$plugin"
200 function ListEnabledPlugins {
201 for plugin ($AXZSH/active_plugins/*(N)); do
202 print ${plugin:t:s/#/\//}
207 function ResetPlugins {
208 local dir="$AXZSH/active_plugins"
211 if [[ -e "$dir" ]]; then
212 ax_msg - "Removing all symbolic links in $dir ..."
213 find "$dir" -type l -print -delete; r1=$?
216 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
217 rm -fr "$AXZSH/repos"; r2=$?
219 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
222 function EnableDefaultPlugins {
223 local dir="$AXZSH/active_plugins"
225 ax_msg - "Activating default plugins ..."
229 ln -sf "$AXZSH/default_plugins/"* "$PWD"
235 local link_name="$AXZSH/active_theme"
237 # --- Powerlevel10k ---
238 # Remove "instant prompt" configuration, if any ...
239 rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
241 if [[ "$1" = "-" ]]; then
242 rm -f "$link_name" || return 1
243 ax_msg 0 "Theme settings have been reset."
247 if [[ -r "$1" ]]; then
249 elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
250 theme="$AXZSH/custom_themes/$1.axzshtheme"
251 elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
252 theme="$AXZSH/themes/$1.axzshtheme"
254 # Look for theme in specific remote module:
256 "$AXZSH/repos/$1/"*.axzshtheme(N[1])
257 "$AXZSH/repos/$1/"*.zsh-theme(N[1])
259 if [[ -r "$f" ]]; then
265 # Look for theme inside of installed plugins:
267 "$AXZSH/custom_themes"
268 "$AXZSH/custom_plugins/"*(N)
271 if [[ -r "$dname/$1.axzshtheme" ]]; then
272 theme="$dname/$1.axzshtheme"
274 elif [[ -r "$dname/$1.zsh-theme" ]]; then
275 theme="$dname/$1.zsh-theme"
280 if [[ -z "$theme" ]]; then
281 ax_error "Theme \"$1\" not found!"
285 ln -fs "$theme" "$link_name" || return 1
289 function UpgradeAXZSH {
290 if [[ $+commands[git] -eq 0 ]]; then
291 ax_error "The git(1) command is not available!"
294 if [[ ! -d "$AXZSH/.git" ]]; then
295 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
299 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
300 ( cd "$AXZSH" && git pull --ff-only )
303 function UpgradeForeignPlugins {
304 if [[ $+commands[git] -eq 0 ]]; then
305 ax_error "The git(1) command is not available!"
309 for dir ($AXZSH/repos/*(N)); do
310 name=${dir:t:s/#/\//}
311 if [[ -d "$dir/.git" ]]; then
312 ax_msg - "Upgrading \"$name\" [git] ..."
315 git pull --ff-only || ax_error "Pull failed!"
318 ax_error "Unknown repository type!"
323 function CheckPlugins {
327 ax_msg - "Checking plugins ..."
328 for dir ($AXZSH/plugins/[a-z0-9]*(N)); do
331 # Test if plugin is already enabled
332 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
333 && enabled=" (enabled)" \
337 printf " - \"%s\"%s ... " "$plugin" "$enabled"
339 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile,ax-io}); do
340 [[ -r "$script" ]] || continue
341 AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
342 if [[ $r -eq 0 ]]; then
347 if [[ -n "$new_plugin" ]]; then
348 detected_plugins+=($new_plugin)
349 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
351 elif [[ $r -eq 91 ]]; then
353 elif [[ $r -eq 92 ]]; then
356 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
363 if [[ -n "$missing_plugins" ]]; then
364 ax_msg 1 "Run the following command to enable all missing plugins:"
365 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
369 ax_msg 0 "All detected plugins are already enabled."
372 if [[ -n "$invalid_plugins" ]]; then
373 ax_msg 1 "Run the following command to disable all failed plugins:"
374 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
377 ax_msg 0 "No failed plugins are enabled."
386 [[ $# -gt 0 ]] || Usage
388 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
389 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
390 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
391 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
401 [[ $# -eq 0 ]] || Usage
405 [[ $# -eq 0 ]] || Usage
409 [[ $# -gt 0 ]] || Usage
410 for plugin in "$@"; do
411 EnablePlugin "$plugin"
416 [[ $# -gt 0 ]] || Usage
417 for plugin in "$@"; do
418 DisablePlugin "$plugin"
423 [[ $# -eq 0 ]] || Usage
427 [[ $# -eq 0 ]] || Usage
432 "enable-default-plugins")
433 [[ $# -eq 0 ]] || Usage
434 EnableDefaultPlugins && UpdatePluginCache
437 [[ $# -eq 0 ]] || Usage
441 [[ $# -eq 1 ]] || Usage
445 [[ $# -eq 0 ]] || Usage
447 UpgradeForeignPlugins
451 [[ $# -eq 0 ]] || Usage
454 "--version"|"version")
461 ax_error "Invalid command \"$cmd\"!"
462 ax_error "Try \"$0 --help\" for more information."