From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.emacs.help Subject: Re: multiple search strings in query-replace-regexp Date: Sat, 07 Oct 2006 10:40:39 +0200 Organization: Organization?!? Message-ID: <8564ewy0vs.fsf@lola.goethe.zz> References: <1160133442.016714.5230@m7g2000cwm.googlegroups.com> <874puhsmzj.fsf@trick.ulm.malte.spiess> <1160134896.365287.138010@e3g2000cwe.googlegroups.com> <85y7rt1x2k.fsf@lola.goethe.zz> <8764ewpme3.fsf@trick.ulm.malte.spiess> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1160214100 19746 80.91.229.2 (7 Oct 2006 09:41:40 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 7 Oct 2006 09:41:40 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Oct 07 11:41:40 2006 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1GW8gZ-00041k-MA for geh-help-gnu-emacs@m.gmane.org; Sat, 07 Oct 2006 11:41:35 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1GW8gZ-0002f0-1X for geh-help-gnu-emacs@m.gmane.org; Sat, 07 Oct 2006 05:41:35 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!news-fra1.dfn.de!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.freenet.de!news.osn.de!diablo1.news.osn.de!news2.arglkargh.de!noris.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help X-Face: 2FEFf>]>q>2iw=B6, xrUubRI>pR&Ml9=ao@P@i)L:\urd*t9M~y1^:+Y]'C0~{mAl`oQuAl \!3KEIp?*w`|bL5qr,H)LFO6Q=qx~iH4DN; i"; /yuIsqbLLCh/!U#X[S~(5eZ41to5f%E@'ELIi$t^ Vc\LWP@J5p^rst0+('>Er0=^1{]M9!p?&:\z]|;&=NP3AhB!B_bi^]Pfkw User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) Cancel-Lock: sha1:BuEe6/LEIfG343IWsLHbhxcLuRE= Original-Lines: 79 Original-NNTP-Posting-Date: 07 Oct 2006 10:41:08 CEST Original-NNTP-Posting-Host: 4018f2da.newsspool2.arcor-online.net Original-X-Trace: DXC=4ZA[DoLJ`<5nBOkdL^Lo7>A9EHlD; 3Yc24Fo<]lROoR14nDHegD_]R5HL3<; JZ0WW6CV`H8_`hhQ4^9QSCVg3dO6lR6Na Original-X-Complaints-To: usenet-abuse@arcor.de Original-Xref: shelby.stanford.edu gnu.emacs.help:142253 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:37872 Archived-At: Malte Spiess writes: > David Kastrup writes: > >> "Florian Kaufmann" writes: >> >>> that works only for multiple searches at once, but not for multiple >>> search-replace at once. >>> >>> replace all foo by bar >>> replace all rock by house >>> replace all metal by goa >>> >>> :-) and so on >> >> With Emacs 22: >> >> C-M-% \(foo\)\|\(rock\)\|metal RET >> \,(cond (\1 "bar") (\2 "house") ("goa")) RET > > Doesn't work with Emacs 21.4.1 :-(... Emacs 22 is about to enter pretest (I'd guess within a week), so there is little reason not to join the testers if one would like the features. > Looks pretty impressive though! > Does \, cause to execute lisp stuff? Yes. The doc string spells this out, but the manual is a bit more verbose: The remainder of this subsection is intended for specialized tasks and requires knowledge of Lisp. Most readers can skip it. You can use Lisp expressions to calculate parts of the replacement string. To do this, write `\,' followed by the expression in the replacement string. Each replacement calculates the value of the expression and converts it to text without quoting (if it's a string, this means using the string's contents), and uses it in the replacement string in place of the expression itself. If the expression is a symbol, one space in the replacement string after the symbol name goes with the symbol name, so the value replaces them both. Inside such an expression, you can use some special sequences. `\&' and `\N' refer here, as usual, to the entire match as a string, and to a submatch as a string. N may be multiple digits, and the value of `\N' is `nil' if subexpression N did not match. You can also use `\#&' and `\#N' to refer to those matches as numbers (this is valid when the match or submatch has the form of a numeral). `\#' here too stands for the number of already-completed replacements. Repeating our example to exchange `x' and `y', we can thus do it also this way: M-x replace-regexp \(x\)\|y \,(if \1 "y" "x") For computing replacement strings for `\,', the `format' function is often useful (*note Formatting Strings: (elisp)Formatting Strings.). For example, to add consecutively numbered strings like `ABC00042' to columns 73 to 80 (unless they are already occupied), you can use M-x replace-regexp ^.\{0,72\}$ \,(format "%-72sABC%05d" \& \#) > Btw: Shouldn't > > C-M-% \(foo\|rock\)\|metal RET > \,(cond (\1 "bar") (\1 "house") ("goa")) RET > > work, too? No. It would replace either "foo" or "rock" with "bar", and "metal" with "goa". -- David Kastrup, Kriemhildstr. 15, 44793 Bochum