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
16 "1"|"2") echo -n "! "; ;;
23 unset dir ax_common ax_common_sourced
26 echo "Usage: $NAME <command> [...]"
28 echo " enable-plugin <name|directory> [<name|directory> [...]]"
29 echo " Enable plugin(s)."
31 echo " disable-plugin <name> [<name> [...]]"
32 echo " Disable plugin(s)."
35 echo " Reset active plugins to the default set."
37 echo " enable-default-plugins"
38 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
54 echo "$1" | sed -e 's|/|#|g'
55 elif [[ "$1" =~ "/" ]]; then
62 function EnablePlugin {
63 local plugin=$(NormalizedPluginName "$1")
64 local dir="$AXZSH/active_plugins"
66 if [[ -h "$dir/$plugin" ]]; then
67 ax_msg 1 "Plugin \"$1\" already active!"
71 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
73 mkdir -p "$AXZSH/repos"
74 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
75 ax_msg - "Cloning plugin from GitHub ..."
76 git clone "https://github.com/$1.git" \
77 "$AXZSH/repos/$plugin"
83 "$AXZSH_PLUGIN_D/$plugin"
85 "$AXZSH/custom_plugins/$plugin"
86 "$AXZSH/repos/$plugin"
87 "$AXZSH/plugins/$plugin"
88 "$AXZSH/default_plugins/$plugin"
91 [[ ! -d "$dname" ]] && continue
95 ln -sv "$dname" "$PWD"
100 ax_msg 2 "Plugin \"$1\" not found!"
104 function DisablePlugin {
105 local plugin=$(NormalizedPluginName "$1")
106 local dir="$AXZSH/active_plugins"
108 if [[ ! -h "$dir/$plugin" ]]; then
109 ax_msg 1 "Plugin \"$1\" not active?"
113 rm -v "$dir/$plugin"; r=$?
114 [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
118 function ResetPlugins {
119 local dir="$AXZSH/active_plugins"
122 if [[ -e "$dir" ]]; then
123 ax_msg - "Removing all symbolic links in $dir ..."
124 find "$dir" -type l -print -delete; r1=$?
127 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
128 rm -fr "$AXZSH/repos"; r2=$?
130 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
133 function EnableDefaultPlugins {
134 local dir="$AXZSH/active_plugins"
136 ax_msg - "Activating (linking) default plugins ..."
140 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
146 local link_name="$AXZSH/active_theme"
148 if [ $# -ne 1 ]; then
149 echo "Usage: axzsh_set_theme <name|->"
153 rm -f "$link_name" || return 1
155 if [ "$1" = "-" ]; then
156 echo "Theme settings have been reset."
162 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
163 theme="$AXZSH/custom_themes/$1.axzshtheme"
164 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
165 theme="$AXZSH/themes/$1.axzshtheme"
167 echo "Theme \"$1\" not found!"
170 ln -sv "$theme" "$link_name" || return 1
174 function UpgradeAXZSH {
175 if [[ $+commands[git] -eq 0 ]]; then
176 ax_msg 2 "The git(1) command is not available!"
179 if [[ ! -d "$AXZSH/.git" ]]; then
180 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
184 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
185 ( cd "$AXZSH" && git pull --ff-only )
188 function UpgradeForeignPlugins {
189 if [[ $+commands[git] -eq 0 ]]; then
190 ax_msg 2 "The git(1) command is not available!"
194 for dir ($AXZSH/repos/*(N)); do
195 name=$(basename "$dir" | sed -e 's|#|/|g')
196 if [ -d "$dir/.git" ]; then
197 ax_msg - "Upgrading \"$name\" [git] ..."
200 git pull --ff-only || ax_msg 2 "Pull failed!"
203 ax_msg 2 "Unknown repository type!"
208 function CheckPlugins {
212 ax_msg - "Checking plugins ..."
213 for dir ($AXZSH/plugins/*(N)); do
214 plugin=$(basename "$dir")
216 # Check plugin blacklist
223 # Test if plugin is already enabled
224 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
225 && enabled=" (enabled)" \
229 printf " - \"%s\"%s ... " "$plugin" "$enabled"
231 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
232 [[ -r "$script" ]] || continue
233 zsh -i -c "source $script"; r=$?
234 if [[ $r -eq 0 ]]; then
239 if [[ -n "$new_plugin" ]]; then
240 detected_plugins+=($new_plugin)
241 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
244 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
251 if [[ -n "$missing_plugins" ]]; then
252 ax_msg 1 "Run the following command to enable all missing plugins:"
253 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
257 ax_msg 0 "All detected plugins are already enabled."
260 if [[ -n "$invalid_plugins" ]]; then
261 ax_msg 1 "Run the following command to disable all failed plugins:"
262 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
265 ax_msg 0 "No failed plugins are enabled."
274 [[ $# -gt 0 ]] || Usage
276 if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
277 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
286 [[ $# -gt 0 ]] || Usage
287 for plugin in "$@"; do
288 EnablePlugin "$plugin"
292 [[ $# -gt 0 ]] || Usage
293 for plugin in "$@"; do
294 DisablePlugin "$plugin"
298 [[ $# -eq 0 ]] || Usage
302 "enable-default-plugins")
303 [[ $# -eq 0 ]] || Usage
307 [[ $# -eq 0 ]] || Usage
311 [[ $# -eq 1 ]] || Usage
315 [[ $# -eq 0 ]] || Usage
317 UpgradeForeignPlugins