[Bash-completion-devel] Get stuck

Igor Murzov e-mail at date.by
Wed May 25 08:52:58 UTC 2011


В сообщении от 24 мая 2011 00:39:45 автор Ville Skyttä написал:
> On 05/23/2011 02:45 AM, Igor Murzov wrote:
> > Hey! I get stuck trying to implement completion script for strings
> > containing two parts for completion delimited by '%' sign.  For example,
> > `cmd opt%arg`. The problem is that, if i hit tab key, when cursor is
> > behind delimeter, bash eats first part of string leaving `cmd arg`.
> 
> Not sure if I understand correctly, but does replacing the above with
> this work the way you'd like?
> 
>     if [[ "$cur" == ?*%* ]]; then
>         COMPREPLY=( $( compgen -P "${cur%%%*}%" \
>             -W 'abc bbb cde' -- "${cur#*%}" ) )
>         return 0
>     fi

There is upgradepkg tool, that upgrades slackware packages. It can be used 
like this:
  upgradepkg old_package_name%new_package_name
I want to handle this notation in completion. I need to complete filenames and 
directories after '%'. I wrote two variants of completion function, but none 
of them works as expected. Actually, prefix not disappears anymore, but now I 
get extra space after directory names.
# 1:
--------------------------------------------
_upgradepkg()
{
    local cur prev words cword
    _init_completion -n % || return
    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--dry-run --install-new --reinstall \
            --verbose' -- "$cur") )
        return 0
    fi

    if [[ "$cur" == ?*%* ]]; then
        prev="${cur%%?(\\)%*}"
        cur="${cur#*%}"
        _filedir "t[bglx]z"
        COMPREPLY=( ${COMPREPLY[@]/#/$prev%} )
        return 0
    fi

    _filedir "t[bglx]z"
} && complete -F _upgradepkg upgradepkg
--------------------------------------------
# 2:
--------------------------------------------
_upgradepkg()
{
    local cur prev words cword
    _init_completion -n % || return
    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--dry-run --install-new --reinstall \
            --verbose' -- "$cur") )
        return 0
    fi

    if [[ "$cur" == ?*%* ]]; then
        prev="${cur%%?(\\)%*}"
        cur="${cur#*%}"
        local IFS=$'\n'
        compopt -o filenames
        COMPREPLY=( $( compgen -P "$prev%" -f -X "!*.@(t[bgxl]z)" -- "$cur" )\
                              $( compgen -P "$prev%" -d -- "$cur" ) )
        return 0
    fi

    _filedir "t[bglx]z"
} && complete -F _upgradepkg upgradepkg
--------------------------------------------

-- Igor



More information about the Bash-completion-devel mailing list