3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2021 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-2019 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>"
28 if [[ -d "$AXZSH/.git" && -n "$commands[git]" ]]; then
29 echo -n "Version: Git ID "
30 ( cd "$AXZSH" && git describe --always )
37 echo "Usage: $NAME <command> [...]"
40 echo " Enable AX-ZSH altogether."
42 echo " Disable AX-ZSH altogether."
44 echo " enable-plugin <name|directory> [<name|directory> [...]]"
45 echo " Enable plugin(s)."
46 echo " disable-plugin <name> [<name> [...]]"
47 echo " Disable plugin(s)."
49 echo " List enabled plugins."
52 echo " Reset active plugins to the default set."
53 echo " enable-default-plugins"
54 echo " Enable all default plugins."
56 echo " Detect plugins which are \"useful\" on this system."
58 echo " set-theme {<name>|-}"
59 echo " Set active theme to <name>, or to the default."
62 echo " Upgrade AX-ZSH installation (requires Git)."
64 echo " Force rebuild of all cache files."
69 function UpdatePluginCache {
70 [[ -r "$AXZSH/cache" ]] || return 0
72 [[ "$1" = "-v" ]] && ax_msg - "Invalidating & updating caches ..."
74 if [[ -d "$ZSH_CACHE_DIR" ]]; then
75 [[ "$1" = "-v" ]] && echo "Removing ZSH cache folder ..."
76 rm -fr "$ZSH_CACHE_DIR"
79 [[ "$1" = "-v" ]] && echo "Removing AX-ZSH cache files ..."
81 $AXZSH/cache/ax-io.cache \
82 $AXZSH/cache/zlogin.cache \
83 $AXZSH/cache/zlogout.cache \
84 $AXZSH/cache/zprofile.cache \
85 $AXZSH/cache/zshrc.cache \
88 echo "Regenerating AX-ZSH cache ..."
89 AXZSH_PLUGIN_CHECK=1 zsh -ilc '' >/dev/null
92 function NormalizedPluginName {
93 if [[ "$1" =~ "^@?[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
95 elif [[ "$1" =~ "/" ]]; then
102 function EnableAXZSH {
103 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
104 ln -s "$AXZSH/ax.zsh" "$f" \
105 || ax_error "Failed to create symbolic link for \"$f\"!"
109 function DisableAXZSH {
110 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
111 if [[ -h "$f" ]]; then
112 rm "$f" || ax_msg 2 "Failed to remove \"$f\"!"
113 elif [[ -e "$f" ]]; then
114 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
116 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
121 function EnablePlugin {
122 local plugin=$(NormalizedPluginName "$1")
123 local dir="$AXZSH/active_plugins"
126 if [[ -h "$dir/$plugin" ]]; then
127 ax_msg 1 "Plugin \"$1\" already active!"
131 if [[ "$1" =~ "^@[[:alnum:]-]+/[[:alnum:]_.+]+$" ]]; then
132 # GitHub plugin repository (like OhMyZsh)
135 mkdir -p "$AXZSH/repos/@$repo"
136 if [[ ! -d "$AXZSH/repos/@$repo/plugins" ]]; then
137 ax_msg - "Cloning \"$repo\" from GitHub ..."
138 git clone --depth=1 "https://github.com/$repo/$repo.git" \
139 "$AXZSH/repos/@$repo" \
140 || ax_error "Failed to clone \"$repo\" repository!"
142 plugin="@$repo/plugins/${1#*/}"
143 echo "Trying to enable \"$1\" ..."
144 elif [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
146 mkdir -p "$AXZSH/repos"
147 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
148 ax_msg - "Cloning module from GitHub ..."
149 git clone --depth=1 "https://github.com/$1.git" \
150 "$AXZSH/repos/$plugin" \
151 || ax_error "Failed to clone repository!"
153 # Try to enable a theme in this "foreign module", but ignore
154 # errors: we don't know if this module provides a theme or is
155 # a "regular" plugin ...
156 if SetTheme "$plugin" 2>/dev/null; then
157 ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
158 # A theme was enabled: So assume that this is a theme
159 # and don't enable it as plugin.
162 echo "Trying to enable \"$1\" as plugin ..."
163 elif ! [[ "$1" =~ "^[[:alnum:]_.-]+$" ]]; then
164 ax_error "Invalid plugin name!"
170 "$AXZSH_PLUGIN_D/$plugin"
171 "$ZSH_CUSTOM/$plugin"
172 "$AXZSH/custom_plugins/$plugin"
173 "$AXZSH/repos/$plugin"
174 "$AXZSH/plugins/$plugin"
175 "$AXZSH/default_plugins/$plugin"
176 "$AXZSH/core/$plugin"
178 [[ ! -d "$dname" ]] && continue
182 ln -s "$dname" "$PWD/$name"
184 ax_error "Failed to create link!"
187 ax_msg 0 "Plugin \"$1\" enabled."
191 ax_error "Plugin \"$1\" not found!"
195 function DisablePlugin {
196 local plugin=$(NormalizedPluginName "$1")
197 local dir="$AXZSH/active_plugins"
201 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
202 rm "$AXZSH/active_theme"; r=$?
206 if [[ -h "$dir/$plugin" ]]; then
207 rm "$dir/$plugin"; r=$?
210 if [[ $r -eq -1 ]]; then
211 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
215 if [[ "$plugin" = *"#"* ]]; then
216 # Name matches a cloned repository, try to clean up!
217 echo "Cleaning up cloned repository ..."
218 rm -fr "$AXZSH/repos/$plugin"
224 function ListEnabledPlugins {
225 for plugin ($AXZSH/active_plugins/*(N)); do
226 print ${plugin:t:s/#/\//}
231 function ResetPlugins {
232 local dir="$AXZSH/active_plugins"
235 if [[ -e "$dir" ]]; then
236 ax_msg - "Removing all symbolic links in $dir ..."
237 find "$dir" -type l -print -delete; r1=$?
240 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
241 rm -fr "$AXZSH/repos"; r2=$?
243 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
246 function EnableDefaultPlugins {
247 local dir="$AXZSH/active_plugins"
249 ax_msg - "Activating default plugins ..."
253 ln -sf "$AXZSH/default_plugins/"* "$PWD"
259 local link_name="$AXZSH/active_theme"
261 # --- Powerlevel10k ---
262 # Remove "instant prompt" configuration, if any ...
263 rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
265 if [[ "$1" = "-" ]]; then
266 rm -f "$link_name" || return 1
267 ax_msg 0 "Theme settings have been reset."
271 if [[ -r "$1" ]]; then
273 elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
274 theme="$AXZSH/custom_themes/$1.axzshtheme"
275 elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
276 theme="$AXZSH/themes/$1.axzshtheme"
278 # Look for theme in specific remote module:
280 "$AXZSH/repos/$1/"*.axzshtheme(N[1])
281 "$AXZSH/repos/$1/"*.zsh-theme(N[1])
283 if [[ -r "$f" ]]; then
289 # Look for theme inside of installed plugins:
291 "$AXZSH/custom_themes"
292 "$AXZSH/custom_plugins/"*(N)
295 if [[ -r "$dname/$1.axzshtheme" ]]; then
296 theme="$dname/$1.axzshtheme"
298 elif [[ -r "$dname/$1.zsh-theme" ]]; then
299 theme="$dname/$1.zsh-theme"
304 if [[ -z "$theme" ]]; then
305 ax_error "Theme \"$1\" not found!"
309 ln -fs "$theme" "$link_name" || return 1
313 function UpgradeAXZSH {
314 if [[ $+commands[git] -eq 0 ]]; then
315 ax_error "The git(1) command is not available!"
318 if [[ ! -d "$AXZSH/.git" ]]; then
319 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
323 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
327 git pull --ff-only || ax_error "Git pull failed!"
328 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
332 function UpgradeForeignPlugins {
333 if [[ $+commands[git] -eq 0 ]]; then
334 ax_error "The git(1) command is not available!"
338 for dir ($AXZSH/repos/*(N)); do
339 name=${dir:t:s/#/\//}
340 if [[ -d "$dir/.git" ]]; then
341 ax_msg - "Upgrading \"$name\" [git] ..."
345 git pull --ff-only || ax_error "Git pull failed!"
346 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
349 ax_error "Unknown repository type!"
354 function CheckPlugins {
358 # Building cache file for all zshrc core files:
359 if ! T=$(mktemp); then
360 ax_error "Failed to create temporary file!"
363 for p in $AXZSH/core/*/*.zshrc; do
364 [[ "$(basename "$p")" == "01_zprofile.zshrc" ]] && continue
365 printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$p" >>"$T"
367 printf "}\nax_plugin_init\n# END: %s\n\n" "$p" >>"$T"
370 ax_msg - "Checking plugins ..."
371 for dir ($AXZSH/plugins/[a-z0-9]*(N)); do
374 # Test if plugin is already enabled
375 if [[ -e "$AXZSH/active_plugins/$plugin" ]]; then
376 printf ' \e[1;32m+\e[m "\e[1m%s\e[m" ... ' "${plugin}"
379 printf ' \e[1;31m-\e[m "%s" ... ' "${plugin}"
385 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile,ax-io}); do
386 [[ -r "$script" ]] || continue
390 ax_plugin_fnc() { source "$script" }
393 [[ $r -eq 0 ]] && new_plugin=$plugin
396 if [[ -n "$new_plugin" ]]; then
397 detected_plugins+=($new_plugin)
398 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
400 elif [[ $r -eq 91 ]]; then
402 elif [[ $r -eq 92 ]]; then
405 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
406 ax_msg 2 "failed ($r)."
413 if [[ -n "$missing_plugins" ]]; then
414 ax_msg 1 "Run the following command to enable all missing plugins:"
415 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
419 ax_msg 0 "All detected plugins are already enabled."
422 if [[ -n "$invalid_plugins" ]]; then
423 ax_msg 1 "Run the following command to disable all failed plugins:"
424 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
427 ax_msg 0 "No failed plugins are enabled."
436 [[ $# -gt 0 ]] || Usage
438 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
439 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
440 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
441 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
451 [[ $# -eq 0 ]] || Usage
455 [[ $# -eq 0 ]] || Usage
459 [[ $# -gt 0 ]] || Usage
460 for plugin in "$@"; do
461 EnablePlugin "$plugin"
466 [[ $# -gt 0 ]] || Usage
467 for plugin in "$@"; do
468 DisablePlugin "$plugin"
473 [[ $# -eq 0 ]] || Usage
477 [[ $# -eq 0 ]] || Usage
482 "enable-default-plugins")
483 [[ $# -eq 0 ]] || Usage
484 EnableDefaultPlugins && UpdatePluginCache
487 [[ $# -eq 0 ]] || Usage
491 [[ $# -eq 1 ]] || Usage
495 [[ $# -eq 0 ]] || Usage
497 UpgradeForeignPlugins
501 [[ $# -eq 0 ]] || Usage
504 "--version"|"version")
511 ax_error "Invalid command \"$cmd\"!"
512 ax_error "Try \"$0 --help\" for more information."