Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
PATCH: Add g:: parameter expansion flag
- X-seq: zsh-workers 29242
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: PATCH: Add g:: parameter expansion flag
- Date: Thu, 12 May 2011 17:49:06 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:subject:date:message-id:x-mailer :in-reply-to:references; bh=CT+SLNaGIt6u57vDwbcxiu5v7kK1VXsQ09lL3Ng1kFo=; b=o5UT2tQNKNUUzsaB+V93S+75GOlgPfPr/vBEB/5QQA+bAcl92IwBVkTVeQouzNBLzW t7uhnEO8bzDyWNkAfW+KNhM/ML2OCuU+Hiz7bdTBzyfypOTx7kcu+n9jNkdXEqJ3YvXB W6sSIfh2OD8x3ZmHt7HZgL9OlsVbc4kA9GhSs=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:subject:date:message-id:x-mailer:in-reply-to:references; b=Emy1QyC1J5fkZQxTuPwBf6l1Qs4l02BVSMWQYpt2VSkF4uUHAdYZYVjcCiB5mZPTbT icloZgTOV83RycIxho0MQFzIPFI4/jaFjc4KyjXfQoEqs8mFQyr1fXgoZxP4ay5y1TsH gvslmgj23kYfdfsa7UmfNaO0qSnnsFD/MqsXU=
- In-reply-to: <110512070408.ZM29757@torch.brasslantern.com>
- 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
- References: <110512070408.ZM29757@torch.brasslantern.com>
Okay, how's this? I swapped the order so g happens first, and changed
the docs as discussed.
---
Doc/Zsh/expn.yo | 17 ++++++++++++--
Src/subst.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++
Test/D04parameter.ztst | 17 ++++++++++++++
3 files changed, 86 insertions(+), 3 deletions(-)
diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 1cad3d2..d3a08c5 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -863,6 +863,14 @@ item(tt(F))(
Join the words of arrays together using newline as a separator.
This is a shorthand for `tt(pj:\n:)'.
)
+item(tt(g:opts:))(
+Process escape sequences like the echo builtin when no options are given
+(tt(g::)). With the tt(o) option, octal escapes don't take a leading
+zero. With the tt(c) option, sequences like `tt(^X)' are also processed.
+With the tt(e) option, processes `tt(\M-t)' and similar sequences like the
+print builtin. With both of the tt(o) and tt(e) options, behaves like the
+print builtin except that in none of these modes is `tt(\c)' interpreted.
+)
item(tt(i))(
Sort case-insensitively. May be combined with `tt(n)' or `tt(O)'.
)
@@ -1108,7 +1116,9 @@ Force field splitting at the
separator var(string). Note that a var(string) of two or more
characters means that all of them must match in sequence; this differs from
the treatment of two or more characters in the tt(IFS) parameter.
-See also the tt(=) flag and the tt(SH_WORD_SPLIT) option.
+See also the tt(=) flag and the tt(SH_WORD_SPLIT) option. An empty
+string may also be given in which case every character will be a separate
+element.
For historical reasons, the usual behaviour that empty array elements
are retained inside double quotes is disabled for arrays generated
@@ -1292,8 +1302,9 @@ item(tt(11.) em(Case modification))(
Any case modification from one of the flags tt((L)), tt((U)) or tt((C))
is applied.
)
-item(tt(12.) em(Prompt evaluation))(
-Any prompt-style formatting from the tt((%)) family of flags is applied.
+item(tt(12.) em(Escape sequence replacement))(
+First any replacements from the tt((g)) flag are performed, then any
+prompt-style formatting from the tt((%)) family of flags is applied.
)
item(tt(13.) em(Quote application))(
Any quoting or unquoting using tt((q)) and tt((Q)) and related flags
diff --git a/Src/subst.c b/Src/subst.c
index 5d7a8b4..7aa07b5 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1607,6 +1607,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
*/
int presc = 0;
/*
+ * The (g) flag. Process escape sequences with various GETKEY_ flags.
+ */
+ int getkeys = -1;
+ /*
* The (@) flag; interacts obscurely with qt and isarr.
* This is one of the things that decides whether multsub
* will produce an array, but in an extremely indirect fashion.
@@ -1932,6 +1936,36 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
presc++;
break;
+ case 'g':
+ t = get_strarg(++s, &arglen);
+ if (getkeys < 0)
+ getkeys = 0;
+ if (*t) {
+ sav = *t;
+ *t = 0;
+ while (*++s) {
+ switch (*s) {
+ case 'e':
+ getkeys |= GETKEY_EMACS;
+ break;
+ case 'o':
+ getkeys |= GETKEY_OCTAL_ESC;
+ break;
+ case 'c':
+ getkeys |= GETKEY_CTRL;
+ break;
+
+ default:
+ *t = sav;
+ goto flagerr;
+ }
+ }
+ *t = sav;
+ s = t + arglen - 1;
+ } else
+ goto flagerr;
+ break;
+
case 'z':
shsplit = LEXFLAGS_ACTIVE;
break;
@@ -3100,6 +3134,27 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub)
}
}
/*
+ * Process echo- and print-style escape sequences.
+ */
+ if (getkeys >= 0) {
+ if (isarr) {
+ char **ap;
+
+ if (!copied)
+ aval = arrdup(aval), copied = 1;
+ ap = aval;
+ for (; *ap; ap++) {
+ int len;
+ *ap = getkeystring(*ap, &len, getkeys, NULL);
+ }
+ } else {
+ int len;
+ if (!copied)
+ val = dupstring(val), copied = 1;
+ val = getkeystring(val, &len, getkeys, NULL);
+ }
+ }
+ /*
* Perform prompt-style modifications.
*/
if (presc) {
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index b91caaa..7322872 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -298,6 +298,23 @@
>Howzat
>usay
+ foo='\u65\123'
+ print -r ${(g:o:)foo}
+ foo='\u65\0123'
+ print -r ${(g::)foo}
+ foo='\u65^X'
+ print -r ${(V)${(g:c:)foo}}
+ foo='\u65\C-x\M-a'
+ print -r ${(V)${(g:e:)foo}}
+ foo='\u65\123\C-x'
+ print -r ${(V)${(g:eo:)foo}}
+0:${(g)...}
+>eS
+>eS
+>e^X
+>e^X\M-a
+>eS^X
+
foo='I'\''m nearly out of my mind with tedium'
bar=foo
print ${(P)bar}
--
1.7.4-rc1
Messages sorted by:
Reverse Date,
Date,
Thread,
Author