Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
bash compgen -W compatibility patch
- X-seq: zsh-workers 29135
- From: Rocky Bernstein <rocky@xxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: bash compgen -W compatibility patch
- Date: Tue, 3 May 2011 08:25:50 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:date:x-google-sender-auth :message-id:subject:from:to:content-type; bh=wK4BIxf5z/B6lfZskGAs2uHUhrGPSgZGPw9JirGb+TM=; b=ah+yRI1M+dJZaXYSqUAiuCR0CWnw9xwwqzb167Lz/ZurcjAaJFe1FkIJLk4CeWdWxF K2oExd7kbOeDmZS4jpJ5hTtEmuByg6WCY4+cowkqy+hcFyI1wPqAsE9zdwMKA7q19GQz /UufF9TRUoxtpuTDuSMT8JRmb1mpvB+udpkkQ=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:date:x-google-sender-auth:message-id:subject :from:to:content-type; b=ZCnjSivEfIecp64C6gfwCxAbt77djFlTgERu0cHg030FrUboBE8Yy0TXroffPnwZhW i+WWrG3FONsH7/lNJrKaZdjAshGloBkE40sVKMeIvKDZr4pO9XnE86J2WmiksI/bA70C Rj42qH9uBEgZNJaGnrgnLqKJifjVQnCwf5+gc=
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- Sender: rocky.bernstein@xxxxxxxxx
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