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."
28 echo " Disable AX-ZSH altogether."
30 echo " enable-plugin <name|directory> [<name|directory> [...]]"
31 echo " Enable plugin(s)."
33 echo " disable-plugin <name> [<name> [...]]"
34 echo " Disable plugin(s)."
37 echo " Reset active plugins to the default set."
39 echo " enable-default-plugins"
40 echo " Enable all default plugins."
43 echo " Detect plugins which are \"useful\" on this system."
45 echo " set-theme <name>|-"
46 echo " Set active theme to <name>, or to the default."
49 echo " Upgrade AX-ZSH installation (requires Git)."
54 function NormalizedPluginName {
55 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
57 elif [[ "$1" =~ "/" ]]; then
64 function EnableAXZSH {
65 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
66 ln -sv "$AXZSH/ax.zsh" "$f" \
67 || ax_msg 2 "Failed to create symbolic link for \"$f\"!"
71 function DisableAXZSH {
72 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
74 rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
75 elif [ -e "$f" ]; then
76 ax_msg 2 "Error: Not removing \"$f\", it is not a symbolic link!"
78 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
83 function EnablePlugin {
84 local plugin=$(NormalizedPluginName "$1")
85 local dir="$AXZSH/active_plugins"
87 if [[ -h "$dir/$plugin" ]]; then
88 ax_msg 1 "Plugin \"$1\" already active!"
92 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
94 mkdir -p "$AXZSH/repos"
95 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
96 ax_msg - "Cloning plugin from GitHub ..."
97 git clone "https://github.com/$1.git" \
98 "$AXZSH/repos/$plugin"
104 "$AXZSH_PLUGIN_D/$plugin"
105 "$ZSH_CUSTOM/$plugin"
106 "$AXZSH/custom_plugins/$plugin"
107 "$AXZSH/repos/$plugin"
108 "$AXZSH/plugins/$plugin"
109 "$AXZSH/default_plugins/$plugin"
110 "$AXZSH/core/$plugin"
112 [[ ! -d "$dname" ]] && continue
116 ln -sv "$dname" "$PWD"
121 ax_msg 2 "Plugin \"$1\" not found!"
125 function DisablePlugin {
126 local plugin=$(NormalizedPluginName "$1")
127 local dir="$AXZSH/active_plugins"
129 if [[ ! -h "$dir/$plugin" ]]; then
130 ax_msg 1 "Plugin \"$1\" not active?"
134 rm -v "$dir/$plugin"; r=$?
135 [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
139 function ResetPlugins {
140 local dir="$AXZSH/active_plugins"
143 if [[ -e "$dir" ]]; then
144 ax_msg - "Removing all symbolic links in $dir ..."
145 find "$dir" -type l -print -delete; r1=$?
148 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
149 rm -fr "$AXZSH/repos"; r2=$?
151 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
154 function EnableDefaultPlugins {
155 local dir="$AXZSH/active_plugins"
157 ax_msg - "Activating (linking) default plugins ..."
161 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
167 local link_name="$AXZSH/active_theme"
169 if [ $# -ne 1 ]; then
170 echo "Usage: axzsh_set_theme <name|->"
174 rm -f "$link_name" || return 1
176 if [ "$1" = "-" ]; then
177 echo "Theme settings have been reset."
183 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
184 theme="$AXZSH/custom_themes/$1.axzshtheme"
185 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
186 theme="$AXZSH/themes/$1.axzshtheme"
188 echo "Theme \"$1\" not found!"
191 ln -sv "$theme" "$link_name" || return 1
195 function UpgradeAXZSH {
196 if [[ $+commands[git] -eq 0 ]]; then
197 ax_msg 2 "The git(1) command is not available!"
200 if [[ ! -d "$AXZSH/.git" ]]; then
201 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
205 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
206 ( cd "$AXZSH" && git pull --ff-only )
209 function UpgradeForeignPlugins {
210 if [[ $+commands[git] -eq 0 ]]; then
211 ax_msg 2 "The git(1) command is not available!"
215 for dir ($AXZSH/repos/*(N)); do
216 name=${dir:t:s/#/\//}
217 if [ -d "$dir/.git" ]; then
218 ax_msg - "Upgrading \"$name\" [git] ..."
221 git pull --ff-only || ax_msg 2 "Pull failed!"
224 ax_msg 2 "Unknown repository type!"
229 function CheckPlugins {
233 ax_msg - "Checking plugins ..."
234 for dir ($AXZSH/plugins/*(N)); do
237 # Test if plugin is already enabled
238 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
239 && enabled=" (enabled)" \
243 printf " - \"%s\"%s ... " "$plugin" "$enabled"
245 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
246 [[ -r "$script" ]] || continue
247 zsh -i -c "AXZSH_PLUGIN_CHECK=1 source $script"; r=$?
248 if [[ $r -eq 0 ]]; then
253 if [[ -n "$new_plugin" ]]; then
254 detected_plugins+=($new_plugin)
255 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
257 elif [[ $r -eq 91 ]]; then
259 elif [[ $r -eq 92 ]]; then
262 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
269 if [[ -n "$missing_plugins" ]]; then
270 ax_msg 1 "Run the following command to enable all missing plugins:"
271 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
275 ax_msg 0 "All detected plugins are already enabled."
278 if [[ -n "$invalid_plugins" ]]; then
279 ax_msg 1 "Run the following command to disable all failed plugins:"
280 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
283 ax_msg 0 "No failed plugins are enabled."
292 [[ $# -gt 0 ]] || Usage
294 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
295 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
296 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
297 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
307 [[ $# -eq 0 ]] || Usage
311 [[ $# -eq 0 ]] || Usage
315 [[ $# -gt 0 ]] || Usage
316 for plugin in "$@"; do
317 EnablePlugin "$plugin"
321 [[ $# -gt 0 ]] || Usage
322 for plugin in "$@"; do
323 DisablePlugin "$plugin"
327 [[ $# -eq 0 ]] || Usage
331 "enable-default-plugins")
332 [[ $# -eq 0 ]] || Usage
336 [[ $# -eq 0 ]] || Usage
340 [[ $# -eq 1 ]] || Usage
344 [[ $# -eq 0 ]] || Usage
346 UpgradeForeignPlugins