unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Different behaviour between M-x replace-regexp in function and in echo window
@ 2005-04-21  9:35 luca.spinacci
  2005-04-21 10:29 ` Peter Dyballa
  0 siblings, 1 reply; 3+ messages in thread
From: luca.spinacci @ 2005-04-21  9:35 UTC (permalink / raw)



If I use M-x replace-regexp followed by
"\(//\)[[:space:]] *" <RET> "\1 " <RET>
the behaviour is correct - replacing any space
between '//' ed comment text
Ex.
//___Text... => //_Text...
where '_' is a space

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?


Thanks in advance,
Luca.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Different behaviour between M-x replace-regexp in function and in echo window
       [not found] <mailman.2365.1114076857.2895.help-gnu-emacs@gnu.org>
@ 2005-04-21  9:54 ` David Kastrup
  0 siblings, 0 replies; 3+ messages in thread
From: David Kastrup @ 2005-04-21  9:54 UTC (permalink / raw)


luca.spinacci@seleniacomms.com writes:

> If I use M-x replace-regexp followed by
> "\(//\)[[:space:]] *" <RET> "\1 " <RET>
> the behaviour is correct - replacing any space
> between '//' ed comment text
> Ex.
> //___Text... => //_Text...
> where '_' is a space
>
> 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?

It hasn't.  Just use
C-x ESC ESC
after the manual replacement to get a peek at what this did.  You'll
notice that the backslashes in string syntax have to be doubled.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Different behaviour between M-x replace-regexp in function and in echo window
  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
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Dyballa @ 2005-04-21 10:29 UTC (permalink / raw)
  Cc: help-gnu-emacs


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)

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-04-21 10:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
     [not found] <mailman.2365.1114076857.2895.help-gnu-emacs@gnu.org>
2005-04-21  9:54 ` David Kastrup

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).