From: Tassilo Horn <tsdh@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: help-gnu-emacs@gnu.org
Subject: Re: save-excursion doesn't restore point with json-pretty-print
Date: Fri, 01 Feb 2019 21:00:04 +0100 [thread overview]
Message-ID: <87d0obqny3.fsf@gnu.org> (raw)
In-Reply-To: <87munfxqvu.fsf@gnu.org> (Tassilo Horn's message of "Fri, 01 Feb 2019 20:14:45 +0100")
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
next prev parent reply other threads:[~2019-02-01 20:00 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-01 9:32 save-excursion doesn't restore point with json-pretty-print Tassilo Horn
2019-02-01 9:55 ` tomas
2019-02-01 10:11 ` Eli Zaretskii
2019-02-01 10:33 ` Robert Pluim
2019-02-01 13:09 ` Eli Zaretskii
2019-02-01 18:02 ` Tassilo Horn
2019-02-01 18:55 ` Eli Zaretskii
2019-02-01 19:14 ` Tassilo Horn
2019-02-01 20:00 ` Tassilo Horn [this message]
2019-02-01 20:58 ` Eli Zaretskii
2019-02-01 21:04 ` Tassilo Horn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87d0obqny3.fsf@gnu.org \
--to=tsdh@gnu.org \
--cc=eliz@gnu.org \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.