diff options
| -rw-r--r-- | lisp/json.el | 35 |
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. | ||
| 762 | The function `json-pretty-print' uses `replace-region-contents' | ||
| 763 | (which see) passing the value of this variable as argument | ||
| 764 | MAX-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. |
| 760 | With prefix argument MINIMIZE, minimize it instead." | 768 | With 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. |