3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2017 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
22 unset dir ax_common ax_common_sourced
25 echo "Usage: $NAME <command> [...]"
28 echo " Enable AX-ZSH altogether."
30 echo " Disable AX-ZSH altogether."
32 echo " enable-plugin <name|directory> [<name|directory> [...]]"
33 echo " Enable plugin(s)."
34 echo " disable-plugin <name> [<name> [...]]"
35 echo " Disable plugin(s)."
37 echo " List enabled plugins."
40 echo " Reset active plugins to the default set."
41 echo " enable-default-plugins"
42 echo " Enable all default plugins."
44 echo " Detect plugins which are \"useful\" on this system."
46 echo " set-theme <name>|-"
47 echo " Set active theme to <name>, or to the default."
50 echo " Upgrade AX-ZSH installation (requires Git)."
52 echo " Force rebuild of all cache files."
57 function UpdatePluginCache {
58 [[ -r "$AXZSH/cache" ]] || return 0
60 ax_msg - "Updating plugin cache ..."
62 $AXZSH/cache/zlogin.cache \
63 $AXZSH/cache/zlogout.cache \
64 $AXZSH/cache/zprofile.cache \
65 $AXZSH/cache/zshrc.cache \
67 echo "Regenerating cache files ..."
68 zsh -ilc '' >/dev/null
71 function NormalizedPluginName {
72 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
74 elif [[ "$1" =~ "/" ]]; then
81 function EnableAXZSH {
82 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
83 ln -sv "$AXZSH/ax.zsh" "$f" \
84 || ax_error "Failed to create symbolic link for \"$f\"!"
88 function DisableAXZSH {
89 for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
91 rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
92 elif [ -e "$f" ]; then
93 ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
95 ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
100 function EnablePlugin {
101 local plugin=$(NormalizedPluginName "$1")
102 local dir="$AXZSH/active_plugins"
104 if [[ -h "$dir/$plugin" ]]; then
105 ax_msg 1 "Plugin \"$1\" already active!"
109 if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
111 mkdir -p "$AXZSH/repos"
112 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
113 ax_msg - "Cloning module from GitHub ..."
114 git clone --depth=1 "https://github.com/$1.git" \
115 "$AXZSH/repos/$plugin"
117 # Try to enable a theme in this "foreign module", but ignore
118 # errors: we don't know if this module provides a theme or is
119 # a "regular" plugin ...
120 if SetTheme "${plugin#*#}" 2>/dev/null; then
121 ax_msg 1 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
122 # A theme was enabled: So assume that this is a theme
123 # and don't enable it as plugin.
126 echo "Module \"$1\" will be enabled as plugin ..."
131 "$AXZSH_PLUGIN_D/$plugin"
132 "$ZSH_CUSTOM/$plugin"
133 "$AXZSH/custom_plugins/$plugin"
134 "$AXZSH/repos/$plugin"
135 "$AXZSH/plugins/$plugin"
136 "$AXZSH/default_plugins/$plugin"
137 "$AXZSH/core/$plugin"
139 [[ ! -d "$dname" ]] && continue
143 ln -sv "$dname" "$PWD"
148 ax_error "Plugin \"$1\" not found!"
152 function DisablePlugin {
153 local plugin=$(NormalizedPluginName "$1")
154 local dir="$AXZSH/active_plugins"
156 if [[ ! -h "$dir/$plugin" ]]; then
157 ax_msg 1 "Plugin \"$1\" not active?"
161 rm -v "$dir/$plugin"; r=$?
162 [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
166 function ListEnabledPlugins {
167 for plugin ($AXZSH/active_plugins/*(N)); do
168 print ${plugin:t:s/#/\//}
173 function ResetPlugins {
174 local dir="$AXZSH/active_plugins"
177 if [[ -e "$dir" ]]; then
178 ax_msg - "Removing all symbolic links in $dir ..."
179 find "$dir" -type l -print -delete; r1=$?
182 ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
183 rm -fr "$AXZSH/repos"; r2=$?
185 [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
188 function EnableDefaultPlugins {
189 local dir="$AXZSH/active_plugins"
191 ax_msg - "Activating (linking) default plugins ..."
195 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
201 local link_name="$AXZSH/active_theme"
203 if [ "$1" = "-" ]; then
204 rm -f "$link_name" || return 1
205 ax_msg 0 "Theme settings have been reset."
211 elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
212 theme="$AXZSH/custom_themes/$1.axzshtheme"
213 elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
214 theme="$AXZSH/themes/$1.axzshtheme"
216 # Look for theme inside of installed plugins:
218 "$AXZSH/custom_themes"
219 "$AXZSH/custom_plugins/"*(N)
222 if [[ -r "$dname/$1.axzshtheme" ]]; then
223 theme="$dname/$1.axzshtheme"
225 elif [[ -r "$dname/$1.zsh-theme" ]]; then
226 theme="$dname/$1.zsh-theme"
230 if [[ -z "$theme" ]]; then
231 ax_error "Theme \"$1\" not found!"
235 ln -fsv "$theme" "$link_name" || return 1
239 function UpgradeAXZSH {
240 if [[ $+commands[git] -eq 0 ]]; then
241 ax_error "The git(1) command is not available!"
244 if [[ ! -d "$AXZSH/.git" ]]; then
245 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
249 ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
250 ( cd "$AXZSH" && git pull --ff-only )
253 function UpgradeForeignPlugins {
254 if [[ $+commands[git] -eq 0 ]]; then
255 ax_error "The git(1) command is not available!"
259 for dir ($AXZSH/repos/*(N)); do
260 name=${dir:t:s/#/\//}
261 if [ -d "$dir/.git" ]; then
262 ax_msg - "Upgrading \"$name\" [git] ..."
265 git pull --ff-only || ax_error "Pull failed!"
268 ax_error "Unknown repository type!"
273 function CheckPlugins {
277 ax_msg - "Checking plugins ..."
278 for dir ($AXZSH/plugins/*(N)); do
281 # Test if plugin is already enabled
282 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
283 && enabled=" (enabled)" \
287 printf " - \"%s\"%s ... " "$plugin" "$enabled"
289 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
290 [[ -r "$script" ]] || continue
291 AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
292 if [[ $r -eq 0 ]]; then
297 if [[ -n "$new_plugin" ]]; then
298 detected_plugins+=($new_plugin)
299 [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
301 elif [[ $r -eq 91 ]]; then
303 elif [[ $r -eq 92 ]]; then
306 [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
313 if [[ -n "$missing_plugins" ]]; then
314 ax_msg 1 "Run the following command to enable all missing plugins:"
315 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
319 ax_msg 0 "All detected plugins are already enabled."
322 if [[ -n "$invalid_plugins" ]]; then
323 ax_msg 1 "Run the following command to disable all failed plugins:"
324 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
327 ax_msg 0 "No failed plugins are enabled."
336 [[ $# -gt 0 ]] || Usage
338 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
339 [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
340 if [[ ! -r "$AXZSH/ax.zsh" ]]; then
341 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
351 [[ $# -eq 0 ]] || Usage
355 [[ $# -eq 0 ]] || Usage
359 [[ $# -gt 0 ]] || Usage
360 for plugin in "$@"; do
361 EnablePlugin "$plugin"
366 [[ $# -gt 0 ]] || Usage
367 for plugin in "$@"; do
368 DisablePlugin "$plugin"
373 [[ $# -eq 0 ]] || Usage
377 [[ $# -eq 0 ]] || Usage
382 "enable-default-plugins")
383 [[ $# -eq 0 ]] || Usage
384 EnableDefaultPlugins && UpdatePluginCache
387 [[ $# -eq 0 ]] || Usage
391 [[ $# -eq 1 ]] || Usage
395 [[ $# -eq 0 ]] || Usage
397 UpgradeForeignPlugins
401 [[ $# -eq 0 ]] || Usage