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> [...]"
24 echo " enable-plugin <name|directory> [<name|directory> [...]]"
25 echo " Enable plugin(s)."
27 echo " disable-plugin <name> [<name> [...]]"
28 echo " Disable plugin(s)."
31 echo " Reset active plugins to the default set."
33 echo " enable-default-plugins"
34 echo " Enable all default plugins."
37 echo " Detect plugins which are \"useful\" on this system."
39 echo " set-theme <name>|-"
40 echo " Set active theme to <name>, or to the default."
43 echo " Upgrade AX-ZSH installation (requires Git)."
48 function NormalizedPluginName {
49 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
50 echo "$1" | sed -e 's|/|#|g'
51 elif [[ "$1" =~ "/" ]]; then
58 function EnablePlugin {
59 local plugin=$(NormalizedPluginName "$1")
60 local dir="$AXZSH/active_plugins"
62 if [[ -h "$dir/$plugin" ]]; then
63 ax_msg 1 "Plugin \"$1\" already active!"
67 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
69 mkdir -p "$AXZSH/repos"
70 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
71 ax_msg - "Cloning plugin from GitHub ..."
72 git clone "https://github.com/$1.git" \
73 "$AXZSH/repos/$plugin"
79 "$AXZSH_PLUGIN_D/$plugin"
81 "$AXZSH/custom_plugins/$plugin"
82 "$AXZSH/repos/$plugin"
83 "$AXZSH/plugins/$plugin"
84 "$AXZSH/default_plugins/$plugin"
87 [[ ! -d "$dname" ]] && continue
91 ln -sv "$dname" "$PWD"
96 ax_msg 2 "Plugin \"$1\" not found!"
100 function DisablePlugin {
101 local plugin=$(NormalizedPluginName "$1")
102 local dir="$AXZSH/active_plugins"
104 if [[ ! -h "$dir/$plugin" ]]; then
105 ax_msg 1 "Plugin \"$1\" not active?"
109 rm -v "$dir/$plugin"; r=$?
110 [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
114 function ResetPlugins {
115 local dir="$AXZSH/active_plugins"
118 if [[ -e "$dir" ]]; then
119 ax_msg - "Removing all symbolic links in $dir ..."
120 find "$dir" -type l -print -delete; r1=$?
123 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
124 rm -fr "$AXZSH/repos"; r2=$?
126 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
129 function EnableDefaultPlugins {
130 local dir="$AXZSH/active_plugins"
132 ax_msg - "Activating (linking) default plugins ..."
136 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
142 local link_name="$AXZSH/active_theme"
144 if [ $# -ne 1 ]; then
145 echo "Usage: axzsh_set_theme <name|->"
149 rm -f "$link_name" || return 1
151 if [ "$1" = "-" ]; then
152 echo "Theme settings have been reset."
158 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
159 theme="$AXZSH/custom_themes/$1.axzshtheme"
160 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
161 theme="$AXZSH/themes/$1.axzshtheme"
163 echo "Theme \"$1\" not found!"
166 ln -sv "$theme" "$link_name" || return 1
170 function UpgradeAXZSH {
171 if [[ $+commands[git] -eq 0 ]]; then
172 ax_msg 2 "The git(1) command is not available!"
175 if [[ ! -d "$AXZSH/.git" ]]; then
176 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
180 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
181 ( cd "$AXZSH" && git pull --ff-only )
184 function UpgradeForeignPlugins {
185 if [[ $+commands[git] -eq 0 ]]; then
186 ax_msg 2 "The git(1) command is not available!"
190 for dir ($AXZSH/repos/*(N)); do
191 name=$(basename "$dir" | sed -e 's|#|/|g')
192 if [ -d "$dir/.git" ]; then
193 ax_msg - "Upgrading \"$name\" [git] ..."
196 git pull --ff-only || ax_msg 2 "Pull failed!"
199 ax_msg 2 "Unknown repository type!"
204 function CheckPlugins {
208 ax_msg - "Checking plugins ..."
209 for dir ($AXZSH/plugins/*(N)); do
210 plugin=$(basename "$dir")
212 # Check plugin blacklist
219 # Test if plugin is already enabled
220 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
221 && enabled=" (enabled)" \
225 printf " - \"%s\"%s ... " "$plugin" "$enabled"
227 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
228 [[ -r "$script" ]] || continue
229 zsh -i -c "source $script"; r=$?
230 if [[ $r -eq 0 ]]; then
235 if [[ -n "$new_plugin" ]]; then
236 detected_plugins+=($new_plugin)
237 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
240 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
247 if [[ -n "$missing_plugins" ]]; then
248 ax_msg 1 "Run the following command to enable all missing plugins:"
249 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
253 ax_msg 0 "All detected plugins are already enabled."
256 if [[ -n "$invalid_plugins" ]]; then
257 ax_msg 1 "Run the following command to disable all failed plugins:"
258 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
261 ax_msg 0 "No failed plugins are enabled."
270 [[ $# -gt 0 ]] || Usage
272 if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
273 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
282 [[ $# -gt 0 ]] || Usage
283 for plugin in "$@"; do
284 EnablePlugin "$plugin"
288 [[ $# -gt 0 ]] || Usage
289 for plugin in "$@"; do
290 DisablePlugin "$plugin"
294 [[ $# -eq 0 ]] || Usage
298 "enable-default-plugins")
299 [[ $# -eq 0 ]] || Usage
303 [[ $# -eq 0 ]] || Usage
307 [[ $# -eq 1 ]] || Usage
311 [[ $# -eq 0 ]] || Usage
313 UpgradeForeignPlugins