[Bash-completion-devel] [SCM] bash-completion branch, master, updated. 1.90-50-g834379e

Igor Murzov e-mail at date.by
Thu Nov 24 01:57:46 UTC 2011


On Sun, 20 Nov 2011 12:05:23 +0200
Ville Skyttä <ville.skytta at iki.fi> wrote:

> >>    # Note: intentionally using COMP_WORDS and COMP_CWORD instead of
> >>    # words and cword here due to splitting on = causing index differences
> > 
> > But using COMP_WORDS directly will brake for any argument, that contains
> > character from COMP_WORDBREAKS, for example for --log-socket=ipaddr:port.
> 
> I don't see a sane and maintainable way to make this work with using
> words instead of COMP_WORDS.

I wrote the code for that purpose. It finds an offset for the $words array at first and then translates found value to an offset for the $COMP_WORDS. Here it is:

--------------------------------------
_valgrind()
{
    local cur prev words cword
    _init_completion -n :=\'\" || return

    local offset i
    offset=0
    for (( i=1; i <= cword; i++ )); do
        if [[ ${words[i]} != --* ]]; then
            offset=$i
            break
        fi
    done

    if [[ $offset -gt 0 && $offset -lt $cword ]]; then
        local j
        local cowords=( "${COMP_WORDS[@]}" )
        i=0; j=0
        while [[ $i -lt $offset ]]; do
            if [[ ${#words[i]} -lt ${#cowords[j]} ]]; then
                cowords[j]=${cowords[j]#"${words[i]}"}
                words[i]=
                ((i++))
                [[ ${cowords[j]} ]] || ((j++))
            else
                words[i]=${words[i]#"${cowords[j]}"}
                cowords[j]=
                ((j++))
                [[ ${words[i]} ]] || ((i++))
            fi
        done
        _command_offset $j
        return
    fi
--------------------------------------

Probably the better way to go is to make _command_offset() work with $words rather then with $COMP_WORDS directly.


-- Igor



More information about the Bash-completion-devel mailing list