zsh-workers
 help / color / mirror / code / Atom feed
* bash compgen -W compatibility patch
@ 2011-05-03 12:25 Rocky Bernstein
  2011-05-03 14:45 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Rocky Bernstein @ 2011-05-03 12:25 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1.1: Type: text/plain, Size: 935 bytes --]

Hi -

It looks like zsh's  "compgen -W" (words) does not match the same behavior
as bash's.  For example
in bash:

$ compgen -W 'abc abe ab a def' ab
compgen -W 'abc abe ab a def' ab
abc
abe
ab
$

versus zsh 4.3.10 (and git sources):

$ autoload -Uz bashcompinit
$ bashcompinit
$ zmodload -ap zsh/parameter parameters
$ compgen -W 'abc abe ab a def' ab
abc
abe
ab
a
def
$


Attached is a patch from git sources as of May 3rd, 2011 with a new test
that I believe corrects this behavior.
Since I do not consider myself a zsh language expert, the changes in
Completion/bashcompinit might be shorted or
made more zsh idomatic.

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.

[-- Attachment #1.2: Type: text/html, Size: 1387 bytes --]

[-- Attachment #2: compgen.patch --]
[-- Type: text/x-patch, Size: 1896 bytes --]

diff --git a/Completion/bashcompinit b/Completion/bashcompinit
index cba436a..400dcc6 100644
--- a/Completion/bashcompinit
+++ b/Completion/bashcompinit
@@ -41,6 +41,20 @@ _bash_complete() {
   return ret
 }
 
+_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[@]}" )
+}
 compgen() {
   local opts prefix suffix job OPTARG OPTIND ret=1 
   local -a name res results jids
@@ -128,7 +142,7 @@ compgen() {
         results+=( ${~OPTARG} )
 	unsetopt nullglob
       ;;
-      W) eval "results+=( $OPTARG )" ;;
+      W) _compgen_opt_words "$OPTARG" "${@[-1]}" ;;
       C) results+=( $(eval $OPTARG) ) ;;
       P) prefix="$OPTARG" ;;
       S) suffix="$OPTARG" ;;
diff --git a/Test/Y02compgen.ztst b/Test/Y02compgen.ztst
new file mode 100644
index 0000000..90dce61
--- /dev/null
+++ b/Test/Y02compgen.ztst
@@ -0,0 +1,21 @@
+# Tests for bash compgen compatibility.
+
+%prep
+  if ( zmodload zsh/parameter ) >/dev/null 2>&1; then
+    . $ZTST_srcdir/compgentest
+    comptestinit -z $ZTST_testdir/../Src/zsh &&
+  else
+    ZTST_unimplemented="the zsh/parameter module is not available"
+  fi
+
+%test
+
+  comptest $': \t\t\t\t\t\t\t'
+0:bash compatibility: compgen -W
+>abc
+>abe
+>ab
+
+%clean
+
+  zmodload -ui zsh/parameter
diff --git a/Test/compgentest b/Test/compgentest
new file mode 100644
index 0000000..26b202c
--- /dev/null
+++ b/Test/compgentest
@@ -0,0 +1,15 @@
+comptestinit () {
+  
+  setopt extendedglob
+  [[ -d $ZTST_testdir/Modules/zsh ]] && module_path=( $ZTST_testdir/Modules )
+
+  zmodload -i zsh/parameter || return $?
+  autoload -Uz bashcompinit || return $?
+  bashcompinit || return $?
+
+}
+
+comptest () {
+  compgen -W 'abc abe ab a def' ab
+}
+

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: bash compgen -W compatibility patch
  2011-05-03 12:25 bash compgen -W compatibility patch Rocky Bernstein
@ 2011-05-03 14:45 ` Peter Stephenson
  2011-05-03 18:27   ` Rocky Bernstein
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2011-05-03 14:45 UTC (permalink / raw)
  To: Rocky Bernstein; +Cc: zsh-workers

On Tue, 3 May 2011 08:25:50 -0400
Rocky Bernstein <rocky@gnu.org> 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@csr.com>            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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: bash compgen -W compatibility patch
  2011-05-03 14:45 ` Peter Stephenson
@ 2011-05-03 18:27   ` Rocky Bernstein
  2011-05-04  8:55     ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Rocky Bernstein @ 2011-05-03 18:27 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 2170 bytes --]

On Tue, May 3, 2011 at 10:45 AM, Peter Stephenson
<Peter.Stephenson@csr.com>wrote:

> On Tue, 3 May 2011 08:25:50 -0400
> Rocky Bernstein <rocky@gnu.org> 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]}
>

Probably simpler would be to use $2, i.e find=$2 I mistakenly had set find
twice in the patch.

 results=(${(M)words[@]:#$find*})
> }
>
> seems to work, unless I'm missing some basic ingredient.  If I'm not,
> I'll commit this.
>

I've replaced my code in zshdb with the above and the  tests run.

No doubt if there's a problem there's a test file which can be expanded.

Thanks for looking over and fixing.



>
> --
> Peter Stephenson <pws@csr.com>            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
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: bash compgen -W compatibility patch
  2011-05-03 18:27   ` Rocky Bernstein
@ 2011-05-04  8:55     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2011-05-04  8:55 UTC (permalink / raw)
  To: zsh-workers

On Tue, 3 May 2011 14:27:07 -0400
Rocky Bernstein <rocky@gnu.org> wrote:
> On Tue, May 3, 2011 at 10:45 AM, Peter Stephenson
> <Peter.Stephenson@csr.com>wrote:
> > _compgen_opt_words() {
> >  typeset -a words
> >  words=( ${~=1} )
> >  local find try
> >  find=${@[-1]}
> >
> 
> Probably simpler would be to use $2, i.e find=$2 I mistakenly had set
> find twice in the patch.

OK, I've committed this (we're in bash mode so should quote assignments
that don't want to be split)...

_compgen_opt_words() {
  typeset -a words
  words=( ${~=1} )
  local find="$2"
  results=(${(M)words[@]:#$find*})
}

-- 
Peter Stephenson <pws@csr.com>            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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-05-04  8:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-03 12:25 bash compgen -W compatibility patch Rocky Bernstein
2011-05-03 14:45 ` Peter Stephenson
2011-05-03 18:27   ` Rocky Bernstein
2011-05-04  8:55     ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).