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 19:02:23 +0100 Message-ID: <874l9nl74g.fsf@gnu.org> References: <87lg2z7t23.fsf@gnu.org> <20190201095516.GB19478@tuxteam.de> <83r2cryg1p.fsf@gnu.org> <83o97vy7rx.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="126026"; 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 19:06:20 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 1gpdCl-000Wh6-Sr for geh-help-gnu-emacs@m.gmane.org; Fri, 01 Feb 2019 19:06:20 +0100 Original-Received: from localhost ([127.0.0.1]:59563 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpdCk-0005nZ-Sq for geh-help-gnu-emacs@m.gmane.org; Fri, 01 Feb 2019 13:06:18 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:38681) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpd99-0003mu-EZ for help-gnu-emacs@gnu.org; Fri, 01 Feb 2019 13:02:37 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:47233) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gpd96-0003NO-Tu for help-gnu-emacs@gnu.org; Fri, 01 Feb 2019 13:02:33 -0500 Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:34801) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1gpd91-0006qv-Cx; Fri, 01 Feb 2019 13:02:31 -0500 Original-Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 0765B21F17; Fri, 1 Feb 2019 13:02:26 -0500 (EST) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Fri, 01 Feb 2019 13:02:26 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrjeekgddutdelucetufdoteggodetrfdotf 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 03A34E4078; Fri, 1 Feb 2019 13:02:24 -0500 (EST) In-Reply-To: <83o97vy7rx.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 01 Feb 2019 15:09:54 +0200") 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:119190 Archived-At: Eli Zaretskii writes: >> From: Robert Pluim >> Cc: help-gnu-emacs@gnu.org >> Date: Fri, 01 Feb 2019 11:33:23 +0100 >> >> > Perhaps json-pretty-print could use the new replace-buffer-contents >> > function? Although I'm not sure this will cure the problem, at least >> > not in all cases. >> >> replace-buffer-contents would be a lot easier to use in this context >> if it could take a string. Otherwise you have to create a buffer, >> select it, insert a string, then pass that buffer to >> replace-buffer-contents, delete the buffer. > > 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. I'm just not sure if the save-excursion should be in `json-pretty-print' itself. I'd always want to have it but maybe there are use-cases where it would be wrong. --8<---------------cut here---------------start------------->8--- diff --git a/lisp/json.el b/lisp/json.el index 26cd48f41d..966b2e680c 100644 --- a/lisp/json.el +++ b/lisp/json.el @@ -740,14 +740,23 @@ json-pretty-print-buffer (defun json-pretty-print (begin end) "Pretty-print selected region." (interactive "r") - (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) - (txt (delete-and-extract-region begin end))) - (insert (json-encode (json-read-from-string txt)))))) + (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)))))))) (defun json-pretty-print-buffer-ordered () "Pretty-print current buffer with object keys ordered." --8<---------------cut here---------------end--------------->8--- Oh, one part where that's not completely right is undoing. That is, pretty-printing keeps point on the character it has been before but undoing the pretty-printing will place point at the end of the json. Bye, Tassilo