help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: replace-regexp


From: Emanuel Berg
Subject: Re: replace-regexp
Date: Sat, 08 May 2021 20:41:28 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

> There are indeed 2 "standard" solutions:
> 1- start your loop at the end, moving towards the beginning.
> 2- use a marker to keep track of the end.

That so? Here is what it says in the help:

  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.

Not so good, ey? Standard solution 2 not mentioned and, yes,
isn't it the opposite of standard solution 1?

Again, I don't think the user, not even the Lisp user, should
do this thing. I'll see if I can show you what I mean, later.
As a side note, (replace-match TO-STRING nil nil) - but aren't
nil nil the default anyway?

> You say "narrowing is easier", but using a marker is as
> simple as:
>
>     (setq end (copy-marker end t))
>
> so it's arguably simpler.

You mean like this? Yes, that works with no error.

(defun md-latex (beg end)
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (while (re-search-forward "_\\(.*\\)_" end t)
      (setq end (copy-marker end t))
      (replace-match "\\\\textit{\\1}") )))

I have used replace-match 6 times before, I wonder why I never
had a problem until now (and now the problem appeared
instantly). Examining...

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]