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