[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: |
Sat, 02 Feb 2019 10:33:23 +0100 |
User-agent: |
mu4e 1.1.0; emacs 27.0.50 |
On 2019-02-01, at 22:20, Tassilo Horn <address@hidden> wrote:
> Hi all,
>
> on gnu-emacs-help I've asked why my wrapper command around
> `json-pretty-print' doesn't restore point although it uses
> `save-excursion'. The reason is that `json-pretty-print' replaces the
> region with copy, delete, and insert. Robert and Eli pointed me to the
> new `replace-buffer-contents' which allowed me to rewrite
> `json-pretty-print' in a way where point stays where it has been before
> pretty-printing:
>
> [...]
>
> --8<---------------cut here---------------start------------->8---
> ;; in subr-x.el (or wherever you please)
> (defun replace-region-contents (beg end extract-fn inject-fn)
> "Replace the region between BEG and END using EXTRACT-FN and INJECT-FN.
>
> The current buffer is narrowed to the region between BEG and END,
> then EXTRACT-FN is called in order to extract some value.
> Thereafter, INJECT-FN is called with that value in a temporary
> buffer which it should populate.
>
> Finally, the region in the source buffer is replaced with the
> contents of the temporary buffer prepared by INJECT-FN using
> `replace-buffer-contents'."
It looks fairly interesting, and I can see quite a few uses for
a function like this. The docstring doesn't seem to explain a lot to
me, though. In particular, what are the arguments and return values of
EXTRACT-FN and INJECT-FN?
(I don't have time now to study the code.)
Best,
--
Marcin Borkowski
http://mbork.pl