aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/find.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-12-02 17:59:29 +1300
committerTom Ryder <tom@sanctum.geek.nz>2018-12-02 17:59:29 +1300
commit7d6fe8b1886f902f8ffbec2a9985fae9f91121cb (patch)
tree282fac0bb1809e726a373916c90260832ab23ced /bash/bash_completion.d/find.bash
parentReduce ud() completion to just dirnames (diff)
downloaddotfiles-7d6fe8b1886f902f8ffbec2a9985fae9f91121cb.tar.gz
dotfiles-7d6fe8b1886f902f8ffbec2a9985fae9f91121cb.zip
Overhaul Bash completion scripts
Some general changes: * Apply case sensitivity switching in more contexts, using a dynamically loaded helper function * Use array counters for appending to COMPREPLY where possible * Lots more short-circuiting to limit structural depth These changes are expansive and there will definitely be bugs.
Diffstat (limited to 'bash/bash_completion.d/find.bash')
-rw-r--r--bash/bash_completion.d/find.bash64
1 files changed, 15 insertions, 49 deletions
diff --git a/bash/bash_completion.d/find.bash b/bash/bash_completion.d/find.bash
index 7593c594..f87029e7 100644
--- a/bash/bash_completion.d/find.bash
+++ b/bash/bash_completion.d/find.bash
@@ -1,41 +1,13 @@
-# compopt requires Bash >=4.0, and I don't think it's worth making a compatible
-# version
-((BASH_VERSINFO[0] >= 4)) || return
-
# Semi-intelligent completion for find(1); nothing too crazy
_find() {
- # Backtrack through words so far; if none of them look like options, we're
- # still completing directory names
- local i
- local -i opts
- for ((i = COMP_CWORD; i >= 0; i--)) ; do
- case ${COMP_WORDS[i]} in
- -*)
- opts=1
- break
- ;;
- esac
- done
- if ! ((opts)) ; then
- compopt -o dirnames
- return
- fi
-
- # For the rest of this, if we end up with an empty COMPREPLY, we should
- # just do what Bash would normally do
- compopt -o bashdefault -o default
-
- # Iterate through whatever the subshell gives us; don't add blank items, though
- local item
- while read -r item ; do
- [[ -n $item ]] || continue
- COMPREPLY+=("$item")
+ # Iterate through completions produced by subshell
+ local ci comp
+ while IFS= read -r comp ; do
+ COMPREPLY[ci++]=$comp
done < <(
- # If the word being completed starts with a dash, just complete it as
- # an option; crude, but simple, and will be right the vast majority of
- # the time
+ # Complete POSIX-specified options
case $2 in
(-*)
compgen -W '
@@ -59,33 +31,27 @@ _find() {
-user
-xdev
' -- "$2"
+ return
;;
esac
- # Otherwise, look at the word *before* this one to figure out what to
+ # Look at the word *before* this one to figure out what to
# complete
case $3 in
# Args to -exec and -execdir should be commands
- (-exec|-execdir)
- compgen -A command -- "$2"
- ;;
-
- # Args to -group should complete group names
- (-group)
- compgen -A group -- "$2"
- ;;
+ (-exec|-execdir) compgen -A command -- "$2" ;;
# Legal POSIX flags for -type
- (-type)
- compgen -W 'b c d f l p s' -- "$2"
- ;;
+ (-type) compgen -W 'b c d f l p s' -- "$2" ;;
+
+ # Args to -group should complete group names
+ (-group) compgen -A group -- "$2" ;;
# Args to -user should complete usernames
- (-user)
- compgen -A user -- "$2"
- ;;
+ (-user) compgen -A user -- "$2" ;;
+
esac
)
}
-complete -F _find find
+complete -F _find -o bashdefault -o default find