]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Try to load Oh My ZSH plugins with "zsh-" prefix stripped
[ax-zsh.git] / ax.zsh
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2017 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 # - $3: cache file (optional)
11 function axzsh_load_plugin {
12         local dname="$1:A"
13         local plugin="$dname:t"
14         [[ -z "$2" ]] && local type="zshrc" || local type="$2"
15         local fname="$dname/$plugin.$type"
16         local cache_file="$3"
17
18         # Strip repository prefix (like "alexbarton#test-plugin"):
19         [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
20
21         # "short plugin name": strip ".zsh" suffix:
22         plugin_short=${plugin%.zsh}
23
24         if [[ ! -d "$dname" ]]; then
25                 # Plugin not found!
26                 if [[ -n "$AXZSH_DEBUG" ]]; then
27                         # Show error message for all stages in "debug mode":
28                         echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
29                 elif [[ "$type" == "zshrc" ]]; then
30                         # Show error message for the "zshrc" stage:
31                         echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
32                 fi
33                 return 1
34         fi
35
36         if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
37                 if [[ -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
38                         # Native AX-ZSH plugin, but for different stage. Skip it!
39                         :
40                 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
41                         # Oh My ZSH plugin
42                         type="plugin.zsh"
43                         fname="$dname/${plugin_short}.plugin.zsh"
44                 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
45                         # Oh My ZSH plugin with "zsh-" prefix stripped
46                         type="plugin.zsh"
47                         fname="$dname/${plugin_short##zsh-}.plugin.zsh"
48                 elif [[ -r "$dname/init.zsh" ]]; then
49                         # Prezto module
50                         type="init.zsh"
51                         fname="$dname/init.zsh"
52                 else
53                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
54                         return 0
55                 fi
56         fi
57
58         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
59                 # Add plugin function path when folder exists
60                 [[ -n "$AXZSH_DEBUG" ]] \
61                         && echo "   - $plugin ($type): functions ..."
62                 axzsh_fpath+=("$dname/functions")
63
64                 # Add function path to cache file.
65                 [[ -n "$cache_file" ]] \
66                         && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
67         fi
68
69         if [[ -r "$fname" ]]; then
70                 # Read plugin ...
71                 [[ -n "$AXZSH_DEBUG" ]] \
72                         && echo "   - $plugin ($type) ..."
73                 source "$fname"
74
75                 if [[ -n "$cache_file" ]]; then
76                         # Add plugin data to cache
77                         printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
78                         case "$fname" in
79                                 *"/repos/"*)
80                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
81                                         echo "source '$fname'" >>$cache_file
82                                         ;;
83                                 *)
84                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
85                                         "$cat_cmd" "$fname" >>"$cache_file"
86                         esac
87                         printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
88                 fi
89         fi
90
91         # It is a success, even if only the plugin directory (and no script!)
92         # exists at all! Rationale: The script could be of an other type ...
93         return 0
94 }
95
96 # Make sure that "my" (=ZSH) directory is in the search path ...
97 if [[ -z "$AXZSH" ]]; then
98         _p="${0:h}"
99         [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
100         unset _p
101 fi
102
103 # Make sure that "SHELL" variable is set and exported
104 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
105
106 # Make sure that "AXZSH" variable is set and exported
107 if [[ -z "$AXZSH" ]]; then
108         export AXZSH="$HOME/.axzsh"
109         if [[ -f "$HOME/.axzsh.debug" ]]; then
110                 export AXZSH_DEBUG=1
111                 echo "AXZSH=$AXZSH"
112                 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
113                 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
114         fi
115 fi
116
117 [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $script_name:"
118
119 # Initialize cache
120 mkdir -p "$AXZSH/cache"
121 cache_file="$AXZSH/cache/$script_type.cache"
122
123 cat_cmd=${commands[cat]:-cat}
124
125 if [[ -r "$cache_file" ]]; then
126         # Cache file exists, use it!
127         # But when in the "zshrc" stage, make sure that the "zprofile" stage
128         # has already been handled (this uses the "01_zprofile" plugin which
129         # is used in the "zshrc.cache" as well, but can't be used successfully
130         # there because it becomes sourced inside of a ZSH function; so we have
131         # to source it here in the global context manually ...):
132         [[ -z "$AXZSH_ZPROFILE_READ" && "$script_type" = "zshrc" ]] \
133                 && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
134         [[ -n "$AXZSH_DEBUG" ]] \
135                 && echo "   - Reading cache file \"$cache_file\" ..."
136         source "$cache_file"
137         unfunction ax_plugin_init
138 else
139         # No cache file available.
140         # Setup list of plugins to load:
141         typeset -U plugin_list
142         plugin_list=(
143                 "$AXZSH/core/"[0-5]*
144                 "$AXZSH/active_plugins/"*(N)
145                 "$AXZSH/core/"[6-9]*
146         )
147
148         # Create new cache file:
149         if [[ -n "$cache_file" ]]; then
150                 [[ -n "$AXZSH_DEBUG" ]] \
151                         && echo "   (Writing new cache file to \"$cache_file\" ...)"
152                 printf "# %s\n\n" "$(LC_ALL=C date)" >"$cache_file"
153         fi
154
155         # Read in all the plugins for the current "type":
156         for plugin ($plugin_list); do
157                 axzsh_load_plugin "$plugin" "$script_type" "$cache_file"
158         done
159 fi
160
161 # Clean up ...
162 unfunction axzsh_load_plugin
163 unset script_name script_type plugin
164 unset plugin_list
165 unset cache_file
166 unset cat_cmd