help-gnu-emacs
[Top][All Lists]
Advanced

[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 20:14:45 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Eli Zaretskii <eliz@gnu.org> 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.

--8<---------------cut here---------------start------------->8---
(defun json-pretty-print (begin end)
  "Pretty-print selected region."
  (interactive "r")
  (atomic-change-group
    (replace-region-contents
     begin end
     (lambda ()
       (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))
         (json-read)))
     (lambda (json-obj)
       (json-encode json-obj)))))
--8<---------------cut here---------------end--------------->8---

If so, where should I put it?  subr.el?

>> 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.
>
> Good question.  I don't know the answer either.

Then let's keep it the current way until someone complains which will
probably never happen.

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]