[Bash-completion-devel] completion for ipv6calc

Freddy Vulto fvulto at gmail.com
Sun Feb 7 14:33:08 UTC 2010


On 100116 11:38, Ville Skyttä wrote:
> On Friday 15 January 2010, Freddy Vulto wrote:
> > Maybe we'd also better merge _get_cword and _get_pword together to a new:
> > 
> >    _get_cwords_by_ref 'nonwordbreakchars' cur [prev [prevprev] ...]
> > 
> > with `prev', `prevprev', etc.  being optional arguments.
> > 
> > This circumvents doing the same cwords-parsing repeatedly when calling
> > both _get_cword and _get_pword.  This also fixes situations where
> > `nonwordbreakchars' passed to _get_cword and _get_pword aren't the same,
> > which they should be.  Plus it saves us a subshell call and we have
> > consistent naming.
> 
> Sounds good to me, even better if it was necessary to specify 
> nonwordbreakchars only when one actually needs to, like
> _get_cwords_by_ref [-n 'nonwordbreakchars'] cur [prev [prevprev] ...]

Done in commit b529cee:

Added _get_comp_words_by_ref()
This solves the following problems:
- now one function call suffices instead of two (_get_cword; _get_pword) if
  subsequent words need to be retrieved.  Also more than two words can be
  retrieved at once, e.g.: _get_comp_words_by_ref cur prev prev2 prev3
  Also this prevents passing of `wordbreakchars' to differ in calls to
  `_get_cword' and `_get_pword', e.g.: _get_comp_words_by_ref -n : cur prev
- passing by reference, no subshell call necessary anymore
- _get_pword now also takes into account the cursor position

If we all agree, we can entirely replace _get_cword() with _get_comp_words_by_ref()?
I've added this as a proposal to the wiki for bash_completion-2, see:

    http://wiki.debian.org/Teams/BashCompletion/Proposals/Roadmap

Word of caution:

The passing-arguments-by-ref system in bash doesn't work if the new variable is
also declared local.  For example:

    t() {
        local a
        # ...
        eval $1=b
    }
    a=c; t a; echo $a  # Outputs "c", should be "b"
                       # Variable "a" is 'forbidden'

To make name collissions like this less likely to happen, but make the real
function still use readable variables, I've wrapped the `*_by_ref'
functions within an additional layer using variables prefixed with double
underscores (__).  For example:

    _t() {
        # Readable variables can still be used here
        local a
        # ...
        eval $1=b
    }
    t() {
        local __a
        _t __a
        eval $1=\$__a
    }
    a=c; t a; echo $a  # Outputs "b"
                       # Variable "__a" is 'forbidden'

Now only more obfuscated variables (starting with double prefix (__)) are
forbidden to use.


Regards,

Freddy Vulto
http://fvue.nl



More information about the Bash-completion-devel mailing list