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.help Subject: Re: save-excursion doesn't restore point with json-pretty-print Date: Fri, 01 Feb 2019 21:00:04 +0100 Message-ID: <87d0obqny3.fsf@gnu.org> References: <87lg2z7t23.fsf@gnu.org> <20190201095516.GB19478@tuxteam.de> <83r2cryg1p.fsf@gnu.org> <83o97vy7rx.fsf@gnu.org> <874l9nl74g.fsf@gnu.org> <835zu3xrry.fsf@gnu.org> <87munfxqvu.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="140511"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: help-gnu-emacs@gnu.org To: Eli Zaretskii Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 01 21:09:38 2019 Return-path: Envelope-to: geh-help-gnu-emacs@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 1gpf86-000aTT-4J for geh-help-gnu-emacs@m.gmane.org; Fri, 01 Feb 2019 21:09:38 +0100 Original-Received: from localhost ([127.0.0.1]:32835 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpf85-0000wc-4D for geh-help-gnu-emacs@m.gmane.org; Fri, 01 Feb 2019 15:09:37 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:33155) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpeyt-0004FM-VC for help-gnu-emacs@gnu.org; Fri, 01 Feb 2019 15:00:08 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:49107) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpeyt-0006ue-QC for help-gnu-emacs@gnu.org; Fri, 01 Feb 2019 15:00:07 -0500 Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:60485) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1gpeyt-0008Ns-MO; Fri, 01 Feb 2019 15:00:07 -0500 Original-Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 538D4220D8; Fri, 1 Feb 2019 15:00:07 -0500 (EST) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Fri, 01 Feb 2019 15:00:07 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrjeekgddufeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvffufhffjg fkfgggtgesthdtredttdertdenucfhrhhomhepvfgrshhsihhlohcujfhorhhnuceothhs ughhsehgnhhurdhorhhgqeenucfkphepleefrddvfeeirdduvdelrdehvdenucfrrghrrg hmpehmrghilhhfrhhomhepthhhohhrnhdomhgvshhmthhprghuthhhphgvrhhsohhnrghl ihhthidqkeeijeefkeejkeegqdeifeehvdelkedqthhsughhpeepghhnuhdrohhrghesfh grshhtmhgrihhlrdhfmhenucevlhhushhtvghrufhiiigvpedt 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 3434610317; Fri, 1 Feb 2019 15:00:06 -0500 (EST) In-Reply-To: <87munfxqvu.fsf@gnu.org> (Tassilo Horn's message of "Fri, 01 Feb 2019 20:14:45 +0100") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:119193 Archived-At: Tassilo Horn writes: > Eli Zaretskii writes: > >>> > json-read-from-string uses a temporary buffer anyway, so a couple of >>> > trivial changes in json-pretty-print should do the trick. >>> >>> Is this what you have in mind? Seems to work well. >> >> Yes. > > Ok, great. Do you think it would make sense to extract the mechanics of > narrowing, extracting from the narrowed source buffer, and injecting > (transformed) into the temporary buffer with which to replace the source > region into some function > > (replace-region-contents beg end extract-fn inject-fn) > > That way, `json-pretty-print' would look like. [...] Nah, but almost. This is the actual working code which I'd happily commit if you agree: --8<---------------cut here---------------start------------->8--- ;; in subr.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--- Bye, Tassilo