3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2017 Alexander Barton <alex@barton.de>
7 # Include "ax-common.sh", if available:
8 for dir ("$HOME/lib" "$HOME/.ax" /usr/local /opt/ax /usr); do
9 [[ -z "$ax_common_sourced" ]] || break
10 ax_common="${dir}/lib/ax/ax-common.sh"
11 [[ -r "$ax_common" ]] && source "$ax_common"
13 if [[ -z "$ax_common_sourced" ]]; then
19 unset dir ax_common ax_common_sourced
22 echo "Usage: $NAME <command> [...]"
25 echo " Enable AX-ZSH altogether."
27 echo " Disable AX-ZSH altogether."
29 echo " enable-plugin <name|directory> [<name|directory> [...]]"
30 echo " Enable plugin(s)."
31 echo " disable-plugin <name> [<name> [...]]"
32 echo " Disable plugin(s)."
34 echo " List enabled plugins."
37 echo " Reset active plugins to the default set."
38 echo " enable-default-plugins"
39 echo " Enable all default plugins."
41 echo " Detect plugins which are \"useful\" on this system."
43 echo " set-theme <name>|-"
44 echo " Set active theme to <name>, or to the default."
47 echo " Upgrade AX-ZSH installation (requires Git)."
52 function UpdatePluginCache {
53 [[ -r "$AXZSH/cache" ]] || return 0
55 ax_msg - "Updating plugin cache ..."
57 $AXZSH/cache/zlogin.cache \
58 $AXZSH/cache/zlogout.cache \
59 $AXZSH/cache/zprofile.cache \
60 $AXZSH/cache/zshrc.cache \
62 echo "Regenerating cache files ..."
63 zsh -ilc '' >/dev/null
66 function NormalizedPluginName {
67 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
69 elif [[ "$1" =~ "/" ]]; then
76 function EnableAXZSH {
77 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
78 ln -sv "$AXZSH/ax.zsh" "$f" \
79 || ax_msg 2 "Failed to create symbolic link for \"$f\"!"
83 function DisableAXZSH {
84 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
86 rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
87 elif [ -e "$f" ]; then
88 ax_msg 2 "Error: Not removing \"$f\", it is not a symbolic link!"
90 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
95 function EnablePlugin {
96 local plugin=$(NormalizedPluginName "$1")
97 local dir="$AXZSH/active_plugins"
99 if [[ -h "$dir/$plugin" ]]; then
100 ax_msg 1 "Plugin \"$1\" already active!"
104 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
106 mkdir -p "$AXZSH/repos"
107 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
108 ax_msg - "Cloning plugin from GitHub ..."
109 git clone "https://github.com/$1.git" \
110 "$AXZSH/repos/$plugin"
116 "$AXZSH_PLUGIN_D/$plugin"
117 "$ZSH_CUSTOM/$plugin"
118 "$AXZSH/custom_plugins/$plugin"
119 "$AXZSH/repos/$plugin"
120 "$AXZSH/plugins/$plugin"
121 "$AXZSH/default_plugins/$plugin"
122 "$AXZSH/core/$plugin"
124 [[ ! -d "$dname" ]] && continue
128 ln -sv "$dname" "$PWD"
133 ax_msg 2 "Plugin \"$1\" not found!"
137 function DisablePlugin {
138 local plugin=$(NormalizedPluginName "$1")
139 local dir="$AXZSH/active_plugins"
141 if [[ ! -h "$dir/$plugin" ]]; then
142 ax_msg 1 "Plugin \"$1\" not active?"
146 rm -v "$dir/$plugin"; r=$?
147 [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
151 function ListEnabledPlugins {
152 for plugin ($AXZSH/active_plugins/*(N)); do
153 print ${plugin:t:s/#/\//}
158 function ResetPlugins {
159 local dir="$AXZSH/active_plugins"
162 if [[ -e "$dir" ]]; then
163 ax_msg - "Removing all symbolic links in $dir ..."
164 find "$dir" -type l -print -delete; r1=$?
167 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
168 rm -fr "$AXZSH/repos"; r2=$?
170 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
173 function EnableDefaultPlugins {
174 local dir="$AXZSH/active_plugins"
176 ax_msg - "Activating (linking) default plugins ..."
180 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
186 local link_name="$AXZSH/active_theme"
188 if [ $# -ne 1 ]; then
189 echo "Usage: axzsh_set_theme <name|->"
193 rm -f "$link_name" || return 1
195 if [ "$1" = "-" ]; then
196 echo "Theme settings have been reset."
202 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
203 theme="$AXZSH/custom_themes/$1.axzshtheme"
204 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
205 theme="$AXZSH/themes/$1.axzshtheme"
207 echo "Theme \"$1\" not found!"
210 ln -sv "$theme" "$link_name" || return 1
214 function UpgradeAXZSH {
215 if [[ $+commands[git] -eq 0 ]]; then
216 ax_msg 2 "The git(1) command is not available!"
219 if [[ ! -d "$AXZSH/.git" ]]; then
220 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
224 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
225 ( cd "$AXZSH" && git pull --ff-only )
228 function UpgradeForeignPlugins {
229 if [[ $+commands[git] -eq 0 ]]; then
230 ax_msg 2 "The git(1) command is not available!"
234 for dir ($AXZSH/repos/*(N)); do
235 name=${dir:t:s/#/\//}
236 if [ -d "$dir/.git" ]; then
237 ax_msg - "Upgrading \"$name\" [git] ..."
240 git pull --ff-only || ax_msg 2 "Pull failed!"
243 ax_msg 2 "Unknown repository type!"
248 function CheckPlugins {
252 ax_msg - "Checking plugins ..."
253 for dir ($AXZSH/plugins/*(N)); do
256 # Test if plugin is already enabled
257 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
258 && enabled=" (enabled)" \
262 printf " - \"%s\"%s ... " "$plugin" "$enabled"
264 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
265 [[ -r "$script" ]] || continue
266 AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
267 if [[ $r -eq 0 ]]; then
272 if [[ -n "$new_plugin" ]]; then
273 detected_plugins+=($new_plugin)
274 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
276 elif [[ $r -eq 91 ]]; then
278 elif [[ $r -eq 92 ]]; then
281 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
288 if [[ -n "$missing_plugins" ]]; then
289 ax_msg 1 "Run the following command to enable all missing plugins:"
290 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
294 ax_msg 0 "All detected plugins are already enabled."
297 if [[ -n "$invalid_plugins" ]]; then
298 ax_msg 1 "Run the following command to disable all failed plugins:"
299 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
302 ax_msg 0 "No failed plugins are enabled."
311 [[ $# -gt 0 ]] || Usage
313 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
314 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
315 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
316 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
326 [[ $# -eq 0 ]] || Usage
330 [[ $# -eq 0 ]] || Usage
334 [[ $# -gt 0 ]] || Usage
335 for plugin in "$@"; do
336 EnablePlugin "$plugin"
341 [[ $# -gt 0 ]] || Usage
342 for plugin in "$@"; do
343 DisablePlugin "$plugin"
348 [[ $# -eq 0 ]] || Usage
352 [[ $# -eq 0 ]] || Usage
357 "enable-default-plugins")
358 [[ $# -eq 0 ]] || Usage
363 [[ $# -eq 0 ]] || Usage
367 [[ $# -eq 1 ]] || Usage
372 [[ $# -eq 0 ]] || Usage
374 UpgradeForeignPlugins