summaryrefslogtreecommitdiff
path: root/lisp/json.el
diff options
context:
space:
mode:
authorTassilo Horn <tsdh@gnu.org>2019-02-23 21:18:36 +0100
committerTassilo Horn <tsdh@gnu.org>2019-02-23 21:31:15 +0100
commite96923c188a2a38d09917c5b7f606187a1413a96 (patch)
tree6ad7b9a1549bf520747db72e36eb62ceb6fcc720 /lisp/json.el
parent5f640bfdf84753322763be23ebaa8ded92dc1c5d (diff)
downloademacs-e96923c188a2a38d09917c5b7f606187a1413a96.tar.gz
Improve replace-buffer-contents/replace-region-contents
* src/editfns.c (Freplace_buffer_contents): Add two optional arguments for mitigating performance issues. * lisp/emacs-lisp/subr-x.el (replace-region-contents): Move from subr.el. Add the same two arguments as for replace-buffer-contents. * lisp/json.el (json-pretty-print-max-secs): New variable holding the default MAX-SECS value json-pretty-print passes to replace-buffer-contents. (json-pretty-print): Use it. * doc/lispref/text.texi (Replacing): Add documentation for replace-buffer-contents two new optional arguments. Document replace-region-contents.
Diffstat (limited to 'lisp/json.el')
-rw-r--r--lisp/json.el15
1 files changed, 14 insertions, 1 deletions
diff --git a/lisp/json.el b/lisp/json.el
index 19b8f09dcda..44b3c33df7c 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -49,10 +49,13 @@
;; 2008-02-21 - Installed in GNU Emacs.
;; 2011-10-17 - Patch `json-alist-p' and `json-plist-p' to avoid recursion -tzz
;; 2012-10-25 - Added pretty-printed reformatting -Ryan Crum (ryan@ryancrum.org)
+;; 2019-02-02 - Pretty-printing now uses replace-region-contents and support for
+;; minimization -tsdh
;;; Code:
(require 'map)
+(require 'subr-x)
;; Parameters
@@ -738,6 +741,12 @@ With prefix argument MINIMIZE, minimize it instead."
(interactive "P")
(json-pretty-print (point-min) (point-max) minimize))
+(defvar json-pretty-print-max-secs 2.0
+ "Maximum time for `json-pretty-print's comparison.
+The function `json-pretty-print' uses `replace-region-contents'
+(which see) passing the value of this variable as argument
+MAX-SECS.")
+
(defun json-pretty-print (begin end &optional minimize)
"Pretty-print selected region.
With prefix argument MINIMIZE, minimize it instead."
@@ -749,7 +758,11 @@ With prefix argument MINIMIZE, minimize it instead."
(json-object-type 'alist))
(replace-region-contents
begin end
- (lambda () (json-encode (json-read))))))
+ (lambda () (json-encode (json-read)))
+ json-pretty-print-max-secs
+ ;; FIXME: What's a good value here? Can we use something better,
+ ;; e.g., by deriving a value from the size of the region?
+ 64)))
(defun json-pretty-print-buffer-ordered (&optional minimize)
"Pretty-print current buffer with object keys ordered.