[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 21:00:04 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
Tassilo Horn <tsdh@gnu.org> writes:
> 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.
[...]
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
- 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, 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
- 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