From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.devel Subject: Re: [RFC]: replace-region-contents Date: Sat, 02 Feb 2019 10:33:23 +0100 Message-ID: <875zu2ttzw.fsf@mbork.pl> References: <871s4rqk7u.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="93407"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: mu4e 1.1.0; emacs 27.0.50 Cc: emacs-devel@gnu.org To: Tassilo Horn Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Feb 02 10:34:24 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gprgr-000O8z-Kg for ged-emacs-devel@m.gmane.org; Sat, 02 Feb 2019 10:34:21 +0100 Original-Received: from localhost ([127.0.0.1]:39874 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gprgq-0003vN-Kj for ged-emacs-devel@m.gmane.org; Sat, 02 Feb 2019 04:34:20 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:47893) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gprgi-0003u9-6G for emacs-devel@gnu.org; Sat, 02 Feb 2019 04:34:13 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gprgg-0007JK-2R for emacs-devel@gnu.org; Sat, 02 Feb 2019 04:34:11 -0500 Original-Received: from mail.mojserwer.eu ([195.110.48.8]:44444) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gprgf-0007CW-Pr; Sat, 02 Feb 2019 04:34:09 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 2BB90E6698; Sat, 2 Feb 2019 10:34:06 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1oqaQaPDAyiW; Sat, 2 Feb 2019 10:34:03 +0100 (CET) Original-Received: from localhost (static-dwadziewiec-jedenpiec7.echostar.pl [109.232.29.157]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 097EDE6557; Sat, 2 Feb 2019 10:34:03 +0100 (CET) In-reply-to: <871s4rqk7u.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 195.110.48.8 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:232904 Archived-At: On 2019-02-01, at 22:20, Tassilo Horn 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