aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/json.el
diff options
context:
space:
mode:
authorLars Ingebrigtsen2019-07-31 22:18:57 +0200
committerLars Ingebrigtsen2019-07-31 22:18:57 +0200
commitee7baca4fa96d4e1ad6bd9ad055d92f435b7eaa6 (patch)
treefe74c4f08fabb6d61e0dbbf7cfe23446d6e46fd3 /lisp/json.el
parenta79e96f0f9133b0577e709f805179ab59b09fe33 (diff)
downloademacs-ee7baca4fa96d4e1ad6bd9ad055d92f435b7eaa6.tar.gz
emacs-ee7baca4fa96d4e1ad6bd9ad055d92f435b7eaa6.zip
Restore `replace-region-contents' in json-pretty-print
* lisp/json.el (json-pretty-print): Switch back to using `replace-region-contents' to preserve markers and fonts which went missing when fixing the bug (bug#34160). (json-pretty-print-max-secs): Restore, too.
Diffstat (limited to 'lisp/json.el')
-rw-r--r--lisp/json.el35
1 files changed, 25 insertions, 10 deletions
diff --git a/lisp/json.el b/lisp/json.el
index d664dae05e4..cdb1be0616c 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -49,6 +49,8 @@
49;; 2008-02-21 - Installed in GNU Emacs. 49;; 2008-02-21 - Installed in GNU Emacs.
50;; 2011-10-17 - Patch `json-alist-p' and `json-plist-p' to avoid recursion -tzz 50;; 2011-10-17 - Patch `json-alist-p' and `json-plist-p' to avoid recursion -tzz
51;; 2012-10-25 - Added pretty-printed reformatting -Ryan Crum (ryan@ryancrum.org) 51;; 2012-10-25 - Added pretty-printed reformatting -Ryan Crum (ryan@ryancrum.org)
52;; 2019-02-02 - Pretty-printing now uses replace-region-contents and support for
53;; minimization -tsdh
52 54
53;;; Code: 55;;; Code:
54 56
@@ -755,6 +757,12 @@ With prefix argument MINIMIZE, minimize it instead."
755 (interactive "P") 757 (interactive "P")
756 (json-pretty-print (point-min) (point-max) minimize)) 758 (json-pretty-print (point-min) (point-max) minimize))
757 759
760(defvar json-pretty-print-max-secs 2.0
761 "Maximum time for `json-pretty-print's comparison.
762The function `json-pretty-print' uses `replace-region-contents'
763(which see) passing the value of this variable as argument
764MAX-SECS.")
765
758(defun json-pretty-print (begin end &optional minimize) 766(defun json-pretty-print (begin end &optional minimize)
759 "Pretty-print selected region. 767 "Pretty-print selected region.
760With prefix argument MINIMIZE, minimize it instead." 768With prefix argument MINIMIZE, minimize it instead."
@@ -766,16 +774,23 @@ With prefix argument MINIMIZE, minimize it instead."
766 (json-object-type 'alist) 774 (json-object-type 'alist)
767 (err (gensym)) 775 (err (gensym))
768 json) 776 json)
769 (save-restriction 777 (replace-region-contents
770 (narrow-to-region begin end) 778 begin end
771 (goto-char begin) 779 (lambda ()
772 (while (not (eq (setq json (condition-case _ 780 (let ((pretty ""))
773 (json-read) 781 (save-restriction
774 (json-error err))) 782 (narrow-to-region begin end)
775 err)) 783 (goto-char begin)
776 (delete-region begin (point)) 784 (while (not (eq (setq json (condition-case nil
777 (insert (json-encode json)) 785 (json-read)
778 (setq begin (point)))))) 786 (json-error err)))
787 err))
788 (setq pretty (concat pretty (json-encode json)))))
789 pretty))
790 json-pretty-print-max-secs
791 ;; FIXME: What's a good value here? Can we use something better,
792 ;; e.g., by deriving a value from the size of the region?
793 64)))
779 794
780(defun json-pretty-print-buffer-ordered (&optional minimize) 795(defun json-pretty-print-buffer-ordered (&optional minimize)
781 "Pretty-print current buffer with object keys ordered. 796 "Pretty-print current buffer with object keys ordered.