From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12962 invoked from network); 4 Nov 2005 11:09:54 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 4 Nov 2005 11:09:54 -0000 Received: (qmail 1405 invoked from network); 4 Nov 2005 11:09:47 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 4 Nov 2005 11:09:47 -0000 Received: (qmail 10240 invoked by alias); 4 Nov 2005 11:09:39 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 9618 Received: (qmail 10231 invoked from network); 4 Nov 2005 11:09:38 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 4 Nov 2005 11:09:38 -0000 Received: (qmail 99994 invoked from network); 4 Nov 2005 11:09:38 -0000 Received: from cluster-d.mailcontrol.com (HELO rly27d.srv.mailcontrol.com) (217.69.20.190) by a.mx.sunsite.dk with SMTP; 4 Nov 2005 11:09:35 -0000 Received: from exchange03.csr.com (mailhost1.csr.com [81.105.217.43]) by rly27d.srv.mailcontrol.com (MailControl) with ESMTP id jA4B9YQ1003051 for ; Fri, 4 Nov 2005 11:09:34 GMT Received: from news01 ([10.103.143.38]) by exchange03.csr.com with Microsoft SMTPSVC(5.0.2195.6713); Fri, 4 Nov 2005 11:12:00 +0000 Date: Fri, 4 Nov 2005 11:09:32 +0000 From: Peter Stephenson To: zsh-users@sunsite.dk Subject: Re: Global History Substitution Message-Id: <20051104110932.612805c7.pws@csr.com> In-Reply-To: <20051103190747.GA16897@namib.cs.utk.edu> References: <20051103190747.GA16897@namib.cs.utk.edu> Organization: Cambridge Silicon Radio X-Mailer: Sylpheed version 0.9.12 (GTK+ 1.2.10; i386-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 04 Nov 2005 11:12:00.0107 (UTC) FILETIME=[939BD3B0:01C5E130] X-Scanned-By: MailControl A-05-40-01 (www.mailcontrol.com) on 10.68.0.137 X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 Chris Johnson wrote: > Is there a way to do global substitution on the previous command in ^ > notation? For instance, > > $ echo a b b b > $ ^b^beta^ > > yields > > $ echo a beta b b > > Certainly I could do !!:gs/b/beta. This isn't near as simple as > something like gs/b/beta or ^b^beta^g. Anything I'm missing in the > manual? No, I think with this particular feature you're stuck. It's a historical oddity that the "g" for global doesn't work as a qualifier, so the ^ notation is restricted. It turns out that fixing the code to use :g like a qualifier isn't that difficult, however... that's ^b^beta^:g, since if the colon is missing the g is treated like ordinary text. Index: Doc/Zsh/expn.yo =================================================================== RCS file: /cvsroot/zsh/zsh/Doc/Zsh/expn.yo,v retrieving revision 1.57 diff -u -r1.57 expn.yo --- Doc/Zsh/expn.yo 1 Nov 2005 18:04:25 -0000 1.57 +++ Doc/Zsh/expn.yo 4 Nov 2005 11:05:01 -0000 @@ -117,6 +117,8 @@ More precisely, the sequence `tt(^)var(foo)tt(^)var(bar)tt(^)' is synonymous with `tt(!!:s)tt(^)var(foo)tt(^)var(bar)tt(^)', hence other modifiers (see noderef(Modifiers)) may follow the final `tt(^)'. +In particular, `tt(^)var(foo)tt(^)var(bar)tt(:g)' performs a global +substitution. If the shell encounters the character sequence `tt(!")' in the input, the history mechanism is temporarily disabled until @@ -254,10 +256,14 @@ ) item(tt(s/)var(l)tt(/)var(r)[tt(/)])( Substitute var(r) for var(l) as described below. -Unless preceded immediately by a tt(g), with no colon between, -the substitution is done only for the +The substitution is done only for the first string that matches var(l). For arrays and for filename generation, this applies to each word of the expanded text. + +The forms `tt(gs/)var(l)tt(/)var(r)' and `tt(s/)var(l)tt(/)var(r)tt(/:g)' +perform global substitution, i.e. substitute every occurrence of var(r) +for var(l). Note that the tt(g) or tt(:g) must appear in exactly the +position shown. ) item(tt(&))( Repeat the previous tt(s) substitution. Like tt(s), may be preceded Index: Src/hist.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/hist.c,v retrieving revision 1.60 diff -u -r1.60 hist.c --- Src/hist.c 10 Aug 2005 10:56:41 -0000 1.60 +++ Src/hist.c 4 Nov 2005 11:05:02 -0000 @@ -295,13 +295,27 @@ hwaddc(ingetc()); } -/* extract :s/foo/bar/ delimiters and arguments */ +/* + * Extract :s/foo/bar/ delimiters and arguments + * + * The first character expected is the first delimiter. + * The arguments are stored in the hsubl and hsubr variables. + * + * subline is the part of the command line to be matched. + * + * *gbalp is set to 1 to indicate a trailing ':g', i.e. a + * global substitution. + * + * If a ':' was found but was not followed by a 'g', + * *cflagp is set to 1 and the input is backed up to the + * character following the colon. + */ /**/ static int -getsubsargs(char *subline) +getsubsargs(char *subline, int *gbalp, int *cflagp) { - int del; + int del, follow; char *ptr1, *ptr2; del = ingetc(); @@ -315,6 +329,17 @@ } zsfree(hsubr); hsubr = ptr2; + follow = ingetc(); + if (follow == ':') { + follow = ingetc(); + if (follow == 'g') + *gbalp = 1; + else { + inungetc(follow); + *cflagp = 1; + } + } else + inungetc(follow); if (hsubl && !strstr(subline, hsubl)) { herrflush(); zerr("substitution failed", NULL, 0); @@ -348,14 +373,16 @@ /* look, no goto's */ if (isfirstch && c == hatchar) { + int gbal = 0; + /* Line begins ^foo^bar */ isfirstch = 0; inungetc(hatchar); if (!(ehist = gethist(defev)) || !(sline = getargs(ehist, 0, getargc(ehist))) - || getsubsargs(sline) || !hsubl) + || getsubsargs(sline, &gbal, &cflag) || !hsubl) return -1; - subst(&sline, hsubl, hsubr, 0); + subst(&sline, hsubl, hsubr, gbal); } else { /* Line doesn't begin ^foo^bar */ if (c != ' ') @@ -577,7 +604,7 @@ } break; case 's': - if (getsubsargs(sline)) + if (getsubsargs(sline, &gbal, &cflag)) return -1; /* fall through */ case '&': if (hsubl && hsubr) -- Peter Stephenson Software Engineer CSR PLC, Churchill House, Cambridge Business Park, Cowley Road Cambridge, CB4 0WZ, UK Tel: +44 (0)1223 692070 This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com