diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2015-10-21 13:08:09 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2015-10-21 13:08:09 +1300 |
commit | dcfc80aaa112498498d93bae3ae0eb2088244c47 (patch) | |
tree | 889854ba1c96f1c86465da1bbd5cc87f0c8d4b9f /bash/bashrc.d/make.bash | |
parent | Handle spaces correctly in completions (diff) | |
download | dotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.tar.gz dotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.zip |
Tidy up completion considerably; no more compgen
* Remove all instances of compgen; for filename completion it's quite
broken as it relies on implicit wordsplitting in array context, and
doesn't have an option to print with a null delimiter; replaced with
manual for/while loops instead
* Add IFS= to while/read loops over filenames
* Use "dirname/s" instead of "dir/s" variables to avoid keyword
collisions and for clarity
* Remove some unnecessary variables
* Use shorter syntax for loop exit conditions
* Move completion options into functions where applicable rather than
having them on the completion definition itself
Diffstat (limited to 'bash/bashrc.d/make.bash')
-rw-r--r-- | bash/bashrc.d/make.bash | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/bash/bashrc.d/make.bash b/bash/bashrc.d/make.bash index df4c0f56..216894fd 100644 --- a/bash/bashrc.d/make.bash +++ b/bash/bashrc.d/make.bash @@ -1,17 +1,16 @@ # Completion setup for Make, completing targets _make() { - local word - word=${COMP_WORDS[COMP_CWORD]} - # Quietly bail if no legible Makefile - if [[ ! -r Makefile ]] ; then - return 1 - fi + # Do default completion if no matches + compopt -o default + + # Bail if no legible Makefile + [[ ! -r Makefile ]] || return 1 # Build a list of targets by parsing the Makefile local -a targets tokens local target line - while read -r line ; do + while read -r line ; do if [[ $line == *:* ]] ; then target=$line target=${target%%:*} @@ -25,7 +24,10 @@ _make() { done < Makefile # Complete with matching targets - COMPREPLY=( $(compgen -W "${targets[*]}" -- "$word") ) + for target in "${targets[@]}" ; do + [[ $target == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue + COMPREPLY=("${COMPREPLY[@]}" "$target") + done } -complete -F _make -o default make +complete -F _make make |