]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
5f8e662fe2a6c108e63854fc6b03041f4d913e19
[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 # Load plugin code of a given type.
8 # - $1: plugin name
9 # - $2: plugin type (optional; defaults to "zshrc")
10 function axzsh_load_plugin {
11         plugin="$1"
12         [[ -z "$2" ]] && type="zshrc" || type="$2"
13
14         for dname in \
15                 "$AXZSH_PLUGIN_D/$plugin" \
16                 "$AXZSH/plugins/$plugin" \
17                 "$AXZSH/core/$plugin" \
18         ; do
19                 [[ ! -d "$dname" ]] && continue
20
21                 fname="$dname/$plugin.$type"
22                 if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
23                         if [[ -r "$dname/$plugin.plugin.zsh" ]]; then
24                                 # Oh My ZSH plugin
25                                 type="plugin.zsh"
26                                 fname="$dname/$plugin.plugin.zsh"
27                         elif [[ -r "$dname/init.zsh" ]]; then
28                                 # Prezto module
29                                 type="init.zsh"
30                                 fname="$dname/init.zsh"
31                         fi
32                 fi
33
34                 if [[ -r "$fname" ]]; then
35                         [[ -f "$HOME/.axzsh.debug" ]] \
36                                 && echo "   - $plugin ($type) ..."
37                         source "$fname"
38                         return 0
39                 fi
40                 return 0
41         done
42         # Plugin not found!
43         if [[ -f "$HOME/.axzsh.debug" ]]; then
44                 # Show error message for all stages in "debug mode":
45                 echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
46         elif [[ "$type" == "zshrc" ]]; then
47                 # Show error message for the "zshrc" stage:
48                 echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
49         fi
50         return 1
51 }
52
53 # Make sure that "AXZSH" variable is set and exported
54 if [[ -z "$AXZSH" ]]; then
55         export AXZSH="$HOME/.axzsh"
56         [[ -f "$HOME/.axzsh.debug" ]] && echo "AXZSH=$AXZSH"
57 fi
58
59 # Setup list of default plugins if not set already. This allows users to
60 # overwrite this list in their "~/.zshenv" file, for example.
61 typeset -U axzsh_default_plugins
62 if ! typeset +m axzsh_default_plugins | fgrep array >/dev/null 2>&1; then
63         axzsh_default_plugins=(
64                 byebye
65                 completion
66                 correction
67                 history
68                 ls
69                 prompt
70                 ssh
71                 std_aliases
72                 std_env
73         )
74 fi
75
76 # Setup list of plugins to load:
77 typeset -U plugin_list
78 plugin_list=(
79         $AXZSH/core/[0-5]*
80         $axzsh_default_plugins
81         $axzsh_plugins
82         $plugins
83         $AXZSH/core/[6-9]*
84 )
85
86 # Read in all the plugins for the current "type":
87 [[ -f "$HOME/.axzsh.debug" ]] && echo "ยป $script_name:"
88 for plugin ($plugin_list); do
89         axzsh_load_plugin "$(basename "$plugin")" "$script_type"
90 done
91 unset script_name script_type plugin
92 unset plugin_list