* PATCH: Enable extendedglob in pattern with ${(*)name/pattern/replacement}
@ 2020-09-06 3:05 Bart Schaefer
2020-09-13 21:50 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2020-09-06 3:05 UTC (permalink / raw)
To: Zsh hackers list
[-- Attachment #1: Type: text/plain, Size: 433 bytes --]
In retrospect I'm surprised this wasn't thought of before. With this
you can for example enable backreferences without having to turn on
extendedglob for the whole surrounding context.
A different character could be chosen if (*) seems unwise for some
reason. It should also be fairly straightforward to make (**) toggle
the option if there's a reason to force extendedglob off.
Patch includes (very brief) doc, and a test case.
[-- Attachment #2: splatqual.txt --]
[-- Type: text/plain, Size: 4171 bytes --]
diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index cbdae4d58..b3396721f 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -1422,7 +1422,7 @@ error, and the flag itself has no effect.
enditem()
The following flags are meaningful with the tt(${)...tt(#)...tt(}) or
-tt(${)...tt(%)...tt(}) forms. The tt(S) and tt(I) flags may also be
+tt(${)...tt(%)...tt(}) forms. The tt(S), tt(I), and tt(*) flags may also be
used with the tt(${)...tt(/)...tt(}) forms.
startitem()
@@ -1488,6 +1488,10 @@ will remove the same matches as for `tt(#)', but in reverse order, and the
form using `tt(%%)' will remove the same matches as for `tt(##)' in reverse
order.
)
+item(tt(*))(
+Enable tt(EXTENDED_GLOB) for substitution via tt(${)...tt(/)...tt(}) or
+tt(${)...tt(//)...tt(}).
+)
item(tt(B))(
Include the index of the beginning of the match in the result.
)
diff --git a/Src/subst.c b/Src/subst.c
index b98ddaf02..2af61653a 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1708,7 +1708,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
/*
* This expressive name refers to the set of flags which
* is applied to matching for #, %, / and their doubled variants:
- * (M), (R), (B), (E), (N), (S).
+ * (M), (R), (B), (E), (N), (S), (*).
*/
int flags = 0;
/* Value from (I) flag, used for ditto. */
@@ -1930,6 +1930,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
case '@':
nojoin = 2; /* nojoin = 2 means force */
break;
+ case '*':
+ case Star:
+ flags |= SUB_EGLOB;
+ break;
case 'M':
flags |= SUB_MATCH;
break;
@@ -2810,7 +2814,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
c == '#' || c == Pound ||
c == '?' || c == Quest ||
c == '/')) {
-
+ int eglob = isset(EXTENDEDGLOB);
/*
* Default index is 1 if no (I) or (I) gave zero. But
* why don't we set the default explicitly at the start
@@ -2832,9 +2836,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
char *ptr;
/*
* previous flags are irrelevant, except for (S) which
- * indicates shortest substring; else look for longest.
+ * indicates shortest substring; else look for longest,
+ # and (*) which temporarily enables extended globbing.
*/
- flags = (flags & SUB_SUBSTR) ? 0 : SUB_LONG;
+ flags = ((flags & SUB_SUBSTR) ? 0 : SUB_LONG)|(flags & SUB_EGLOB);
if ((c = *s) == '/') {
/* doubled, so replace all occurrences */
flags |= SUB_GLOBAL;
@@ -3136,7 +3141,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
for (ap = aval; *ap; ap++) {
untokenize(*ap);
}
+ if (flags & SUB_EGLOB)
+ opts[EXTENDEDGLOB] = 1;
getmatcharr(&aval, s, flags, flnum, replstr);
+ opts[EXTENDEDGLOB] = eglob;
} else {
if (vunset) {
if (vunset > 0 && unset(UNSET)) {
@@ -3151,7 +3159,10 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
copied = 1;
untokenize(val);
}
+ if (flags & SUB_EGLOB)
+ opts[EXTENDEDGLOB] = 1;
getmatch(&val, s, flags, flnum, replstr);
+ opts[EXTENDEDGLOB] = eglob;
}
break;
}
diff --git a/Src/zsh.h b/Src/zsh.h
index 94f5099c6..6cf1b4186 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -1991,6 +1991,7 @@ struct tieddata {
#define SUB_START 0x1000 /* force match at start with SUB_END
* and no SUB_SUBSTR */
#define SUB_LIST 0x2000 /* no substitution, return list of matches */
+#define SUB_EGLOB 0x4000 /* use extended globbing in patterns */
/*
* Structure recording multiple matches inside a test string.
diff --git a/Test/D02glob.ztst b/Test/D02glob.ztst
index f250244e7..176846d46 100644
--- a/Test/D02glob.ztst
+++ b/Test/D02glob.ztst
@@ -792,6 +792,11 @@
*>*/glob.tmp/(flip|flop)
*>*/glob.tmp/(flip|flop)/trailing/components
+ unsetopt extendedglob
+ print -r -- ${(*)=${(@s.+.):-A+B}/(#b)(?)/-${(L)match[1]} ${match[1]}}
+0:the '*' qualfier enables extended_glob for pattern matching
+>-a A -b B
+
%clean
# Fix unreadable-directory permissions so ztst can clean up properly
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: PATCH: Enable extendedglob in pattern with ${(*)name/pattern/replacement}
2020-09-06 3:05 PATCH: Enable extendedglob in pattern with ${(*)name/pattern/replacement} Bart Schaefer
@ 2020-09-13 21:50 ` Bart Schaefer
2020-09-13 22:47 ` PATCH: _brace_parameter: Add (*) completion Mikael Magnusson
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2020-09-13 21:50 UTC (permalink / raw)
To: Zsh hackers list
On Sat, Sep 5, 2020 at 8:05 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> In retrospect I'm surprised this wasn't thought of before. With this
> you can for example enable backreferences without having to turn on
> extendedglob for the whole surrounding context.
Ordinarily I'd expect to wait for commentary on something like this
before I push it, but apparently it's entirely non-controversial?
^ permalink raw reply [flat|nested] 4+ messages in thread
* PATCH: _brace_parameter: Add (*) completion
2020-09-13 21:50 ` Bart Schaefer
@ 2020-09-13 22:47 ` Mikael Magnusson
2020-09-13 23:25 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2020-09-13 22:47 UTC (permalink / raw)
To: zsh-workers
---
Feel free to fold this in too.
Completion/Zsh/Context/_brace_parameter | 1 +
1 file changed, 1 insertion(+)
diff --git a/Completion/Zsh/Context/_brace_parameter b/Completion/Zsh/Context/_brace_parameter
index 6960cec9b7..d4b750c3d6 100644
--- a/Completion/Zsh/Context/_brace_parameter
+++ b/Completion/Zsh/Context/_brace_parameter
@@ -142,6 +142,7 @@ if [[ $PREFIX = *'${('[^\)]# ]]; then
flags+=(
"#:evaluate as numeric expression"
"@:prevent double-quoted joining of arrays"
+ "*:enable extended globs for pattern"
"A:assign as an array parameter"
"a:sort in array index order (with O to reverse)"
"b:backslash quote pattern characters only"
--
2.15.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: PATCH: _brace_parameter: Add (*) completion
2020-09-13 22:47 ` PATCH: _brace_parameter: Add (*) completion Mikael Magnusson
@ 2020-09-13 23:25 ` Bart Schaefer
0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2020-09-13 23:25 UTC (permalink / raw)
To: Mikael Magnusson; +Cc: zsh-workers
On Sun, Sep 13, 2020 at 3:51 PM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> Feel free to fold this in too.
Thank you, I have done so.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-09-13 23:25 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-06 3:05 PATCH: Enable extendedglob in pattern with ${(*)name/pattern/replacement} Bart Schaefer
2020-09-13 21:50 ` Bart Schaefer
2020-09-13 22:47 ` PATCH: _brace_parameter: Add (*) completion Mikael Magnusson
2020-09-13 23:25 ` Bart Schaefer
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).