all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [RFC]: replace-region-contents
@ 2019-02-01 21:20 Tassilo Horn
  2019-02-02  9:33 ` Marcin Borkowski
                   ` (2 more replies)
  0 siblings, 3 replies; 35+ messages in thread
From: Tassilo Horn @ 2019-02-01 21:20 UTC (permalink / raw)
  To: emacs-devel

Hi all,

on gnu-emacs-help I've asked why my wrapper command around
`json-pretty-print' doesn't restore point although it uses
`save-excursion'.  The reason is that `json-pretty-print' replaces the
region with copy, delete, and insert.  Robert and Eli pointed me to the
new `replace-buffer-contents' which allowed me to rewrite
`json-pretty-print' in a way where point stays where it has been before
pretty-printing:

--8<---------------cut here---------------start------------->8---
(defun json-pretty-print (begin end)
  "Pretty-print selected region."
  (interactive "r")
  (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))))))))
--8<---------------cut here---------------end--------------->8---

I think that replacing a region by transforming its contents in some
arbitray way is some generally useful feature, so I'd propose to extract
that functionality into some new function `replace-region-contents' I'd
like to add to subr-x.el.  Here it is and how it is applied in the
json-pretty-printing scenario.

--8<---------------cut here---------------start------------->8---
;; in subr-x.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---

Does that seem like a good idea, is there anything to improve, or should
I just fix `json-pretty-print' as in the first snippet?

Bye,
Tassilo



^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2019-02-09  8:52 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-02-01 21:20 [RFC]: replace-region-contents Tassilo Horn
2019-02-02  9:33 ` Marcin Borkowski
2019-02-02 13:57   ` Tassilo Horn
2019-02-02 15:42 ` Stefan Monnier
2019-02-04  5:23   ` Tassilo Horn
2019-02-05  2:56     ` Stefan Monnier
2019-02-05  5:57       ` Tassilo Horn
2019-02-05 13:21         ` Tassilo Horn
2019-02-05 16:36           ` Eli Zaretskii
2019-02-05 17:19             ` Tassilo Horn
2019-02-06 16:00               ` Eli Zaretskii
2019-02-06 17:02                 ` Tassilo Horn
2019-02-06 17:33                   ` Eli Zaretskii
2019-02-06 18:07                     ` Tassilo Horn
2019-02-08 16:23                       ` Tassilo Horn
2019-02-08 16:28                         ` Stefan Monnier
2019-02-08 17:17                           ` Tassilo Horn
2019-02-08 21:37                             ` Eli Zaretskii
2019-02-08 21:53                               ` Stefan Monnier
2019-02-08 21:27                         ` Eli Zaretskii
2019-02-08 22:03                           ` Tassilo Horn
2019-02-08 22:19                             ` Eli Zaretskii
2019-02-09  0:00                           ` Tassilo Horn
2019-02-09  8:26                             ` Eli Zaretskii
2019-02-09  8:52                               ` Tassilo Horn
2019-02-05 13:43         ` Stefan Monnier
2019-02-06  8:07           ` Tassilo Horn
2019-02-06  9:55             ` Marcin Borkowski
2019-02-06 11:10               ` Tassilo Horn
2019-02-06 14:09             ` Stefan Monnier
2019-02-05 16:11         ` Eli Zaretskii
2019-02-02 16:17 ` Stefan Monnier
2019-02-03 15:59   ` Eli Zaretskii
2019-02-03 17:05     ` Stefan Monnier
2019-02-03 17:18       ` Eli Zaretskii

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.