From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.devel Subject: [RFC]: replace-region-contents Date: Fri, 01 Feb 2019 22:20:37 +0100 Message-ID: <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="256027"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Feb 01 22:35:12 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 1gpgSt-0014V3-RZ for ged-emacs-devel@m.gmane.org; Fri, 01 Feb 2019 22:35:11 +0100 Original-Received: from localhost ([127.0.0.1]:33622 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpgSs-0001Jc-NI for ged-emacs-devel@m.gmane.org; Fri, 01 Feb 2019 16:35:10 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:37829) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpgRl-0000o4-Q7 for emacs-devel@gnu.org; Fri, 01 Feb 2019 16:34:04 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:50501) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpgEq-0002ZB-IJ for emacs-devel@gnu.org; Fri, 01 Feb 2019 16:20:41 -0500 Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:53377) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1gpgEq-0003J5-EP for emacs-devel@gnu.org; Fri, 01 Feb 2019 16:20:40 -0500 Original-Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id E73F021C4D for ; Fri, 1 Feb 2019 16:20:39 -0500 (EST) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Fri, 01 Feb 2019 16:20:39 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrjeekgddugeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecunecujfgurhephffvufffkfgfgggtsehttdertddtredtnecuhfhrohhmpefvrg hsshhilhhoucfjohhrnhcuoehtshguhhesghhnuhdrohhrgheqnecuffhomhgrihhnpegv lhdrhhgvrhgvnecukfhppeelfedrvdefiedruddvledrhedvnecurfgrrhgrmhepmhgrih hlfhhrohhmpehthhhorhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdek ieejfeekjeekgedqieefhedvleekqdhtshguhheppehgnhhurdhorhhgsehfrghsthhmrg hilhdrfhhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Original-Received: from thinkpad-t440p (p5dec8134.dip0.t-ipconnect.de [93.236.129.52]) by mail.messagingengine.com (Postfix) with ESMTPA id E1EE910086 for ; Fri, 1 Feb 2019 16:20:38 -0500 (EST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] 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:232892 Archived-At: 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--- (defun json-pretty-print (begin end) "Pretty-print selected region." (interactive "r") (save-excursion (save-restriction (narrow-to-region begin end) (goto-char (point-min)) (atomic-change-group (let ((json-encoding-pretty-print t) ;; Distinguish an empty objects from 'null' (json-null :json-null) ;; Ensure that ordering is maintained (json-object-type 'alist) (obj (json-read)) (orig-buffer (current-buffer))) (with-temp-buffer (insert (json-encode obj)) (let ((tmp-buffer (current-buffer))) (set-buffer orig-buffer) (replace-buffer-contents tmp-buffer)))))))) --8<---------------cut here---------------end--------------->8--- I think that replacing a region by transforming its contents in some arbitray way is some generally useful feature, so I'd propose to extract that functionality into some new function `replace-region-contents' I'd like to add to subr-x.el. Here it is and how it is applied in the json-pretty-printing scenario. --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'." (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (atomic-change-group (let ((source-buffer (current-buffer)) (extracted (funcall extract-fn))) (with-temp-buffer (funcall inject-fn extracted) (let ((tmp-buffer (current-buffer))) (set-buffer source-buffer) (replace-buffer-contents tmp-buffer)))))))) ;; in json.el (defun json-pretty-print (begin end) "Pretty-print selected region." (interactive "r") (replace-region-contents begin end (lambda () (let ((json-null :json-null) ;; Ensure that ordering is maintained (json-object-type 'alist)) (json-read))) (lambda (json-obj) (let ((json-encoding-pretty-print t) ;; Distinguish an empty objects from 'null' (json-null :json-null)) (insert (json-encode json-obj)))))) --8<---------------cut here---------------end--------------->8--- Does that seem like a good idea, is there anything to improve, or should I just fix `json-pretty-print' as in the first snippet? Bye, Tassilo