[Bash-completion-devel] bug #311708: multiple bash_completion.d directories in $BASH_COMPLETION_DIR

gibboris at gmail.com gibboris at gmail.com
Tue Dec 29 22:14:12 UTC 2009


Oups, the same without the debug echo.

Raph
-------------- next part --------------
diff --git a/bash_completion b/bash_completion
index 248bd25..ec89e18 100644
--- a/bash_completion
+++ b/bash_completion
@@ -1768,30 +1768,41 @@ if [ ${#list[@]} -gt 0 ]; then
 fi
 unset list
 
+# arg 1 : directory to load completions from
+load_completion_dir()
+{
+    [ -z "$1" ] && return 1
+    local i
+    if [[ -d "$1" && -r "$1" && -x "$1" ]]; then
+        for i in $(LC_ALL=C command ls "$1"); do
+            i="$1/$i"
+            [[ ${i##*/} != @(*~|*.bak|*.swp|\#*\#|*.dpkg*|*.rpm@(orig|new|save)) \
+                && ( -f $i || -h $i ) && -r $i ]] && . $i
+        done
+        return 0
+    fi
+    return 1
+}
+
 # source completion directory definitions
-if [[ -d $BASH_COMPLETION_COMPAT_DIR && -r $BASH_COMPLETION_COMPAT_DIR && \
-    -x $BASH_COMPLETION_COMPAT_DIR ]]; then
-    for i in $(LC_ALL=C command ls $BASH_COMPLETION_COMPAT_DIR); do
-        i=$BASH_COMPLETION_COMPAT_DIR/$i
-        [[ ${i##*/} != @(*~|*.bak|*.swp|\#*\#|*.dpkg*|*.rpm@(orig|new|save)) \
-            && ( -f $i || -h $i ) && -r $i ]] && . $i
-    done
-fi
-if [[ $BASH_COMPLETION_DIR != $BASH_COMPLETION_COMPAT_DIR && \
-    -d $BASH_COMPLETION_DIR && -r $BASH_COMPLETION_DIR && \
-    -x $BASH_COMPLETION_DIR ]]; then
-    for i in $(LC_ALL=C command ls $BASH_COMPLETION_DIR); do
-        i=$BASH_COMPLETION_DIR/$i
-        [[ ${i##*/} != @(*~|*.bak|*.swp|\#*\#|*.dpkg*|*.rpm@(orig|new|save)) \
-            && ( -f $i || -h $i ) && -r $i ]] && . $i
-    done
+load_completion_dir "$BASH_COMPLETION_COMPAT_DIR"
+if [[ $BASH_COMPLETION_DIR != $BASH_COMPLETION_COMPAT_DIR ]]; then
+    # if we can't load $BASH_COMPLETION_DIR, then split the variable
+    if ! load_completion_dir "$BASH_COMPLETION_DIR"; then
+        # and attempt to load each part
+        # (spaces in completion directories would mess it up)
+        for each_comp_dir in ${BASH_COMPLETION_DIR[*]//:/ }; do
+            [[ $each_comp_dir != $BASH_COMPLETION_COMPAT_DIR ]] && \
+                load_completion_dir "$each_comp_dir"
+        done
+        unset each_comp_dir
+    fi
 fi
-unset i
 
 # source user completion file
 [[ $BASH_COMPLETION != ~/.bash_completion && -r ~/.bash_completion ]] \
     && . ~/.bash_completion
-unset -f have
+unset -f have load_completion_dir
 unset UNAME USERLAND have
 
 set $BASH_COMPLETION_ORIGINAL_V_VALUE


More information about the Bash-completion-devel mailing list