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 [[ -z "$AXZSH_DEBUG" ]] \
90 && AXZSH_PLUGIN_CHECK=1 zsh -ilc '' >/dev/null \
91 || AXZSH_PLUGIN_CHECK=1 zsh -ilc ''
94 function NormalizedPluginName {
95 if [[ "$1" =~ "^@?[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
97 elif [[ "$1" =~ "/" ]]; then
104 function EnableAXZSH {
105 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
106 ln -s "$AXZSH/ax.zsh" "$f" \
107 || ax_error "Failed to create symbolic link for \"$f\"!"
109 if [[ -z "$AXZSH_FPATH" ]]; then
110 echo "AX-ZSH was enabled. Now you should restart your shell, for example like this:"
111 echo "$ exec -l \"\$SHELL\""
115 function DisableAXZSH {
116 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
117 if [[ -h "$f" ]]; then
118 rm "$f" || ax_msg 2 "Failed to remove \"$f\"!"
119 elif [[ -e "$f" ]]; then
120 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
122 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
127 function EnablePlugin {
128 local plugin=$(NormalizedPluginName "$1")
129 local dir="$AXZSH/active_plugins"
132 if [[ -h "$dir/$plugin" ]]; then
133 ax_msg 1 "Plugin \"$1\" already active!"
137 if [[ "$1" =~ "^@[[:alnum:]-]+/[[:alnum:]_.+-]+$" ]]; then
138 # GitHub plugin repository (like OhMyZsh)
141 mkdir -p "$AXZSH/repos/@$repo"
142 if [[ ! -d "$AXZSH/repos/@$repo/plugins" ]]; then
143 ax_msg - "Cloning \"$repo\" from GitHub ..."
144 git clone --depth=1 "https://github.com/$repo/$repo.git" \
145 "$AXZSH/repos/@$repo" \
146 || ax_error "Failed to clone \"$repo\" repository!"
148 plugin="@$repo/plugins/${1#*/}"
149 echo "Trying to enable \"$1\" ..."
150 elif [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
152 mkdir -p "$AXZSH/repos"
153 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
154 ax_msg - "Cloning module from GitHub ..."
155 git clone --depth=1 "https://github.com/$1.git" \
156 "$AXZSH/repos/$plugin" \
157 || ax_error "Failed to clone repository!"
159 # Try to enable a theme in this "foreign module", but ignore
160 # errors: we don't know if this module provides a theme or is
161 # a "regular" plugin ...
162 if SetTheme "$plugin" 2>/dev/null; then
163 ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
164 # A theme was enabled: So assume that this is a theme
165 # and don't enable it as plugin.
168 echo "Trying to enable \"$1\" as plugin ..."
169 elif ! [[ "$1" =~ "^[[:alnum:]_.-]+$" ]]; then
170 ax_error "Invalid plugin name!"
176 "$AXZSH_PLUGIN_D/$plugin"
177 "$ZSH_CUSTOM/$plugin"
178 "$AXZSH/custom_plugins/$plugin"
179 "$AXZSH/repos/$plugin"
180 "$AXZSH/plugins/$plugin"
181 "$AXZSH/default_plugins/$plugin"
182 "$AXZSH/core/$plugin"
184 [[ ! -d "$dname" ]] && continue
188 ln -s "$dname" "$PWD/$name"
190 ax_error "Failed to create link!"
193 ax_msg 0 "Plugin \"$1\" enabled."
197 ax_error "Plugin \"$1\" not found!"
201 function DisablePlugin {
202 local plugin=$(NormalizedPluginName "$1")
203 local dir="$AXZSH/active_plugins"
207 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
208 rm "$AXZSH/active_theme"; r=$?
212 if [[ -h "$dir/$plugin" ]]; then
213 rm "$dir/$plugin"; r=$?
216 if [[ $r -eq -1 ]]; then
217 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
221 if [[ "$plugin" = *"#"* ]]; then
222 # Name matches a cloned repository, try to clean up!
223 echo "Cleaning up cloned repository ..."
224 rm -fr "$AXZSH/repos/$plugin"
230 function ListEnabledPlugins {
231 for plugin ($AXZSH/active_plugins/*(N)); do
232 print ${plugin:t:s/#/\//}
237 function ResetPlugins {
238 local dir="$AXZSH/active_plugins"
241 if [[ -e "$dir" ]]; then
242 ax_msg - "Removing all symbolic links in $dir ..."
243 find "$dir" -type l -print -delete; r1=$?
246 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
247 rm -fr "$AXZSH/repos"; r2=$?
249 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
252 function EnableDefaultPlugins {
253 local dir="$AXZSH/active_plugins"
255 ax_msg - "Activating default plugins ..."
259 ln -sf "$AXZSH/default_plugins/"* "$PWD"
265 local link_name="$AXZSH/active_theme"
267 # --- Powerlevel10k ---
268 # Remove "instant prompt" configuration, if any ...
269 rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
271 if [[ "$1" = "-" ]]; then
272 rm -f "$link_name" || return 1
273 ax_msg 0 "Theme settings have been reset."
277 if [[ -r "$1" ]]; then
279 elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
280 theme="$AXZSH/custom_themes/$1.axzshtheme"
281 elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
282 theme="$AXZSH/themes/$1.axzshtheme"
284 # Look for theme in specific remote module:
286 "$AXZSH/repos/$1/"*.axzshtheme(N[1])
287 "$AXZSH/repos/$1/"*.zsh-theme(N[1])
289 if [[ -r "$f" ]]; then
295 # Look for theme inside of installed plugins:
297 "$AXZSH/custom_themes"
298 "$AXZSH/custom_plugins/"*(N)
301 if [[ -r "$dname/$1.axzshtheme" ]]; then
302 theme="$dname/$1.axzshtheme"
304 elif [[ -r "$dname/$1.zsh-theme" ]]; then
305 theme="$dname/$1.zsh-theme"
310 if [[ -z "$theme" ]]; then
311 ax_error "Theme \"$1\" not found!"
315 ln -fs "$theme" "$link_name" || return 1
319 function UpgradeAXZSH {
320 if [[ $+commands[git] -eq 0 ]]; then
321 ax_error "The git(1) command is not available!"
324 if [[ ! -d "$AXZSH/.git" ]]; then
325 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
329 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
333 git pull --ff-only || ax_error "Git pull failed!"
334 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
338 function UpgradeForeignPlugins {
339 if [[ $+commands[git] -eq 0 ]]; then
340 ax_error "The git(1) command is not available!"
344 for dir ($AXZSH/repos/*(N)); do
345 name=${dir:t:s/#/\//}
346 if [[ -d "$dir/.git" ]]; then
347 ax_msg - "Upgrading \"$name\" [git] ..."
351 git pull --ff-only || ax_error "Git pull failed!"
352 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
355 ax_error "Unknown repository type!"
360 function CheckPlugins {
364 # Building cache file for all zshrc core files:
365 if ! T=$(mktemp); then
366 ax_error "Failed to create temporary file!"
369 for p in $AXZSH/core/*/*.zshrc; do
370 [[ "$(basename "$p")" == "01_zprofile.zshrc" ]] && continue
371 printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$p" >>"$T"
373 printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$p" >>"$T"
376 ax_msg - "Checking plugins ..."
377 for dir ($AXZSH/plugins/[a-z0-9]*(N)); do
380 # Test if plugin is already enabled
381 if [[ -e "$AXZSH/active_plugins/$plugin" ]]; then
382 printf ' \e[1;32m+\e[m "\e[1m%s\e[m" ... ' "${plugin}"
385 printf ' \e[1;31m-\e[m "%s" ... ' "${plugin}"
391 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile,ax-io}); do
392 [[ -r "$script" ]] || continue
396 axzsh_plugin_fnc() { source "$script" }
399 [[ $r -eq 0 ]] && new_plugin=$plugin
402 if [[ -n "$new_plugin" ]]; then
403 detected_plugins+=($new_plugin)
404 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
406 elif [[ $r -eq 91 ]]; then
408 elif [[ $r -eq 92 ]]; then
411 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
412 ax_msg 2 "failed ($r)."
419 if [[ -n "$missing_plugins" ]]; then
420 ax_msg 1 "Run the following command to enable all missing plugins:"
421 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
425 ax_msg 0 "All detected plugins are already enabled."
428 if [[ -n "$invalid_plugins" ]]; then
429 ax_msg 1 "Run the following command to disable all failed plugins:"
430 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
433 ax_msg 0 "No failed plugins are enabled."
442 [[ $# -gt 0 ]] || Usage
444 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
445 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
446 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
447 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
457 [[ $# -eq 0 ]] || Usage
461 [[ $# -eq 0 ]] || Usage
465 [[ $# -gt 0 ]] || Usage
466 for plugin in "$@"; do
467 EnablePlugin "$plugin"
472 [[ $# -gt 0 ]] || Usage
473 for plugin in "$@"; do
474 DisablePlugin "$plugin"
479 [[ $# -eq 0 ]] || Usage
483 [[ $# -eq 0 ]] || Usage
488 "enable-default-plugins")
489 [[ $# -eq 0 ]] || Usage
490 EnableDefaultPlugins && UpdatePluginCache
493 [[ $# -eq 0 ]] || Usage
497 [[ $# -eq 1 ]] || Usage
501 [[ $# -eq 0 ]] || Usage
503 UpgradeForeignPlugins
507 [[ $# -eq 0 ]] || Usage
510 "--version"|"version")
517 ax_error "Invalid command \"$cmd\"!"
518 ax_error "Try \"$0 --help\" for more information."