Change OUTPUT-UGLY-OBJECT to resemble a pprint function.
[sbcl.git] / src / code / pprint.lisp
blob502e35cbb53200b55acab4a59ffbe35d49fcfe48
1 ;;;; Common Lisp pretty printer
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!PRETTY")
14 #!-sb-fluid (declaim (inline index-posn posn-index posn-column))
15 (defun index-posn (index stream)
16 (declare (type index index) (type pretty-stream stream)
17 (values posn))
18 (+ index (pretty-stream-buffer-offset stream)))
19 (defun posn-index (posn stream)
20 (declare (type posn posn) (type pretty-stream stream)
21 (values index))
22 (- posn (pretty-stream-buffer-offset stream)))
23 (defun posn-column (posn stream)
24 (declare (type posn posn) (type pretty-stream stream)
25 (values posn))
26 (truly-the posn (index-column (posn-index posn stream) stream)))
28 ;;; Is it OK to do pretty printing on this stream at this time?
29 (defun print-pretty-on-stream-p (stream)
30 (and (pretty-stream-p stream)
31 *print-pretty*))
33 ;;;; stream interface routines
35 (defun pretty-out (stream char)
36 (declare (type pretty-stream stream)
37 (type character char))
38 (let ((f (pretty-stream-char-out-oneshot-hook stream)))
39 (when f
40 (setf (pretty-stream-char-out-oneshot-hook stream) nil)
41 (funcall f stream char)))
42 (cond ((char= char #\newline)
43 (enqueue-newline stream :literal))
45 (ensure-space-in-buffer stream 1)
46 (let ((fill-pointer (pretty-stream-buffer-fill-pointer stream)))
47 (setf (schar (pretty-stream-buffer stream) fill-pointer) char)
48 (setf (pretty-stream-buffer-fill-pointer stream)
49 (1+ fill-pointer))))))
51 (defun pretty-sout (stream string start end)
52 (declare (type pretty-stream stream)
53 (type simple-string string)
54 (type index start)
55 (type (or index null) end))
56 (let* ((end (or end (length string))))
57 (unless (= start end)
58 (sb!impl::string-dispatch (simple-base-string
59 #!+sb-unicode
60 (simple-array character (*)))
61 string
62 ;; For POSITION transform
63 (declare (optimize (speed 2)))
64 (let ((f (pretty-stream-char-out-oneshot-hook stream)))
65 (when f
66 (setf (pretty-stream-char-out-oneshot-hook stream) nil)
67 (funcall f stream (aref string start))))
68 (let ((newline (position #\newline string :start start :end end)))
69 (cond
70 (newline
71 (pretty-sout stream string start newline)
72 (enqueue-newline stream :literal)
73 (pretty-sout stream string (1+ newline) end))
75 (let ((chars (- end start)))
76 (loop
77 (let* ((available (ensure-space-in-buffer stream chars))
78 (count (min available chars))
79 (fill-pointer (pretty-stream-buffer-fill-pointer
80 stream))
81 (new-fill-ptr (+ fill-pointer count)))
82 (declare (fixnum available count))
83 (if (typep string 'simple-base-string)
84 ;; FIXME: Reimplementing REPLACE, since it
85 ;; can't be inlined and we don't have a
86 ;; generic "simple-array -> simple-array"
87 ;; transform for it.
88 (loop for i from fill-pointer below new-fill-ptr
89 for j from start
90 with target = (pretty-stream-buffer stream)
91 do (setf (aref target i)
92 (aref string j)))
93 (replace (pretty-stream-buffer stream)
94 string
95 :start1 fill-pointer :end1 new-fill-ptr
96 :start2 start))
97 (setf (pretty-stream-buffer-fill-pointer stream)
98 new-fill-ptr)
99 (decf chars count)
100 (when (zerop count)
101 (return))
102 (incf start count)))))))))))
104 (defun pretty-misc (stream op &optional arg1 arg2)
105 (declare (ignore stream op arg1 arg2)))
107 ;;;; logical blocks
109 (defstruct (logical-block (:copier nil))
110 ;; The column this logical block started in.
111 (start-column 0 :type column)
112 ;; The column the current section started in.
113 (section-column 0 :type column)
114 ;; The length of the per-line prefix. We can't move the indentation
115 ;; left of this.
116 (per-line-prefix-end 0 :type index)
117 ;; The overall length of the prefix, including any indentation.
118 (prefix-length 0 :type index)
119 ;; The overall length of the suffix.
120 (suffix-length 0 :type index)
121 ;; The line number
122 (section-start-line 0 :type index))
124 (defun really-start-logical-block (stream column prefix suffix)
125 (let* ((blocks (pretty-stream-blocks stream))
126 (prev-block (car blocks))
127 (per-line-end (logical-block-per-line-prefix-end prev-block))
128 (prefix-length (logical-block-prefix-length prev-block))
129 (suffix-length (logical-block-suffix-length prev-block))
130 (block (make-logical-block
131 :start-column column
132 :section-column column
133 :per-line-prefix-end per-line-end
134 :prefix-length prefix-length
135 :suffix-length suffix-length
136 :section-start-line (pretty-stream-line-number stream))))
137 (setf (pretty-stream-blocks stream) (cons block blocks))
138 (set-indentation stream column)
139 (when prefix
140 (setf (logical-block-per-line-prefix-end block) column)
141 (replace (pretty-stream-prefix stream) prefix
142 :start1 (- column (length prefix)) :end1 column))
143 (when suffix
144 (let* ((total-suffix (pretty-stream-suffix stream))
145 (total-suffix-len (length total-suffix))
146 (additional (length suffix))
147 (new-suffix-len (+ suffix-length additional)))
148 (when (> new-suffix-len total-suffix-len)
149 (let ((new-total-suffix-len
150 (max (* total-suffix-len 2)
151 (+ suffix-length
152 (floor (* additional 5) 4)))))
153 (setf total-suffix
154 (replace (make-string new-total-suffix-len) total-suffix
155 :start1 (- new-total-suffix-len suffix-length)
156 :start2 (- total-suffix-len suffix-length)))
157 (setf total-suffix-len new-total-suffix-len)
158 (setf (pretty-stream-suffix stream) total-suffix)))
159 (replace total-suffix suffix
160 :start1 (- total-suffix-len new-suffix-len)
161 :end1 (- total-suffix-len suffix-length))
162 (setf (logical-block-suffix-length block) new-suffix-len))))
163 nil)
165 (defun set-indentation (stream column)
166 (let* ((prefix (pretty-stream-prefix stream))
167 (prefix-len (length prefix))
168 (block (car (pretty-stream-blocks stream)))
169 (current (logical-block-prefix-length block))
170 (minimum (logical-block-per-line-prefix-end block))
171 (column (max minimum column)))
172 (when (> column prefix-len)
173 (setf prefix
174 (replace (make-string (max (* prefix-len 2)
175 (+ prefix-len
176 (floor (* (- column prefix-len) 5)
177 4))))
178 prefix
179 :end1 current))
180 (setf (pretty-stream-prefix stream) prefix))
181 (when (> column current)
182 (fill prefix #\space :start current :end column))
183 (setf (logical-block-prefix-length block) column)))
185 (defun really-end-logical-block (stream)
186 (let* ((old (pop (pretty-stream-blocks stream)))
187 (old-indent (logical-block-prefix-length old))
188 (new (car (pretty-stream-blocks stream)))
189 (new-indent (logical-block-prefix-length new)))
190 (when (> new-indent old-indent)
191 (fill (pretty-stream-prefix stream) #\space
192 :start old-indent :end new-indent)))
193 nil)
195 ;;;; the pending operation queue
197 (defmacro enqueue (stream type &rest args)
198 (let ((constructor (symbolicate "MAKE-" type)))
199 (once-only ((stream stream)
200 (entry `(,constructor :posn
201 (index-posn
202 (pretty-stream-buffer-fill-pointer
203 ,stream)
204 ,stream)
205 ,@args))
206 (op `(list ,entry))
207 (head `(pretty-stream-queue-head ,stream)))
208 `(progn
209 (if ,head
210 (setf (cdr ,head) ,op)
211 (setf (pretty-stream-queue-tail ,stream) ,op))
212 (setf (pretty-stream-queue-head ,stream) ,op)
213 ,entry))))
215 (defun enqueue-newline (stream kind)
216 (let* ((depth (length (pretty-stream-pending-blocks stream)))
217 (newline (enqueue stream newline :kind kind :depth depth)))
218 (dolist (entry (pretty-stream-queue-tail stream))
219 (when (and (not (eq newline entry))
220 (section-start-p entry)
221 (null (section-start-section-end entry))
222 (<= depth (section-start-depth entry)))
223 (setf (section-start-section-end entry) newline))))
224 (maybe-output stream (or (eq kind :literal) (eq kind :mandatory))))
226 (defstruct (indentation (:include queued-op)
227 (:copier nil))
228 (kind (missing-arg) :type (member :block :current))
229 (amount 0 :type fixnum))
231 (defun enqueue-indent (stream kind amount)
232 (enqueue stream indentation :kind kind :amount amount))
234 (defstruct (block-start (:include section-start)
235 (:copier nil))
236 (block-end nil :type (or null block-end))
237 (prefix nil :type (or null simple-string))
238 (suffix nil :type (or null simple-string)))
240 (defun start-logical-block (stream prefix per-line-p suffix)
241 ;; (In the PPRINT-LOGICAL-BLOCK form which calls us,
242 ;; :PREFIX and :PER-LINE-PREFIX have hairy defaulting behavior,
243 ;; and might end up being NIL.)
244 (declare (type (or null string) prefix))
245 ;; (But the defaulting behavior of PPRINT-LOGICAL-BLOCK :SUFFIX is
246 ;; trivial, so it should always be a string.)
247 (declare (type string suffix))
248 (when prefix
249 (unless (typep prefix 'simple-string)
250 (setq prefix (coerce prefix '(simple-array character (*)))))
251 (pretty-sout stream prefix 0 (length prefix)))
252 (unless (typep suffix 'simple-string)
253 (setq suffix (coerce suffix '(simple-array character (*)))))
254 (let* ((pending-blocks (pretty-stream-pending-blocks stream))
255 (start (enqueue stream block-start
256 :prefix (and per-line-p prefix)
257 :suffix suffix
258 :depth (length pending-blocks))))
259 (setf (pretty-stream-pending-blocks stream)
260 (cons start pending-blocks))))
262 (defun end-logical-block (stream)
263 (let* ((start (pop (pretty-stream-pending-blocks stream)))
264 (suffix (block-start-suffix start))
265 (end (enqueue stream block-end :suffix suffix)))
266 (when suffix
267 (pretty-sout stream suffix 0 (length suffix)))
268 (setf (block-start-block-end start) end)))
270 (defstruct (tab (:include queued-op)
271 (:copier nil))
272 (sectionp nil :type (member t nil))
273 (relativep nil :type (member t nil))
274 (colnum 0 :type column)
275 (colinc 0 :type column))
277 (defun enqueue-tab (stream kind colnum colinc)
278 (multiple-value-bind (sectionp relativep)
279 (ecase kind
280 (:line (values nil nil))
281 (:line-relative (values nil t))
282 (:section (values t nil))
283 (:section-relative (values t t)))
284 (enqueue stream tab :sectionp sectionp :relativep relativep
285 :colnum colnum :colinc colinc)))
287 ;;;; tab support
289 (defun compute-tab-size (tab section-start column)
290 (let* ((origin (if (tab-sectionp tab) section-start 0))
291 (colnum (tab-colnum tab))
292 (colinc (tab-colinc tab))
293 (position (- column origin)))
294 (cond ((tab-relativep tab)
295 (unless (<= colinc 1)
296 (let ((newposn (+ position colnum)))
297 (let ((rem (rem newposn colinc)))
298 (unless (zerop rem)
299 (incf colnum (- colinc rem))))))
300 colnum)
301 ((< position colnum)
302 (- colnum position))
303 ((zerop colinc) 0)
305 (- colinc
306 (rem (- position colnum) colinc))))))
308 (defun index-column (index stream)
309 (let ((column (pretty-stream-buffer-start-column stream))
310 (section-start (logical-block-section-column
311 (first (pretty-stream-blocks stream))))
312 (end-posn (index-posn index stream)))
313 (dolist (op (pretty-stream-queue-tail stream))
314 (when (>= (queued-op-posn op) end-posn)
315 (return))
316 (typecase op
317 (tab
318 (incf column
319 (compute-tab-size op
320 section-start
321 (+ column
322 (posn-index (tab-posn op)
323 stream)))))
324 ((or newline block-start)
325 (setf section-start
326 (+ column (posn-index (queued-op-posn op)
327 stream))))))
328 (+ column index)))
330 (defun expand-tabs (stream through)
331 (let ((insertions nil)
332 (additional 0)
333 (column (pretty-stream-buffer-start-column stream))
334 (section-start (logical-block-section-column
335 (first (pretty-stream-blocks stream)))))
336 (dolist (op (pretty-stream-queue-tail stream))
337 (typecase op
338 (tab
339 (let* ((index (posn-index (tab-posn op) stream))
340 (tabsize (compute-tab-size op
341 section-start
342 (+ column index))))
343 (unless (zerop tabsize)
344 (push (cons index tabsize) insertions)
345 (incf additional tabsize)
346 (incf column tabsize))))
347 ((or newline block-start)
348 (setf section-start
349 (+ column (posn-index (queued-op-posn op) stream)))))
350 (when (eq op through)
351 (return)))
352 (when insertions
353 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
354 (new-fill-ptr (+ fill-ptr additional))
355 (buffer (pretty-stream-buffer stream))
356 (new-buffer buffer)
357 (length (length buffer))
358 (end fill-ptr))
359 (when (> new-fill-ptr length)
360 (let ((new-length (max (* length 2)
361 (+ fill-ptr
362 (floor (* additional 5) 4)))))
363 (setf new-buffer (make-string new-length))
364 (setf (pretty-stream-buffer stream) new-buffer)))
365 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
366 (decf (pretty-stream-buffer-offset stream) additional)
367 (dolist (insertion insertions)
368 (let* ((srcpos (car insertion))
369 (amount (cdr insertion))
370 (dstpos (+ srcpos additional)))
371 (replace new-buffer buffer :start1 dstpos :start2 srcpos :end2 end)
372 (fill new-buffer #\space :start (- dstpos amount) :end dstpos)
373 (decf additional amount)
374 (setf end srcpos)))
375 (unless (eq new-buffer buffer)
376 (replace new-buffer buffer :end1 end :end2 end))))))
378 ;;;; stuff to do the actual outputting
380 (defun ensure-space-in-buffer (stream want)
381 (declare (type pretty-stream stream)
382 (type index want))
383 (let* ((buffer (pretty-stream-buffer stream))
384 (length (length buffer))
385 (fill-ptr (pretty-stream-buffer-fill-pointer stream))
386 (available (- length fill-ptr)))
387 (cond ((plusp available)
388 available)
389 ((> fill-ptr (pretty-stream-line-length stream))
390 (unless (maybe-output stream nil)
391 (output-partial-line stream))
392 (ensure-space-in-buffer stream want))
394 (let* ((new-length (max (* length 2)
395 (+ length
396 (floor (* want 5) 4))))
397 (new-buffer (make-string new-length)))
398 (setf (pretty-stream-buffer stream) new-buffer)
399 (replace new-buffer buffer :end1 fill-ptr)
400 (- new-length fill-ptr))))))
402 (defun maybe-output (stream force-newlines-p)
403 (declare (type pretty-stream stream))
404 (let ((tail (pretty-stream-queue-tail stream))
405 (output-anything nil))
406 (loop
407 (unless tail
408 (setf (pretty-stream-queue-head stream) nil)
409 (return))
410 (let ((next (pop tail)))
411 (etypecase next
412 (newline
413 (when (ecase (newline-kind next)
414 ((:literal :mandatory :linear) t)
415 (:miser (misering-p stream))
416 (:fill
417 (or (misering-p stream)
418 (> (pretty-stream-line-number stream)
419 (logical-block-section-start-line
420 (first (pretty-stream-blocks stream))))
421 (ecase (fits-on-line-p stream
422 (newline-section-end next)
423 force-newlines-p)
424 ((t) nil)
425 ((nil) t)
426 (:dont-know
427 (return))))))
428 (setf output-anything t)
429 (output-line stream next)))
430 (indentation
431 (unless (misering-p stream)
432 (set-indentation stream
433 (+ (ecase (indentation-kind next)
434 (:block
435 (logical-block-start-column
436 (car (pretty-stream-blocks stream))))
437 (:current
438 (posn-column
439 (indentation-posn next)
440 stream)))
441 (indentation-amount next)))))
442 (block-start
443 (ecase (fits-on-line-p stream (block-start-section-end next)
444 force-newlines-p)
445 ((t)
446 ;; Just nuke the whole logical block and make it look
447 ;; like one nice long literal.
448 (let ((end (block-start-block-end next)))
449 (expand-tabs stream end)
450 (setf tail (cdr (member end tail)))))
451 ((nil)
452 (really-start-logical-block
453 stream
454 (posn-column (block-start-posn next) stream)
455 (block-start-prefix next)
456 (block-start-suffix next)))
457 (:dont-know
458 (return))))
459 (block-end
460 (really-end-logical-block stream))
461 (tab
462 (expand-tabs stream next))))
463 (setf (pretty-stream-queue-tail stream) tail))
464 output-anything))
466 (defun misering-p (stream)
467 (declare (type pretty-stream stream))
468 (and *print-miser-width*
469 (<= (- (pretty-stream-line-length stream)
470 (logical-block-start-column (car (pretty-stream-blocks stream))))
471 *print-miser-width*)))
473 (defun fits-on-line-p (stream until force-newlines-p)
474 (let ((available (pretty-stream-line-length stream)))
475 (when (and (not *print-readably*)
476 (pretty-stream-print-lines stream)
477 (= (pretty-stream-print-lines stream)
478 (pretty-stream-line-number stream)))
479 (decf available 3) ; for the `` ..''
480 (decf available (logical-block-suffix-length
481 (car (pretty-stream-blocks stream)))))
482 (cond (until
483 (<= (posn-column (queued-op-posn until) stream) available))
484 (force-newlines-p nil)
485 ((> (index-column (pretty-stream-buffer-fill-pointer stream) stream)
486 available)
487 nil)
489 :dont-know))))
491 (defun output-line (stream until)
492 (declare (type pretty-stream stream)
493 (type newline until))
494 (let* ((target (pretty-stream-target stream))
495 (buffer (pretty-stream-buffer stream))
496 (kind (newline-kind until))
497 (literal-p (eq kind :literal))
498 (amount-to-consume (posn-index (newline-posn until) stream))
499 (amount-to-print
500 (if literal-p
501 amount-to-consume
502 (let ((last-non-blank
503 (position #\space buffer :end amount-to-consume
504 :from-end t :test #'char/=)))
505 (if last-non-blank
506 (1+ last-non-blank)
507 0)))))
508 (write-string buffer target :end amount-to-print)
509 (let ((line-number (pretty-stream-line-number stream)))
510 (incf line-number)
511 (when (and (not *print-readably*)
512 (pretty-stream-print-lines stream)
513 (>= line-number (pretty-stream-print-lines stream)))
514 (write-string " .." target)
515 (let ((suffix-length (logical-block-suffix-length
516 (car (pretty-stream-blocks stream)))))
517 (unless (zerop suffix-length)
518 (let* ((suffix (pretty-stream-suffix stream))
519 (len (length suffix)))
520 (write-string suffix target
521 :start (- len suffix-length)
522 :end len))))
523 (throw 'line-limit-abbreviation-happened t))
524 (setf (pretty-stream-line-number stream) line-number)
525 (write-char #\newline target)
526 (setf (pretty-stream-buffer-start-column stream) 0)
527 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
528 (block (first (pretty-stream-blocks stream)))
529 (prefix-len
530 (if literal-p
531 (logical-block-per-line-prefix-end block)
532 (logical-block-prefix-length block)))
533 (shift (- amount-to-consume prefix-len))
534 (new-fill-ptr (- fill-ptr shift))
535 (new-buffer buffer)
536 (buffer-length (length buffer)))
537 (when (> new-fill-ptr buffer-length)
538 (setf new-buffer
539 (make-string (max (* buffer-length 2)
540 (+ buffer-length
541 (floor (* (- new-fill-ptr buffer-length)
543 4)))))
544 (setf (pretty-stream-buffer stream) new-buffer))
545 (replace new-buffer buffer
546 :start1 prefix-len :start2 amount-to-consume :end2 fill-ptr)
547 (replace new-buffer (pretty-stream-prefix stream)
548 :end1 prefix-len)
549 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
550 (incf (pretty-stream-buffer-offset stream) shift)
551 (unless literal-p
552 (setf (logical-block-section-column block) prefix-len)
553 (setf (logical-block-section-start-line block) line-number))))))
555 (defun output-partial-line (stream)
556 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
557 (tail (pretty-stream-queue-tail stream))
558 (count
559 (if tail
560 (posn-index (queued-op-posn (car tail)) stream)
561 fill-ptr))
562 (new-fill-ptr (- fill-ptr count))
563 (buffer (pretty-stream-buffer stream)))
564 (when (zerop count)
565 (error "Output-partial-line called when nothing can be output."))
566 (write-string buffer (pretty-stream-target stream)
567 :start 0 :end count)
568 (incf (pretty-stream-buffer-start-column stream) count)
569 (replace buffer buffer :end1 new-fill-ptr :start2 count :end2 fill-ptr)
570 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
571 (incf (pretty-stream-buffer-offset stream) count)))
573 (defun force-pretty-output (stream)
574 (maybe-output stream nil)
575 (expand-tabs stream nil)
576 (write-string (pretty-stream-buffer stream)
577 (pretty-stream-target stream)
578 :end (pretty-stream-buffer-fill-pointer stream)))
580 ;;;; user interface to the pretty printer
582 (defun pprint-newline (kind &optional stream)
583 #!+sb-doc
584 "Output a conditional newline to STREAM (which defaults to
585 *STANDARD-OUTPUT*) if it is a pretty-printing stream, and do
586 nothing if not. KIND can be one of:
587 :LINEAR - A line break is inserted if and only if the immediately
588 containing section cannot be printed on one line.
589 :MISER - Same as LINEAR, but only if ``miser-style'' is in effect.
590 (See *PRINT-MISER-WIDTH*.)
591 :FILL - A line break is inserted if and only if either:
592 (a) the following section cannot be printed on the end of the
593 current line,
594 (b) the preceding section was not printed on a single line, or
595 (c) the immediately containing section cannot be printed on one
596 line and miser-style is in effect.
597 :MANDATORY - A line break is always inserted.
598 When a line break is inserted by any type of conditional newline, any
599 blanks that immediately precede the conditional newline are omitted
600 from the output and indentation is introduced at the beginning of the
601 next line. (See PPRINT-INDENT.)"
602 (declare (type (member :linear :miser :fill :mandatory) kind)
603 (type stream-designator stream)
604 (values null))
605 (let ((stream (out-synonym-of stream)))
606 (when (print-pretty-on-stream-p stream)
607 (enqueue-newline stream kind)))
608 nil)
610 (defun pprint-indent (relative-to n &optional stream)
611 #!+sb-doc
612 "Specify the indentation to use in the current logical block if
613 STREAM \(which defaults to *STANDARD-OUTPUT*) is a pretty-printing
614 stream and do nothing if not. (See PPRINT-LOGICAL-BLOCK.) N is the
615 indentation to use (in ems, the width of an ``m'') and RELATIVE-TO can
616 be either:
618 :BLOCK - Indent relative to the column the current logical block
619 started on.
621 :CURRENT - Indent relative to the current column.
623 The new indentation value does not take effect until the following
624 line break."
625 (declare (type (member :block :current) relative-to)
626 (type real n)
627 (type stream-designator stream)
628 (values null))
629 (let ((stream (out-synonym-of stream)))
630 (when (print-pretty-on-stream-p stream)
631 (enqueue-indent stream relative-to (truncate n))))
632 nil)
634 (defun pprint-tab (kind colnum colinc &optional stream)
635 #!+sb-doc
636 "If STREAM (which defaults to *STANDARD-OUTPUT*) is a pretty-printing
637 stream, perform tabbing based on KIND, otherwise do nothing. KIND can
638 be one of:
639 :LINE - Tab to column COLNUM. If already past COLNUM tab to the next
640 multiple of COLINC.
641 :SECTION - Same as :LINE, but count from the start of the current
642 section, not the start of the line.
643 :LINE-RELATIVE - Output COLNUM spaces, then tab to the next multiple of
644 COLINC.
645 :SECTION-RELATIVE - Same as :LINE-RELATIVE, but count from the start
646 of the current section, not the start of the line."
647 (declare (type (member :line :section :line-relative :section-relative) kind)
648 (type unsigned-byte colnum colinc)
649 (type stream-designator stream)
650 (values null))
651 (let ((stream (out-synonym-of stream)))
652 (when (print-pretty-on-stream-p stream)
653 (enqueue-tab stream kind colnum colinc)))
654 nil)
656 (defun pprint-fill (stream list &optional (colon? t) atsign?)
657 #!+sb-doc
658 "Output LIST to STREAM putting :FILL conditional newlines between each
659 element. If COLON? is NIL (defaults to T), then no parens are printed
660 around the output. ATSIGN? is ignored (but allowed so that PPRINT-FILL
661 can be used with the ~/.../ format directive."
662 (declare (ignore atsign?))
663 (pprint-logical-block (stream list
664 :prefix (if colon? "(" "")
665 :suffix (if colon? ")" ""))
666 (pprint-exit-if-list-exhausted)
667 (loop
668 (output-object (pprint-pop) stream)
669 (pprint-exit-if-list-exhausted)
670 (write-char #\space stream)
671 (pprint-newline :fill stream))))
673 (defun pprint-linear (stream list &optional (colon? t) atsign?)
674 #!+sb-doc
675 "Output LIST to STREAM putting :LINEAR conditional newlines between each
676 element. If COLON? is NIL (defaults to T), then no parens are printed
677 around the output. ATSIGN? is ignored (but allowed so that PPRINT-LINEAR
678 can be used with the ~/.../ format directive."
679 (declare (ignore atsign?))
680 (pprint-logical-block (stream list
681 :prefix (if colon? "(" "")
682 :suffix (if colon? ")" ""))
683 (pprint-exit-if-list-exhausted)
684 (loop
685 (output-object (pprint-pop) stream)
686 (pprint-exit-if-list-exhausted)
687 (write-char #\space stream)
688 (pprint-newline :linear stream))))
690 (defun pprint-tabular (stream list &optional (colon? t) atsign? tabsize)
691 #!+sb-doc
692 "Output LIST to STREAM tabbing to the next column that is an even multiple
693 of TABSIZE (which defaults to 16) between each element. :FILL style
694 conditional newlines are also output between each element. If COLON? is
695 NIL (defaults to T), then no parens are printed around the output.
696 ATSIGN? is ignored (but allowed so that PPRINT-TABULAR can be used with
697 the ~/.../ format directive."
698 (declare (ignore atsign?))
699 (pprint-logical-block (stream list
700 :prefix (if colon? "(" "")
701 :suffix (if colon? ")" ""))
702 (pprint-exit-if-list-exhausted)
703 (loop
704 (output-object (pprint-pop) stream)
705 (pprint-exit-if-list-exhausted)
706 (write-char #\space stream)
707 (pprint-tab :section-relative 0 (or tabsize 16) stream)
708 (pprint-newline :fill stream))))
710 ;;;; pprint-dispatch tables
712 (defglobal *standard-pprint-dispatch-table* nil)
713 (defglobal *initial-pprint-dispatch-table* nil)
715 (defstruct (pprint-dispatch-entry
716 (:constructor make-pprint-dispatch-entry (type priority fun test-fn))
717 (:copier nil) (:predicate nil))
718 ;; the type specifier for this entry
719 (type nil :type t :read-only t)
720 ;; a function to test to see whether an object is of this type,
721 ;; either (LAMBDA (OBJ) (TYPEP OBJECT TYPE)) or a builtin predicate.
722 ;; We don't bother computing this for entries in the CONS
723 ;; hash table, because we don't need it.
724 (test-fn nil :type (or function null))
725 ;; the priority for this guy
726 (priority 0 :type real :read-only t)
727 ;; T iff one of the original entries.
728 (initial-p (null *initial-pprint-dispatch-table*) :type boolean :read-only t)
729 ;; and the associated function
730 (fun nil :type callable :read-only t))
731 (def!method print-object ((entry pprint-dispatch-entry) stream)
732 (print-unreadable-object (entry stream :type t)
733 (format stream "type=~S, priority=~S~@[ [initial]~]"
734 (pprint-dispatch-entry-type entry)
735 (pprint-dispatch-entry-priority entry)
736 (pprint-dispatch-entry-initial-p entry))))
738 ;; Return T iff E1 is strictly less preferable than E2.
739 (defun entry< (e1 e2)
740 (declare (type pprint-dispatch-entry e1 e2))
741 (if (pprint-dispatch-entry-initial-p e1)
742 (if (pprint-dispatch-entry-initial-p e2)
743 (< (pprint-dispatch-entry-priority e1)
744 (pprint-dispatch-entry-priority e2))
746 (if (pprint-dispatch-entry-initial-p e2)
748 (< (pprint-dispatch-entry-priority e1)
749 (pprint-dispatch-entry-priority e2)))))
751 ;; Return the predicate for CTYPE, equivalently TYPE-SPEC.
752 ;; This used to involve rewriting into a sexpr if CONS was involved,
753 ;; since it was not an official specifier. But now it is.
754 (defun compute-test-fn (ctype type-spec function)
755 (declare (special sb!c::*backend-type-predicates*))
756 ;; Avoid compiling code for an existing structure predicate
757 (or (and (eq (info :type :kind type-spec) :instance)
758 (let ((layout (info :type :compiler-layout type-spec)))
759 (and layout
760 (let ((info (layout-info layout)))
761 (and info
762 (let ((pred (dd-predicate-name info)))
763 (and pred (fboundp pred)
764 (symbol-function pred))))))))
765 ;; avoid compiling code for CONS, ARRAY, VECTOR, etc
766 (awhen (assoc ctype sb!c::*backend-type-predicates* :test #'type=)
767 (symbol-function (cdr it)))
768 ;; OK, compile something
769 (let ((name
770 ;; Keep name as a string, because NAMED-LAMBDA with a symbol
771 ;; affects the global environment, when all you want
772 ;; is to give the lambda a human-readable label.
773 (format nil "~A-P"
774 (cond ((symbolp type-spec) type-spec)
775 ((symbolp function) function)
776 ((%fun-name function))
778 (write-to-string type-spec :pretty nil :escape nil
779 :readably nil))))))
780 (compile nil
781 `(named-lambda ,name (object) (typep object ',type-spec))))))
783 (defun copy-pprint-dispatch (&optional (table *print-pprint-dispatch*))
784 (declare (type (or pprint-dispatch-table null) table))
785 (let* ((orig (or table *initial-pprint-dispatch-table*))
786 (new (make-pprint-dispatch-table
787 (copy-list (pprint-dispatch-table-entries orig)))))
788 (replace/eql-hash-table (pprint-dispatch-table-cons-entries new)
789 (pprint-dispatch-table-cons-entries orig))
790 new))
792 (defun pprint-dispatch (object &optional (table *print-pprint-dispatch*))
793 (declare (type (or pprint-dispatch-table null) table))
794 (let* ((table (or table *initial-pprint-dispatch-table*))
795 (cons-entry
796 (and (consp object)
797 (gethash (car object)
798 (pprint-dispatch-table-cons-entries table))))
799 (entry
800 (dolist (entry (pprint-dispatch-table-entries table) cons-entry)
801 (when (and cons-entry
802 (entry< entry cons-entry))
803 (return cons-entry))
804 (when (funcall (pprint-dispatch-entry-test-fn entry) object)
805 (return entry)))))
806 (if entry
807 (values (pprint-dispatch-entry-fun entry) t)
808 (values #'output-ugly-object nil))))
810 (defun assert-not-standard-pprint-dispatch-table (pprint-dispatch operation)
811 (when (eq pprint-dispatch *standard-pprint-dispatch-table*)
812 (cerror "Frob it anyway!" 'standard-pprint-dispatch-table-modified-error
813 :operation operation)))
815 (defun defer-type-checker (entry)
816 (let ((saved-nonce sb!c::*type-cache-nonce*))
817 (lambda (obj)
818 (let ((nonce sb!c::*type-cache-nonce*))
819 (if (eq nonce saved-nonce)
821 (let ((ctype (specifier-type (pprint-dispatch-entry-type entry))))
822 (setq saved-nonce nonce)
823 (if (testable-type-p ctype)
824 (funcall (setf (pprint-dispatch-entry-test-fn entry)
825 (compute-test-fn
826 ctype
827 (pprint-dispatch-entry-type entry)
828 (pprint-dispatch-entry-fun entry)))
829 obj)
830 nil)))))))
832 ;; The dispatch mechanism is not quite sophisticated enough to have a guard
833 ;; condition on CONS entries. One place this would impact is that you could
834 ;; write the full matcher for QUOTE as just a type-specifier. It can be done
835 ;; now, but using the non-cons table entails linear scan.
836 ;; A test-fn in the cons table would require storing multiple entries per
837 ;; key though because any might fail. Conceivably you could have
838 ;; (cons (eql foo) cons) and (cons (eql foo) bit-vector) as two FOO entries.
839 (defun set-pprint-dispatch (type function &optional
840 (priority 0) (table *print-pprint-dispatch*))
841 (declare (type (or null callable) function)
842 (type real priority)
843 (type pprint-dispatch-table table))
844 (declare (explicit-check))
845 (/show0 "entering SET-PPRINT-DISPATCH, TYPE=...")
846 (/hexstr type)
847 (assert-not-standard-pprint-dispatch-table table 'set-pprint-dispatch)
848 (let* ((ctype (or (handler-bind
849 ((parse-unknown-type
850 (lambda (c)
851 (warn "~S is not a recognized type specifier"
852 (parse-unknown-type-specifier c)))))
853 (sb!c::careful-specifier-type type))
854 (error "~S is not a valid type-specifier" type)))
855 (consp (and (cons-type-p ctype)
856 (eq (cons-type-cdr-type ctype) *universal-type*)
857 (member-type-p (cons-type-car-type ctype))))
858 (disabled-p (not (testable-type-p ctype)))
859 (entry (if function
860 (make-pprint-dispatch-entry
861 type priority function
862 (unless (or consp disabled-p)
863 (compute-test-fn ctype type function))))))
864 (when (and function disabled-p)
865 ;; a DISABLED-P test function has to close over the ENTRY
866 (setf (pprint-dispatch-entry-test-fn entry) (defer-type-checker entry)))
867 (if consp
868 (let ((hashtable (pprint-dispatch-table-cons-entries table)))
869 (dolist (key (member-type-members (cons-type-car-type ctype)))
870 (if function
871 (setf (gethash key hashtable) entry)
872 (remhash key hashtable))))
873 (setf (pprint-dispatch-table-entries table)
874 (let ((list (delete type (pprint-dispatch-table-entries table)
875 :key #'pprint-dispatch-entry-type
876 :test #'equal)))
877 (if function
878 ;; ENTRY< is T if lower in priority, which should sort to
879 ;; the end, but MERGE's predicate wants T for the (a,b) pair
880 ;; if 'a' should go in front of 'b', so swap them.
881 ;; (COMPLEMENT #'entry<) is unstable wrt insertion order.
882 (merge 'list list (list entry) (lambda (a b) (entry< b a)))
883 list)))))
884 (/show0 "about to return NIL from SET-PPRINT-DISPATCH")
885 nil)
887 ;;;; standard pretty-printing routines
889 (defun pprint-array (stream array)
890 (cond ((and (null *print-array*) (null *print-readably*))
891 (output-ugly-object stream array))
892 ((and *print-readably*
893 (not (array-readably-printable-p array)))
894 (if *read-eval*
895 (if (vectorp array)
896 (sb!impl::output-unreadable-vector-readably array stream)
897 (sb!impl::output-unreadable-array-readably array stream))
898 (print-not-readable-error array stream)))
899 ((vectorp array)
900 (pprint-vector stream array))
902 (pprint-multi-dim-array stream array))))
904 (defun pprint-vector (stream vector)
905 (pprint-logical-block (stream nil :prefix "#(" :suffix ")")
906 (dotimes (i (length vector))
907 (unless (zerop i)
908 (format stream " ~:_"))
909 (pprint-pop)
910 (output-object (aref vector i) stream))))
912 (defun pprint-multi-dim-array (stream array)
913 (funcall (formatter "#~DA") stream (array-rank array))
914 (with-array-data ((data array) (start) (end))
915 (declare (ignore end))
916 (labels ((output-guts (stream index dimensions)
917 (if (null dimensions)
918 (output-object (aref data index) stream)
919 (pprint-logical-block
920 (stream nil :prefix "(" :suffix ")")
921 (let ((dim (car dimensions)))
922 (unless (zerop dim)
923 (let* ((dims (cdr dimensions))
924 (index index)
925 (step (reduce #'* dims))
926 (count 0))
927 (loop
928 (pprint-pop)
929 (output-guts stream index dims)
930 (when (= (incf count) dim)
931 (return))
932 (write-char #\space stream)
933 (pprint-newline (if dims :linear :fill)
934 stream)
935 (incf index step)))))))))
936 (output-guts stream start (array-dimensions array)))))
938 (defun pprint-lambda-list (stream lambda-list &rest noise)
939 (declare (ignore noise))
940 (pprint-logical-block (stream lambda-list :prefix "(" :suffix ")")
941 (let ((state :required)
942 (first t))
943 (loop
944 (pprint-exit-if-list-exhausted)
945 (unless first
946 (write-char #\space stream))
947 (let ((arg (pprint-pop)))
948 (case arg
949 ((&optional &aux)
950 (setf state :optional)
951 (pprint-newline :linear stream))
952 ((&rest &body)
953 (setf state :required)
954 (pprint-newline :linear stream))
955 (&key
956 (setf state :key)
957 (pprint-newline :linear stream))
959 (pprint-newline :fill stream)))
960 (ecase state
961 (:required
962 ;; Make sure method specializers like
963 ;; (function (eql #'foo)) are printed right
964 (pprint-logical-block
965 (stream arg :prefix "(" :suffix ")")
966 (pprint-exit-if-list-exhausted)
967 (loop
968 (output-object (pprint-pop) stream)
969 (pprint-exit-if-list-exhausted)
970 (write-char #\space stream)
971 (pprint-newline :linear stream))))
972 ((:optional :key)
973 (pprint-logical-block
974 (stream arg :prefix "(" :suffix ")")
975 (pprint-exit-if-list-exhausted)
976 (if (eq state :key)
977 (pprint-logical-block
978 (stream (pprint-pop) :prefix "(" :suffix ")")
979 (pprint-exit-if-list-exhausted)
980 (output-object (pprint-pop) stream)
981 (pprint-exit-if-list-exhausted)
982 (write-char #\space stream)
983 (pprint-newline :fill stream)
984 (output-object (pprint-pop) stream)
985 (loop
986 (pprint-exit-if-list-exhausted)
987 (write-char #\space stream)
988 (pprint-newline :fill stream)
989 (output-object (pprint-pop) stream)))
990 (output-object (pprint-pop) stream))
991 (loop
992 (pprint-exit-if-list-exhausted)
993 (write-char #\space stream)
994 (pprint-newline :linear stream)
995 (output-object (pprint-pop) stream))))))
996 (setf first nil)))))
998 (defun pprint-lambda (stream list &rest noise)
999 (declare (ignore noise))
1000 (funcall (formatter
1001 ;; KLUDGE: This format string, and other format strings which also
1002 ;; refer to SB!PRETTY, rely on the current SBCL not-quite-ANSI
1003 ;; behavior of FORMATTER in order to make code which survives the
1004 ;; transition when SB!PRETTY is renamed to SB-PRETTY after cold
1005 ;; init. (ANSI says that the FORMATTER functions should be
1006 ;; equivalent to the format string, but the SBCL FORMATTER
1007 ;; functions contain references to package objects, not package
1008 ;; names, so they keep right on going if the packages are renamed.)
1009 ;; If our FORMATTER behavior is ever made more compliant, the code
1010 ;; here will have to change. -- WHN 19991207
1011 "~:<~^~W~^~3I ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1012 stream
1013 list))
1015 (defun pprint-block (stream list &rest noise)
1016 (declare (ignore noise))
1017 (funcall (formatter "~:<~^~W~^~3I ~:_~W~1I~@{ ~_~W~}~:>") stream list))
1019 (defun pprint-flet (stream list &rest noise)
1020 (declare (ignore noise))
1021 (if (and (consp list)
1022 (consp (cdr list))
1023 (cddr list)
1024 ;; Filter out (FLET FOO :IN BAR) names.
1025 (and (consp (cddr list))
1026 (not (eq :in (third list)))))
1027 (funcall (formatter
1028 "~:<~^~W~^ ~@_~:<~@{~:<~^~W~^~3I ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~:@_~@{~W~^ ~_~}~:>~^ ~_~}~:>~1I~@:_~@{~W~^ ~_~}~:>")
1029 stream
1030 list)
1031 ;; for printing function names like (flet foo)
1032 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1033 (pprint-exit-if-list-exhausted)
1034 (write (pprint-pop) :stream stream)
1035 (loop
1036 (pprint-exit-if-list-exhausted)
1037 (write-char #\space stream)
1038 (write (pprint-pop) :stream stream)))))
1040 (defun pprint-let (stream list &rest noise)
1041 (declare (ignore noise))
1042 (funcall (formatter "~:<~^~W~^ ~@_~:<~@{~:<~^~W~@{ ~_~W~}~:>~^ ~_~}~:>~1I~:@_~@{~W~^ ~_~}~:>")
1043 stream
1044 list))
1046 (defun pprint-progn (stream list &rest noise)
1047 (declare (ignore noise))
1048 (pprint-linear stream list))
1050 (defun pprint-progv (stream list &rest noise)
1051 (declare (ignore noise))
1052 (funcall (formatter "~:<~^~W~^~3I ~_~W~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1053 stream list))
1055 (defun pprint-prog2 (stream list &rest noise)
1056 (declare (ignore noise))
1057 (funcall (formatter "~:<~^~W~^~3I ~:_~W~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1058 stream list))
1060 (defun pprint-unquoting-comma (stream obj &rest noise)
1061 (declare (ignore noise))
1062 (write-string (svref #("," ",." ",@") (comma-kind obj)) stream)
1063 (when (eql (comma-kind obj) 0)
1064 ;; Ensure a space is written before any output that would change the meaning
1065 ;; of the preceding the comma to ",." or ",@" such as a symbol named "@BAR".
1066 (setf (pretty-stream-char-out-oneshot-hook stream)
1067 (lambda (stream char)
1068 (when (member char '(#\. #\@))
1069 (write-char #\Space stream)))))
1070 (output-object (comma-expr obj) stream))
1072 (defvar *pprint-quote-with-syntactic-sugar* t)
1074 (defun pprint-quote (stream list &rest noise)
1075 (declare (ignore noise))
1076 (when (and (listp list) (singleton-p (cdr list)))
1077 (let* ((pretty-p nil)
1078 (sigil (case (car list)
1079 (function "#'")
1080 ;; QUASIQUOTE can't choose not to print prettily.
1081 ;; Wrongly nested commas beget unreadable sexprs.
1082 (quasiquote (setq pretty-p t) "`")
1083 (t "'")))) ; ordinary QUOTE
1084 (when (or pretty-p *pprint-quote-with-syntactic-sugar*)
1085 (write-string sigil stream)
1086 (return-from pprint-quote (output-object (cadr list) stream)))))
1087 (pprint-fill stream list))
1089 (defun pprint-declare (stream list &rest noise)
1090 (declare (ignore noise))
1091 ;; Make sure to print (DECLARE (FUNCTION F)) not (DECLARE #'A).
1092 (let ((*pprint-quote-with-syntactic-sugar* nil))
1093 (pprint-spread-fun-call stream list)))
1095 ;;; Try to print every variable-value pair on one line; if that doesn't
1096 ;;; work print the value indented by 2 spaces:
1098 ;;; (setq foo bar
1099 ;;; quux xoo)
1100 ;;; vs.
1101 ;;; (setf foo
1102 ;;; (long form ...)
1103 ;;; quux xoo)
1104 (defun pprint-setq (stream list &rest noise)
1105 (declare (ignore noise))
1106 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1107 (pprint-exit-if-list-exhausted)
1108 (output-object (pprint-pop) stream)
1109 (pprint-exit-if-list-exhausted)
1110 (write-char #\space stream)
1111 (unless (listp (cdr list))
1112 (write-string ". " stream))
1113 (pprint-newline :miser stream)
1114 (pprint-logical-block (stream (cdr list) :prefix "" :suffix "")
1115 (loop
1116 (pprint-indent :block 2 stream)
1117 (output-object (pprint-pop) stream)
1118 (pprint-exit-if-list-exhausted)
1119 (write-char #\space stream)
1120 (pprint-newline :fill stream)
1121 (pprint-indent :block 0 stream)
1122 (output-object (pprint-pop) stream)
1123 (pprint-exit-if-list-exhausted)
1124 (write-char #\space stream)
1125 (pprint-newline :mandatory stream)))))
1127 (eval-when (:compile-toplevel :execute)
1128 (sb!xc:defmacro pprint-tagbody-guts (stream)
1129 `(loop
1130 (pprint-exit-if-list-exhausted)
1131 (write-char #\space ,stream)
1132 (let ((form-or-tag (pprint-pop)))
1133 (pprint-indent :block
1134 (if (atom form-or-tag) 0 1)
1135 ,stream)
1136 (pprint-newline :linear ,stream)
1137 (output-object form-or-tag ,stream)))))
1139 (defun pprint-tagbody (stream list &rest noise)
1140 (declare (ignore noise))
1141 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1142 (pprint-exit-if-list-exhausted)
1143 (output-object (pprint-pop) stream)
1144 (pprint-tagbody-guts stream)))
1146 (defun pprint-case (stream list &rest noise)
1147 (declare (ignore noise))
1148 (funcall (formatter
1149 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~:/SB!PRETTY:PPRINT-FILL/~^~@{ ~_~W~}~:>~}~:>")
1150 stream
1151 list))
1153 (defun pprint-defun (stream list &rest noise)
1154 (declare (ignore noise))
1155 (funcall (formatter
1156 "~:<~^~W~^ ~@_~:I~W~^ ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1157 stream
1158 list))
1160 (defun pprint-defmethod (stream list &rest noise)
1161 (declare (ignore noise))
1162 (if (and (consp (cdr list))
1163 (consp (cddr list))
1164 (consp (third list)))
1165 (pprint-defun stream list)
1166 (funcall (formatter
1167 "~:<~^~W~^ ~@_~:I~W~^ ~W~^ ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1168 stream
1169 list)))
1171 (defun pprint-defpackage (stream list &rest noise)
1172 (declare (ignore noise))
1173 (funcall (formatter
1174 "~:<~W~^ ~3I~:_~W~^~1I~@{~:@_~:<~^~W~^ ~:I~@_~@{~W~^ ~_~}~:>~}~:>")
1175 stream
1176 list))
1178 (defun pprint-destructuring-bind (stream list &rest noise)
1179 (declare (ignore noise))
1180 (funcall (formatter
1181 "~:<~^~W~^~3I ~_~:/SB!PRETTY:PPRINT-LAMBDA-LIST/~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1182 stream list))
1184 (defun pprint-do (stream list &rest noise)
1185 (declare (ignore noise))
1186 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1187 (pprint-exit-if-list-exhausted)
1188 (output-object (pprint-pop) stream)
1189 (pprint-exit-if-list-exhausted)
1190 (write-char #\space stream)
1191 (pprint-indent :current 0 stream)
1192 (funcall (formatter "~:<~@{~:<~^~W~^ ~@_~:I~W~@{ ~_~W~}~:>~^~:@_~}~:>")
1193 stream
1194 (pprint-pop))
1195 (pprint-exit-if-list-exhausted)
1196 (write-char #\space stream)
1197 (pprint-newline :linear stream)
1198 (pprint-linear stream (pprint-pop))
1199 (pprint-tagbody-guts stream)))
1201 (defun pprint-dolist (stream list &rest noise)
1202 (declare (ignore noise))
1203 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1204 (pprint-exit-if-list-exhausted)
1205 (output-object (pprint-pop) stream)
1206 (pprint-exit-if-list-exhausted)
1207 (pprint-indent :block 3 stream)
1208 (write-char #\space stream)
1209 (pprint-newline :fill stream)
1210 (funcall (formatter "~:<~^~W~^ ~:_~:I~W~@{ ~_~W~}~:>")
1211 stream
1212 (pprint-pop))
1213 (pprint-tagbody-guts stream)))
1215 (defun pprint-typecase (stream list &rest noise)
1216 (declare (ignore noise))
1217 (funcall (formatter
1218 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~W~^~@{ ~_~W~}~:>~}~:>")
1219 stream
1220 list))
1222 (defun pprint-prog (stream list &rest noise)
1223 (declare (ignore noise))
1224 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1225 (pprint-exit-if-list-exhausted)
1226 (output-object (pprint-pop) stream)
1227 (pprint-exit-if-list-exhausted)
1228 (write-char #\space stream)
1229 (pprint-newline :miser stream)
1230 (pprint-fill stream (pprint-pop))
1231 (pprint-tagbody-guts stream)))
1233 ;;; Each clause in this list will get its own line.
1234 ;;; FIXME: (LOOP for x in list summing (f x) into count finally ...)
1235 ;;; puts a newline in between INTO and COUNT.
1236 ;;; It would be awesome to have code in common with the macro
1237 ;;; the properly represents each clauses.
1238 (defvar *loop-seperating-clauses*
1239 '(:and
1240 :with :for
1241 :initially :finally
1242 :do :doing
1243 :collect :collecting
1244 :append :appending
1245 :nconc :nconcing
1246 :count :counting
1247 :sum :summing
1248 :maximize :maximizing
1249 :minimize :minimizing
1250 :if :when :unless :end
1251 :for :while :until :repeat :always :never :thereis
1254 (defun pprint-extended-loop (stream list)
1255 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1256 (output-object (pprint-pop) stream)
1257 (pprint-exit-if-list-exhausted)
1258 (write-char #\space stream)
1259 (pprint-indent :current 0 stream)
1260 (output-object (pprint-pop) stream)
1261 (pprint-exit-if-list-exhausted)
1262 (write-char #\space stream)
1263 (loop for thing = (pprint-pop)
1264 when (and (symbolp thing)
1265 (member thing *loop-seperating-clauses* :test #'string=))
1266 do (pprint-newline :mandatory stream)
1267 do (output-object thing stream)
1268 do (pprint-exit-if-list-exhausted)
1269 do (write-char #\space stream))))
1271 (defun pprint-loop (stream list &rest noise)
1272 (declare (ignore noise))
1273 (destructuring-bind (loop-symbol . clauses) list
1274 (declare (ignore loop-symbol))
1275 (if (or (atom clauses) (consp (car clauses)))
1276 (pprint-spread-fun-call stream list)
1277 (pprint-extended-loop stream list))))
1279 (defun pprint-if (stream list &rest noise)
1280 (declare (ignore noise))
1281 ;; Indent after the ``predicate'' form, and the ``then'' form.
1282 (funcall (formatter "~:<~^~W~^ ~:I~W~^ ~:@_~@{~W~^ ~:@_~}~:>")
1283 stream
1284 list))
1286 (defun pprint-fun-call (stream list &rest noise)
1287 (declare (ignore noise))
1288 (funcall (formatter "~:<~^~W~^ ~:_~:I~@{~W~^ ~:_~}~:>")
1289 stream
1290 list))
1292 (defun pprint-spread-fun-call (stream list &rest noise)
1293 (declare (ignore noise))
1294 ;; Similiar to PPRINT-FUN-CALL but emit a mandatory newline after
1295 ;; each parameter. I.e. spread out each parameter on its own line.
1296 (funcall (formatter "~:<~^~W~^ ~:_~:I~@{~W~^ ~:@_~}~:>")
1297 stream
1298 list))
1300 (defun pprint-data-list (stream list &rest noise)
1301 (declare (ignore noise))
1302 (pprint-fill stream list))
1304 ;;; Return the number of positional arguments that macro NAME accepts
1305 ;;; by looking for &BODY. A dotted list is indented as it it had &BODY.
1306 ;;; ANSI says that a dotted tail is like &REST, but the pretty-printer
1307 ;;; can do whatever it likes anyway. I happen to think this makes sense.
1308 (defun macro-indentation (name)
1309 (do ((n 0)
1310 (list (%fun-lambda-list (macro-function name)) (cdr list)))
1311 ((or (atom list) (eq (car list) '&body))
1312 (if (null list) nil n))
1313 (unless (eq (car list) '&optional)
1314 (incf n))))
1316 ;;; Pretty-Print macros by looking where &BODY appears in a macro's
1317 ;;; lambda-list.
1318 (defun pprint-macro-call (stream list &rest noise)
1319 (declare (ignore noise))
1320 (let ((indentation (and (car list) (macro-indentation (car list)))))
1321 (unless indentation
1322 (return-from pprint-macro-call
1323 (pprint-fun-call stream list)))
1324 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1325 (output-object (pprint-pop) stream)
1326 (pprint-exit-if-list-exhausted)
1327 (write-char #\space stream)
1328 (loop for indent from 0 below indentation do
1329 (cond
1330 ;; Place the very first argument next to the macro name
1331 ((zerop indent)
1332 (output-object (pprint-pop) stream)
1333 (pprint-exit-if-list-exhausted))
1334 ;; Indent any other non-body argument by the same
1335 ;; amount. It's what Emacs seems to do, too.
1337 (pprint-indent :block 3 stream)
1338 (pprint-newline :mandatory stream)
1339 (output-object (pprint-pop) stream)
1340 (pprint-exit-if-list-exhausted))))
1341 ;; Indent back for the body.
1342 (pprint-indent :block 1 stream)
1343 (pprint-newline :mandatory stream)
1344 (loop
1345 (output-object (pprint-pop) stream)
1346 (pprint-exit-if-list-exhausted)
1347 (pprint-newline :mandatory stream)))))
1349 ;;;; the interface seen by regular (ugly) printer and initialization routines
1351 (eval-when (:compile-toplevel :execute)
1352 (sb!xc:defmacro with-pretty-stream ((stream-var
1353 &optional (stream-expression stream-var))
1354 &body body)
1355 (let ((flet-name (sb!xc:gensym "WITH-PRETTY-STREAM")))
1356 `(flet ((,flet-name (,stream-var)
1357 ,@body))
1358 (let ((stream ,stream-expression))
1359 (if (pretty-stream-p stream)
1360 (,flet-name stream)
1361 (catch 'line-limit-abbreviation-happened
1362 (let ((stream (make-pretty-stream stream)))
1363 (,flet-name stream)
1364 (force-pretty-output stream)))))
1365 nil))))
1367 (defun call-logical-block-printer (proc stream prefix per-line-p suffix
1368 &optional (object nil obj-supplied-p))
1369 ;; PREFIX and SUFFIX will be checked for stringness by START-LOGICAL-BLOCK.
1370 ;; Doing it here would be more strict, but I really don't think it's worth
1371 ;; an extra check. The only observable difference would occur when you have
1372 ;; a non-list object which bypasses START-LOGICAL-BLOCK.
1373 ;; Also, START-LOGICAL-BLOCK could become an FLET inside here.
1374 (declare (function proc))
1375 (with-pretty-stream (stream (out-synonym-of stream))
1376 (if (or (not (listp object)) ; implies obj-supplied-p
1377 (and (eq (car object) 'quasiquote)
1378 ;; We can only bail out from printing this logical block
1379 ;; if the quasiquote printer would *NOT* punt.
1380 ;; If it would punt, then we have to forge ahead.
1381 (singleton-p (cdr object))))
1382 ;; the spec says "If object is not a list, it is printed using WRITE"
1383 ;; but I guess this is close enough.
1384 (output-object object stream)
1385 (dx-let ((state (cons 0 stream)))
1386 (if obj-supplied-p
1387 (with-circularity-detection (object stream)
1388 (descend-into (stream)
1389 (start-logical-block stream prefix per-line-p suffix)
1390 (funcall proc object state stream)
1391 ;; Comment preserved for posterity:
1392 ;; FIXME: Don't we need UNWIND-PROTECT to ensure this
1393 ;; always gets executed?
1394 ;; I think not because I wouldn't characterize this as
1395 ;; "cleanup" code. If and only if you follow the accepted
1396 ;; protocol for defining and using print functions should
1397 ;; the behavior be expected to be reasonable and predictable.
1398 ;; Throwing to LINE-LIMIT-ABBREVIATION-HAPPENED is designed
1399 ;; to do the right thing, and printing should not generally
1400 ;; continue to have side-effects if the user felt it necessary
1401 ;; to nonlocally exit in an unexpected way for other reasons.
1402 (end-logical-block stream)))
1403 (descend-into (stream)
1404 (start-logical-block stream prefix per-line-p suffix)
1405 (funcall proc state stream)
1406 (end-logical-block stream)))))))
1408 ;; Return non-nil if we should keep printing within the logical-block,
1409 ;; or NIL to stop printing due to non-list, length cutoff, or circularity.
1410 (defun pprint-length-check (obj state)
1411 (let ((stream (cdr state)))
1412 (cond ((or (not (listp obj))
1413 ;; Consider (A . `(,B C)) = (A QUASIQUOTE ,B C)
1414 ;; We have to detect this and print as the form on the left,
1415 ;; since pretty commas with no containing #\` will be unreadable
1416 ;; due to a nesting error.
1417 (and (eq (car obj) 'quasiquote) (singleton-p (cdr obj))))
1418 (write-string ". " stream)
1419 (output-object obj stream)
1420 nil)
1421 ((and (not *print-readably*) (eql (car state) *print-length*))
1422 (write-string "..." stream)
1423 nil)
1424 ((and obj
1425 (plusp (car state))
1426 (check-for-circularity obj nil :logical-block))
1427 (write-string ". " stream)
1428 (output-object obj stream)
1429 nil)
1431 (incf (car state))))))
1433 ;; As above, but for logical blocks with an unspecific object.
1434 (defun pprint-length-check* (state)
1435 (let ((stream (cdr state)))
1436 (cond ((and (not *print-readably*) (eql (car state) *print-length*))
1437 (write-string "..." stream)
1438 nil)
1440 (incf (car state))))))
1442 (defun !pprint-cold-init ()
1443 (/show0 "entering !PPRINT-COLD-INIT")
1444 ;; Kludge: We set *STANDARD-PP-D-TABLE* to a new table even though
1445 ;; it's going to be set to a copy of *INITIAL-PP-D-T* below because
1446 ;; it's used in WITH-STANDARD-IO-SYNTAX, and condition reportery
1447 ;; possibly performed in the following extent may use W-S-IO-SYNTAX.
1448 (setf *standard-pprint-dispatch-table* (make-pprint-dispatch-table))
1449 (setf *initial-pprint-dispatch-table* nil)
1450 (let ((*print-pprint-dispatch* (make-pprint-dispatch-table)))
1451 (/show0 "doing SET-PPRINT-DISPATCH for regular types")
1452 (set-pprint-dispatch '(and array (not (or string bit-vector))) 'pprint-array)
1453 ;; MACRO-FUNCTION must have effectively higher priority than FBOUNDP.
1454 ;; The implementation happens to check identical priorities in the order added,
1455 ;; but that's unspecified behavior. Both must be _strictly_ lower than the
1456 ;; default cons entries though.
1457 (set-pprint-dispatch '(cons (and symbol (satisfies macro-function)))
1458 'pprint-macro-call -1)
1459 (set-pprint-dispatch '(cons (and symbol (satisfies fboundp)))
1460 'pprint-fun-call -1)
1461 (set-pprint-dispatch '(cons symbol)
1462 'pprint-data-list -2)
1463 (set-pprint-dispatch 'cons 'pprint-fill -2)
1464 (set-pprint-dispatch 'sb!impl::comma 'pprint-unquoting-comma -3)
1465 ;; cons cells with interesting things for the car
1466 (/show0 "doing SET-PPRINT-DISPATCH for CONS with interesting CAR")
1468 (dolist (magic-form '((lambda pprint-lambda)
1469 ((declare declaim) pprint-declare)
1471 ;; special forms
1472 ((block catch return-from throw eval-when
1473 multiple-value-call multiple-value-prog1
1474 unwind-protect) pprint-block)
1475 ((flet labels macrolet dx-flet) pprint-flet)
1476 ((function quasiquote quote) pprint-quote)
1477 (if pprint-if)
1478 ((let let* symbol-macrolet dx-let) pprint-let)
1479 ((locally progn) pprint-progn)
1480 (progv pprint-progv)
1481 ((setq psetq setf psetf) pprint-setq)
1482 (tagbody pprint-tagbody)
1484 ;; macros
1485 ((case ccase ecase) pprint-case)
1486 ((ctypecase etypecase typecase) pprint-typecase)
1487 ((defconstant defparameter defstruct defvar)
1488 pprint-block)
1489 ((define-modify-macro define-setf-expander
1490 defmacro defsetf deftype defun) pprint-defun)
1491 (defmethod pprint-defmethod)
1492 (defpackage pprint-defpackage)
1493 (destructuring-bind pprint-destructuring-bind)
1494 ((do do*) pprint-do)
1495 ((do-all-symbols do-external-symbols do-symbols
1496 dolist dotimes) pprint-dolist)
1497 #+nil (handler-bind ...)
1498 #+nil (handler-case ...)
1499 (loop pprint-loop)
1500 ((multiple-value-bind prog2) pprint-prog2)
1501 ((multiple-value-setq prog1 pprint-logical-block
1502 print-unreadable-object prog1) pprint-block)
1503 ((prog prog*) pprint-prog)
1504 #+nil (restart-bind ...)
1505 #+nil (restart-case ...)
1506 ((step time) pprint-progn)
1507 ((unless when) pprint-block)
1508 #+nil (with-condition-restarts ...)
1509 ((with-compilation-unit with-simple-restart
1510 with-hash-table-iterator with-package-iterator
1511 with-input-from-string with-output-to-string
1512 with-open-file with-open-stream) pprint-block)
1513 (with-standard-io-syntax pprint-progn)))
1515 ;; Grouping some symbols together in the above list looks pretty.
1516 ;; The sharing of dispatch entries is inconsequential.
1517 (set-pprint-dispatch `(cons (member ,@(ensure-list (first magic-form))))
1518 (second magic-form)))
1519 (setf *initial-pprint-dispatch-table* *print-pprint-dispatch*))
1521 (setf *standard-pprint-dispatch-table*
1522 (copy-pprint-dispatch *initial-pprint-dispatch-table*))
1523 (setf *print-pprint-dispatch*
1524 (copy-pprint-dispatch *initial-pprint-dispatch-table*))
1525 (setf *print-pretty* t))