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/zlogin.cache \
76 $AXZSH/cache/zlogout.cache \
77 $AXZSH/cache/zprofile.cache \
78 $AXZSH/cache/zshrc.cache \
80 echo "Regenerating cache files ..."
81 zsh -ilc '' >/dev/null
84 function NormalizedPluginName {
85 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
87 elif [[ "$1" =~ "/" ]]; then
94 function EnableAXZSH {
95 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
96 ln -s "$AXZSH/ax.zsh" "$f" \
97 || ax_error "Failed to create symbolic link for \"$f\"!"
101 function DisableAXZSH {
102 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
103 if [[ -h "$f" ]]; then
104 rm "$f" || ax_msg 2 "Failed to remove \"$f\"!"
105 elif [[ -e "$f" ]]; then
106 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
108 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
113 function EnablePlugin {
114 local plugin=$(NormalizedPluginName "$1")
115 local dir="$AXZSH/active_plugins"
117 if [[ -h "$dir/$plugin" ]]; then
118 ax_msg 1 "Plugin \"$1\" already active!"
122 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
124 mkdir -p "$AXZSH/repos"
125 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
126 ax_msg - "Cloning module from GitHub ..."
127 git clone --depth=1 "https://github.com/$1.git" \
128 "$AXZSH/repos/$plugin" \
129 || ax_error "Failed to clone repository!"
131 # Try to enable a theme in this "foreign module", but ignore
132 # errors: we don't know if this module provides a theme or is
133 # a "regular" plugin ...
134 if SetTheme "$plugin" 2>/dev/null; then
135 ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
136 # A theme was enabled: So assume that this is a theme
137 # and don't enable it as plugin.
140 echo "Trying to enable \"$1\" as plugin ..."
145 "$AXZSH_PLUGIN_D/$plugin"
146 "$ZSH_CUSTOM/$plugin"
147 "$AXZSH/custom_plugins/$plugin"
148 "$AXZSH/repos/$plugin"
149 "$AXZSH/plugins/$plugin"
150 "$AXZSH/default_plugins/$plugin"
151 "$AXZSH/core/$plugin"
153 [[ ! -d "$dname" ]] && continue
157 ln -s "$dname" "$PWD"
159 ax_error "Failed to create link!"
162 ax_msg 0 "Plugin \"$plugin\" enabled."
166 ax_error "Plugin \"$1\" not found!"
170 function DisablePlugin {
171 local plugin=$(NormalizedPluginName "$1")
172 local dir="$AXZSH/active_plugins"
176 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
177 rm "$AXZSH/active_theme"; r=$?
181 if [[ -h "$dir/$plugin" ]]; then
182 rm "$dir/$plugin"; r=$?
185 if [[ $r -eq -1 ]]; then
186 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
190 if [[ "$plugin" = *"#"* ]]; then
191 # Name matches a cloned repository, try to clean up!
192 echo "Cleaning up cloned repository ..."
193 rm -fr "$AXZSH/repos/$plugin"
199 function ListEnabledPlugins {
200 for plugin ($AXZSH/active_plugins/*(N)); do
201 print ${plugin:t:s/#/\//}
206 function ResetPlugins {
207 local dir="$AXZSH/active_plugins"
210 if [[ -e "$dir" ]]; then
211 ax_msg - "Removing all symbolic links in $dir ..."
212 find "$dir" -type l -print -delete; r1=$?
215 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
216 rm -fr "$AXZSH/repos"; r2=$?
218 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
221 function EnableDefaultPlugins {
222 local dir="$AXZSH/active_plugins"
224 ax_msg - "Activating default plugins ..."
228 ln -sf "$AXZSH/default_plugins/"* "$PWD"
234 local link_name="$AXZSH/active_theme"
236 # --- Powerlevel10k ---
237 # Remove "instant prompt" configuration, if any ...
238 rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
240 if [[ "$1" = "-" ]]; then
241 rm -f "$link_name" || return 1
242 ax_msg 0 "Theme settings have been reset."
246 if [[ -r "$1" ]]; then
248 elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
249 theme="$AXZSH/custom_themes/$1.axzshtheme"
250 elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
251 theme="$AXZSH/themes/$1.axzshtheme"
253 # Look for theme in specific remote module:
255 "$AXZSH/repos/$1/"*.axzshtheme(N[1])
256 "$AXZSH/repos/$1/"*.zsh-theme(N[1])
258 if [[ -r "$f" ]]; then
264 # Look for theme inside of installed plugins:
266 "$AXZSH/custom_themes"
267 "$AXZSH/custom_plugins/"*(N)
270 if [[ -r "$dname/$1.axzshtheme" ]]; then
271 theme="$dname/$1.axzshtheme"
273 elif [[ -r "$dname/$1.zsh-theme" ]]; then
274 theme="$dname/$1.zsh-theme"
279 if [[ -z "$theme" ]]; then
280 ax_error "Theme \"$1\" not found!"
284 ln -fs "$theme" "$link_name" || return 1
288 function UpgradeAXZSH {
289 if [[ $+commands[git] -eq 0 ]]; then
290 ax_error "The git(1) command is not available!"
293 if [[ ! -d "$AXZSH/.git" ]]; then
294 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
298 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
299 ( cd "$AXZSH" && git pull --ff-only )
302 function UpgradeForeignPlugins {
303 if [[ $+commands[git] -eq 0 ]]; then
304 ax_error "The git(1) command is not available!"
308 for dir ($AXZSH/repos/*(N)); do
309 name=${dir:t:s/#/\//}
310 if [[ -d "$dir/.git" ]]; then
311 ax_msg - "Upgrading \"$name\" [git] ..."
314 git pull --ff-only || ax_error "Pull failed!"
317 ax_error "Unknown repository type!"
322 function CheckPlugins {
326 ax_msg - "Checking plugins ..."
327 for dir ($AXZSH/plugins/*(N)); do
330 # Test if plugin is already enabled
331 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
332 && enabled=" (enabled)" \
336 printf " - \"%s\"%s ... " "$plugin" "$enabled"
338 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
339 [[ -r "$script" ]] || continue
340 AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
341 if [[ $r -eq 0 ]]; then
346 if [[ -n "$new_plugin" ]]; then
347 detected_plugins+=($new_plugin)
348 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
350 elif [[ $r -eq 91 ]]; then
352 elif [[ $r -eq 92 ]]; then
355 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
362 if [[ -n "$missing_plugins" ]]; then
363 ax_msg 1 "Run the following command to enable all missing plugins:"
364 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
368 ax_msg 0 "All detected plugins are already enabled."
371 if [[ -n "$invalid_plugins" ]]; then
372 ax_msg 1 "Run the following command to disable all failed plugins:"
373 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
376 ax_msg 0 "No failed plugins are enabled."
385 [[ $# -gt 0 ]] || Usage
387 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
388 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
389 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
390 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
400 [[ $# -eq 0 ]] || Usage
404 [[ $# -eq 0 ]] || Usage
408 [[ $# -gt 0 ]] || Usage
409 for plugin in "$@"; do
410 EnablePlugin "$plugin"
415 [[ $# -gt 0 ]] || Usage
416 for plugin in "$@"; do
417 DisablePlugin "$plugin"
422 [[ $# -eq 0 ]] || Usage
426 [[ $# -eq 0 ]] || Usage
431 "enable-default-plugins")
432 [[ $# -eq 0 ]] || Usage
433 EnableDefaultPlugins && UpdatePluginCache
436 [[ $# -eq 0 ]] || Usage
440 [[ $# -eq 1 ]] || Usage
444 [[ $# -eq 0 ]] || Usage
446 UpgradeForeignPlugins
450 [[ $# -eq 0 ]] || Usage
453 "--version"|"version")
460 ax_error "Invalid command \"$cmd\"!"
461 ax_error "Try \"$0 --help\" for more information."