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

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).