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

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

Re: replace-regexp


From: Tassilo Horn
Subject: Re: replace-regexp
Date: Fri, 07 May 2021 08:54:53 +0200
User-agent: mu4e 1.5.13; emacs 28.0.50

Emanuel Berg via Users list for the GNU Emacs text editor 
<help-gnu-emacs@gnu.org> writes:

> so I wrote this and it does the job but reports the error
> "while: Invalid search bound (wrong side of point)"
>
> (defun md-latex (beg end)
>   (interactive "r")
>   (save-excursion
>     (goto-char beg)
>     (while (re-search-forward "_\\(.*\\)_" end t)
>       (replace-match  "\\\\textit{\\1}") )))

I think you didn't call your command interactively but through code like
(md-latex 170 82) where the BEG was larger than END.  When being called
interactively the "r" spec makes sure that BEG is always the smaller
one, no matter if point is before mark or the other way round.

> Also this "interactive use only", in general, what's the deal with
> that?
>
> If it is just a matter of re-writing it the way I did (only without
> the error) I don't see why that cannot be dealt with so the user can
> just call the/a function with a simple interface, be it interactively
> or in Lisp...

I guess it's because `replace-regexp' does much more than just
`re-search-forward' and `replace-match' in order to provide a convenient
interface for the user.  Also it supports stuff like \? as replacement
which will query the user.  You'd normally wouldn't want/need that.  So
if all you want is replace some regexp, `re-search-forward' with
`replace-match' is the non-overhead way to go.

Bye,
Tassilo



reply via email to

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