emacs-devel
[Top][All Lists]
Advanced

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

Re: [RFC]: replace-region-contents


From: Marcin Borkowski
Subject: Re: [RFC]: replace-region-contents
Date: Wed, 06 Feb 2019 10:55:36 +0100
User-agent: mu4e 1.1.0; emacs 27.0.50

On 2019-02-06, at 09:07, Tassilo Horn <address@hidden> wrote:

> Stefan Monnier <address@hidden> writes:
>
>>> --8<---------------cut here---------------start------------->8---
>>> (defun replace-region-contents (beg end replace-fn)
>>>   (save-excursion
>>>     (save-restriction
>>>       (narrow-to-region beg end)
>>>       (goto-char (point-min))
>>>       (let ((repl (funcall replace-fn)))
>>>     (if (bufferp repl)
>>>         (replace-buffer-contents repl)
>>>       (let ((source-buffer (current-buffer)))
>>>         (with-temp-buffer
>>>           (insert repl)
>>>           (let ((tmp-buffer (current-buffer)))
>>>             (set-buffer source-buffer)
>>>             (replace-buffer-contents tmp-buffer)))))))))
>>> --8<---------------cut here---------------end--------------->8---
>>
>> LGTM
>
> How would I actually use that version with a replace-fn returning a
> buffer and not a string?  It looks to me that I need to do the whole
> ceremony of creating a temporary buffer, setting buffers, and ensuring
> that the temporary buffer is killed even in the case of an abnormal exit
> myself.  That's the hassle my original version tried to eliminate in the
> first place...

I did not follow the whole thread, but why wouldn't `with-temp-buffer'
be a suitable candidate to conduct exactly the ceremony you mentioned?

Also, at least sometimes, buffers are better than strings to perform
e.g. replacements.  (I have a blog post in the works where I actually
measure the performance of one over the other, and due to immutability
of strings there is a lot of GC overhead when you do string replacements
a lot.)

Best,

--
Marcin Borkowski
http://mbork.pl



reply via email to

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