]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Add some more comments ...
[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                 fi
49         fi
50
51         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
52                 # Add plugin function path when folder exists
53                 [[ -n "$AXZSH_DEBUG" ]] \
54                         && echo "   - $plugin ($type): functions ..."
55                 axzsh_fpath+=("$dname/functions")
56         fi
57
58         if [[ -r "$fname" ]]; then
59                 # Read plugin ...
60                 [[ -n "$AXZSH_DEBUG" ]] \
61                         && echo "   - $plugin ($type) ..."
62                 source "$fname"
63         fi
64
65         # It is a success, even if only the plugin directory (and no script!)
66         # exists at all! Rationale: The script could be of an other type ...
67         return 0
68 }
69
70 # Make sure that "AXZSH" variable is set and exported
71 if [[ -z "$AXZSH" ]]; then
72         export AXZSH="$HOME/.axzsh"
73         if [[ -f "$HOME/.axzsh.debug" ]]; then
74                 export AXZSH_DEBUG=1
75                 echo "AXZSH=$AXZSH"
76                 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
77                 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
78         fi
79 fi
80
81 [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $script_name:"
82
83 # Setup list of plugins to load:
84 typeset -U plugin_list
85 plugin_list=(
86         "$AXZSH/core/"[0-5]*
87         "$AXZSH/active_plugins/"*(N)
88         "$AXZSH/core/"[6-9]*
89 )
90
91 # Read in all the plugins for the current "type":
92 for plugin ($plugin_list); do
93         axzsh_load_plugin "$plugin" "$script_type"
94 done
95
96 # Clean up ...
97 unfunction axzsh_load_plugin
98 unset script_name script_type plugin
99 unset plugin_list