From b3092b2873829317de56043a8247ad2631d24d68 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Fri, 8 Feb 2019 20:39:00 +0100 Subject: [PATCH] Impl. json-pretty-print with replace-region-contents + minimization * lisp/json.el (json-pretty-print): Use the new replace-region-contents. Add prefix arg for minimzation. (json-pretty-print-buffer): Add prefix arg for minimzation. (json-pretty-print-buffer-ordered): Add prefix arg for minimzation. (json-pretty-print-ordered): Add prefix arg for minimzation. --- lisp/json.el | 56 ++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/lisp/json.el b/lisp/json.el index 26cd48f41db..3271c373b4a 100644 --- a/lisp/json.el +++ b/lisp/json.el @@ -730,36 +730,40 @@ Advances point just past JSON object." ((hash-table-p object) (json-encode-hash-table object)) (t (signal 'json-error (list object))))) -;; Pretty printing - -(defun json-pretty-print-buffer () - "Pretty-print current buffer." - (interactive) - (json-pretty-print (point-min) (point-max))) - -(defun json-pretty-print (begin end) - "Pretty-print selected region." - (interactive "r") - (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) - (txt (delete-and-extract-region begin end))) - (insert (json-encode (json-read-from-string txt)))))) - -(defun json-pretty-print-buffer-ordered () - "Pretty-print current buffer with object keys ordered." +;; Pretty printing & minimizing + +(defun json-pretty-print-buffer (&optional minimize) + "Pretty-print current buffer. +With prefix argument MINIMIZE, minimize it instead." + (interactive "P") + (json-pretty-print (point-min) (point-max) minimize)) + +(defun json-pretty-print (begin end &optional minimize) + "Pretty-print selected region. +With prefix argument MINIMIZE, minimize it instead." + (interactive "r\nP") + (let ((json-encoding-pretty-print (null minimize)) + ;; Distinguish an empty objects from 'null' + (json-null :json-null) + ;; Ensure that ordering is maintained + (json-object-type 'alist)) + (replace-region-contents + begin end + (lambda () (json-encode (json-read)))))) + +(defun json-pretty-print-buffer-ordered (&optional minimize) + "Pretty-print current buffer with object keys ordered. +With prefix argument MINIMIZE, minimize it instead." (interactive) (let ((json-encoding-object-sort-predicate 'string<)) - (json-pretty-print-buffer))) + (json-pretty-print-buffer minimize))) -(defun json-pretty-print-ordered (begin end) - "Pretty-print the region with object keys ordered." - (interactive "r") +(defun json-pretty-print-ordered (begin end &optional minimize) + "Pretty-print the region with object keys ordered. +With prefix argument MINIMIZE, minimize it instead." + (interactive "r\nP") (let ((json-encoding-object-sort-predicate 'string<)) - (json-pretty-print begin end))) + (json-pretty-print begin end minimize))) (provide 'json) -- 2.39.5