From 345d95999fb018372bf22eb75d4201e4aac764ee Mon Sep 17 00:00:00 2001 From: "Alexander Gehrke (crater2150)" Date: Mon, 25 Jul 2016 13:48:52 +0200 Subject: [PATCH] Fix handling of ZDOTDIR variable --- modules/autoloader/init | 43 +++++++++++++++++++++++++++-------------- modules/loader.zsh | 41 +++++++++++++++++++++++++++------------ zshrc | 22 +++++++++++++++++---- 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/modules/autoloader/init b/modules/autoloader/init index 0ca7128..7a7bc07 100644 --- a/modules/autoloader/init +++ b/modules/autoloader/init @@ -7,25 +7,38 @@ # Lets you load custom functions and zle widgets split into single files. # # zle widgets are stored in the "widgets" folder in your configuration directory -# (set with $ZDOTDIR, see manpage. defaults to /etc/zsh) +# (set with $ZDOTDIR, see manpage). The widgets in /etc/zsh/widgets are also +# considered. # # functions are stored in the "functions" folder analogous. # -ZWIDGETPATH=${ZWIDGETPATH:-"${ZDOTDIR:-/etc/zsh}/widgets"} -ZFUNCTIONPATH=${ZFUNCTIONPATH:-"${ZDOTDIR:-/etc/zsh}/functions"} -fpath+=( "${ZWIDGETPATH}" ) -fpath+=( "${ZFUNCTIONPATH}" ) - -if [ -d $ZWIDGETPATH ]; then - for i in $ZWIDGETPATH/*(N:t); do - autoload -Uz $i - zle -N $i - done +if [[ -z $ZWIDGETPATH ]]; then + ZWIDGETPATH=( ${ZDOTDIR:+$ZDOTDIR/widgets} ) + [[ -d /etc/zsh/widgets ]] && ZWIDGETPATH+=/etc/zsh/widgets fi -if [ -d $ZFUNCTIONPATH ]; then - for i in $ZFUNCTIONPATH/*(N:t); do - autoload -Uz $i && $i - done +if [[ -z $ZFUNCTIONPATH ]]; then + ZFUNCTIONPATH=( ${ZDOTDIR:+$ZDOTDIR/functions} ) + [[ -d /etc/zsh/functions ]] && ZFUNCTIONPATH+=/etc/zsh/functions fi + +fpath+=(${ZWIDGETPATH}) +fpath+=(${ZFUNCTIONPATH}) + +for dir in $ZWIDGETPATH; do + if [ -d $dir ]; then + for i in $dir/*~*.zwc(N:t); do + autoload -Uz $i + zle -N $i + done + fi +done + +for dir in $ZFUNCTIONPATH; do + if [ -d $dir ]; then + for i in $dir/*~*.zwc(N:t); do + autoload -Uz $i && $i + done + fi +done diff --git a/modules/loader.zsh b/modules/loader.zsh index ad52207..dae6834 100644 --- a/modules/loader.zsh +++ b/modules/loader.zsh @@ -21,8 +21,23 @@ ################################# # Path to module directory -ZMODPATH=${ZMODPATH:-"${ZDOTDIR:-/etc/zsh}/modules"} -. $ZMODPATH/helpers.zsh +if [[ -z $ZMODPATH ]]; then + ZMODPATH=( ${ZDOTDIR:+$ZDOTDIR/modules} ) + [[ -d /etc/zsh/modules ]] && ZMODPATH+=/etc/zsh/modules +fi + +local mod_path() { + for dir in $ZMODPATH; do + if [[ -e $dir/$1 ]]; then + echo "$dir/$1" + return 0 + fi + done + return 1 +} + + +. $(mod_path helpers.zsh) errdetails="" modqueue=( ) @@ -41,7 +56,7 @@ modqueue=( ) # mod_queue() { local module="$1" - local modsource="$ZMODPATH/$module" + local modsource=$(mod_path $module) if ! mod_exists "$module" ; then if [[ "$2" == "is_dep" ]]; then @@ -65,7 +80,7 @@ mod_queue() { } mod_exists() { - [ -e "$ZMODPATH/$module" ] + mod_path "$1" &> /dev/null } # Checks for module dependencies @@ -132,8 +147,8 @@ mod_check_dep() { mod_load() { local MPATH for module in "${(@)modqueue}"; do - MPATH="$ZMODPATH/$module" - [ -e "$ZMODPATH/$module/init" ] && . "$ZMODPATH/$module/init" + MPATH=$(mod_path $module) + [ -e "$(mod_path $module)/init" ] && . "$(mod_path $module)/init" done } @@ -146,14 +161,16 @@ mod_init() { if [ -n "$ZMODLOAD_ONLY" ]; then for module in "${(@)ZMODLOAD_ONLY}"; do - [ -d "$ZMODPATH/$module" ] && mod_queue "$module" + [ -d "$(mod_path $module)" ] && mod_queue "$module" done else - for module in $ZMODPATH/*(/N); do - if [ -z "$ZMODLOAD_BLACKLIST" ] \ - || ! in_array ${module:t} ${(@)ZMODLOAD_BLACKLIST}; then - mod_queue "${module:t}" - fi + for moddir in $ZMODPATH; do + for module in $moddir/*(/N); do + if [ -z "$ZMODLOAD_BLACKLIST" ] \ + || ! in_array ${module:t} ${(@)ZMODLOAD_BLACKLIST}; then + mod_queue "${module:t}" + fi + done done fi diff --git a/zshrc b/zshrc index 838719e..ed5f421 100644 --- a/zshrc +++ b/zshrc @@ -13,8 +13,22 @@ setopt short_loops setopt cdable_vars # autoload completions -fpath+=( "${ZDOTDIR:-/etc/zsh}/compdef" ) -autoload -U ${ZDOTDIR:-/etc/zsh}/compdef/*(:t) +fpath+=( "${ZDOTDIR:+ZDOTDIR}/compdef" ) +fpath+=( "/etc/zsh/compdef" ) +autoload -U /etc/zsh/compdef/*(:t) + +if [[ -d $ZDOTDIR/compdef ]]; then + autoload -U /etc/zsh/compdef/*(N:t) +fi + +# get a file from ZDOTDIR, return file in /etc/zsh if it does not exist +zdotfile() { + if [[ -e $ZDOTDIR/$1 ]]; then + echo $ZDOTDIR/$1 + else + echo /etc/zsh/$1 + fi +} bindkey -v @@ -29,9 +43,9 @@ function exists { command -v "$@" >/dev/null } stty -ixon -. ${ZDOTDIR:-/etc/zsh}/modules/loader.zsh && mod_init +. $(zdotfile modules/loader.zsh) && mod_init -for i in ${ZDOTDIR:-/etc/zsh}/aliases/*~${ZDOTDIR:-/etc/zsh}/aliases/*.zwc; do +for i in ${ZDOTDIR:+ZDOTDIR/aliases/*~*.zwc(N)} /etc/zsh/aliases/*~*.zwc(N); do . $i done