-
Notifications
You must be signed in to change notification settings - Fork 1
/
pprint-to-buffer.el
86 lines (78 loc) · 2.83 KB
/
pprint-to-buffer.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
;;; pprint-to-buffer.el --- pprint elisp expressions, just like in CIDER
;;
;; Filename: pprint-to-buffer.el
;; Description:
;; Author: Arne Brasseur
;; Maintainer:
;; Created: Do Jul 19 17:10:31 2018 (+0200)
;; Version: 0.1.1
;; Package-Requires: ((cider "0.17.0"))
;; URL:
;; Doc URL:
;; Keywords:
;; Compatibility:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; C-c C-p evaluates sexp before point and pops up a buffer with the result
;; pretty printed.
;;
;; Add a prefix argument to insert at point instead.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or (at
;; your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'cider-popup)
(defun pprint-to-buffer--pop-to-buffer (form)
"Pretty print FORM in popup buffer."
(let* ((result-buffer (cider-popup-buffer "*pprint*" nil 'emacs-lisp-mode)))
(with-current-buffer result-buffer
(read-only-mode -1)
(insert (format "%S" (eval form)))
(goto-char 1)
(cl--do-prettyprint))))
(defun pprint-to-buffer--eval-and-insert (form)
"Pretty print FORM and insert at point."
(lisp-indent-line)
(let ((pos (point)))
(insert (format "%S" (eval form)))
(goto-char pos)
(cl--do-prettyprint)))
(defun pprint-to-buffer-last-sexp-to-current-buffer ()
"Evaluate the sexp preceding point and pprint its value into the current buffer."
(interactive)
(pprint-to-buffer--eval-and-insert (elisp--preceding-sexp)))
(defun pprint-to-buffer-last-sexp (&optional output-to-current-buffer)
"Evaluate the sexp preceding point and pprint its value.
If invoked with OUTPUT-TO-CURRENT-BUFFER, insert as comment in the current
buffer, else display in a popup buffer."
(interactive "P")
(if output-to-current-buffer
(pprint-to-buffer-last-sexp-to-current-buffer)
(pprint-to-buffer--pop-to-buffer (elisp--preceding-sexp))))
;; (define-key emacs-lisp-mode-map (kbd "C-c C-p") 'pprint-to-buffer-last-sexp)
(provide 'pprint-to-buffer)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; pprint-to-buffer.el ends here