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)."
52 function NormalizedPluginName {
53 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
55 elif [[ "$1" =~ "/" ]]; then
62 function EnableAXZSH {
63 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
64 ln -sv "$AXZSH/ax.zsh" "$f" \
65 || ax_msg 2 "Failed to create symbolic link for \"$f\"!"
69 function DisableAXZSH {
70 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
72 rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
73 elif [ -e "$f" ]; then
74 ax_msg 2 "Error: Not removing \"$f\", it is not a symbolic link!"
76 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
81 function EnablePlugin {
82 local plugin=$(NormalizedPluginName "$1")
83 local dir="$AXZSH/active_plugins"
85 if [[ -h "$dir/$plugin" ]]; then
86 ax_msg 1 "Plugin \"$1\" already active!"
90 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
92 mkdir -p "$AXZSH/repos"
93 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
94 ax_msg - "Cloning plugin from GitHub ..."
95 git clone "https://github.com/$1.git" \
96 "$AXZSH/repos/$plugin"
102 "$AXZSH_PLUGIN_D/$plugin"
103 "$ZSH_CUSTOM/$plugin"
104 "$AXZSH/custom_plugins/$plugin"
105 "$AXZSH/repos/$plugin"
106 "$AXZSH/plugins/$plugin"
107 "$AXZSH/default_plugins/$plugin"
108 "$AXZSH/core/$plugin"
110 [[ ! -d "$dname" ]] && continue
114 ln -sv "$dname" "$PWD"
119 ax_msg 2 "Plugin \"$1\" not found!"
123 function DisablePlugin {
124 local plugin=$(NormalizedPluginName "$1")
125 local dir="$AXZSH/active_plugins"
127 if [[ ! -h "$dir/$plugin" ]]; then
128 ax_msg 1 "Plugin \"$1\" not active?"
132 rm -v "$dir/$plugin"; r=$?
133 [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
137 function ListEnabledPlugins {
138 for plugin ($AXZSH/active_plugins/*(N)); do
139 print ${plugin:t:s/#/\//}
144 function ResetPlugins {
145 local dir="$AXZSH/active_plugins"
148 if [[ -e "$dir" ]]; then
149 ax_msg - "Removing all symbolic links in $dir ..."
150 find "$dir" -type l -print -delete; r1=$?
153 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
154 rm -fr "$AXZSH/repos"; r2=$?
156 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
159 function EnableDefaultPlugins {
160 local dir="$AXZSH/active_plugins"
162 ax_msg - "Activating (linking) default plugins ..."
166 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
172 local link_name="$AXZSH/active_theme"
174 if [ $# -ne 1 ]; then
175 echo "Usage: axzsh_set_theme <name|->"
179 rm -f "$link_name" || return 1
181 if [ "$1" = "-" ]; then
182 echo "Theme settings have been reset."
188 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
189 theme="$AXZSH/custom_themes/$1.axzshtheme"
190 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
191 theme="$AXZSH/themes/$1.axzshtheme"
193 echo "Theme \"$1\" not found!"
196 ln -sv "$theme" "$link_name" || return 1
200 function UpgradeAXZSH {
201 if [[ $+commands[git] -eq 0 ]]; then
202 ax_msg 2 "The git(1) command is not available!"
205 if [[ ! -d "$AXZSH/.git" ]]; then
206 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
210 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
211 ( cd "$AXZSH" && git pull --ff-only )
214 function UpgradeForeignPlugins {
215 if [[ $+commands[git] -eq 0 ]]; then
216 ax_msg 2 "The git(1) command is not available!"
220 for dir ($AXZSH/repos/*(N)); do
221 name=${dir:t:s/#/\//}
222 if [ -d "$dir/.git" ]; then
223 ax_msg - "Upgrading \"$name\" [git] ..."
226 git pull --ff-only || ax_msg 2 "Pull failed!"
229 ax_msg 2 "Unknown repository type!"
234 function CheckPlugins {
238 ax_msg - "Checking plugins ..."
239 for dir ($AXZSH/plugins/*(N)); do
242 # Test if plugin is already enabled
243 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
244 && enabled=" (enabled)" \
248 printf " - \"%s\"%s ... " "$plugin" "$enabled"
250 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
251 [[ -r "$script" ]] || continue
252 zsh -i -c "AXZSH_PLUGIN_CHECK=1 source $script"; r=$?
253 if [[ $r -eq 0 ]]; then
258 if [[ -n "$new_plugin" ]]; then
259 detected_plugins+=($new_plugin)
260 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
262 elif [[ $r -eq 91 ]]; then
264 elif [[ $r -eq 92 ]]; then
267 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
274 if [[ -n "$missing_plugins" ]]; then
275 ax_msg 1 "Run the following command to enable all missing plugins:"
276 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
280 ax_msg 0 "All detected plugins are already enabled."
283 if [[ -n "$invalid_plugins" ]]; then
284 ax_msg 1 "Run the following command to disable all failed plugins:"
285 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
288 ax_msg 0 "No failed plugins are enabled."
297 [[ $# -gt 0 ]] || Usage
299 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
300 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
301 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
302 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
312 [[ $# -eq 0 ]] || Usage
316 [[ $# -eq 0 ]] || Usage
320 [[ $# -gt 0 ]] || Usage
321 for plugin in "$@"; do
322 EnablePlugin "$plugin"
326 [[ $# -gt 0 ]] || Usage
327 for plugin in "$@"; do
328 DisablePlugin "$plugin"
332 [[ $# -eq 0 ]] || Usage
336 [[ $# -eq 0 ]] || Usage
340 "enable-default-plugins")
341 [[ $# -eq 0 ]] || Usage
345 [[ $# -eq 0 ]] || Usage
349 [[ $# -eq 1 ]] || Usage
353 [[ $# -eq 0 ]] || Usage
355 UpgradeForeignPlugins