3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2016 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)."
53 [[ -r "$AXZSH/cache" ]] \
54 && echo "Dropping caches ..."
58 function NormalizedPluginName {
59 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
61 elif [[ "$1" =~ "/" ]]; then
68 function EnableAXZSH {
69 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
70 ln -sv "$AXZSH/ax.zsh" "$f" \
71 || ax_msg 2 "Failed to create symbolic link for \"$f\"!"
75 function DisableAXZSH {
76 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
78 rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
79 elif [ -e "$f" ]; then
80 ax_msg 2 "Error: Not removing \"$f\", it is not a symbolic link!"
82 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
87 function EnablePlugin {
88 local plugin=$(NormalizedPluginName "$1")
89 local dir="$AXZSH/active_plugins"
91 if [[ -h "$dir/$plugin" ]]; then
92 ax_msg 1 "Plugin \"$1\" already active!"
96 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
98 mkdir -p "$AXZSH/repos"
99 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
100 ax_msg - "Cloning plugin from GitHub ..."
101 git clone "https://github.com/$1.git" \
102 "$AXZSH/repos/$plugin"
108 "$AXZSH_PLUGIN_D/$plugin"
109 "$ZSH_CUSTOM/$plugin"
110 "$AXZSH/custom_plugins/$plugin"
111 "$AXZSH/repos/$plugin"
112 "$AXZSH/plugins/$plugin"
113 "$AXZSH/default_plugins/$plugin"
114 "$AXZSH/core/$plugin"
116 [[ ! -d "$dname" ]] && continue
120 ln -sv "$dname" "$PWD"
125 ax_msg 2 "Plugin \"$1\" not found!"
129 function DisablePlugin {
130 local plugin=$(NormalizedPluginName "$1")
131 local dir="$AXZSH/active_plugins"
133 if [[ ! -h "$dir/$plugin" ]]; then
134 ax_msg 1 "Plugin \"$1\" not active?"
138 rm -v "$dir/$plugin"; r=$?
139 [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
143 function ListEnabledPlugins {
144 for plugin ($AXZSH/active_plugins/*(N)); do
145 print ${plugin:t:s/#/\//}
150 function ResetPlugins {
151 local dir="$AXZSH/active_plugins"
154 if [[ -e "$dir" ]]; then
155 ax_msg - "Removing all symbolic links in $dir ..."
156 find "$dir" -type l -print -delete; r1=$?
159 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
160 rm -fr "$AXZSH/repos"; r2=$?
162 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
165 function EnableDefaultPlugins {
166 local dir="$AXZSH/active_plugins"
168 ax_msg - "Activating (linking) default plugins ..."
172 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
178 local link_name="$AXZSH/active_theme"
180 if [ $# -ne 1 ]; then
181 echo "Usage: axzsh_set_theme <name|->"
185 rm -f "$link_name" || return 1
187 if [ "$1" = "-" ]; then
188 echo "Theme settings have been reset."
194 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
195 theme="$AXZSH/custom_themes/$1.axzshtheme"
196 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
197 theme="$AXZSH/themes/$1.axzshtheme"
199 echo "Theme \"$1\" not found!"
202 ln -sv "$theme" "$link_name" || return 1
206 function UpgradeAXZSH {
207 if [[ $+commands[git] -eq 0 ]]; then
208 ax_msg 2 "The git(1) command is not available!"
211 if [[ ! -d "$AXZSH/.git" ]]; then
212 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
216 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
217 ( cd "$AXZSH" && git pull --ff-only )
220 function UpgradeForeignPlugins {
221 if [[ $+commands[git] -eq 0 ]]; then
222 ax_msg 2 "The git(1) command is not available!"
226 for dir ($AXZSH/repos/*(N)); do
227 name=${dir:t:s/#/\//}
228 if [ -d "$dir/.git" ]; then
229 ax_msg - "Upgrading \"$name\" [git] ..."
232 git pull --ff-only || ax_msg 2 "Pull failed!"
235 ax_msg 2 "Unknown repository type!"
240 function CheckPlugins {
244 ax_msg - "Checking plugins ..."
245 for dir ($AXZSH/plugins/*(N)); do
248 # Test if plugin is already enabled
249 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
250 && enabled=" (enabled)" \
254 printf " - \"%s\"%s ... " "$plugin" "$enabled"
256 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
257 [[ -r "$script" ]] || continue
258 AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
259 if [[ $r -eq 0 ]]; then
264 if [[ -n "$new_plugin" ]]; then
265 detected_plugins+=($new_plugin)
266 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
268 elif [[ $r -eq 91 ]]; then
270 elif [[ $r -eq 92 ]]; then
273 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
280 if [[ -n "$missing_plugins" ]]; then
281 ax_msg 1 "Run the following command to enable all missing plugins:"
282 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
286 ax_msg 0 "All detected plugins are already enabled."
289 if [[ -n "$invalid_plugins" ]]; then
290 ax_msg 1 "Run the following command to disable all failed plugins:"
291 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
294 ax_msg 0 "No failed plugins are enabled."
303 [[ $# -gt 0 ]] || Usage
305 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
306 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
307 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
308 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
318 [[ $# -eq 0 ]] || Usage
322 [[ $# -eq 0 ]] || Usage
326 [[ $# -gt 0 ]] || Usage
327 for plugin in "$@"; do
328 EnablePlugin "$plugin"
333 [[ $# -gt 0 ]] || Usage
334 for plugin in "$@"; do
335 DisablePlugin "$plugin"
340 [[ $# -eq 0 ]] || Usage
344 [[ $# -eq 0 ]] || Usage
349 "enable-default-plugins")
350 [[ $# -eq 0 ]] || Usage
355 [[ $# -eq 0 ]] || Usage
359 [[ $# -eq 1 ]] || Usage
364 [[ $# -eq 0 ]] || Usage
366 UpgradeForeignPlugins