From 0e4c02d9cb91bbe2ea8f9b9b6f7b529f0c33ad6f Mon Sep 17 00:00:00 2001 From: Anthony Fok Date: Sat, 7 Nov 2015 04:33:18 -0700 Subject: [PATCH] Restore bash completion behaviour for bash 4.x In Pull Request #178, the use of `builtin compopt` as a test condition is inappropriate. Use `[[ $(type -t compopt) = "builtin" ]]` instead. Also clean up formatting of the resulting bash completion script. --- bash_completions.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index 6259f8c..965bfdb 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -60,8 +60,8 @@ __handle_reply() __debug "${FUNCNAME}" case $cur in -*) - if builtin compopt > /dev/null 2>&1; then - compopt -o nospace + if [[ $(type -t compopt) = "builtin" ]]; then + compopt -o nospace fi local allflags if [ ${#must_have_one_flag[@]} -ne 0 ]; then @@ -70,8 +70,8 @@ __handle_reply() allflags=("${flags[*]} ${two_word_flags[*]}") fi COMPREPLY=( $(compgen -W "${allflags[*]}" -- "$cur") ) - if builtin compopt > /dev/null 2>&1; then - [[ $COMPREPLY == *= ]] || compopt +o nospace + if [[ $(type -t compopt) = "builtin" ]]; then + [[ $COMPREPLY == *= ]] || compopt +o nospace fi return 0; ;; @@ -179,11 +179,11 @@ __handle_word() { if [[ $c -ge $cword ]]; then __handle_reply - return + return fi __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}" if [[ "${words[c]}" == -* ]]; then - __handle_flag + __handle_flag elif __contains_word "${words[c]}" "${commands[@]}"; then __handle_command else @@ -220,13 +220,13 @@ func postscript(out *bytes.Buffer, name string) { } `, name) - fmt.Fprintf(out, ` - if builtin compopt > /dev/null 2>&1; then - complete -F __start_%s %s - else - complete -o nospace -F __start_%s %s - fi - `, name, name, name, name) + fmt.Fprintf(out, `if [[ $(type -t compopt) = "builtin" ]]; then + complete -F __start_%s %s +else + complete -o nospace -F __start_%s %s +fi + +`, name, name, name, name) fmt.Fprintf(out, "# ex: ts=4 sw=4 et filetype=sh\n") }