3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2023 Alexander Barton <alex@barton.de>
7 # Embedded ax-common compatibility functions ...
17 printf "\e[${c}m%s\e[0m\n" "$@"
24 echo "ax-zsh -- Modular configuration system for the Z shell (ZSH)"
25 echo "Copyright (c) 2015-2023 Alexander Barton <alex@barton.de>."
26 echo "Licensed under the terms of the MIT license, see LICENSE.md for details."
27 echo "Homepage: <https://github.com/alexbarton/ax-zsh>"
29 echo "Installation prefix: $AXZSH"
31 if [[ -d "$AXZSH/.git" && -n "$commands[git]" ]]; then
32 echo -n "Git Commit-ID "
33 ( cd "$AXZSH" && git describe --always )
37 echo -n "Active theme: "
38 if [[ -n "$AXZSH_THEME" ]]; then
39 echo "${AXZSH_THEME:A:t:r}"
48 echo "Usage: $NAME <command> [...]"
51 echo " Enable AX-ZSH altogether."
53 echo " Disable AX-ZSH altogether."
55 echo " enable-plugin <name|directory> [<name|directory> [...]]"
56 echo " Enable plugin(s)."
57 echo " disable-plugin <name> [<name> [...]]"
58 echo " Disable plugin(s)."
60 echo " List enabled plugins."
63 echo " Reset active plugins to the default set."
64 echo " enable-default-plugins"
65 echo " Enable all default plugins."
67 echo " Detect plugins which are \"useful\" on this system."
69 echo " set-theme {<name>|-}"
70 echo " Set active theme to <name>, or to the default."
73 echo " Upgrade AX-ZSH installation (requires Git)."
75 echo " Force rebuild of all cache files."
78 echo " Show this help text."
80 echo " Show version and setup information."
85 function UpdatePluginCache {
86 [[ -r "$AXZSH/cache" ]] || return 0
88 [[ "$1" = "-v" ]] && ax_msg - "Invalidating & updating caches ..."
90 if [[ -d "$ZSH_CACHE_DIR" ]]; then
91 [[ "$1" = "-v" ]] && echo "Removing ZSH cache folder ..."
92 rm -fr "$ZSH_CACHE_DIR"
95 [[ "$1" = "-v" ]] && echo "Removing AX-ZSH cache files ..."
97 $AXZSH/cache/ax-io.cache \
98 $AXZSH/cache/zlogin.cache \
99 $AXZSH/cache/zlogout.cache \
100 $AXZSH/cache/zprofile.cache \
101 $AXZSH/cache/zshrc.cache \
104 echo "Regenerating AX-ZSH cache ..."
105 [[ -z "$AXZSH_DEBUG" ]] \
106 && AXZSH_PLUGIN_CHECK=1 zsh -ilc '' >/dev/null \
107 || AXZSH_PLUGIN_CHECK=1 zsh -ilc ''
110 function NormalizedPluginName {
111 if [[ "$1" =~ "^@?[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
113 elif [[ "$1" =~ "/" ]]; then
120 function EnableAXZSH {
121 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
122 ln -s "$AXZSH/ax.zsh" "$f" \
123 || ax_error "Failed to create symbolic link for \"$f\"!"
125 if [[ -z "$AXZSH_FPATH" ]]; then
126 echo "AX-ZSH was enabled. Now you should restart your shell, for example like this:"
127 echo "$ exec -l \"\$SHELL\""
131 function DisableAXZSH {
132 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
133 if [[ -h "$f" ]]; then
134 rm "$f" || ax_msg 2 "Failed to remove \"$f\"!"
135 elif [[ -e "$f" ]]; then
136 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
138 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
143 function EnablePlugin {
144 local plugin=$(NormalizedPluginName "$1")
145 local dir="$AXZSH/active_plugins"
148 if [[ -h "$dir/$plugin" ]]; then
149 ax_msg 1 "Plugin \"$1\" already active!"
153 if [[ "$1" =~ "^@[[:alnum:]-]+/[[:alnum:]_.+-]+$" ]]; then
154 # GitHub plugin repository (like OhMyZsh)
157 mkdir -p "$AXZSH/repos/@$repo"
158 if [[ ! -d "$AXZSH/repos/@$repo/plugins" ]]; then
159 ax_msg - "Cloning \"$repo\" from GitHub ..."
160 git clone --depth=1 "https://github.com/$repo/$repo.git" \
161 "$AXZSH/repos/@$repo" \
162 || ax_error "Failed to clone \"$repo\" repository!"
164 plugin="@$repo/plugins/${1#*/}"
165 echo "Trying to enable \"$1\" ..."
166 elif [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
168 mkdir -p "$AXZSH/repos"
169 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
170 ax_msg - "Cloning module from GitHub ..."
171 git clone --depth=1 "https://github.com/$1.git" \
172 "$AXZSH/repos/$plugin" \
173 || ax_error "Failed to clone repository!"
175 # Try to enable a theme in this "foreign module", but ignore
176 # errors: we don't know if this module provides a theme or is
177 # a "regular" plugin ...
178 if SetTheme "$plugin" 2>/dev/null; then
179 ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
180 # A theme was enabled: So assume that this is a theme
181 # and don't enable it as plugin.
184 echo "Trying to enable \"$1\" as plugin ..."
185 elif ! [[ "$1" =~ "^[[:alnum:]_.-]+$" ]]; then
186 ax_error "Invalid plugin name!"
192 "$AXZSH_PLUGIN_D/$plugin"
193 "$ZSH_CUSTOM/$plugin"
194 "$AXZSH/custom_plugins/$plugin"
195 "$AXZSH/repos/$plugin"
196 "$AXZSH/plugins/$plugin"
197 "$AXZSH/default_plugins/$plugin"
198 "$AXZSH/core/$plugin"
200 [[ ! -d "$dname" ]] && continue
204 ln -s "$dname" "$PWD/$name"
206 ax_error "Failed to create link!"
209 ax_msg 0 "Plugin \"$1\" enabled."
213 ax_error "Plugin \"$1\" not found!"
217 function DisablePlugin {
218 local plugin=$(NormalizedPluginName "$1")
219 local dir="$AXZSH/active_plugins"
223 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
224 rm "$AXZSH/active_theme"; r=$?
228 if [[ -h "$dir/$plugin" ]]; then
229 rm "$dir/$plugin"; r=$?
232 if [[ $r -eq -1 ]]; then
233 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
237 if [[ "$plugin" = *"#"* ]]; then
238 # Name matches a cloned repository, try to clean up!
239 echo "Cleaning up cloned repository ..."
240 rm -fr "$AXZSH/repos/$plugin"
246 function ListEnabledPlugins {
247 for plugin ($AXZSH/active_plugins/*(N)); do
248 print ${plugin:t:s/#/\//}
253 function ResetPlugins {
254 local dir="$AXZSH/active_plugins"
257 if [[ -e "$dir" ]]; then
258 ax_msg - "Removing all symbolic links in $dir ..."
259 find "$dir" -type l -print -delete; r1=$?
262 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
263 rm -fr "$AXZSH/repos"; r2=$?
265 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
268 function EnableDefaultPlugins {
269 local dir="$AXZSH/active_plugins"
271 ax_msg - "Activating default plugins ..."
275 ln -sf "$AXZSH/default_plugins/"* "$PWD"
281 local link_name="$AXZSH/active_theme"
283 # --- Powerlevel10k ---
284 # Remove "instant prompt" configuration, if any ...
285 rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
287 if [[ "$1" = "-" ]]; then
288 rm -f "$link_name" || return 1
289 ax_msg 0 "Theme settings have been reset."
293 if [[ -r "$1" ]]; then
295 elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
296 theme="$AXZSH/custom_themes/$1.axzshtheme"
297 elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
298 theme="$AXZSH/themes/$1.axzshtheme"
300 # Look for theme in specific remote module:
302 "$AXZSH/repos/$1/"*.axzshtheme(N[1])
303 "$AXZSH/repos/$1/"*.zsh-theme(N[1])
305 if [[ -r "$f" ]]; then
311 # Look for theme inside of installed plugins:
313 "$AXZSH/custom_themes"
314 "$AXZSH/custom_plugins/"*(N)
317 if [[ -r "$dname/$1.axzshtheme" ]]; then
318 theme="$dname/$1.axzshtheme"
320 elif [[ -r "$dname/$1.zsh-theme" ]]; then
321 theme="$dname/$1.zsh-theme"
326 if [[ -z "$theme" ]]; then
327 ax_error "Theme \"$1\" not found!"
331 ln -fs "$theme" "$link_name" || return 1
335 function UpgradeAXZSH {
336 if [[ $+commands[git] -eq 0 ]]; then
337 ax_error "The git(1) command is not available!"
340 if [[ ! -d "$AXZSH/.git" ]]; then
341 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
345 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
349 git pull --ff-only || ax_error "Git pull failed!"
350 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
354 function UpgradeForeignPlugins {
355 if [[ $+commands[git] -eq 0 ]]; then
356 ax_error "The git(1) command is not available!"
360 for dir ($AXZSH/repos/*(N)); do
361 name=${dir:t:s/#/\//}
362 if [[ -d "$dir/.git" ]]; then
363 ax_msg - "Upgrading \"$name\" [git] ..."
367 git pull --ff-only || ax_error "Git pull failed!"
368 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
371 ax_error "Unknown repository type!"
376 function CheckPlugins {
380 # Building cache file for all zshrc core files:
381 if ! T=$(mktemp); then
382 ax_error "Failed to create temporary file!"
385 for p in $AXZSH/core/*/*.zshrc; do
386 [[ "$(basename "$p")" == "01_zprofile.zshrc" ]] && continue
387 printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$p" >>"$T"
389 printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$p" >>"$T"
392 ax_msg - "Checking plugins ..."
393 for dir ($AXZSH/plugins/[a-z0-9]*(N)); do
396 # Test if plugin is already enabled
397 if [[ -e "$AXZSH/active_plugins/$plugin" ]]; then
398 printf ' \e[1;32m+\e[m "\e[1m%s\e[m" ... ' "${plugin}"
401 printf ' \e[1;31m-\e[m "%s" ... ' "${plugin}"
407 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile,ax-io}); do
408 [[ -r "$script" ]] || continue
412 axzsh_plugin_fnc() { source "$script" }
415 [[ $r -eq 0 ]] && new_plugin=$plugin
418 if [[ -n "$new_plugin" ]]; then
419 detected_plugins+=($new_plugin)
420 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
422 elif [[ $r -eq 91 ]]; then
424 elif [[ $r -eq 92 ]]; then
427 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
428 ax_msg 2 "failed ($r)."
435 if [[ -n "$missing_plugins" ]]; then
436 ax_msg 1 "Run the following command to enable all missing plugins:"
437 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
441 ax_msg 0 "All detected plugins are already enabled."
444 if [[ -n "$invalid_plugins" ]]; then
445 ax_msg 1 "Run the following command to disable all failed plugins:"
446 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
449 ax_msg 0 "No failed plugins are enabled."
458 [[ $# -gt 0 ]] || Usage
460 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
461 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
462 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
463 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
473 [[ $# -eq 0 ]] || Usage
477 [[ $# -eq 0 ]] || Usage
481 [[ $# -gt 0 ]] || Usage
482 for plugin in "$@"; do
483 EnablePlugin "$plugin"
488 [[ $# -gt 0 ]] || Usage
489 for plugin in "$@"; do
490 DisablePlugin "$plugin"
495 [[ $# -eq 0 ]] || Usage
499 [[ $# -eq 0 ]] || Usage
504 "enable-default-plugins")
505 [[ $# -eq 0 ]] || Usage
506 EnableDefaultPlugins && UpdatePluginCache
509 [[ $# -eq 0 ]] || Usage
513 [[ $# -eq 1 ]] || Usage
517 [[ $# -eq 0 ]] || Usage
519 UpgradeForeignPlugins
523 [[ $# -eq 0 ]] || Usage
526 "--version"|"version")
533 ax_error "Invalid command \"$cmd\"!"
534 ax_error "Try \"$0 --help\" for more information."