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