[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: save-excursion doesn't restore point with json-pretty-print
From: |
Tassilo Horn |
Subject: |
Re: save-excursion doesn't restore point with json-pretty-print |
Date: |
Fri, 01 Feb 2019 19:02:23 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Robert Pluim <rpluim@gmail.com>
>> 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
- save-excursion doesn't restore point with json-pretty-print, Tassilo Horn, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print, tomas, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print, Eli Zaretskii, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print, Robert Pluim, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print, Eli Zaretskii, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print,
Tassilo Horn <=
- Re: save-excursion doesn't restore point with json-pretty-print, Eli Zaretskii, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print, Tassilo Horn, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print, Tassilo Horn, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print, Eli Zaretskii, 2019/02/01
- Re: save-excursion doesn't restore point with json-pretty-print, Tassilo Horn, 2019/02/01