Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: bash compgen -W compatibility patch
On Tue, 3 May 2011 08:25:50 -0400
Rocky Bernstein <rocky@xxxxxxx> wrote:
> For example simplicity, to match the beginning of a word, I used
> [[ $try =~ "^$find" ]]
> matching using == and substrings might be faster, not that I think
> speed is all that important here. Ditt ofo inlining a newly added
> _compgen_opt_words function; but I think that makes things uglier and
> harder to test.
>
>...
>
> +_compgen_opt_words() {
> + typeset -a words
> + eval "words=( $1 )"
> + local find
> + find=$2
> + local try
> + find=${@[-1]}
> + for try in ${words[@]} ; do
> + if [[ $try =~ "^$find" ]] ; then
> + results+=( $try )
> + fi
> + done
> + results=( "${(k)results[@]}" )
> +}
Thanks. It's actually possible to abbreviate this quite a lot, in ways
which are used widely in the completion code. Also, the eval looks a
bit hairy because $1 can contain anything, and I think it's good enough
to ensure that $1 is split into words and any pattern characters are
active, which can be done without an eval.
_compgen_opt_words() {
typeset -a words
words=( ${~=1} )
local find try
find=${@[-1]}
results=(${(M)words[@]:#$find*})
}
seems to work, unless I'm missing some basic ingredient. If I'm not,
I'll commit this.
--
Peter Stephenson <pws@xxxxxxx> Software Engineer
Tel: +44 (0)1223 692070 Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK
Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
Messages sorted by:
Reverse Date,
Date,
Thread,
Author