-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprintv.lisp
293 lines (251 loc) · 12.2 KB
/
printv.lisp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
;;;;; -*- mode: common-lisp; common-lisp-style: modern; coding: utf-8; -*-
;;;;;
(in-package :printv)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Configurables - Adjust to Individual Taste
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *default-printv-output* (make-synonym-stream '*trace-output*))
(defvar *printv-output* *default-printv-output*)
(defvar *printv-lock* (make-recursive-lock))
(defparameter *figlet-executable* "figlet")
(defparameter *figlet-font* "standard")
(defparameter *major-separator* :ff)
(defparameter *minor-separator* :hr)
(defparameter *timestamp-designator* :ts)
(defparameter *printv-macro-char* #\^)
(defparameter *ppmx-macro-char* #\$)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Output Enablement and Control
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun enable-printv-output (&optional (stream *default-printv-output*))
(setf *printv-output* stream))
(defun disable-printv-output ()
(setf *printv-output* (make-broadcast-stream)))
(defmacro with-printv-output-to ((&optional (destination *default-printv-output*))
&body body)
`(let ((*printv-output* ,destination))
,@body))
(defun disable-printv ()
(enable-printv-output nil))
(defun enable-printv ()
(enable-printv-output))
(defmacro with-printv-disabled (&body body)
`(with-printv-output-to (nil)
,@body))
(defmacro with-printv-enabled (&body body)
`(with-printv-output-to (*default-printv-output*)
,@body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PPMX - Macroexpansion Pretty-Printer (from CCL)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro ppmx (form)
"Pretty prints the macro expansion of FORM."
`(flet ((mx ()
(let* ((exp1 (macroexpand-1 ',form))
(exp (macroexpand exp1))
(*print-circle* nil))
(format *printv-output* "~%;;; Form: ~W" (quote ,form))
(cond
((equal exp exp1) (format *printv-output* "~%;;;~%;;; Macro expansion:~%")
(pprint exp *printv-output*))
(t (format *printv-output* "~&;;; First step of expansion:~%")
(pprint exp1 *printv-output*)
(format *printv-output* "~%;;;~%;;; Final expansion:~%")
(pprint exp *printv-output*)))
(format *printv-output* "~%;;;~%;;; ")
(values))))
(etypecase *printv-output*
(null (values))
(pathname (with-recursive-lock-held (*printv-lock*)
(with-open-file (logfile *printv-output*
:direction :output
:if-does-not-exist :create
:if-exists :append)
(with-printv-output-to (logfile)
(mx)))))
(stream (with-recursive-lock-held (*printv-lock*)
(mx))))))
(setf (macro-function :ppmx) (macro-function 'ppmx))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PRINTV - Extended Edition
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Adapted from the Handy PRINTV Macro Written by Dan Corkill
;; Copyright (C) 2006-2010, Dan Corkill <corkill@GBBopen.org>
;; Licensed under Apache License 2.0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun minor-separator ()
(format *printv-output* "~&;;; ~72,,,'-<-~> ;;;~%")
(force-output *printv-output*))
(defun major-separator ()
(format *printv-output* "~&;;;~77,,,';<;~>~%")
(format *printv-output* "~&;;; ~72,,,'=<=~> ;;;~%")
(format *printv-output* "~&;;;~77,,,';<;~>~%")
(force-output *printv-output*))
(defun timestamp (&optional (time (get-universal-time)))
(funcall #'format *printv-output*
(let* ((ts-str (format-universal-time nil time))
(ts-len (length ts-str))
(pad0 (floor (- 80 ts-len 10) 2))
(pad1 (ceiling (- 80 ts-len 10) 2)))
(concatenate 'string
"~&;;; "
(make-string pad0 :initial-element #\.)
" " ts-str " "
(make-string pad1 :initial-element #\.)
" ;;;~%"))))
(defun form-printer (form)
(typecase form
;; String (label):
(string (if (and (> (length form) 2) (equal (subseq form 0 2) "#|"))
(format *printv-output* "~&~a~%" form)
(format *printv-output* "~&;;; ~a~%" form)))
;; Evaluated form:
((or cons (and symbol (not keyword)))
(format *printv-output* "~&;;; ~w =>" form))
(vector (format *printv-output* "~&;; ~s~%" form))
;; Self-evaluating form:
(t (format *printv-output* "~&;;; ~s~%" form)))
(force-output *printv-output*))
(defun values-printer (values-list)
(format *printv-output* "~:[ [returned 0 values]~;~:*~{ ~w~^,~}~]~%" values-list)
(force-output *printv-output*))
(defmacro vlet* (bind-forms &body body)
`(progn (format *printv-output* "~& [")
(let* ,(mapcar #'(lambda (form)
(if (listp form)
`(,(car form) (let ((v ,(cadr form)))
(format *printv-output*
" [~S=~S] " ',(car form) v)
v))
form))
bind-forms)
(format *printv-output* "]~&;;; =>")
,@body)))
(defmacro vlet (bind-forms &body body)
`(progn (format *printv-output* "~& [")
(let ,(mapcar #'(lambda (form)
(if (listp form)
`(,(car form) (let ((v ,(cadr form)))
(format *printv-output*
" [~S=~S] " ',(car form) v)
v))
form))
bind-forms)
(format *printv-output* "]~&;;; =>")
,@body)))
(defmacro vcond (&body clauses)
`(progn (format *printv-output* "~&~% ")
(multiple-value-prog1 (cond ,@(mapcar #'(lambda (clause)
`((let ((x ,(car clause)))
(format *printv-output*
" [~S -> ~S]~% "
',(car clause) x)
x)
,@(cdr clause)))
clauses))
(format *printv-output* "~&;;; =>"))))
(defun expander (forms &optional (values-trans-fn 'identity))
(let ((result-sym (gensym)))
`(flet ((exp-1 ()
(let ((*print-readably* nil) ,result-sym)
,@(loop for form in forms nconcing
(cond
;; Markup form:
((eq form *major-separator*) (list '(major-separator)))
((eq form *minor-separator*) (list '(minor-separator)))
((eq form *timestamp-designator*) (list '(timestamp)))
;; Binding form:
((and (consp form) (or (eq (car form) 'let) (eq (car form) 'let*)))
`((form-printer (append '(,(car form) ,(cadr form)) ',(cddr form)))
(values-printer
(setf ,result-sym (funcall ',values-trans-fn
(multiple-value-list
,(case (car form)
(let `(vlet ,@(rest form)))
(let* `(vlet* ,@(rest form))))))))))
;; COND form:
((and (consp form) (eq (car form) 'cond))
`((form-printer (append '(,(car form)) ',(rest form)))
(values-printer
(setf ,result-sym (funcall ',values-trans-fn
(multiple-value-list (vcond ,@(rest form))))))))
;; FIGLET banner:
((and (keywordp form) (every (lambda (c)
(or
(and (alpha-char-p c) (lower-case-p c))
(not (alpha-char-p c))))
(symbol-name form)))
`((form-printer
,(with-output-to-string (s)
(princ "#||" s)
(terpri s)
(ignore-errors
(asdf/run-program:run-program
(format nil "~A -f ~A -w ~D ~A" *figlet-executable*
*figlet-font* *print-right-margin* (symbol-name form))
:output s))
(princ "||#" s)))))
;; Evaluated form:
((or (consp form) (and (symbolp form) (not (keywordp form))))
`((form-printer ',form)
(values-printer
(setf ,result-sym (funcall ',values-trans-fn
(multiple-value-list ,form))))))
;; Self-evaluating form:
(t `((form-printer
(car (setf ,result-sym (list ,form))))))))
(values-list ,result-sym))))
(etypecase *printv-output*
(null ,(append '(progn) forms))
(pathname (with-recursive-lock-held (*printv-lock*)
(with-open-file (logfile *printv-output*
:direction :output
:if-does-not-exist :create
:if-exists :append)
(with-printv-output-to (logfile)
(exp-1)))))
(stream (with-recursive-lock-held (*printv-lock*)
(exp-1)))))))
(defmacro printv (&rest forms)
(expander forms))
(setf (macro-function :printv) (macro-function 'printv))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PRINTV/PPMX Readtable Extensions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#+swank
(defun register-readtable (&optional (package *package*))
(let ((package-name (typecase package
(string package)
(symbol (symbol-name package))
(package (package-name package)))))
(push (cons package-name *readtable*) swank:*readtable-alist*)))
#-swank
(defun register-readtable (&optional (package *package*))
nil)
(defun ppmx-reader (stream char)
(declare (ignore char))
(let ((body (read stream t nil t)))
`(ppmx ,body)))
(defun enable-ppmx-reader (&optional (char *ppmx-macro-char*))
(prog1 char
(setf *readtable* (copy-readtable *readtable*))
(set-macro-character char 'ppmx-reader)
(register-readtable)))
(defun printv-reader (stream char)
(declare (ignore char))
(let ((body (read stream t nil t)))
`(printv ,body)))
(defun enable-printv-reader (&optional (char *printv-macro-char*))
(prog1 char
(setf *readtable* (copy-readtable *readtable*))
(set-macro-character char 'printv-reader)
(register-readtable)))
;; (when (find-package :readtable)
;; (let ((pubsyms '(ppmx-reader enable-ppmx-reader
;; printv-reader enable-printv-reader)))
;; (import pubsyms :readtable)
;; (export pubsyms :readtable)))
(enable-ppmx-reader)
(enable-printv-reader)
(register-readtable)
(pushnew :printv *features*)