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