[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 Nov 3 20:38:18 UTC 2009


Bugs item #311628, was changed 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?

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

Comment By: Martin von Gagern (gagern-guest)
Date: 03.11.2009 20:38

Message:
OK, the fact that case constructs require the extglob shopt both at parse time and at run time is going to stay, according to the mentioned mailing thread. So there is still need for a way to set the shopt at runtime unless you accept that some completions break if extglob isn't activated.

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

Comment By: Martin von Gagern (gagern-guest)
Date: 01.10.2009 14:23

Message:
OK, it seems that extglob constructs are in general a thing dealt with at parse-time, i.e. at the time a function is defined. Only case constructs violate this, which might well be a bug in bash. Reported this aspect at the bug-bash mailing list:
http://thread.gmane.org/gmane.comp.shells.bash.bugs/13518
Depending on how that issue is dealt with, my approach to set the option at runtime might become unneccessary.

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

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