]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Plugins are activated using symlinks in $AXZSH/active_plugins
[ax-zsh.git] / ax.zsh
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015 Alexander Barton <alex@barton.de>
3
4 script_name="$(basename -- "${(%):-%N}")"
5 script_type="$script_name[2,-1]"
6
7 [[ -f "$HOME/.axzsh.debug" ]] && echo "ยป $script_name:"
8
9 # Load plugin code of a given type.
10 # - $1: plugin name
11 # - $2: plugin type (optional; defaults to "zshrc")
12 function axzsh_load_plugin {
13         dname="$(readlink "$1")" || dname="$1"
14         plugin="$(basename "$dname")"
15         [[ -z "$2" ]] && type="zshrc" || type="$2"
16         fname="$dname/$plugin.$type"
17
18         if [[ ! -d "$dname" ]]; then
19                 # Plugin not found!
20                 if [[ -f "$HOME/.axzsh.debug" ]]; then
21                         # Show error message for all stages in "debug mode":
22                         echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
23                 elif [[ "$type" == "zshrc" ]]; then
24                         # Show error message for the "zshrc" stage:
25                         echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
26                 fi
27                 return 1
28         fi
29
30         if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
31                 if [[ -r "$dname/$plugin.plugin.zsh" ]]; then
32                         # Oh My ZSH plugin
33                         type="plugin.zsh"
34                         fname="$dname/$plugin.plugin.zsh"
35                 elif [[ -r "$dname/init.zsh" ]]; then
36                         # Prezto module
37                         type="init.zsh"
38                         fname="$dname/init.zsh"
39                 fi
40         fi
41
42         if [[ -r "$fname" ]]; then
43                 [[ -f "$HOME/.axzsh.debug" ]] \
44                         && echo "   - $plugin ($type) ..."
45                 source "$fname"
46         fi
47
48         # It is a success, even if only the plugin directory (and no script!)
49         # exists at all! Rationale: The script could be of an other type ...
50         return 0
51 }
52
53 # Make sure that "AXZSH" variable is set and exported
54 if [[ -z "$AXZSH" ]]; then
55         export AXZSH="$HOME/.axzsh"
56         if [[ -f "$HOME/.axzsh.debug" ]]; then
57                 echo "AXZSH=$AXZSH"
58                 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
59         fi
60 fi
61
62 # Setup list of plugins to load:
63 typeset -U plugin_list
64 plugin_list=(
65         "$AXZSH/core/"[0-5]*
66         "$AXZSH/active_plugins/"*
67         "$AXZSH/core/"[6-9]*
68 )
69
70 # Read in all the plugins for the current "type":
71 for plugin ($plugin_list); do
72         axzsh_load_plugin "$plugin" "$script_type"
73 done
74 unset script_name script_type plugin
75 unset plugin_list