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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

save-excursion doesn't restore point with json-pretty-print


From: Tassilo Horn
Subject: save-excursion doesn't restore point with json-pretty-print
Date: Fri, 01 Feb 2019 10:32:20 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Hi all,

I have this small command in my ~/.emacs:

--8<---------------cut here---------------start------------->8---
(defun th/json-pretty-print-snippet-at-point ()
  "Pretty-print the json snippet at point."
  (interactive)
  (save-excursion
    (when (beginning-of-defun)
      (let ((beg (point)))
        (end-of-defun)
        (when (looking-back "\n")
          (backward-char))
        (json-pretty-print beg (point))))))
--8<---------------cut here---------------end--------------->8---

It works pretty well except that point is not restored.  No matter where
I execute it, after reformatting the snippet, point will sit on the
character where `beginning-of-defun' moved it.

For example, when point is on the 1 below,

{"foo": 1, "bar":27}

I'll end up with

{
  "foo": 1,
  "bar": 27
}

and point is on the {.  I'd expect it to still sit on the 1.

Obviously, `save-excursion' works just fine in all other places.  It
seems like it's just not playing well with `json-pretty-print', and I
don't know why.

Bye,
Tassilo



reply via email to

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