]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Implement plugin loading from GitHub
[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         [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
17
18         if [[ ! -d "$dname" ]]; then
19                 # Plugin not found!
20                 if [[ -n "$AXZSH_DEBUG" ]]; then
21                         # Show error message for all stages in "debug mode":
22                         echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
23                 elif [[ "$type" == "zshrc" ]]; then
24                         # Show error message for the "zshrc" stage:
25                         echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
26                 fi
27                 return 1
28         fi
29
30         if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
31                 if [[ -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
32                         # Native AX-ZSH plugin, but for different stage. Skip it!
33                         :
34                 elif [[ -r "$dname/$plugin.plugin.zsh" ]]; then
35                         # Oh My ZSH plugin
36                         type="plugin.zsh"
37                         fname="$dname/$plugin.plugin.zsh"
38                 elif [[ -r "$dname/init.zsh" ]]; then
39                         # Prezto module
40                         type="init.zsh"
41                         fname="$dname/init.zsh"
42                 else
43                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
44                 fi
45         fi
46
47         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
48                 # Add plugin function path when folder exists
49                 axzsh_fpath+=("$dname/functions")
50         fi
51
52         if [[ -r "$fname" ]]; then
53                 [[ -n "$AXZSH_DEBUG" ]] \
54                         && echo "   - $plugin ($type) ..."
55                 source "$fname"
56         fi
57
58         # It is a success, even if only the plugin directory (and no script!)
59         # exists at all! Rationale: The script could be of an other type ...
60         return 0
61 }
62
63 # Make sure that "AXZSH" variable is set and exported
64 if [[ -z "$AXZSH" ]]; then
65         export AXZSH="$HOME/.axzsh"
66         if [[ -f "$HOME/.axzsh.debug" ]]; then
67                 export AXZSH_DEBUG=1
68                 echo "AXZSH=$AXZSH"
69                 echo "AXZSH_DEBUG=$AXZSH_DEBUG"
70                 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
71         fi
72 fi
73
74 [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $script_name:"
75
76 # Setup list of plugins to load:
77 typeset -U plugin_list
78 plugin_list=(
79         "$AXZSH/core/"[0-5]*
80         "$AXZSH/active_plugins/"*(N)
81         "$AXZSH/core/"[6-9]*
82 )
83
84 # Read in all the plugins for the current "type":
85 for plugin ($plugin_list); do
86         axzsh_load_plugin "$plugin" "$script_type"
87 done
88 unfunction axzsh_load_plugin
89 unset script_name script_type plugin
90 unset plugin_list