3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2022 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>"
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."
80 function UpdatePluginCache {
81 [[ -r "$AXZSH/cache" ]] || return 0
83 [[ "$1" = "-v" ]] && ax_msg - "Invalidating & updating caches ..."
85 if [[ -d "$ZSH_CACHE_DIR" ]]; then
86 [[ "$1" = "-v" ]] && echo "Removing ZSH cache folder ..."
87 rm -fr "$ZSH_CACHE_DIR"
90 [[ "$1" = "-v" ]] && echo "Removing AX-ZSH cache files ..."
92 $AXZSH/cache/ax-io.cache \
93 $AXZSH/cache/zlogin.cache \
94 $AXZSH/cache/zlogout.cache \
95 $AXZSH/cache/zprofile.cache \
96 $AXZSH/cache/zshrc.cache \
99 echo "Regenerating AX-ZSH cache ..."
100 [[ -z "$AXZSH_DEBUG" ]] \
101 && AXZSH_PLUGIN_CHECK=1 zsh -ilc '' >/dev/null \
102 || AXZSH_PLUGIN_CHECK=1 zsh -ilc ''
105 function NormalizedPluginName {
106 if [[ "$1" =~ "^@?[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
108 elif [[ "$1" =~ "/" ]]; then
115 function EnableAXZSH {
116 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
117 ln -s "$AXZSH/ax.zsh" "$f" \
118 || ax_error "Failed to create symbolic link for \"$f\"!"
120 if [[ -z "$AXZSH_FPATH" ]]; then
121 echo "AX-ZSH was enabled. Now you should restart your shell, for example like this:"
122 echo "$ exec -l \"\$SHELL\""
126 function DisableAXZSH {
127 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
128 if [[ -h "$f" ]]; then
129 rm "$f" || ax_msg 2 "Failed to remove \"$f\"!"
130 elif [[ -e "$f" ]]; then
131 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
133 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
138 function EnablePlugin {
139 local plugin=$(NormalizedPluginName "$1")
140 local dir="$AXZSH/active_plugins"
143 if [[ -h "$dir/$plugin" ]]; then
144 ax_msg 1 "Plugin \"$1\" already active!"
148 if [[ "$1" =~ "^@[[:alnum:]-]+/[[:alnum:]_.+-]+$" ]]; then
149 # GitHub plugin repository (like OhMyZsh)
152 mkdir -p "$AXZSH/repos/@$repo"
153 if [[ ! -d "$AXZSH/repos/@$repo/plugins" ]]; then
154 ax_msg - "Cloning \"$repo\" from GitHub ..."
155 git clone --depth=1 "https://github.com/$repo/$repo.git" \
156 "$AXZSH/repos/@$repo" \
157 || ax_error "Failed to clone \"$repo\" repository!"
159 plugin="@$repo/plugins/${1#*/}"
160 echo "Trying to enable \"$1\" ..."
161 elif [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
163 mkdir -p "$AXZSH/repos"
164 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
165 ax_msg - "Cloning module from GitHub ..."
166 git clone --depth=1 "https://github.com/$1.git" \
167 "$AXZSH/repos/$plugin" \
168 || ax_error "Failed to clone repository!"
170 # Try to enable a theme in this "foreign module", but ignore
171 # errors: we don't know if this module provides a theme or is
172 # a "regular" plugin ...
173 if SetTheme "$plugin" 2>/dev/null; then
174 ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
175 # A theme was enabled: So assume that this is a theme
176 # and don't enable it as plugin.
179 echo "Trying to enable \"$1\" as plugin ..."
180 elif ! [[ "$1" =~ "^[[:alnum:]_.-]+$" ]]; then
181 ax_error "Invalid plugin name!"
187 "$AXZSH_PLUGIN_D/$plugin"
188 "$ZSH_CUSTOM/$plugin"
189 "$AXZSH/custom_plugins/$plugin"
190 "$AXZSH/repos/$plugin"
191 "$AXZSH/plugins/$plugin"
192 "$AXZSH/default_plugins/$plugin"
193 "$AXZSH/core/$plugin"
195 [[ ! -d "$dname" ]] && continue
199 ln -s "$dname" "$PWD/$name"
201 ax_error "Failed to create link!"
204 ax_msg 0 "Plugin \"$1\" enabled."
208 ax_error "Plugin \"$1\" not found!"
212 function DisablePlugin {
213 local plugin=$(NormalizedPluginName "$1")
214 local dir="$AXZSH/active_plugins"
218 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
219 rm "$AXZSH/active_theme"; r=$?
223 if [[ -h "$dir/$plugin" ]]; then
224 rm "$dir/$plugin"; r=$?
227 if [[ $r -eq -1 ]]; then
228 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
232 if [[ "$plugin" = *"#"* ]]; then
233 # Name matches a cloned repository, try to clean up!
234 echo "Cleaning up cloned repository ..."
235 rm -fr "$AXZSH/repos/$plugin"
241 function ListEnabledPlugins {
242 for plugin ($AXZSH/active_plugins/*(N)); do
243 print ${plugin:t:s/#/\//}
248 function ResetPlugins {
249 local dir="$AXZSH/active_plugins"
252 if [[ -e "$dir" ]]; then
253 ax_msg - "Removing all symbolic links in $dir ..."
254 find "$dir" -type l -print -delete; r1=$?
257 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
258 rm -fr "$AXZSH/repos"; r2=$?
260 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
263 function EnableDefaultPlugins {
264 local dir="$AXZSH/active_plugins"
266 ax_msg - "Activating default plugins ..."
270 ln -sf "$AXZSH/default_plugins/"* "$PWD"
276 local link_name="$AXZSH/active_theme"
278 # --- Powerlevel10k ---
279 # Remove "instant prompt" configuration, if any ...
280 rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
282 if [[ "$1" = "-" ]]; then
283 rm -f "$link_name" || return 1
284 ax_msg 0 "Theme settings have been reset."
288 if [[ -r "$1" ]]; then
290 elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
291 theme="$AXZSH/custom_themes/$1.axzshtheme"
292 elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
293 theme="$AXZSH/themes/$1.axzshtheme"
295 # Look for theme in specific remote module:
297 "$AXZSH/repos/$1/"*.axzshtheme(N[1])
298 "$AXZSH/repos/$1/"*.zsh-theme(N[1])
300 if [[ -r "$f" ]]; then
306 # Look for theme inside of installed plugins:
308 "$AXZSH/custom_themes"
309 "$AXZSH/custom_plugins/"*(N)
312 if [[ -r "$dname/$1.axzshtheme" ]]; then
313 theme="$dname/$1.axzshtheme"
315 elif [[ -r "$dname/$1.zsh-theme" ]]; then
316 theme="$dname/$1.zsh-theme"
321 if [[ -z "$theme" ]]; then
322 ax_error "Theme \"$1\" not found!"
326 ln -fs "$theme" "$link_name" || return 1
330 function UpgradeAXZSH {
331 if [[ $+commands[git] -eq 0 ]]; then
332 ax_error "The git(1) command is not available!"
335 if [[ ! -d "$AXZSH/.git" ]]; then
336 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
340 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
344 git pull --ff-only || ax_error "Git pull failed!"
345 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
349 function UpgradeForeignPlugins {
350 if [[ $+commands[git] -eq 0 ]]; then
351 ax_error "The git(1) command is not available!"
355 for dir ($AXZSH/repos/*(N)); do
356 name=${dir:t:s/#/\//}
357 if [[ -d "$dir/.git" ]]; then
358 ax_msg - "Upgrading \"$name\" [git] ..."
362 git pull --ff-only || ax_error "Git pull failed!"
363 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
366 ax_error "Unknown repository type!"
371 function CheckPlugins {
375 # Building cache file for all zshrc core files:
376 if ! T=$(mktemp); then
377 ax_error "Failed to create temporary file!"
380 for p in $AXZSH/core/*/*.zshrc; do
381 [[ "$(basename "$p")" == "01_zprofile.zshrc" ]] && continue
382 printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$p" >>"$T"
384 printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$p" >>"$T"
387 ax_msg - "Checking plugins ..."
388 for dir ($AXZSH/plugins/[a-z0-9]*(N)); do
391 # Test if plugin is already enabled
392 if [[ -e "$AXZSH/active_plugins/$plugin" ]]; then
393 printf ' \e[1;32m+\e[m "\e[1m%s\e[m" ... ' "${plugin}"
396 printf ' \e[1;31m-\e[m "%s" ... ' "${plugin}"
402 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile,ax-io}); do
403 [[ -r "$script" ]] || continue
407 axzsh_plugin_fnc() { source "$script" }
410 [[ $r -eq 0 ]] && new_plugin=$plugin
413 if [[ -n "$new_plugin" ]]; then
414 detected_plugins+=($new_plugin)
415 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
417 elif [[ $r -eq 91 ]]; then
419 elif [[ $r -eq 92 ]]; then
422 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
423 ax_msg 2 "failed ($r)."
430 if [[ -n "$missing_plugins" ]]; then
431 ax_msg 1 "Run the following command to enable all missing plugins:"
432 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
436 ax_msg 0 "All detected plugins are already enabled."
439 if [[ -n "$invalid_plugins" ]]; then
440 ax_msg 1 "Run the following command to disable all failed plugins:"
441 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
444 ax_msg 0 "No failed plugins are enabled."
453 [[ $# -gt 0 ]] || Usage
455 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
456 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
457 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
458 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
468 [[ $# -eq 0 ]] || Usage
472 [[ $# -eq 0 ]] || Usage
476 [[ $# -gt 0 ]] || Usage
477 for plugin in "$@"; do
478 EnablePlugin "$plugin"
483 [[ $# -gt 0 ]] || Usage
484 for plugin in "$@"; do
485 DisablePlugin "$plugin"
490 [[ $# -eq 0 ]] || Usage
494 [[ $# -eq 0 ]] || Usage
499 "enable-default-plugins")
500 [[ $# -eq 0 ]] || Usage
501 EnableDefaultPlugins && UpdatePluginCache
504 [[ $# -eq 0 ]] || Usage
508 [[ $# -eq 1 ]] || Usage
512 [[ $# -eq 0 ]] || Usage
514 UpgradeForeignPlugins
518 [[ $# -eq 0 ]] || Usage
521 "--version"|"version")
528 ax_error "Invalid command \"$cmd\"!"
529 ax_error "Try \"$0 --help\" for more information."