[Bash-completion-devel] [bash-completion-Bugs][311628] clean handling of extglob and other shopt settings

bash-completion-bugs at alioth.debian.org bash-completion-bugs at alioth.debian.org
Tue Apr 28 17:24:40 UTC 2009


Bugs item #311628, was opened at 28.04.2009 17:24 by Martin von Gagern
You can respond by visiting: 
https://alioth.debian.org/tracker/?func=detail&atid=413095&aid=311628&group_id=100114

Status: Open
Priority: 3
Submitted By: Martin von Gagern (gagern-guest)
Assigned to: Nobody (None)
Summary: clean handling of extglob and other shopt settings 
Distribution: Gentoo
Originally reported in: Gentoo Bugzilla
Milestone: 1.0
Status: None
Original bug number: 267788


Initial Comment:
Some bash completions require extglob to be set. Some of them set it when defining their completion functions, some don't, but most seem to hope it will be set when the function gets executed. monotone tries to clean up after its work, but that attempt is broken for several reasons.

It would be great to improve the situation, i.e. use extglob in completion functions without polluting the user environment. Today I was contemplating this whole mess, spending a bit of time on freenode in #bash, and someone pointed out that a subshell would be suitable.

With that piece of information, I cooked up some neat solution. The idea is to have the following line at the beginning of every bash completion function using extglobs:

_my_completion_function() {
  _with_shopt extglob || return
  # completion implementation using extglobs
}

The magic _with_shopt function would be implemented in this way:

_with_shopt () { 
    shopt -q "$@" && return 0
    local IFS=$'\n'
    COMPREPLY=($(
        shopt -s "$@"
        ${FUNCNAME[1]}
        IFS=$'\n'
        echo "${COMPREPLY[*]}"
    ))
    return 1
}

It first checks for the requested option(s), and returns 0 if all are set, allowing the function to complete unimpeded. If at least one is missing, it will execute the calling function in a subshell environment, where the option has been enabled.

Communication between those two is via stdout of the subprocess, formatted as one array element per line. The child sends its compreply, generated by the completion function in the correct environment, to its parent, who turns it back into an array. Of course this prevents linebreaks as parts of the completion words, but I don't believe I care.

I have given this some testing, and it works well enough. Would you consider including that function with bash completion, so that both the completion functions shipped with it as well as any third party completion functions can build on this?

----------------------------------------------------------------------

You can respond by visiting: 
https://alioth.debian.org/tracker/?func=detail&atid=413095&aid=311628&group_id=100114



More information about the Bash-completion-devel mailing list