diff options
Diffstat (limited to 'bash/bash_completion.d/path.bash')
-rw-r--r-- | bash/bash_completion.d/path.bash | 101 |
1 files changed, 52 insertions, 49 deletions
diff --git a/bash/bash_completion.d/path.bash b/bash/bash_completion.d/path.bash index 8db6a74a..9234f132 100644 --- a/bash/bash_completion.d/path.bash +++ b/bash/bash_completion.d/path.bash @@ -1,3 +1,8 @@ +# Load _completion_ignore_case helper function +if ! declare -F _completion_ignore_case >/dev/null ; then + source "$HOME"/.bash_completion.d/_completion_ignore_case.bash +fi + # Completion for path _path() { @@ -5,9 +10,9 @@ _path() { if ((COMP_CWORD == 1)) ; then # Complete operation as first word - local cmd - while read -r cmd ; do - COMPREPLY[${#COMPREPLY[@]}]=$cmd + local ci comp + while read -r comp ; do + COMPREPLY[ci++]=$comp done < <(compgen -W ' append check @@ -18,62 +23,60 @@ _path() { remove shift ' -- "$2") + return + fi # Complete with either directories or $PATH entries as all other words - else - case ${COMP_WORDS[1]} in + case ${COMP_WORDS[1]} in - # Complete with a directory - insert|append|check) - local dirname - while IFS= read -rd '' dirname ; do - [[ -n $dirname ]] || continue - COMPREPLY[${#COMPREPLY[@]}]=$dirname - done < <( + # Complete with a directory + insert|append|check) + local ci comp + while IFS= read -d '' -r comp ; do + COMPREPLY[ci++]=$comp + done < <( - # Set options to glob correctly - shopt -s dotglob nullglob + # Make globs expand appropriately + shopt -s dotglob nullglob + if _completion_ignore_case ; then + shopt -s nocaseglob + fi - # Make globbing case-insensitive if appropriate - while read -r _ setting ; do - case $setting in - ('completion-ignore-case on') - shopt -s nocaseglob - break - ;; - esac - done < <(bind -v) + # Print shell-quoted matching directories, null-terminated + for dir in "$2"*/ ; do + printf '%q\0' "${dir%/}" + done + ) + ;; - # Collect directory names, strip trailing slash - local -a dirnames - dirnames=("$2"*/) - dirnames=("${dirnames[@]%/}") + # Complete with directories from PATH + remove) + local ci comp + while IFS= read -d '' -r comp ; do + COMPREPLY[ci++]=$comp + done < <( - # Print quoted entries, null-delimited - printf '%q\0' "${dirnames[@]}" - ) - ;; + # Make matches work appropriately + if _completion_ignore_case ; then + shopt -s nocasematch 2>/dev/null + fi - # Complete with directories from PATH - remove) - local -a promptarr - IFS=: read -rd '' -a promptarr < \ - <(printf '%s\0' "$PATH") - local part - for part in "${promptarr[@]}" ; do - case $part in - "$2"*) - COMPREPLY[${#COMPREPLY[@]}]=$(printf '%q' "$part") - ;; + # Break PATH into parts + declare -a paths + IFS=: read -a paths -d '' -r \ + < <(printf '%s\0' "$PATH") + + # Print shell-quoted matching parts, null-terminated + for path in "${paths[@]}" ; do + case $path in + ("$2"*) printf '%q\0' "$path" ;; esac done - ;; + ) + ;; - # No completion - *) - return 1 - ;; - esac - fi + # No completion + *) return 1 ;; + esac } complete -F _path path |