[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
- replace-regexp, Emanuel Berg, 2021/05/06
- Re: replace-regexp, Tassilo Horn, 2021/05/07
- Re: replace-regexp, Emanuel Berg, 2021/05/07
- Re: replace-regexp, Emanuel Berg, 2021/05/07
- Re: replace-regexp, Emanuel Berg, 2021/05/07
- Re: replace-regexp, Yuri Khan, 2021/05/08
- Re: replace-regexp, Stefan Monnier, 2021/05/08
- Re: replace-regexp,
Emanuel Berg <=
- Re: replace-regexp, Stefan Monnier, 2021/05/08
- Re: replace-regexp, Emanuel Berg, 2021/05/08
- Re: replace-regexp, Tassilo Horn, 2021/05/08
- Re: replace-regexp, Emanuel Berg, 2021/05/08
- RE: [External] : Re: replace-regexp, Drew Adams, 2021/05/08
- Re: replace-regexp, Emanuel Berg, 2021/05/08
- Re: replace-regexp, Emanuel Berg, 2021/05/08
- Re: replace-regexp, Stefan Monnier, 2021/05/08
- Re: replace-regexp, Emanuel Berg, 2021/05/08
- Re: replace-regexp, Stefan Monnier, 2021/05/08