unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Peter Dyballa <Peter_Dyballa@Web.DE>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Different behaviour between M-x replace-regexp in function and in echo window
Date: Thu, 21 Apr 2005 12:29:28 +0200	[thread overview]
Message-ID: <9d37412d62fe6cd13a19d448dfdf9051@Web.DE> (raw)
In-Reply-To: <OFA0442A2F.07E11F1C-ONC1256FEA.0033CC79@SeleniaComms.com>


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)

  reply	other threads:[~2005-04-21 10:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-21  9:35 Different behaviour between M-x replace-regexp in function and in echo window luca.spinacci
2005-04-21 10:29 ` Peter Dyballa [this message]
     [not found] <mailman.2365.1114076857.2895.help-gnu-emacs@gnu.org>
2005-04-21  9:54 ` David Kastrup

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9d37412d62fe6cd13a19d448dfdf9051@Web.DE \
    --to=peter_dyballa@web.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).