Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

bash compgen -W compatibility patch



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. 
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
+}
+


Messages sorted by: Reverse Date, Date, Thread, Author