Bugs item #312030, was changed at 2009-10-26 15:00 by Freddy Vulto
You can respond by visiting: 
https://alioth.debian.org/tracker/?func=detail&atid=413095&aid=312030&group_id=100114

>Status: Closed
Priority: 3
Submitted By: Sung Pae (guns-guest)
Assigned to: Nobody (None)
Summary: [PATCH] Implementation of _get_pword() as a replacement for 
${COMP_WORDS[COMP_CWORD-1]} 
Distribution: None
Originally reported in: None
Milestone: None
Status: None
Original bug number: 


Initial Comment:
A workaround for surprising behaviour in bash4's handling of COMP_WORDS,
wrt getting the previous word in a completion.

So far it works as a drop in replacement for ${COMP_WORDS[COMP_CWORD-1]}.

>From the patch header:

Since the behaviour of COMP_WORDS has changed from bash3 to bash4
(COMP_WORDS is now split along COMP_WORDBREAKS, instead of along `shell
metacharacters'), there are times when the following common convention
produces an overlapping set:

        local cur=`_get_cword`
        local prev="${COMP_WORDS[COMP_CWORD-1]}"

This is especially true when _get_cword() is called with an optional
argument of exceptions to COMP_WORDBREAKS. For example:

        $ command subcommand --user=root:toor:<TAB>

        # in the completion function
        local cur=`_get_cword =:`
        local prev="${COMP_WORDS[COMP_CWORD-1]}"

bash3 returns:

        cur:    --user=root:toor:
        prev:   subcommand

while bash4 returns a less useful:

        cur:    --user=root:toor:
        prev:   toor


This patch extends both __get_cword3() and __get_cword4() to accept an
additional integer argument that specifies how many places previous to
the current word the desired word resides. It also respects any user
exceptions to COMP_WORDBREAKS.

_get_pword() is then implemented as a wrapper: `_get_cword "${@:-}" 1'

So:

        local cur=`_get_cword =:`               => --user=root:toor:
        local prev=`_get_pword =:`              => subcommand

produces the same results in bash3 and bash4.


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

>Comment By: Freddy Vulto (fvu-guest)
Date: 2009-10-31 10:01

Message:
Hi,

Thanks for the patch.  This is just what we needed for bash-4.  I changed the 
last `echo' in __get_cword4 to a `printf' to make completing `-n' or '-e' come 
through.  Declaring a function local (break_index & word_start) also doesn't 
work on my machine: the functions become visible global.  I considered doing an 
unset -f at the end of __get_cword4, but I made them global (__break_index & 
__word_start) because they're used so often.

See git commit: 884c3f5

Freddy Vulto
http://fvue.nl

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

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

_______________________________________________
Bash-completion-devel mailing list
[email protected]
http://lists.alioth.debian.org/mailman/listinfo/bash-completion-devel

Reply via email to