From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: harven Newsgroups: gmane.emacs.help Subject: Re: Advanced query-replace-regexp in code Date: Thu, 28 May 2009 21:50:41 +0200 Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1243543254 6422 80.91.229.12 (28 May 2009 20:40:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 28 May 2009 20:40:54 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu May 28 22:40:52 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1M9mP2-0007io-8M for geh-help-gnu-emacs@m.gmane.org; Thu, 28 May 2009 22:40:40 +0200 Original-Received: from localhost ([127.0.0.1]:42568 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1M9mP1-0003ij-Gh for geh-help-gnu-emacs@m.gmane.org; Thu, 28 May 2009 16:40:39 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!news.glorb.com!news2.glorb.com!de-l.enfer-du-nord.net!usenet-fr.net!nerim.net!news.cs.univ-paris8.fr!proxad.net!feeder1-2.proxad.net!cleanfeed2-b.proxad.net!nnrp5-2.free.fr!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin) Cancel-Lock: sha1:kz1TxSiInstZLiWfA2nWUxqEaT0= Original-Lines: 30 Original-NNTP-Posting-Date: 28 May 2009 21:50:41 MEST Original-NNTP-Posting-Host: 82.240.200.149 Original-X-Trace: 1243540241 news-2.free.fr 10793 82.240.200.149:49530 Original-X-Complaints-To: abuse@proxad.net Original-Xref: news.stanford.edu gnu.emacs.help:169569 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:64802 Archived-At: Nordlöw writes: > I can't get the following example to work programmatically. > > M-x replace-regexp > Replace regexp: \(\w+\) > Replace regexp with: \,(capitalize \1) > > Is this only possible in interactive query-replace? > If so should I use a combination of while(), looking-at(), re-search- > forward(), replace-match(), match-string() etc. > > Thanks in advance, > Per Nordlöw (defun my-cap () (interactive) (while (re-search-forward "\\w+" nil t) (replace-match (capitalize (match-string 0)) nil t))) >From the doc string of replace-regexp: [replace-regexp] is usually the wrong thing to use in a Lisp program. What you probably want is a loop like this: (while (re-search-forward REGEXP nil t) (replace-match TO-STRING nil nil)) which will run faster and will not set the mark or print anything. hope that helps