emacs-devel
[Top][All Lists]
Advanced

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

Re: [RFC]: replace-region-contents


From: Stefan Monnier
Subject: Re: [RFC]: replace-region-contents
Date: Tue, 05 Feb 2019 08:43:25 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

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

> I guess Eli might argue that this version encourages passing strings
> around instead of using buffers.

Passing strings around is not bad.  I think many applications where
replace-region-contents is desirable will generate strings more
efficiently than buffers rather than the other way around (after all,
if the rest can be in a buffer, there's a good chance that you can do
the modifications in-place on-the-fly rather than go through
replace-region-contents).

> I'd explain in the doc string that in the case of a string return
> value, we're going thru a temporary buffer anyway, so if your
> REPLACE-FN ends in (buffer-substring ...), you're clearly doing
> something wrong...

Sounds good.


        Stefan



reply via email to

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