]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Make sure that $SHELL is set and available
[ax-zsh.git] / ax.zsh
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2016 Alexander Barton <alex@barton.de>
3
4 script_name="${${(%):-%N}:t}"
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         dname="$1:A"
12         plugin="$dname:t"
13         [[ -z "$2" ]] && type="zshrc" || type="$2"
14         fname="$dname/$plugin.$type"
15
16         # Strip repository prefix (like "alexbarton#test-plugin"):
17         [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
18
19         # "short plugin name": strip ".zsh" suffix:
20         plugin_short=${plugin%.zsh}
21
22         if [[ ! -d "$dname" ]]; then
23                 # Plugin not found!
24                 if [[ -n "$AXZSH_DEBUG" ]]; then
25                         # Show error message for all stages in "debug mode":
26                         echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
27                 elif [[ "$type" == "zshrc" ]]; then
28                         # Show error message for the "zshrc" stage:
29                         echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
30                 fi
31                 return 1
32         fi
33
34         if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
35                 if [[ -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
36                         # Native AX-ZSH plugin, but for different stage. Skip it!
37                         :
38                 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
39                         # Oh My ZSH plugin
40                         type="plugin.zsh"
41                         fname="$dname/${plugin_short}.plugin.zsh"
42                 elif [[ -r "$dname/init.zsh" ]]; then
43                         # Prezto module
44                         type="init.zsh"
45                         fname="$dname/init.zsh"
46                 else
47                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
48                         return 0
49                 fi
50         fi
51
52         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
53                 # Add plugin function path when folder exists
54                 [[ -n "$AXZSH_DEBUG" ]] \
55                         && echo "   - $plugin ($type): functions ..."
56                 axzsh_fpath+=("$dname/functions")
57         fi
58
59         if [[ -r "$fname" ]]; then
60                 # Read plugin ...
61                 [[ -n "$AXZSH_DEBUG" ]] \
62                         && echo "   - $plugin ($type) ..."
63                 source "$fname"
64         fi
65
66         # It is a success, even if only the plugin directory (and no script!)
67         # exists at all! Rationale: The script could be of an other type ...
68         return 0
69 }
70
71 # Make sure that "my" (=ZSH) directory is in the search path ...
72 _p="${0:h}"
73 [[ "$_p" != "." ]] && PATH="${0:h}:$PATH"
74 unset _p
75
76 # Make sure that "SHELL" variable is set and exported
77 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
78
79 # Make sure that "AXZSH" variable is set and exported
80 if [[ -z "$AXZSH" ]]; then
81         export AXZSH="$HOME/.axzsh"
82         if [[ -f "$HOME/.axzsh.debug" ]]; then
83                 export AXZSH_DEBUG=1
84                 echo "AXZSH=$AXZSH"
85                 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
86                 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
87         fi
88 fi
89
90 [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $script_name:"
91
92 # Setup list of plugins to load:
93 typeset -U plugin_list
94 plugin_list=(
95         "$AXZSH/core/"[0-5]*
96         "$AXZSH/active_plugins/"*(N)
97         "$AXZSH/core/"[6-9]*
98 )
99
100 # Read in all the plugins for the current "type":
101 for plugin ($plugin_list); do
102         axzsh_load_plugin "$plugin" "$script_type"
103 done
104
105 # Clean up ...
106 unfunction axzsh_load_plugin
107 unset script_name script_type plugin
108 unset plugin_list