]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Don't call external tools, use variable modifiers
[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="${${(%):-%N}:t}"
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         dname="$1:A"
14         plugin="$dname:t"
15         [[ -z "$2" ]] && type="zshrc" || type="$2"
16         fname="$dname/$plugin.$type"
17
18         if [[ ! -d "$dname" ]]; then
19                 # Plugin not found!
20                 if [[ -f "$HOME/.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.plugin.zsh" ]]; then
32                         # Oh My ZSH plugin
33                         type="plugin.zsh"
34                         fname="$dname/$plugin.plugin.zsh"
35                 elif [[ -r "$dname/init.zsh" ]]; then
36                         # Prezto module
37                         type="init.zsh"
38                         fname="$dname/init.zsh"
39                 fi
40         fi
41
42         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
43                 # Add plugin function path when folder exists
44                 axzsh_fpath+=("$dname/functions")
45         fi
46
47         if [[ -r "$fname" ]]; then
48                 [[ -f "$HOME/.axzsh.debug" ]] \
49                         && echo "   - $plugin ($type) ..."
50                 source "$fname"
51         fi
52
53         # It is a success, even if only the plugin directory (and no script!)
54         # exists at all! Rationale: The script could be of an other type ...
55         return 0
56 }
57
58 # Make sure that "AXZSH" variable is set and exported
59 if [[ -z "$AXZSH" ]]; then
60         export AXZSH="$HOME/.axzsh"
61         if [[ -f "$HOME/.axzsh.debug" ]]; then
62                 echo "AXZSH=$AXZSH"
63                 echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
64         fi
65 fi
66
67 # Setup list of plugins to load:
68 typeset -U plugin_list
69 plugin_list=(
70         "$AXZSH/core/"[0-5]*
71         "$AXZSH/active_plugins/"*(N)
72         "$AXZSH/core/"[6-9]*
73 )
74
75 # Read in all the plugins for the current "type":
76 for plugin ($plugin_list); do
77         axzsh_load_plugin "$plugin" "$script_type"
78 done
79 unfunction axzsh_load_plugin
80 unset script_name script_type plugin
81 unset plugin_list