1 ;;;; Common Lisp pretty printer
3 ;;;; This software is part of the SBCL system. See the README file for
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
)
18 (+ index
(pretty-stream-buffer-offset stream
)))
19 (defun posn-index (posn stream
)
20 (declare (type posn posn
) (type pretty-stream stream
)
22 (- posn
(pretty-stream-buffer-offset stream
)))
23 (defun posn-column (posn stream
)
24 (declare (type posn posn
) (type pretty-stream stream
)
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
)
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
)))
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
)
55 (type (or index null
) end
))
56 (let* ((end (or end
(length string
))))
58 (sb!impl
::string-dispatch
(simple-base-string
60 (simple-array character
(*)))
62 ;; For POSITION transform
63 (declare (optimize (speed 2)))
64 (let ((f (pretty-stream-char-out-oneshot-hook stream
)))
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
)))
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
)))
77 (let* ((available (ensure-space-in-buffer stream chars
))
78 (count (min available chars
))
79 (fill-pointer (pretty-stream-buffer-fill-pointer
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"
88 (loop for i from fill-pointer below new-fill-ptr
90 with target
= (pretty-stream-buffer stream
)
91 do
(setf (aref target i
)
93 (replace (pretty-stream-buffer stream
)
95 :start1 fill-pointer
:end1 new-fill-ptr
97 (setf (pretty-stream-buffer-fill-pointer stream
)
102 (incf start count
)))))))))))
104 (defun pretty-misc (stream op
&optional arg1 arg2
)
105 (declare (ignore stream op arg1 arg2
)))
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
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
)
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
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
)
140 (setf (logical-block-per-line-prefix-end block
) column
)
141 (replace (pretty-stream-prefix stream
) prefix
142 :start1
(- column
(length prefix
)) :end1 column
))
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)
152 (floor (* additional
5) 4)))))
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
))))
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
)
174 (replace (make-string (max (* prefix-len
2)
176 (floor (* (- column prefix-len
) 5)
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
)))
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
202 (pretty-stream-buffer-fill-pointer
207 (head `(pretty-stream-queue-head ,stream
)))
210 (setf (cdr ,head
) ,op
)
211 (setf (pretty-stream-queue-tail ,stream
) ,op
))
212 (setf (pretty-stream-queue-head ,stream
) ,op
)
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
)
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
)
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
))
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
)
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
)))
267 (pretty-sout stream suffix
0 (length suffix
)))
268 (setf (block-start-block-end start
) end
)))
270 (defstruct (tab (:include queued-op
)
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
)
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
)))
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
)))
299 (incf colnum
(- colinc rem
))))))
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
)
322 (posn-index (tab-posn op
)
324 ((or newline block-start
)
326 (+ column
(posn-index (queued-op-posn op
)
330 (defun expand-tabs (stream through
)
331 (let ((insertions nil
)
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
))
339 (let* ((index (posn-index (tab-posn op
) stream
))
340 (tabsize (compute-tab-size op
343 (unless (zerop tabsize
)
344 (push (cons index tabsize
) insertions
)
345 (incf additional tabsize
)
346 (incf column tabsize
))))
347 ((or newline block-start
)
349 (+ column
(posn-index (queued-op-posn op
) stream
)))))
350 (when (eq op through
)
353 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream
))
354 (new-fill-ptr (+ fill-ptr additional
))
355 (buffer (pretty-stream-buffer stream
))
357 (length (length buffer
))
359 (when (> new-fill-ptr length
)
360 (let ((new-length (max (* length
2)
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
)
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
)
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
)
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)
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
))
408 (setf (pretty-stream-queue-head stream
) nil
)
410 (let ((next (pop tail
)))
413 (when (ecase (newline-kind next
)
414 ((:literal
:mandatory
:linear
) t
)
415 (:miser
(misering-p stream
))
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
)
428 (setf output-anything t
)
429 (output-line stream next
)))
431 (unless (misering-p stream
)
432 (set-indentation stream
433 (+ (ecase (indentation-kind next
)
435 (logical-block-start-column
436 (car (pretty-stream-blocks stream
))))
439 (indentation-posn next
)
441 (indentation-amount next
)))))
443 (ecase (fits-on-line-p stream
(block-start-section-end next
)
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
)))))
452 (really-start-logical-block
454 (posn-column (block-start-posn next
) stream
)
455 (block-start-prefix next
)
456 (block-start-suffix next
)))
460 (really-end-logical-block stream
))
462 (expand-tabs stream next
))))
463 (setf (pretty-stream-queue-tail stream
) tail
))
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
)))))
483 (<= (posn-column (queued-op-posn until
) stream
) available
))
484 (force-newlines-p nil
)
485 ((> (index-column (pretty-stream-buffer-fill-pointer stream
) stream
)
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
))
502 (let ((last-non-blank
503 (position #\space buffer
:end amount-to-consume
504 :from-end t
:test
#'char
/=)))
508 (write-string buffer target
:end amount-to-print
)
509 (let ((line-number (pretty-stream-line-number stream
)))
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
)
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
)))
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
))
536 (buffer-length (length buffer
)))
537 (when (> new-fill-ptr buffer-length
)
539 (make-string (max (* buffer-length
2)
541 (floor (* (- new-fill-ptr buffer-length
)
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
)
549 (setf (pretty-stream-buffer-fill-pointer stream
) new-fill-ptr
)
550 (incf (pretty-stream-buffer-offset stream
) shift
)
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
))
560 (posn-index (queued-op-posn (car tail
)) stream
)
562 (new-fill-ptr (- fill-ptr count
))
563 (buffer (pretty-stream-buffer stream
)))
565 (error "Output-partial-line called when nothing can be output."))
566 (write-string buffer
(pretty-stream-target stream
)
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 "Output a conditional newline to STREAM (which defaults to
584 *STANDARD-OUTPUT*) if it is a pretty-printing stream, and do
585 nothing if not. KIND can be one of:
586 :LINEAR - A line break is inserted if and only if the immediately
587 containing section cannot be printed on one line.
588 :MISER - Same as LINEAR, but only if ``miser-style'' is in effect.
589 (See *PRINT-MISER-WIDTH*.)
590 :FILL - A line break is inserted if and only if either:
591 (a) the following section cannot be printed on the end of the
593 (b) the preceding section was not printed on a single line, or
594 (c) the immediately containing section cannot be printed on one
595 line and miser-style is in effect.
596 :MANDATORY - A line break is always inserted.
597 When a line break is inserted by any type of conditional newline, any
598 blanks that immediately precede the conditional newline are omitted
599 from the output and indentation is introduced at the beginning of the
600 next line. (See PPRINT-INDENT.)"
601 (declare (type (member :linear
:miser
:fill
:mandatory
) kind
)
602 (type stream-designator stream
)
604 (let ((stream (out-stream-from-designator stream
)))
605 (when (print-pretty-on-stream-p stream
)
606 (enqueue-newline stream kind
)))
609 (defun pprint-indent (relative-to n
&optional stream
)
610 "Specify the indentation to use in the current logical block if
611 STREAM \(which defaults to *STANDARD-OUTPUT*) is a pretty-printing
612 stream and do nothing if not. (See PPRINT-LOGICAL-BLOCK.) N is the
613 indentation to use (in ems, the width of an ``m'') and RELATIVE-TO can
616 :BLOCK - Indent relative to the column the current logical block
619 :CURRENT - Indent relative to the current column.
621 The new indentation value does not take effect until the following
623 (declare (type (member :block
:current
) relative-to
)
625 (type stream-designator stream
)
627 (let ((stream (out-stream-from-designator stream
)))
628 (when (print-pretty-on-stream-p stream
)
629 (enqueue-indent stream relative-to
(truncate n
))))
632 (defun pprint-tab (kind colnum colinc
&optional stream
)
633 "If STREAM (which defaults to *STANDARD-OUTPUT*) is a pretty-printing
634 stream, perform tabbing based on KIND, otherwise do nothing. KIND can
636 :LINE - Tab to column COLNUM. If already past COLNUM tab to the next
638 :SECTION - Same as :LINE, but count from the start of the current
639 section, not the start of the line.
640 :LINE-RELATIVE - Output COLNUM spaces, then tab to the next multiple of
642 :SECTION-RELATIVE - Same as :LINE-RELATIVE, but count from the start
643 of the current section, not the start of the line."
644 (declare (type (member :line
:section
:line-relative
:section-relative
) kind
)
645 (type unsigned-byte colnum colinc
)
646 (type stream-designator stream
)
648 (let ((stream (out-stream-from-designator stream
)))
649 (when (print-pretty-on-stream-p stream
)
650 (enqueue-tab stream kind colnum colinc
)))
653 (defun pprint-fill (stream list
&optional
(colon? t
) atsign?
)
654 "Output LIST to STREAM putting :FILL conditional newlines between each
655 element. If COLON? is NIL (defaults to T), then no parens are printed
656 around the output. ATSIGN? is ignored (but allowed so that PPRINT-FILL
657 can be used with the ~/.../ format directive."
658 (declare (ignore atsign?
))
659 (pprint-logical-block (stream list
660 :prefix
(if colon?
"(" "")
661 :suffix
(if colon?
")" ""))
662 (pprint-exit-if-list-exhausted)
664 (output-object (pprint-pop) stream
)
665 (pprint-exit-if-list-exhausted)
666 (write-char #\space stream
)
667 (pprint-newline :fill stream
))))
669 (defun pprint-linear (stream list
&optional
(colon? t
) atsign?
)
670 "Output LIST to STREAM putting :LINEAR conditional newlines between each
671 element. If COLON? is NIL (defaults to T), then no parens are printed
672 around the output. ATSIGN? is ignored (but allowed so that PPRINT-LINEAR
673 can be used with the ~/.../ format directive."
674 (declare (ignore atsign?
))
675 (pprint-logical-block (stream list
676 :prefix
(if colon?
"(" "")
677 :suffix
(if colon?
")" ""))
678 (pprint-exit-if-list-exhausted)
680 (output-object (pprint-pop) stream
)
681 (pprint-exit-if-list-exhausted)
682 (write-char #\space stream
)
683 (pprint-newline :linear stream
))))
685 (defun pprint-tabular (stream list
&optional
(colon? t
) atsign? tabsize
)
686 "Output LIST to STREAM tabbing to the next column that is an even multiple
687 of TABSIZE (which defaults to 16) between each element. :FILL style
688 conditional newlines are also output between each element. If COLON? is
689 NIL (defaults to T), then no parens are printed around the output.
690 ATSIGN? is ignored (but allowed so that PPRINT-TABULAR can be used with
691 the ~/.../ format directive."
692 (declare (ignore atsign?
))
693 (pprint-logical-block (stream list
694 :prefix
(if colon?
"(" "")
695 :suffix
(if colon?
")" ""))
696 (pprint-exit-if-list-exhausted)
698 (output-object (pprint-pop) stream
)
699 (pprint-exit-if-list-exhausted)
700 (write-char #\space stream
)
701 (pprint-tab :section-relative
0 (or tabsize
16) stream
)
702 (pprint-newline :fill stream
))))
704 ;;;; pprint-dispatch tables
706 (defglobal *standard-pprint-dispatch-table
* nil
)
707 (defglobal *initial-pprint-dispatch-table
* nil
)
709 (defstruct (pprint-dispatch-entry
710 (:constructor make-pprint-dispatch-entry
(type priority fun test-fn
))
711 (:copier nil
) (:predicate nil
))
712 ;; the type specifier for this entry
713 (type nil
:type t
:read-only t
)
714 ;; a function to test to see whether an object is of this type,
715 ;; either (LAMBDA (OBJ) (TYPEP OBJECT TYPE)) or a builtin predicate.
716 ;; We don't bother computing this for entries in the CONS
717 ;; hash table, because we don't need it.
718 (test-fn nil
:type
(or function null
))
719 ;; the priority for this guy
720 (priority 0 :type real
:read-only t
)
721 ;; T iff one of the original entries.
722 (initial-p (null *initial-pprint-dispatch-table
*) :type boolean
:read-only t
)
723 ;; and the associated function
724 (fun nil
:type callable
:read-only t
))
726 (declaim (freeze-type pprint-dispatch-entry
))
728 (defmethod print-object ((entry pprint-dispatch-entry
) stream
)
729 (print-unreadable-object (entry stream
:type t
)
730 (format stream
"type=~S, priority=~S~@[ [initial]~]"
731 (pprint-dispatch-entry-type entry
)
732 (pprint-dispatch-entry-priority entry
)
733 (pprint-dispatch-entry-initial-p entry
))))
735 ;; Return T iff E1 is strictly less preferable than E2.
736 (defun entry< (e1 e2
)
737 (declare (type pprint-dispatch-entry e1 e2
))
738 (if (pprint-dispatch-entry-initial-p e1
)
739 (if (pprint-dispatch-entry-initial-p e2
)
740 (< (pprint-dispatch-entry-priority e1
)
741 (pprint-dispatch-entry-priority e2
))
743 (if (pprint-dispatch-entry-initial-p e2
)
745 (< (pprint-dispatch-entry-priority e1
)
746 (pprint-dispatch-entry-priority e2
)))))
748 ;; Return the predicate for CTYPE, equivalently TYPE-SPEC.
749 ;; This used to involve rewriting into a sexpr if CONS was involved,
750 ;; since it was not an official specifier. But now it is.
751 (defun compute-test-fn (ctype type-spec function
)
752 ;; Avoid compiling code for an existing structure predicate
753 (or (and (eq (info :type
:kind type-spec
) :instance
)
754 (let ((layout (info :type
:compiler-layout type-spec
)))
756 (let ((info (layout-info layout
)))
758 (let ((pred (dd-predicate-name info
)))
759 (and pred
(fboundp pred
)
760 (symbol-function pred
))))))))
761 ;; avoid compiling code for CONS, ARRAY, VECTOR, etc
762 (awhen (assoc ctype sb
!c
::*backend-type-predicates
* :test
#'type
=)
763 (symbol-function (cdr it
)))
764 ;; OK, compile something
766 ;; Keep name as a string, because NAMED-LAMBDA with a symbol
767 ;; affects the global environment, when all you want
768 ;; is to give the lambda a human-readable label.
770 (cond ((symbolp type-spec
) type-spec
)
771 ((symbolp function
) function
)
772 ((%fun-name function
))
774 (write-to-string type-spec
:pretty nil
:escape nil
777 `(named-lambda ,(possibly-base-stringize name
) (object)
778 (typep object
',type-spec
))))))
780 (defun copy-pprint-dispatch (&optional
(table *print-pprint-dispatch
*))
781 (declare (type (or pprint-dispatch-table null
) table
))
782 (let* ((orig (or table
*initial-pprint-dispatch-table
*))
783 (new (make-pprint-dispatch-table (copy-list (pp-dispatch-entries orig
)))))
784 (replace/eql-hash-table
(pp-dispatch-cons-entries new
)
785 (pp-dispatch-cons-entries orig
))
788 (defun pprint-dispatch (object &optional
(table *print-pprint-dispatch
*))
789 (declare (type (or pprint-dispatch-table null
) table
))
790 (let* ((table (or table
*initial-pprint-dispatch-table
*))
792 (when (or (not (numberp object
)) (pp-dispatch-number-matchable-p table
))
795 (gethash (car object
) (pp-dispatch-cons-entries table
)))))
797 (dolist (entry (pp-dispatch-entries table
) nil
)
798 (when (funcall (pprint-dispatch-entry-test-fn entry
) object
)
800 (dolist (entry (pp-dispatch-entries table
) cons-entry
)
801 (when (entry< entry cons-entry
)
803 (when (funcall (pprint-dispatch-entry-test-fn entry
) object
)
804 (return entry
))))))))
806 (values (pprint-dispatch-entry-fun entry
) t
)
807 (values #'output-ugly-object nil
))))
809 (defun assert-not-standard-pprint-dispatch-table (pprint-dispatch operation
)
810 (when (eq pprint-dispatch
*standard-pprint-dispatch-table
*)
811 (cerror "Frob it anyway!" 'standard-pprint-dispatch-table-modified-error
812 :operation operation
)))
814 (defun defer-type-checker (entry)
815 (let ((saved-nonce sb
!c
::*type-cache-nonce
*))
817 (let ((nonce sb
!c
::*type-cache-nonce
*))
818 (if (eq nonce saved-nonce
)
820 (let ((ctype (specifier-type (pprint-dispatch-entry-type entry
))))
821 (setq saved-nonce nonce
)
822 (if (testable-type-p ctype
)
823 (funcall (setf (pprint-dispatch-entry-test-fn entry
)
826 (pprint-dispatch-entry-type entry
)
827 (pprint-dispatch-entry-fun entry
)))
831 ;; The dispatch mechanism is not quite sophisticated enough to have a guard
832 ;; condition on CONS entries. One place this would impact is that you could
833 ;; write the full matcher for QUOTE as just a type-specifier. It can be done
834 ;; now, but using the non-cons table entails linear scan.
835 ;; A test-fn in the cons table would require storing multiple entries per
836 ;; key though because any might fail. Conceivably you could have
837 ;; (cons (eql foo) cons) and (cons (eql foo) bit-vector) as two FOO entries.
838 (defun set-pprint-dispatch (type function
&optional
839 (priority 0) (table *print-pprint-dispatch
*))
840 (declare (type (or null callable
) function
)
842 (type pprint-dispatch-table table
))
843 (declare (explicit-check))
844 #!+(and sb-show
(host-feature sb-xc
)) (format t
"* SET-PP-DISPATCH ~S~%" type
)
845 (assert-not-standard-pprint-dispatch-table table
'set-pprint-dispatch
)
846 (let* ((ctype (or (handler-bind
849 (warn "~S is not a recognized type specifier"
850 (parse-unknown-type-specifier c
)))))
851 (sb!c
::careful-specifier-type type
))
852 (error "~S is not a valid type-specifier" type
)))
853 (consp (and (cons-type-p ctype
)
854 (eq (cons-type-cdr-type ctype
) *universal-type
*)
855 (member-type-p (cons-type-car-type ctype
))))
856 (disabled-p (not (testable-type-p ctype
)))
858 (make-pprint-dispatch-entry
859 type priority function
860 (unless (or consp disabled-p
)
861 (compute-test-fn ctype type function
))))))
862 (when (and function disabled-p
)
863 ;; a DISABLED-P test function has to close over the ENTRY
864 (setf (pprint-dispatch-entry-test-fn entry
) (defer-type-checker entry
)))
865 (when (types-equal-or-intersect ctype
(specifier-type 'number
))
866 (setf (pp-dispatch-number-matchable-p table
) t
))
868 (let ((hashtable (pp-dispatch-cons-entries table
)))
869 (dolist (key (member-type-members (cons-type-car-type ctype
)))
871 (setf (gethash key hashtable
) entry
)
872 (remhash key hashtable
))))
873 (setf (pp-dispatch-entries table
)
874 (let ((list (delete type
(pp-dispatch-entries table
)
875 :key
#'pprint-dispatch-entry-type
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
)))
886 ;;;; standard pretty-printing routines
888 (defun pprint-array (stream array
)
889 (cond ((and (null *print-array
*) (null *print-readably
*))
890 (output-ugly-object stream array
))
891 ((and *print-readably
*
892 (not (array-readably-printable-p array
)))
893 (sb!impl
::output-unreadable-array-readably array stream
))
895 (pprint-vector stream array
))
897 (pprint-multi-dim-array stream array
))))
899 (defun pprint-vector (stream vector
)
900 (pprint-logical-block (stream nil
:prefix
"#(" :suffix
")")
901 (dotimes (i (length vector
))
903 (format stream
" ~:_"))
905 (output-object (aref vector i
) stream
))))
907 (defun pprint-multi-dim-array (stream array
)
908 (funcall (formatter "#~DA") stream
(array-rank array
))
909 (with-array-data ((data array
) (start) (end))
910 (declare (ignore end
))
911 (labels ((output-guts (stream index dimensions
)
912 (if (null dimensions
)
913 (output-object (aref data index
) stream
)
914 (pprint-logical-block
915 (stream nil
:prefix
"(" :suffix
")")
916 (let ((dim (car dimensions
)))
918 (let* ((dims (cdr dimensions
))
920 (step (reduce #'* dims
))
924 (output-guts stream index dims
)
925 (when (= (incf count
) dim
)
927 (write-char #\space stream
)
928 (pprint-newline (if dims
:linear
:fill
)
930 (incf index step
)))))))))
931 (output-guts stream start
(array-dimensions array
)))))
933 (defun pprint-lambda-list (stream lambda-list
&rest noise
)
934 (declare (ignore noise
))
935 (pprint-logical-block (stream lambda-list
:prefix
"(" :suffix
")")
936 (let ((state :required
)
939 (pprint-exit-if-list-exhausted)
941 (write-char #\space stream
))
942 (let ((arg (pprint-pop)))
945 (setf state
:optional
)
946 (pprint-newline :linear stream
))
948 (setf state
:required
)
949 (pprint-newline :linear stream
))
952 (pprint-newline :linear stream
))
954 (pprint-newline :fill stream
)))
957 ;; Make sure method specializers like
958 ;; (function (eql #'foo)) are printed right
959 (pprint-logical-block
960 (stream arg
:prefix
"(" :suffix
")")
961 (pprint-exit-if-list-exhausted)
963 (output-object (pprint-pop) stream
)
964 (pprint-exit-if-list-exhausted)
965 (write-char #\space stream
)
966 (pprint-newline :linear stream
))))
968 (pprint-logical-block
969 (stream arg
:prefix
"(" :suffix
")")
970 (pprint-exit-if-list-exhausted)
972 (pprint-logical-block
973 (stream (pprint-pop) :prefix
"(" :suffix
")")
974 (pprint-exit-if-list-exhausted)
975 (output-object (pprint-pop) stream
)
976 (pprint-exit-if-list-exhausted)
977 (write-char #\space stream
)
978 (pprint-newline :fill stream
)
979 (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 (output-object (pprint-pop) stream
))
987 (pprint-exit-if-list-exhausted)
988 (write-char #\space stream
)
989 (pprint-newline :linear stream
)
990 (output-object (pprint-pop) stream
))))))
993 (defun pprint-lambda (stream list
&rest noise
)
994 (declare (ignore noise
))
996 ;; KLUDGE: This format string, and other format strings which also
997 ;; refer to SB!PRETTY, rely on the current SBCL not-quite-ANSI
998 ;; behavior of FORMATTER in order to make code which survives the
999 ;; transition when SB!PRETTY is renamed to SB-PRETTY after cold
1000 ;; init. (ANSI says that the FORMATTER functions should be
1001 ;; equivalent to the format string, but the SBCL FORMATTER
1002 ;; functions contain references to package objects, not package
1003 ;; names, so they keep right on going if the packages are renamed.)
1004 ;; If our FORMATTER behavior is ever made more compliant, the code
1005 ;; here will have to change. -- WHN 19991207
1006 "~:<~^~W~^~3I ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1010 (defun pprint-block (stream list
&rest noise
)
1011 (declare (ignore noise
))
1012 (funcall (formatter "~:<~^~W~^~3I ~:_~W~1I~@{ ~_~W~}~:>") stream list
))
1014 (defun pprint-flet (stream list
&rest noise
)
1015 (declare (ignore noise
))
1016 (if (and (consp list
)
1019 ;; Filter out (FLET FOO :IN BAR) names.
1020 (and (consp (cddr list
))
1021 (not (eq :in
(third list
)))))
1023 "~:<~^~W~^ ~@_~:<~@{~:<~^~W~^~3I ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~:@_~@{~W~^ ~_~}~:>~^ ~_~}~:>~1I~@:_~@{~W~^ ~_~}~:>")
1026 ;; for printing function names like (flet foo)
1027 (pprint-logical-block (stream list
:prefix
"(" :suffix
")")
1028 (pprint-exit-if-list-exhausted)
1029 (write (pprint-pop) :stream stream
)
1031 (pprint-exit-if-list-exhausted)
1032 (write-char #\space stream
)
1033 (write (pprint-pop) :stream stream
)))))
1035 (defun pprint-let (stream list
&rest noise
)
1036 (declare (ignore noise
))
1037 (funcall (formatter "~:<~^~W~^ ~@_~:<~@{~:<~^~W~@{ ~_~W~}~:>~^ ~_~}~:>~1I~:@_~@{~W~^ ~_~}~:>")
1041 (defun pprint-progn (stream list
&rest noise
)
1042 (declare (ignore noise
))
1043 (pprint-linear stream list
))
1045 (defun pprint-progv (stream list
&rest noise
)
1046 (declare (ignore noise
))
1047 (funcall (formatter "~:<~^~W~^~3I ~_~W~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1050 (defun pprint-prog2 (stream list
&rest noise
)
1051 (declare (ignore noise
))
1052 (funcall (formatter "~:<~^~W~^~3I ~:_~W~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1055 (defun pprint-unquoting-comma (stream obj
&rest noise
)
1056 (declare (ignore noise
))
1057 (write-string (svref #("," ",." ",@") (comma-kind obj
)) stream
)
1058 (when (eql (comma-kind obj
) 0)
1059 ;; Ensure a space is written before any output that would change the meaning
1060 ;; of the preceding the comma to ",." or ",@" such as a symbol named "@BAR".
1061 (setf (pretty-stream-char-out-oneshot-hook stream
)
1062 (lambda (stream char
)
1063 (when (member char
'(#\.
#\
@))
1064 (write-char #\Space stream
)))))
1065 (output-object (comma-expr obj
) stream
))
1067 (defvar *pprint-quote-with-syntactic-sugar
* t
)
1069 (defun pprint-quote (stream list
&rest noise
)
1070 (declare (ignore noise
))
1071 (when (and (listp list
) (singleton-p (cdr list
)))
1072 (let* ((pretty-p nil
)
1073 (sigil (case (car list
)
1075 ;; QUASIQUOTE can't choose not to print prettily.
1076 ;; Wrongly nested commas beget unreadable sexprs.
1077 (quasiquote (setq pretty-p t
) "`")
1078 (t "'")))) ; ordinary QUOTE
1079 (when (or pretty-p
*pprint-quote-with-syntactic-sugar
*)
1080 (write-string sigil stream
)
1081 (return-from pprint-quote
(output-object (cadr list
) stream
)))))
1082 (pprint-fill stream list
))
1084 (defun pprint-declare (stream list
&rest noise
)
1085 (declare (ignore noise
))
1086 ;; Make sure to print (DECLARE (FUNCTION F)) not (DECLARE #'A).
1087 (let ((*pprint-quote-with-syntactic-sugar
* nil
))
1088 (pprint-spread-fun-call stream list
)))
1090 ;;; Try to print every variable-value pair on one line; if that doesn't
1091 ;;; work print the value indented by 2 spaces:
1099 (defun pprint-setq (stream list
&rest noise
)
1100 (declare (ignore noise
))
1101 (pprint-logical-block (stream list
:prefix
"(" :suffix
")")
1102 (pprint-exit-if-list-exhausted)
1103 (output-object (pprint-pop) stream
)
1104 (pprint-exit-if-list-exhausted)
1105 (write-char #\space stream
)
1106 (unless (listp (cdr list
))
1107 (write-string ". " stream
))
1108 (pprint-newline :miser stream
)
1109 (pprint-logical-block (stream (cdr list
) :prefix
"" :suffix
"")
1111 (pprint-indent :block
2 stream
)
1112 (output-object (pprint-pop) stream
)
1113 (pprint-exit-if-list-exhausted)
1114 (write-char #\space stream
)
1115 (pprint-newline :fill stream
)
1116 (pprint-indent :block
0 stream
)
1117 (output-object (pprint-pop) stream
)
1118 (pprint-exit-if-list-exhausted)
1119 (write-char #\space stream
)
1120 (pprint-newline :mandatory stream
)))))
1122 (eval-when (:compile-toplevel
:execute
)
1123 (sb!xc
:defmacro pprint-tagbody-guts
(stream)
1125 (pprint-exit-if-list-exhausted)
1126 (write-char #\space
,stream
)
1127 (let ((form-or-tag (pprint-pop)))
1128 (pprint-indent :block
1129 (if (atom form-or-tag
) 0 1)
1131 (pprint-newline :linear
,stream
)
1132 (output-object form-or-tag
,stream
)))))
1134 (defun pprint-tagbody (stream list
&rest noise
)
1135 (declare (ignore noise
))
1136 (pprint-logical-block (stream list
:prefix
"(" :suffix
")")
1137 (pprint-exit-if-list-exhausted)
1138 (output-object (pprint-pop) stream
)
1139 (pprint-tagbody-guts stream
)))
1141 (defun pprint-case (stream list
&rest noise
)
1142 (declare (ignore noise
))
1144 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~:/SB!PRETTY:PPRINT-FILL/~^~@{ ~_~W~}~:>~}~:>")
1148 (defun pprint-defun (stream list
&rest noise
)
1149 (declare (ignore noise
))
1151 "~:<~^~W~^ ~@_~:I~W~^ ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1155 (defun pprint-defmethod (stream list
&rest noise
)
1156 (declare (ignore noise
))
1157 (if (and (consp (cdr list
))
1159 (consp (third list
)))
1160 (pprint-defun stream list
)
1162 "~:<~^~W~^ ~@_~:I~W~^ ~W~^ ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1166 (defun pprint-defpackage (stream list
&rest noise
)
1167 (declare (ignore noise
))
1169 "~:<~W~^ ~3I~:_~W~^~1I~@{~:@_~:<~^~W~^ ~:I~@_~@{~W~^ ~_~}~:>~}~:>")
1173 (defun pprint-destructuring-bind (stream list
&rest noise
)
1174 (declare (ignore noise
))
1176 "~:<~^~W~^~3I ~_~:/SB!PRETTY:PPRINT-LAMBDA-LIST/~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1179 (defun pprint-do (stream list
&rest noise
)
1180 (declare (ignore noise
))
1181 (pprint-logical-block (stream list
:prefix
"(" :suffix
")")
1182 (pprint-exit-if-list-exhausted)
1183 (output-object (pprint-pop) stream
)
1184 (pprint-exit-if-list-exhausted)
1185 (write-char #\space stream
)
1186 (pprint-indent :current
0 stream
)
1187 (funcall (formatter "~:<~@{~:<~^~W~^ ~@_~:I~W~@{ ~_~W~}~:>~^~:@_~}~:>")
1190 (pprint-exit-if-list-exhausted)
1191 (write-char #\space stream
)
1192 (pprint-newline :linear stream
)
1193 (pprint-linear stream
(pprint-pop))
1194 (pprint-tagbody-guts stream
)))
1196 (defun pprint-dolist (stream list
&rest noise
)
1197 (declare (ignore noise
))
1198 (pprint-logical-block (stream list
:prefix
"(" :suffix
")")
1199 (pprint-exit-if-list-exhausted)
1200 (output-object (pprint-pop) stream
)
1201 (pprint-exit-if-list-exhausted)
1202 (pprint-indent :block
3 stream
)
1203 (write-char #\space stream
)
1204 (pprint-newline :fill stream
)
1205 (funcall (formatter "~:<~^~W~^ ~:_~:I~W~@{ ~_~W~}~:>")
1208 (pprint-tagbody-guts stream
)))
1210 (defun pprint-typecase (stream list
&rest noise
)
1211 (declare (ignore noise
))
1213 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~W~^~@{ ~_~W~}~:>~}~:>")
1217 (defun pprint-prog (stream list
&rest noise
)
1218 (declare (ignore noise
))
1219 (pprint-logical-block (stream list
:prefix
"(" :suffix
")")
1220 (pprint-exit-if-list-exhausted)
1221 (output-object (pprint-pop) stream
)
1222 (pprint-exit-if-list-exhausted)
1223 (write-char #\space stream
)
1224 (pprint-newline :miser stream
)
1225 (pprint-fill stream
(pprint-pop))
1226 (pprint-tagbody-guts stream
)))
1228 ;;; Each clause in this list will get its own line.
1229 ;;; FIXME: (LOOP for x in list summing (f x) into count finally ...)
1230 ;;; puts a newline in between INTO and COUNT.
1231 ;;; It would be awesome to have code in common with the macro
1232 ;;; the properly represents each clauses.
1233 (defglobal *loop-separating-clauses
*
1238 :collect
:collecting
1243 :maximize
:maximizing
1244 :minimize
:minimizing
1245 :if
:when
:unless
:end
1246 :for
:while
:until
:repeat
:always
:never
:thereis
1249 (defun pprint-extended-loop (stream list
)
1250 (pprint-logical-block (stream list
:prefix
"(" :suffix
")")
1251 (output-object (pprint-pop) stream
)
1252 (pprint-exit-if-list-exhausted)
1253 (write-char #\space stream
)
1254 (pprint-indent :current
0 stream
)
1255 (output-object (pprint-pop) stream
)
1256 (pprint-exit-if-list-exhausted)
1257 (write-char #\space stream
)
1258 (loop for thing
= (pprint-pop)
1259 when
(and (symbolp thing
)
1260 (member thing
*loop-separating-clauses
* :test
#'string
=))
1261 do
(pprint-newline :mandatory stream
)
1262 do
(output-object thing stream
)
1263 do
(pprint-exit-if-list-exhausted)
1264 do
(write-char #\space stream
))))
1266 (defun pprint-loop (stream list
&rest noise
)
1267 (declare (ignore noise
))
1268 (destructuring-bind (loop-symbol . clauses
) list
1269 (declare (ignore loop-symbol
))
1270 (if (or (atom clauses
) (consp (car clauses
)))
1271 (pprint-spread-fun-call stream list
)
1272 (pprint-extended-loop stream list
))))
1274 (defun pprint-if (stream list
&rest noise
)
1275 (declare (ignore noise
))
1276 ;; Indent after the ``predicate'' form, and the ``then'' form.
1277 (funcall (formatter "~:<~^~W~^ ~:I~W~^ ~:@_~@{~W~^ ~:@_~}~:>")
1281 (defun pprint-fun-call (stream list
&rest noise
)
1282 (declare (ignore noise
))
1283 (funcall (formatter "~:<~^~W~^ ~:_~:I~@{~W~^ ~:_~}~:>")
1287 (defun pprint-spread-fun-call (stream list
&rest noise
)
1288 (declare (ignore noise
))
1289 ;; Similiar to PPRINT-FUN-CALL but emit a mandatory newline after
1290 ;; each parameter. I.e. spread out each parameter on its own line.
1291 (funcall (formatter "~:<~^~W~^ ~:_~:I~@{~W~^ ~:@_~}~:>")
1295 (defun pprint-data-list (stream list
&rest noise
)
1296 (declare (ignore noise
))
1297 (pprint-fill stream list
))
1299 ;;; Return the number of positional arguments that macro NAME accepts
1300 ;;; by looking for &BODY. A dotted list is indented as it it had &BODY.
1301 ;;; ANSI says that a dotted tail is like &REST, but the pretty-printer
1302 ;;; can do whatever it likes anyway. I happen to think this makes sense.
1303 (defun macro-indentation (name)
1305 (list (%fun-lambda-list
(macro-function name
)) (cdr list
)))
1306 ((or (atom list
) (eq (car list
) '&body
))
1307 (if (null list
) nil n
))
1308 (unless (eq (car list
) '&optional
)
1311 ;;; Pretty-Print macros by looking where &BODY appears in a macro's
1313 (defun pprint-macro-call (stream list
&rest noise
)
1314 (declare (ignore noise
))
1315 (let ((indentation (and (car list
) (macro-indentation (car list
)))))
1317 (return-from pprint-macro-call
1318 (pprint-fun-call stream list
)))
1319 (pprint-logical-block (stream list
:prefix
"(" :suffix
")")
1320 (output-object (pprint-pop) stream
)
1321 (pprint-exit-if-list-exhausted)
1322 (write-char #\space stream
)
1323 (loop for indent from
0 below indentation do
1325 ;; Place the very first argument next to the macro name
1327 (output-object (pprint-pop) stream
)
1328 (pprint-exit-if-list-exhausted))
1329 ;; Indent any other non-body argument by the same
1330 ;; amount. It's what Emacs seems to do, too.
1332 (pprint-indent :block
3 stream
)
1333 (pprint-newline :mandatory stream
)
1334 (output-object (pprint-pop) stream
)
1335 (pprint-exit-if-list-exhausted))))
1336 ;; Indent back for the body.
1337 (pprint-indent :block
1 stream
)
1338 (pprint-newline :mandatory stream
)
1340 (output-object (pprint-pop) stream
)
1341 (pprint-exit-if-list-exhausted)
1342 (pprint-newline :mandatory stream
)))))
1344 ;;;; the interface seen by regular (ugly) printer and initialization routines
1346 (eval-when (:compile-toplevel
:execute
)
1347 (sb!xc
:defmacro with-pretty-stream
((stream-var
1348 &optional
(stream-expression stream-var
))
1350 (let ((flet-name (sb!xc
:gensym
"WITH-PRETTY-STREAM")))
1351 `(flet ((,flet-name
(,stream-var
)
1353 (let ((stream ,stream-expression
))
1354 (if (pretty-stream-p stream
)
1356 (catch 'line-limit-abbreviation-happened
1357 (let ((stream (make-pretty-stream stream
)))
1359 (force-pretty-output stream
)))))
1362 (defun call-logical-block-printer (proc stream prefix per-line-p suffix
1363 &optional
(object nil obj-supplied-p
))
1364 ;; PREFIX and SUFFIX will be checked for stringness by START-LOGICAL-BLOCK.
1365 ;; Doing it here would be more strict, but I really don't think it's worth
1366 ;; an extra check. The only observable difference would occur when you have
1367 ;; a non-list object which bypasses START-LOGICAL-BLOCK.
1368 ;; Also, START-LOGICAL-BLOCK could become an FLET inside here.
1369 (declare (function proc
))
1370 (with-pretty-stream (stream (out-stream-from-designator stream
))
1371 (if (or (not (listp object
)) ; implies obj-supplied-p
1372 (and (eq (car object
) 'quasiquote
)
1373 ;; We can only bail out from printing this logical block
1374 ;; if the quasiquote printer would *NOT* punt.
1375 ;; If it would punt, then we have to forge ahead.
1376 (singleton-p (cdr object
))))
1377 ;; the spec says "If object is not a list, it is printed using WRITE"
1378 ;; but I guess this is close enough.
1379 (output-object object stream
)
1380 (dx-let ((state (cons 0 stream
)))
1382 (with-circularity-detection (object stream
)
1383 (descend-into (stream)
1384 (start-logical-block stream prefix per-line-p suffix
)
1385 (funcall proc object state stream
)
1386 ;; Comment preserved for posterity:
1387 ;; FIXME: Don't we need UNWIND-PROTECT to ensure this
1388 ;; always gets executed?
1389 ;; I think not because I wouldn't characterize this as
1390 ;; "cleanup" code. If and only if you follow the accepted
1391 ;; protocol for defining and using print functions should
1392 ;; the behavior be expected to be reasonable and predictable.
1393 ;; Throwing to LINE-LIMIT-ABBREVIATION-HAPPENED is designed
1394 ;; to do the right thing, and printing should not generally
1395 ;; continue to have side-effects if the user felt it necessary
1396 ;; to nonlocally exit in an unexpected way for other reasons.
1397 (end-logical-block stream
)))
1398 (descend-into (stream)
1399 (start-logical-block stream prefix per-line-p suffix
)
1400 (funcall proc state stream
)
1401 (end-logical-block stream
)))))))
1403 ;; Return non-nil if we should keep printing within the logical-block,
1404 ;; or NIL to stop printing due to non-list, length cutoff, or circularity.
1405 (defun pprint-length-check (obj state
)
1406 (let ((stream (cdr state
)))
1407 (cond ((or (not (listp obj
))
1408 ;; Consider (A . `(,B C)) = (A QUASIQUOTE ,B C)
1409 ;; We have to detect this and print as the form on the left,
1410 ;; since pretty commas with no containing #\` will be unreadable
1411 ;; due to a nesting error.
1412 (and (eq (car obj
) 'quasiquote
) (singleton-p (cdr obj
))))
1413 (write-string ". " stream
)
1414 (output-object obj stream
)
1416 ((and (not *print-readably
*) (eql (car state
) *print-length
*))
1417 (write-string "..." stream
)
1421 (check-for-circularity obj nil
:logical-block
))
1422 (write-string ". " stream
)
1423 (output-object obj stream
)
1426 (incf (car state
))))))
1428 ;; As above, but for logical blocks with an unspecific object.
1429 (defun pprint-length-check* (state)
1430 (let ((stream (cdr state
)))
1431 (cond ((and (not *print-readably
*) (eql (car state
) *print-length
*))
1432 (write-string "..." stream
)
1435 (incf (car state
))))))
1437 (defun !pprint-cold-init
()
1438 (/show0
"entering !PPRINT-COLD-INIT")
1439 ;; Kludge: We set *STANDARD-PP-D-TABLE* to a new table even though
1440 ;; it's going to be set to a copy of *INITIAL-PP-D-T* below because
1441 ;; it's used in WITH-STANDARD-IO-SYNTAX, and condition reportery
1442 ;; possibly performed in the following extent may use W-S-IO-SYNTAX.
1443 (setf *standard-pprint-dispatch-table
* (make-pprint-dispatch-table))
1444 (setf *initial-pprint-dispatch-table
* nil
)
1445 (let ((*print-pprint-dispatch
* (make-pprint-dispatch-table)))
1446 (/show0
"doing SET-PPRINT-DISPATCH for regular types")
1447 (set-pprint-dispatch '(and array
(not (or string bit-vector
))) 'pprint-array
)
1448 ;; MACRO-FUNCTION must have effectively higher priority than FBOUNDP.
1449 ;; The implementation happens to check identical priorities in the order added,
1450 ;; but that's unspecified behavior. Both must be _strictly_ lower than the
1451 ;; default cons entries though.
1452 (set-pprint-dispatch '(cons (and symbol
(satisfies macro-function
)))
1453 'pprint-macro-call -
1)
1454 (set-pprint-dispatch '(cons (and symbol
(satisfies fboundp
)))
1455 'pprint-fun-call -
1)
1456 (set-pprint-dispatch '(cons symbol
)
1457 'pprint-data-list -
2)
1458 (set-pprint-dispatch 'cons
'pprint-fill -
2)
1459 (set-pprint-dispatch 'sb
!impl
::comma
'pprint-unquoting-comma -
3)
1460 ;; cons cells with interesting things for the car
1461 (/show0
"doing SET-PPRINT-DISPATCH for CONS with interesting CAR")
1463 (dolist (magic-form '((lambda pprint-lambda
)
1464 ((declare declaim
) pprint-declare
)
1467 ((block catch return-from throw eval-when
1468 multiple-value-call multiple-value-prog1
1469 unwind-protect
) pprint-block
)
1470 ((flet labels macrolet dx-flet
) pprint-flet
)
1471 ((function quasiquote quote
) pprint-quote
)
1473 ((let let
* symbol-macrolet dx-let
) pprint-let
)
1474 ((locally progn
) pprint-progn
)
1475 (progv pprint-progv
)
1476 ((setq psetq setf psetf
) pprint-setq
)
1477 (tagbody pprint-tagbody
)
1480 ((case ccase ecase
) pprint-case
)
1481 ((ctypecase etypecase typecase
) pprint-typecase
)
1482 ((defconstant defparameter defstruct defvar
)
1484 ((define-modify-macro define-setf-expander
1485 defmacro defsetf deftype defun
) pprint-defun
)
1486 (defmethod pprint-defmethod)
1487 (defpackage pprint-defpackage
)
1488 (destructuring-bind pprint-destructuring-bind
)
1489 ((do do
*) pprint-do
)
1490 ((do-all-symbols do-external-symbols do-symbols
1491 dolist dotimes
) pprint-dolist
)
1492 #+nil
(handler-bind ...
)
1493 #+nil
(handler-case ...
)
1495 ((multiple-value-bind prog2
) pprint-prog2
)
1496 ((multiple-value-setq prog1 pprint-logical-block
1497 print-unreadable-object prog1
) pprint-block
)
1498 ((prog prog
*) pprint-prog
)
1499 #+nil
(restart-bind ...
)
1500 #+nil
(restart-case ...
)
1501 ((step time
) pprint-progn
)
1502 ((unless when
) pprint-block
)
1503 #+nil
(with-condition-restarts ...
)
1504 ((with-compilation-unit with-simple-restart
1505 with-hash-table-iterator with-package-iterator
1506 with-input-from-string with-output-to-string
1507 with-open-file with-open-stream
) pprint-block
)
1508 (with-standard-io-syntax pprint-progn
)))
1510 ;; Grouping some symbols together in the above list looks pretty.
1511 ;; The sharing of dispatch entries is inconsequential.
1512 (set-pprint-dispatch `(cons (member ,@(ensure-list (first magic-form
))))
1513 (second magic-form
)))
1514 (setf *initial-pprint-dispatch-table
* *print-pprint-dispatch
*))
1516 (setf *standard-pprint-dispatch-table
*
1517 (copy-pprint-dispatch *initial-pprint-dispatch-table
*))
1518 (setf *print-pprint-dispatch
*
1519 (copy-pprint-dispatch *initial-pprint-dispatch-table
*))
1520 (setf *print-pretty
* t
))