[Bash-completion-devel] _get_comp_words_by_ref

Freddy Vulto fvulto at gmail.com
Wed Feb 10 20:48:22 UTC 2010


On 100209 13:14, Crestez Dan Leonard wrote:
> I'm not sure about the arguments to the _get_comp_words_by_ref function.
> Passing multiple [cur] [prev] [prev2].. arguments doesn't seem all that
> useful. More than cur and prev would very rarely be useful; and in such
> a case it might be better to just fetch the array and current index.

You've got a point there.  Returning `words' and `cword' would be useful
as well.

> So what about turning this into --cur --prev with default arguments?

I'd like to use builtin `getopts' to parse arguments but `getopts'
doesn't support long options, so that would turn into something like
this:

   Usage: _get_comp_words_by_ref OPTIONS
   Available OPTIONS:
   -c [VARNAME]   Return cur within specified varname (defalt "cur")
   -p [VARNAME]   Return prev within specified varname (default "prev")
   -w [VARNAME]   Return words within specified varname (default "words")
   -i [VARNAME]   Return words within specified varname (default "cword")

This has the drawback that from a first glance, variables cur/prev/words/cword
'magically' get filled.  For example, after calling:

    _get_comp_words_by_ref -cpwi

suddenly variables: cur, prev, words and cword contain values.  I don't think
we should do that.

Maybe we can make the varnames non-optional, and also provide shortcuts by
using the arguments, indicating which fixed varnames to return:

    Usage: _get_comp_words_by_ref [OPTIONS] [VARNAMES]
    Available OPTIONS:
        -c VARNAME  Return cur within specified varname (default "cur")
        -p VARNAME  Return prev within specified varname (default "prev")
        -w VARNAME  Return words within specified varname (default "words")
        -i VARNAME  Return words within specified varname (default "cword")
    Available VARNAMES:
        cur  Return cur within varname "cur"
        prev  Return prev within varname "prev"
        words  Return words within varname "words"
        cword  Return cword within varname "cword"

This way:
- because VARNAME's to OPTIONS aren't optional, within a completion it's always
  obvious where the variables are being set
- we make it advantageous (because of a simpler function call) for people to
  use the preferred variables "cur", "prev", "cword" and "words".
- the preferred variable names can be overridden by using the options syntax

Example calls would then be:

    # Fill local $cur
    _get_comp_words_by_ref cur
    # Or by using longer syntax
    _get_comp_words_by_ref -c cur
    # Using another variable name for retrieving cur
    _get_comp_words_by_ref -c current_word_to_complete
    # Using unknown varname would give error
    _get_comp_words_by_ref current  # Error: unknown varname "current"
    # Retrieving all variables
    _get_comp_words_by_ref cur prev words cword
    # Different order is ok
    _get_comp_words_by_ref words cword cur
    # Using non-default variables
    _get_comp_words_by_ref -c comp_cur -i comp_cword -w comp_words -p comp_prev

> Having a single function avoids multiple __reassemble_comp_words_by_ref
> calls further down, so I guess it's worth doing for performance. But it
> would be nicer to just reimplement _get_cword with a cache. Otherwise
> anything that uses __reassemble_comp_words_by_ref would be turn into an
> argument to _get_comp_words_by_ref (think _count_args).
> 
> Unfortunately a cache is impossible to implement without leaking
> variables to the shell environment. Maybe bash-completion-lib has a way
> to execute cleanup code after every completion?

It is possible with bash-completion-lib, but only if you'd set COMP_INSTALL=0.
This way `comp_load' would remain active for every completion and it'll call
`comp_load_deinit()' after completion has been done.  This might be an idea
indeed once we do the merge with bash-completion-lib.  I don't see how we could
do so right now.  Maybe if we'd put things inside a bash `trap' or something
but I think that's getting too complicated let alone possible.

But for now, what are the opinions about a "_get_comp_words_by_ref [OPTIONS]
[VARNAMES]" as proposed above?


Greetings,

Freddy Vulto
http://fvue.nl



More information about the Bash-completion-devel mailing list