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