Allow modules to be loaded from multiple places (#1458)

* Allow modules to be loaded from multiple places
* Add setting for user specified module dirs

This is initial work for the contrib repo, mentioned in #1424
main
Kaleb Elwert 7 years ago committed by GitHub
parent ad79f78fbe
commit ce349dff81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

1
.gitignore vendored

@ -1,3 +1,4 @@
*.zwc *.zwc
*.zwc.old *.zwc.old
modules/*/cache.zsh modules/*/cache.zsh
contrib

@ -72,14 +72,44 @@ function zprezto-update {
# Loads Prezto modules. # Loads Prezto modules.
function pmodload { function pmodload {
local -a pmodules local -a pmodules
local -a pmodule_dirs
local -a locations
local pmodule local pmodule
local pmodule_location
local pfunction_glob='^([_.]*|prompt_*_setup|README*|*~)(-.N:t)' local pfunction_glob='^([_.]*|prompt_*_setup|README*|*~)(-.N:t)'
# Load in any additional directories and warn if they don't exist
zstyle -a ':prezto:load' pmodule-dirs 'user_pmodule_dirs'
for user_dir in "$user_pmodule_dirs[@]"; do
if [[ ! -d "$user_dir" ]]; then
echo "$0: Missing user module dir: $user_dir"
fi
done
pmodule_dirs=("$ZPREZTODIR/modules" "$ZPREZTODIR/contrib" "$user_pmodule_dirs[@]")
# $argv is overridden in the anonymous function. # $argv is overridden in the anonymous function.
pmodules=("$argv[@]") pmodules=("$argv[@]")
# Load Prezto modules.
for pmodule in "$pmodules[@]"; do
if zstyle -t ":prezto:module:$pmodule" loaded 'yes' 'no'; then
continue
else
locations=(${pmodule_dirs:+${^pmodule_dirs}/$pmodule(/FN)})
if (( ${#locations} > 1 )); then
print "$0: conflicting module locations: $locations"
continue
elif (( ${#locations} < 1 )); then
print "$0: no such module: $pmodule"
continue
fi
# Grab the full path to this module
pmodule_location=${locations[1]}
# Add functions to $fpath. # Add functions to $fpath.
fpath=(${pmodules:+$ZPREZTODIR/modules/${^pmodules}/functions(/FN)} $fpath) fpath=(${pmodule_location}/functions(/FN) $fpath)
function { function {
local pfunction local pfunction
@ -88,19 +118,11 @@ function pmodload {
setopt LOCAL_OPTIONS EXTENDED_GLOB setopt LOCAL_OPTIONS EXTENDED_GLOB
# Load Prezto functions. # Load Prezto functions.
for pfunction in $ZPREZTODIR/modules/${^pmodules}/functions/$~pfunction_glob; do for pfunction in ${pmodule_location}/functions/$~pfunction_glob; do
autoload -Uz "$pfunction" autoload -Uz "$pfunction"
done done
} }
# Load Prezto modules.
for pmodule in "$pmodules[@]"; do
if zstyle -t ":prezto:module:$pmodule" loaded 'yes' 'no'; then
continue
elif [[ ! -d "$ZPREZTODIR/modules/$pmodule" ]]; then
print "$0: no such module: $pmodule" >&2
continue
else
if [[ -s "$ZPREZTODIR/modules/$pmodule/init.zsh" ]]; then if [[ -s "$ZPREZTODIR/modules/$pmodule/init.zsh" ]]; then
source "$ZPREZTODIR/modules/$pmodule/init.zsh" source "$ZPREZTODIR/modules/$pmodule/init.zsh"
fi fi
@ -109,7 +131,7 @@ function pmodload {
zstyle ":prezto:module:$pmodule" loaded 'yes' zstyle ":prezto:module:$pmodule" loaded 'yes'
else else
# Remove the $fpath entry. # Remove the $fpath entry.
fpath[(r)${ZPREZTODIR}/modules/${pmodule}/functions]=() fpath[(r)${pmodule_location}/functions]=()
function { function {
local pfunction local pfunction
@ -119,7 +141,7 @@ function pmodload {
setopt LOCAL_OPTIONS EXTENDED_GLOB setopt LOCAL_OPTIONS EXTENDED_GLOB
# Unload Prezto functions. # Unload Prezto functions.
for pfunction in $ZPREZTODIR/modules/$pmodule/functions/$~pfunction_glob; do for pfunction in ${pmodule_location}/functions/$~pfunction_glob; do
unfunction "$pfunction" unfunction "$pfunction"
done done
} }

@ -15,6 +15,9 @@
# Color output (auto set to 'no' on dumb terminals). # Color output (auto set to 'no' on dumb terminals).
zstyle ':prezto:*:*' color 'yes' zstyle ':prezto:*:*' color 'yes'
# Add additional directories to load prezto modules from
# zstyle ':prezto:load' pmodule-dirs $HOME/.zprezto-contrib
# Set the Zsh modules to load (man zshmodules). # Set the Zsh modules to load (man zshmodules).
# zstyle ':prezto:load' zmodule 'attr' 'stat' # zstyle ':prezto:load' zmodule 'attr' 'stat'

Loading…
Cancel
Save