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\"!"
107 if [[ -z "$AXZSH_FPATH" ]]; then
108 echo "AX-ZSH was enabled. Now you should restart your shell, for example like this:"
109 echo "$ exec -l \"\$SHELL\""
113 function DisableAXZSH {
114 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
115 if [[ -h "$f" ]]; then
116 rm "$f" || ax_msg 2 "Failed to remove \"$f\"!"
117 elif [[ -e "$f" ]]; then
118 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
120 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
125 function EnablePlugin {
126 local plugin=$(NormalizedPluginName "$1")
127 local dir="$AXZSH/active_plugins"
130 if [[ -h "$dir/$plugin" ]]; then
131 ax_msg 1 "Plugin \"$1\" already active!"
135 if [[ "$1" =~ "^@[[:alnum:]-]+/[[:alnum:]_.+-]+$" ]]; then
136 # GitHub plugin repository (like OhMyZsh)
139 mkdir -p "$AXZSH/repos/@$repo"
140 if [[ ! -d "$AXZSH/repos/@$repo/plugins" ]]; then
141 ax_msg - "Cloning \"$repo\" from GitHub ..."
142 git clone --depth=1 "https://github.com/$repo/$repo.git" \
143 "$AXZSH/repos/@$repo" \
144 || ax_error "Failed to clone \"$repo\" repository!"
146 plugin="@$repo/plugins/${1#*/}"
147 echo "Trying to enable \"$1\" ..."
148 elif [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
150 mkdir -p "$AXZSH/repos"
151 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
152 ax_msg - "Cloning module from GitHub ..."
153 git clone --depth=1 "https://github.com/$1.git" \
154 "$AXZSH/repos/$plugin" \
155 || ax_error "Failed to clone repository!"
157 # Try to enable a theme in this "foreign module", but ignore
158 # errors: we don't know if this module provides a theme or is
159 # a "regular" plugin ...
160 if SetTheme "$plugin" 2>/dev/null; then
161 ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
162 # A theme was enabled: So assume that this is a theme
163 # and don't enable it as plugin.
166 echo "Trying to enable \"$1\" as plugin ..."
167 elif ! [[ "$1" =~ "^[[:alnum:]_.-]+$" ]]; then
168 ax_error "Invalid plugin name!"
174 "$AXZSH_PLUGIN_D/$plugin"
175 "$ZSH_CUSTOM/$plugin"
176 "$AXZSH/custom_plugins/$plugin"
177 "$AXZSH/repos/$plugin"
178 "$AXZSH/plugins/$plugin"
179 "$AXZSH/default_plugins/$plugin"
180 "$AXZSH/core/$plugin"
182 [[ ! -d "$dname" ]] && continue
186 ln -s "$dname" "$PWD/$name"
188 ax_error "Failed to create link!"
191 ax_msg 0 "Plugin \"$1\" enabled."
195 ax_error "Plugin \"$1\" not found!"
199 function DisablePlugin {
200 local plugin=$(NormalizedPluginName "$1")
201 local dir="$AXZSH/active_plugins"
205 if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
206 rm "$AXZSH/active_theme"; r=$?
210 if [[ -h "$dir/$plugin" ]]; then
211 rm "$dir/$plugin"; r=$?
214 if [[ $r -eq -1 ]]; then
215 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
219 if [[ "$plugin" = *"#"* ]]; then
220 # Name matches a cloned repository, try to clean up!
221 echo "Cleaning up cloned repository ..."
222 rm -fr "$AXZSH/repos/$plugin"
228 function ListEnabledPlugins {
229 for plugin ($AXZSH/active_plugins/*(N)); do
230 print ${plugin:t:s/#/\//}
235 function ResetPlugins {
236 local dir="$AXZSH/active_plugins"
239 if [[ -e "$dir" ]]; then
240 ax_msg - "Removing all symbolic links in $dir ..."
241 find "$dir" -type l -print -delete; r1=$?
244 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
245 rm -fr "$AXZSH/repos"; r2=$?
247 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
250 function EnableDefaultPlugins {
251 local dir="$AXZSH/active_plugins"
253 ax_msg - "Activating default plugins ..."
257 ln -sf "$AXZSH/default_plugins/"* "$PWD"
263 local link_name="$AXZSH/active_theme"
265 # --- Powerlevel10k ---
266 # Remove "instant prompt" configuration, if any ...
267 rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
269 if [[ "$1" = "-" ]]; then
270 rm -f "$link_name" || return 1
271 ax_msg 0 "Theme settings have been reset."
275 if [[ -r "$1" ]]; then
277 elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
278 theme="$AXZSH/custom_themes/$1.axzshtheme"
279 elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
280 theme="$AXZSH/themes/$1.axzshtheme"
282 # Look for theme in specific remote module:
284 "$AXZSH/repos/$1/"*.axzshtheme(N[1])
285 "$AXZSH/repos/$1/"*.zsh-theme(N[1])
287 if [[ -r "$f" ]]; then
293 # Look for theme inside of installed plugins:
295 "$AXZSH/custom_themes"
296 "$AXZSH/custom_plugins/"*(N)
299 if [[ -r "$dname/$1.axzshtheme" ]]; then
300 theme="$dname/$1.axzshtheme"
302 elif [[ -r "$dname/$1.zsh-theme" ]]; then
303 theme="$dname/$1.zsh-theme"
308 if [[ -z "$theme" ]]; then
309 ax_error "Theme \"$1\" not found!"
313 ln -fs "$theme" "$link_name" || return 1
317 function UpgradeAXZSH {
318 if [[ $+commands[git] -eq 0 ]]; then
319 ax_error "The git(1) command is not available!"
322 if [[ ! -d "$AXZSH/.git" ]]; then
323 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
327 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
331 git pull --ff-only || ax_error "Git pull failed!"
332 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
336 function UpgradeForeignPlugins {
337 if [[ $+commands[git] -eq 0 ]]; then
338 ax_error "The git(1) command is not available!"
342 for dir ($AXZSH/repos/*(N)); do
343 name=${dir:t:s/#/\//}
344 if [[ -d "$dir/.git" ]]; then
345 ax_msg - "Upgrading \"$name\" [git] ..."
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..
353 ax_error "Unknown repository type!"
358 function CheckPlugins {
362 # Building cache file for all zshrc core files:
363 if ! T=$(mktemp); then
364 ax_error "Failed to create temporary file!"
367 for p in $AXZSH/core/*/*.zshrc; do
368 [[ "$(basename "$p")" == "01_zprofile.zshrc" ]] && continue
369 printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$p" >>"$T"
371 printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$p" >>"$T"
374 ax_msg - "Checking plugins ..."
375 for dir ($AXZSH/plugins/[a-z0-9]*(N)); do
378 # Test if plugin is already enabled
379 if [[ -e "$AXZSH/active_plugins/$plugin" ]]; then
380 printf ' \e[1;32m+\e[m "\e[1m%s\e[m" ... ' "${plugin}"
383 printf ' \e[1;31m-\e[m "%s" ... ' "${plugin}"
389 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile,ax-io}); do
390 [[ -r "$script" ]] || continue
394 axzsh_plugin_fnc() { source "$script" }
397 [[ $r -eq 0 ]] && new_plugin=$plugin
400 if [[ -n "$new_plugin" ]]; then
401 detected_plugins+=($new_plugin)
402 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
404 elif [[ $r -eq 91 ]]; then
406 elif [[ $r -eq 92 ]]; then
409 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
410 ax_msg 2 "failed ($r)."
417 if [[ -n "$missing_plugins" ]]; then
418 ax_msg 1 "Run the following command to enable all missing plugins:"
419 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
423 ax_msg 0 "All detected plugins are already enabled."
426 if [[ -n "$invalid_plugins" ]]; then
427 ax_msg 1 "Run the following command to disable all failed plugins:"
428 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
431 ax_msg 0 "No failed plugins are enabled."
440 [[ $# -gt 0 ]] || Usage
442 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
443 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
444 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
445 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
455 [[ $# -eq 0 ]] || Usage
459 [[ $# -eq 0 ]] || Usage
463 [[ $# -gt 0 ]] || Usage
464 for plugin in "$@"; do
465 EnablePlugin "$plugin"
470 [[ $# -gt 0 ]] || Usage
471 for plugin in "$@"; do
472 DisablePlugin "$plugin"
477 [[ $# -eq 0 ]] || Usage
481 [[ $# -eq 0 ]] || Usage
486 "enable-default-plugins")
487 [[ $# -eq 0 ]] || Usage
488 EnableDefaultPlugins && UpdatePluginCache
491 [[ $# -eq 0 ]] || Usage
495 [[ $# -eq 1 ]] || Usage
499 [[ $# -eq 0 ]] || Usage
501 UpgradeForeignPlugins
505 [[ $# -eq 0 ]] || Usage
508 "--version"|"version")
515 ax_error "Invalid command \"$cmd\"!"
516 ax_error "Try \"$0 --help\" for more information."