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: Fri, 07 May 2021 20:28:28 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Tassilo Horn wrote:

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

Nope, I called it with M-x. Tried again now, still the
same error. It works but I get the error.

Maybe it has something to do with the replacement being longer
than the original string?

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

But why can't there be a function that does this so one
doesn't have to write all that Lisp for every use case?

Also, how about a general function that branches on
"interactiveness"?

It gets even more strange since the non-recommended function
works just fine...

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




reply via email to

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