From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Peter Dyballa Newsgroups: gmane.emacs.help Subject: Re: Different behaviour between M-x replace-regexp in function and in echo window Date: Thu, 21 Apr 2005 12:29:28 +0200 Message-ID: <9d37412d62fe6cd13a19d448dfdf9051@Web.DE> References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1114079581 23410 80.91.229.2 (21 Apr 2005 10:33:01 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 21 Apr 2005 10:33:01 +0000 (UTC) Cc: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Apr 21 12:32:59 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1DOYyp-0007AR-Mw for geh-help-gnu-emacs@m.gmane.org; Thu, 21 Apr 2005 12:32:20 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1DOZ3V-0004XQ-SH for geh-help-gnu-emacs@m.gmane.org; Thu, 21 Apr 2005 06:37:10 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1DOZ09-0002uK-K2 for help-gnu-emacs@gnu.org; Thu, 21 Apr 2005 06:33:42 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1DOZ07-0002sr-CS for help-gnu-emacs@gnu.org; Thu, 21 Apr 2005 06:33:40 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1DOZ04-0002fy-HM for help-gnu-emacs@gnu.org; Thu, 21 Apr 2005 06:33:37 -0400 Original-Received: from [217.72.192.225] (helo=smtp07.web.de) by monty-python.gnu.org with esmtp (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA:24) (Exim 4.34) id 1DOYyX-00037w-Fr for help-gnu-emacs@gnu.org; Thu, 21 Apr 2005 06:32:01 -0400 Original-Received: from [84.245.189.164] (helo=[192.168.1.2]) by smtp07.web.de with asmtp (TLSv1:RC4-SHA:128) (WEB.DE 4.105 #275) id 1DOYw7-00054O-00; Thu, 21 Apr 2005 12:29:31 +0200 In-Reply-To: Original-To: luca.spinacci@seleniacomms.com X-Mailer: Apple Mail (2.619.2) X-Sender: Peter_Dyballa@web.de 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:25875 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:25875 Am 21.04.2005 um 11:35 schrieb luca.spinacci@seleniacomms.com: > If I use the same > replace-regexp "\(//\)[[:space:]] *" "\1 " > in a function no replacing is done - Replaced 0 occurences - > > (defun comment-formatting(start end) > (interactive "*r") > (save-excursion > (save-restriction > (narrow-to-region start end) > (goto-char start) > (replace-regexp "\(//\)[[:space:]] *" "\1 ")))) > > So M-x comment-formatting has differnt behaviour. Why? > The function's description says it's an interactive function, and ends with this recommendation: This function 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. I wonder why you make the REGEXP that complicated: "[[:space:]]*" is equivalent to "[[:space:]] *", isn't it? And you don't need to replace "\(//\)" with "\1" since "//" is *always* "//". This would be sufficient: (replace-regexp "//[[:space:]]*" "// "). Whenever there are two slashes followed by any amount of white space, replace it with two (other?) slashes and exactly one space. The \n mechanism is good when you're searching for an expression that is variable according to some law. Therefore you can't set a fixed replacement for it but have to "cite" it. I have learned basic and extended regular expressions for UNIX shell use and sed, awk ... The specialities with POSIX (as in Korn shell) or in Perl and Emacs I do not try to learn actively, they're some special cases, OK for programmers in those languages. So for me it's overkill to define special replace functions, I always find a way to do it interactively -- which helps to learn by practice. Therefore I have two keys bound to replace-string and to replace-regexp. I hit one of them, enter FROM, enter RET, enter TO, and a final RET. When I want to apply the change for the whole buffer, I go to its beginning. When I want to change the remainder of the buffer, I don't move that much. When I want to apply a change to some region, I mark it. Modern Emacsen have a history of replacements done. You can scroll in that and pick one FROM and one TO ... -- Greetings Pete Make it simple, as simple as possible but no simpler. (Albert Einstein)