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")
16 ;;; There are three different units for measuring character positions:
17 ;;; COLUMN - offset (if characters) from the start of the current line
18 ;;; INDEX - index into the output buffer
19 ;;; POSN - some position in the stream of characters cycling through
22 '(and fixnum unsigned-byte))
23 ;;; The INDEX type is picked up from the kernel package.
27 (defconstant initial-buffer-size 128)
29 (defconstant default-line-length 80)
31 (defstruct (pretty-stream (:include sb!kernel:ansi-stream
35 (:constructor make-pretty-stream (target))
37 ;; Where the output is going to finally go.
38 (target (missing-arg) :type stream)
39 ;; Line length we should format to. Cached here so we don't have to keep
40 ;; extracting it from the target stream.
41 (line-length (or *print-right-margin*
42 (sb!impl::line-length target)
45 ;; A simple string holding all the text that has been output but not yet
47 (buffer (make-string initial-buffer-size) :type (simple-array character (*)))
48 ;; The index into BUFFER where more text should be put.
49 (buffer-fill-pointer 0 :type index)
50 ;; Whenever we output stuff from the buffer, we shift the remaining noise
51 ;; over. This makes it difficult to keep references to locations in
52 ;; the buffer. Therefore, we have to keep track of the total amount of
53 ;; stuff that has been shifted out of the buffer.
54 (buffer-offset 0 :type posn)
55 ;; The column the first character in the buffer will appear in. Normally
56 ;; zero, but if we end up with a very long line with no breaks in it we
57 ;; might have to output part of it. Then this will no longer be zero.
58 (buffer-start-column (or (sb!impl::charpos target) 0) :type column)
59 ;; The line number we are currently on. Used for *PRINT-LINES*
60 ;; abbreviations and to tell when sections have been split across
62 (line-number 0 :type index)
63 ;; the value of *PRINT-LINES* captured at object creation time. We
64 ;; use this, instead of the dynamic *PRINT-LINES*, to avoid
66 ;; (let ((*print-lines* 50))
67 ;; (pprint-logical-block ..
69 ;; (let ((*print-lines* 8))
70 ;; (print (aref possiblybigthings i) prettystream)))))
71 ;; terminating the output of the entire logical blockafter 8 lines.
72 (print-lines *print-lines* :type (or index null) :read-only t)
73 ;; Stack of logical blocks in effect at the buffer start.
74 (blocks (list (make-logical-block)) :type list)
75 ;; Buffer holding the per-line prefix active at the buffer start.
76 ;; Indentation is included in this. The length of this is stored
77 ;; in the logical block stack.
78 (prefix (make-string initial-buffer-size) :type (simple-array character (*)))
79 ;; Buffer holding the total remaining suffix active at the buffer start.
80 ;; The characters are right-justified in the buffer to make it easier
81 ;; to output the buffer. The length is stored in the logical block
83 (suffix (make-string initial-buffer-size) :type (simple-array character (*)))
84 ;; Queue of pending operations. When empty, HEAD=TAIL=NIL. Otherwise,
85 ;; TAIL holds the first (oldest) cons and HEAD holds the last (newest)
86 ;; cons. Adding things to the queue is basically (setf (cdr head) (list
87 ;; new)) and removing them is basically (pop tail) [except that care must
88 ;; be taken to handle the empty queue case correctly.]
89 (queue-tail nil :type list)
90 (queue-head nil :type list)
91 ;; Block-start queue entries in effect at the queue head.
92 (pending-blocks nil :type list))
93 (def!method print-object ((pstream pretty-stream) stream)
94 ;; FIXME: CMU CL had #+NIL'ed out this code and done a hand-written
95 ;; FORMAT hack instead. Make sure that this code actually works instead
96 ;; of falling into infinite regress or something.
97 (print-unreadable-object (pstream stream :type t :identity t)))
99 #!-sb-fluid (declaim (inline index-posn posn-index posn-column))
100 (defun index-posn (index stream)
101 (declare (type index index) (type pretty-stream stream)
103 (+ index (pretty-stream-buffer-offset stream)))
104 (defun posn-index (posn stream)
105 (declare (type posn posn) (type pretty-stream stream)
107 (- posn (pretty-stream-buffer-offset stream)))
108 (defun posn-column (posn stream)
109 (declare (type posn posn) (type pretty-stream stream)
111 (index-column (posn-index posn stream) stream))
113 ;;; Is it OK to do pretty printing on this stream at this time?
114 (defun print-pretty-on-stream-p (stream)
115 (and (pretty-stream-p stream)
118 ;;;; stream interface routines
120 (defun pretty-out (stream char)
121 (declare (type pretty-stream stream)
122 (type base-char char))
123 (cond ((char= char #\newline)
124 (enqueue-newline stream :literal))
126 (ensure-space-in-buffer stream 1)
127 (let ((fill-pointer (pretty-stream-buffer-fill-pointer stream)))
128 (setf (schar (pretty-stream-buffer stream) fill-pointer) char)
129 (setf (pretty-stream-buffer-fill-pointer stream)
130 (1+ fill-pointer))))))
132 (defun pretty-sout (stream string start end)
133 (declare (type pretty-stream stream)
134 (type simple-string string)
136 (type (or index null) end))
137 (let* ((string (if (typep string '(simple-array character (*)))
139 (coerce string '(simple-array character (*)))))
140 (end (or end (length string))))
141 (unless (= start end)
142 (let ((newline (position #\newline string :start start :end end)))
145 (pretty-sout stream string start newline)
146 (enqueue-newline stream :literal)
147 (pretty-sout stream string (1+ newline) end))
149 (let ((chars (- end start)))
151 (let* ((available (ensure-space-in-buffer stream chars))
152 (count (min available chars))
153 (fill-pointer (pretty-stream-buffer-fill-pointer stream))
154 (new-fill-ptr (+ fill-pointer count)))
155 (replace (pretty-stream-buffer stream)
157 :start1 fill-pointer :end1 new-fill-ptr
159 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
163 (incf start count))))))))))
165 (defun pretty-misc (stream op &optional arg1 arg2)
166 (declare (ignore stream op arg1 arg2)))
170 (defstruct (logical-block (:copier nil))
171 ;; The column this logical block started in.
172 (start-column 0 :type column)
173 ;; The column the current section started in.
174 (section-column 0 :type column)
175 ;; The length of the per-line prefix. We can't move the indentation
177 (per-line-prefix-end 0 :type index)
178 ;; The overall length of the prefix, including any indentation.
179 (prefix-length 0 :type index)
180 ;; The overall length of the suffix.
181 (suffix-length 0 :type index)
183 (section-start-line 0 :type index))
185 (defun really-start-logical-block (stream column prefix suffix)
186 (let* ((blocks (pretty-stream-blocks stream))
187 (prev-block (car blocks))
188 (per-line-end (logical-block-per-line-prefix-end prev-block))
189 (prefix-length (logical-block-prefix-length prev-block))
190 (suffix-length (logical-block-suffix-length prev-block))
191 (block (make-logical-block
193 :section-column column
194 :per-line-prefix-end per-line-end
195 :prefix-length prefix-length
196 :suffix-length suffix-length
197 :section-start-line (pretty-stream-line-number stream))))
198 (setf (pretty-stream-blocks stream) (cons block blocks))
199 (set-indentation stream column)
201 (setf (logical-block-per-line-prefix-end block) column)
202 (replace (pretty-stream-prefix stream) prefix
203 :start1 (- column (length prefix)) :end1 column))
205 (let* ((total-suffix (pretty-stream-suffix stream))
206 (total-suffix-len (length total-suffix))
207 (additional (length suffix))
208 (new-suffix-len (+ suffix-length additional)))
209 (when (> new-suffix-len total-suffix-len)
210 (let ((new-total-suffix-len
211 (max (* total-suffix-len 2)
213 (floor (* additional 5) 4)))))
215 (replace (make-string new-total-suffix-len) total-suffix
216 :start1 (- new-total-suffix-len suffix-length)
217 :start2 (- total-suffix-len suffix-length)))
218 (setf total-suffix-len new-total-suffix-len)
219 (setf (pretty-stream-suffix stream) total-suffix)))
220 (replace total-suffix suffix
221 :start1 (- total-suffix-len new-suffix-len)
222 :end1 (- total-suffix-len suffix-length))
223 (setf (logical-block-suffix-length block) new-suffix-len))))
226 (defun set-indentation (stream column)
227 (let* ((prefix (pretty-stream-prefix stream))
228 (prefix-len (length prefix))
229 (block (car (pretty-stream-blocks stream)))
230 (current (logical-block-prefix-length block))
231 (minimum (logical-block-per-line-prefix-end block))
232 (column (max minimum column)))
233 (when (> column prefix-len)
235 (replace (make-string (max (* prefix-len 2)
237 (floor (* (- column prefix-len) 5)
241 (setf (pretty-stream-prefix stream) prefix))
242 (when (> column current)
243 (fill prefix #\space :start current :end column))
244 (setf (logical-block-prefix-length block) column)))
246 (defun really-end-logical-block (stream)
247 (let* ((old (pop (pretty-stream-blocks stream)))
248 (old-indent (logical-block-prefix-length old))
249 (new (car (pretty-stream-blocks stream)))
250 (new-indent (logical-block-prefix-length new)))
251 (when (> new-indent old-indent)
252 (fill (pretty-stream-prefix stream) #\space
253 :start old-indent :end new-indent)))
256 ;;;; the pending operation queue
258 (defstruct (queued-op (:constructor nil)
262 (defmacro enqueue (stream type &rest args)
263 (let ((constructor (symbolicate "MAKE-" type)))
264 (once-only ((stream stream)
265 (entry `(,constructor :posn
267 (pretty-stream-buffer-fill-pointer
272 (head `(pretty-stream-queue-head ,stream)))
275 (setf (cdr ,head) ,op)
276 (setf (pretty-stream-queue-tail ,stream) ,op))
277 (setf (pretty-stream-queue-head ,stream) ,op)
280 (defstruct (section-start (:include queued-op)
283 (depth 0 :type index)
284 (section-end nil :type (or null newline block-end)))
286 (defstruct (newline (:include section-start)
289 :type (member :linear :fill :miser :literal :mandatory)))
291 (defun enqueue-newline (stream kind)
292 (let* ((depth (length (pretty-stream-pending-blocks stream)))
293 (newline (enqueue stream newline :kind kind :depth depth)))
294 (dolist (entry (pretty-stream-queue-tail stream))
295 (when (and (not (eq newline entry))
296 (section-start-p entry)
297 (null (section-start-section-end entry))
298 (<= depth (section-start-depth entry)))
299 (setf (section-start-section-end entry) newline))))
300 (maybe-output stream (or (eq kind :literal) (eq kind :mandatory))))
302 (defstruct (indentation (:include queued-op)
304 (kind (missing-arg) :type (member :block :current))
305 (amount 0 :type fixnum))
307 (defun enqueue-indent (stream kind amount)
308 (enqueue stream indentation :kind kind :amount amount))
310 (defstruct (block-start (:include section-start)
312 (block-end nil :type (or null block-end))
313 (prefix nil :type (or null (simple-array character (*))))
314 (suffix nil :type (or null (simple-array character (*)))))
316 (defun start-logical-block (stream prefix per-line-p suffix)
317 ;; (In the PPRINT-LOGICAL-BLOCK form which calls us,
318 ;; :PREFIX and :PER-LINE-PREFIX have hairy defaulting behavior,
319 ;; and might end up being NIL.)
320 (declare (type (or null string) prefix))
321 ;; (But the defaulting behavior of PPRINT-LOGICAL-BLOCK :SUFFIX is
322 ;; trivial, so it should always be a string.)
323 (declare (type string suffix))
325 (setq prefix (coerce prefix '(simple-array character (*))))
326 (pretty-sout stream prefix 0 (length prefix)))
327 (let* ((pending-blocks (pretty-stream-pending-blocks stream))
328 (start (enqueue stream block-start
329 :prefix (and per-line-p prefix)
330 :suffix (coerce suffix '(simple-array character (*)))
331 :depth (length pending-blocks))))
332 (setf (pretty-stream-pending-blocks stream)
333 (cons start pending-blocks))))
335 (defstruct (block-end (:include queued-op)
337 (suffix nil :type (or null (simple-array character (*)))))
339 (defun end-logical-block (stream)
340 (let* ((start (pop (pretty-stream-pending-blocks stream)))
341 (suffix (block-start-suffix start))
342 (end (enqueue stream block-end :suffix suffix)))
344 (pretty-sout stream suffix 0 (length suffix)))
345 (setf (block-start-block-end start) end)))
347 (defstruct (tab (:include queued-op)
349 (sectionp nil :type (member t nil))
350 (relativep nil :type (member t nil))
351 (colnum 0 :type column)
352 (colinc 0 :type column))
354 (defun enqueue-tab (stream kind colnum colinc)
355 (multiple-value-bind (sectionp relativep)
357 (:line (values nil nil))
358 (:line-relative (values nil t))
359 (:section (values t nil))
360 (:section-relative (values t t)))
361 (enqueue stream tab :sectionp sectionp :relativep relativep
362 :colnum colnum :colinc colinc)))
366 (defun compute-tab-size (tab section-start column)
367 (let ((origin (if (tab-sectionp tab) section-start 0))
368 (colnum (tab-colnum tab))
369 (colinc (tab-colinc tab)))
370 (cond ((tab-relativep tab)
371 (unless (<= colinc 1)
372 (let ((newposn (+ column colnum)))
373 (let ((rem (rem newposn colinc)))
375 (incf colnum (- colinc rem))))))
377 ((<= column (+ colnum origin))
378 (- (+ colnum origin) column))
381 (rem (- column origin) colinc))))))
383 (defun index-column (index stream)
384 (let ((column (pretty-stream-buffer-start-column stream))
385 (section-start (logical-block-section-column
386 (first (pretty-stream-blocks stream))))
387 (end-posn (index-posn index stream)))
388 (dolist (op (pretty-stream-queue-tail stream))
389 (when (>= (queued-op-posn op) end-posn)
397 (posn-index (tab-posn op)
399 ((or newline block-start)
401 (+ column (posn-index (queued-op-posn op)
405 (defun expand-tabs (stream through)
406 (let ((insertions nil)
408 (column (pretty-stream-buffer-start-column stream))
409 (section-start (logical-block-section-column
410 (first (pretty-stream-blocks stream)))))
411 (dolist (op (pretty-stream-queue-tail stream))
414 (let* ((index (posn-index (tab-posn op) stream))
415 (tabsize (compute-tab-size op
418 (unless (zerop tabsize)
419 (push (cons index tabsize) insertions)
420 (incf additional tabsize)
421 (incf column tabsize))))
422 ((or newline block-start)
424 (+ column (posn-index (queued-op-posn op) stream)))))
425 (when (eq op through)
428 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
429 (new-fill-ptr (+ fill-ptr additional))
430 (buffer (pretty-stream-buffer stream))
432 (length (length buffer))
434 (when (> new-fill-ptr length)
435 (let ((new-length (max (* length 2)
437 (floor (* additional 5) 4)))))
438 (setf new-buffer (make-string new-length))
439 (setf (pretty-stream-buffer stream) new-buffer)))
440 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
441 (decf (pretty-stream-buffer-offset stream) additional)
442 (dolist (insertion insertions)
443 (let* ((srcpos (car insertion))
444 (amount (cdr insertion))
445 (dstpos (+ srcpos additional)))
446 (replace new-buffer buffer :start1 dstpos :start2 srcpos :end2 end)
447 (fill new-buffer #\space :start (- dstpos amount) :end dstpos)
448 (decf additional amount)
450 (unless (eq new-buffer buffer)
451 (replace new-buffer buffer :end1 end :end2 end))))))
453 ;;;; stuff to do the actual outputting
455 (defun ensure-space-in-buffer (stream want)
456 (declare (type pretty-stream stream)
458 (let* ((buffer (pretty-stream-buffer stream))
459 (length (length buffer))
460 (fill-ptr (pretty-stream-buffer-fill-pointer stream))
461 (available (- length fill-ptr)))
462 (cond ((plusp available)
464 ((> fill-ptr (pretty-stream-line-length stream))
465 (unless (maybe-output stream nil)
466 (output-partial-line stream))
467 (ensure-space-in-buffer stream want))
469 (let* ((new-length (max (* length 2)
471 (floor (* want 5) 4))))
472 (new-buffer (make-string new-length)))
473 (setf (pretty-stream-buffer stream) new-buffer)
474 (replace new-buffer buffer :end1 fill-ptr)
475 (- new-length fill-ptr))))))
477 (defun maybe-output (stream force-newlines-p)
478 (declare (type pretty-stream stream))
479 (let ((tail (pretty-stream-queue-tail stream))
480 (output-anything nil))
483 (setf (pretty-stream-queue-head stream) nil)
485 (let ((next (pop tail)))
488 (when (ecase (newline-kind next)
489 ((:literal :mandatory :linear) t)
490 (:miser (misering-p stream))
492 (or (misering-p stream)
493 (> (pretty-stream-line-number stream)
494 (logical-block-section-start-line
495 (first (pretty-stream-blocks stream))))
496 (ecase (fits-on-line-p stream
497 (newline-section-end next)
503 (setf output-anything t)
504 (output-line stream next)))
506 (unless (misering-p stream)
507 (set-indentation stream
508 (+ (ecase (indentation-kind next)
510 (logical-block-start-column
511 (car (pretty-stream-blocks stream))))
514 (indentation-posn next)
516 (indentation-amount next)))))
518 (ecase (fits-on-line-p stream (block-start-section-end next)
521 ;; Just nuke the whole logical block and make it look
522 ;; like one nice long literal.
523 (let ((end (block-start-block-end next)))
524 (expand-tabs stream end)
525 (setf tail (cdr (member end tail)))))
527 (really-start-logical-block
529 (posn-column (block-start-posn next) stream)
530 (block-start-prefix next)
531 (block-start-suffix next)))
535 (really-end-logical-block stream))
537 (expand-tabs stream next))))
538 (setf (pretty-stream-queue-tail stream) tail))
541 (defun misering-p (stream)
542 (declare (type pretty-stream stream))
543 (and *print-miser-width*
544 (<= (- (pretty-stream-line-length stream)
545 (logical-block-start-column (car (pretty-stream-blocks stream))))
546 *print-miser-width*)))
548 (defun fits-on-line-p (stream until force-newlines-p)
549 (let ((available (pretty-stream-line-length stream)))
550 (when (and (not *print-readably*)
551 (pretty-stream-print-lines stream)
552 (= (pretty-stream-print-lines stream)
553 (pretty-stream-line-number stream)))
554 (decf available 3) ; for the `` ..''
555 (decf available (logical-block-suffix-length
556 (car (pretty-stream-blocks stream)))))
558 (<= (posn-column (queued-op-posn until) stream) available))
559 (force-newlines-p nil)
560 ((> (index-column (pretty-stream-buffer-fill-pointer stream) stream)
566 (defun output-line (stream until)
567 (declare (type pretty-stream stream)
568 (type newline until))
569 (let* ((target (pretty-stream-target stream))
570 (buffer (pretty-stream-buffer stream))
571 (kind (newline-kind until))
572 (literal-p (eq kind :literal))
573 (amount-to-consume (posn-index (newline-posn until) stream))
577 (let ((last-non-blank
578 (position #\space buffer :end amount-to-consume
579 :from-end t :test #'char/=)))
583 (write-string buffer target :end amount-to-print)
584 (let ((line-number (pretty-stream-line-number stream)))
586 (when (and (not *print-readably*)
587 (pretty-stream-print-lines stream)
588 (>= line-number (pretty-stream-print-lines stream)))
589 (write-string " .." target)
590 (let ((suffix-length (logical-block-suffix-length
591 (car (pretty-stream-blocks stream)))))
592 (unless (zerop suffix-length)
593 (let* ((suffix (pretty-stream-suffix stream))
594 (len (length suffix)))
595 (write-string suffix target
596 :start (- len suffix-length)
598 (throw 'line-limit-abbreviation-happened t))
599 (setf (pretty-stream-line-number stream) line-number)
600 (write-char #\newline target)
601 (setf (pretty-stream-buffer-start-column stream) 0)
602 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
603 (block (first (pretty-stream-blocks stream)))
606 (logical-block-per-line-prefix-end block)
607 (logical-block-prefix-length block)))
608 (shift (- amount-to-consume prefix-len))
609 (new-fill-ptr (- fill-ptr shift))
611 (buffer-length (length buffer)))
612 (when (> new-fill-ptr buffer-length)
614 (make-string (max (* buffer-length 2)
616 (floor (* (- new-fill-ptr buffer-length)
619 (setf (pretty-stream-buffer stream) new-buffer))
620 (replace new-buffer buffer
621 :start1 prefix-len :start2 amount-to-consume :end2 fill-ptr)
622 (replace new-buffer (pretty-stream-prefix stream)
624 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
625 (incf (pretty-stream-buffer-offset stream) shift)
627 (setf (logical-block-section-column block) prefix-len)
628 (setf (logical-block-section-start-line block) line-number))))))
630 (defun output-partial-line (stream)
631 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
632 (tail (pretty-stream-queue-tail stream))
635 (posn-index (queued-op-posn (car tail)) stream)
637 (new-fill-ptr (- fill-ptr count))
638 (buffer (pretty-stream-buffer stream)))
640 (error "Output-partial-line called when nothing can be output."))
641 (write-string buffer (pretty-stream-target stream)
643 (incf (pretty-stream-buffer-start-column stream) count)
644 (replace buffer buffer :end1 new-fill-ptr :start2 count :end2 fill-ptr)
645 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
646 (incf (pretty-stream-buffer-offset stream) count)))
648 (defun force-pretty-output (stream)
649 (maybe-output stream nil)
650 (expand-tabs stream nil)
651 (write-string (pretty-stream-buffer stream)
652 (pretty-stream-target stream)
653 :end (pretty-stream-buffer-fill-pointer stream)))
655 ;;;; user interface to the pretty printer
657 (defun pprint-newline (kind &optional stream)
659 "Output a conditional newline to STREAM (which defaults to
660 *STANDARD-OUTPUT*) if it is a pretty-printing stream, and do
661 nothing if not. KIND can be one of:
662 :LINEAR - A line break is inserted if and only if the immediatly
663 containing section cannot be printed on one line.
664 :MISER - Same as LINEAR, but only if ``miser-style'' is in effect.
665 (See *PRINT-MISER-WIDTH*.)
666 :FILL - A line break is inserted if and only if either:
667 (a) the following section cannot be printed on the end of the
669 (b) the preceding section was not printed on a single line, or
670 (c) the immediately containing section cannot be printed on one
671 line and miser-style is in effect.
672 :MANDATORY - A line break is always inserted.
673 When a line break is inserted by any type of conditional newline, any
674 blanks that immediately precede the conditional newline are ommitted
675 from the output and indentation is introduced at the beginning of the
676 next line. (See PPRINT-INDENT.)"
677 (declare (type (member :linear :miser :fill :mandatory) kind)
678 (type (or stream (member t nil)) stream)
680 (let ((stream (case stream
682 ((nil) *standard-output*)
684 (when (print-pretty-on-stream-p stream)
685 (enqueue-newline stream kind)))
688 (defun pprint-indent (relative-to n &optional stream)
690 "Specify the indentation to use in the current logical block if STREAM
691 (which defaults to *STANDARD-OUTPUT*) is it is a pretty-printing stream
692 and do nothing if not. (See PPRINT-LOGICAL-BLOCK.) N is the indention
693 to use (in ems, the width of an ``m'') and RELATIVE-TO can be either:
694 :BLOCK - Indent relative to the column the current logical block
696 :CURRENT - Indent relative to the current column.
697 The new indention value does not take effect until the following line
699 (declare (type (member :block :current) relative-to)
701 (type (or stream (member t nil)) stream)
703 (let ((stream (case stream
705 ((nil) *standard-output*)
707 (when (print-pretty-on-stream-p stream)
708 (enqueue-indent stream relative-to n)))
711 (defun pprint-tab (kind colnum colinc &optional stream)
713 "If STREAM (which defaults to *STANDARD-OUTPUT*) is a pretty-printing
714 stream, perform tabbing based on KIND, otherwise do nothing. KIND can
716 :LINE - Tab to column COLNUM. If already past COLNUM tab to the next
718 :SECTION - Same as :LINE, but count from the start of the current
719 section, not the start of the line.
720 :LINE-RELATIVE - Output COLNUM spaces, then tab to the next multiple of
722 :SECTION-RELATIVE - Same as :LINE-RELATIVE, but count from the start
723 of the current section, not the start of the line."
724 (declare (type (member :line :section :line-relative :section-relative) kind)
725 (type unsigned-byte colnum colinc)
726 (type (or stream (member t nil)) stream)
728 (let ((stream (case stream
730 ((nil) *standard-output*)
732 (when (print-pretty-on-stream-p stream)
733 (enqueue-tab stream kind colnum colinc)))
736 (defun pprint-fill (stream list &optional (colon? t) atsign?)
738 "Output LIST to STREAM putting :FILL conditional newlines between each
739 element. If COLON? is NIL (defaults to T), then no parens are printed
740 around the output. ATSIGN? is ignored (but allowed so that PPRINT-FILL
741 can be used with the ~/.../ format directive."
742 (declare (ignore atsign?))
743 (pprint-logical-block (stream list
744 :prefix (if colon? "(" "")
745 :suffix (if colon? ")" ""))
746 (pprint-exit-if-list-exhausted)
748 (output-object (pprint-pop) stream)
749 (pprint-exit-if-list-exhausted)
750 (write-char #\space stream)
751 (pprint-newline :fill stream))))
753 (defun pprint-linear (stream list &optional (colon? t) atsign?)
755 "Output LIST to STREAM putting :LINEAR conditional newlines between each
756 element. If COLON? is NIL (defaults to T), then no parens are printed
757 around the output. ATSIGN? is ignored (but allowed so that PPRINT-LINEAR
758 can be used with the ~/.../ format directive."
759 (declare (ignore atsign?))
760 (pprint-logical-block (stream list
761 :prefix (if colon? "(" "")
762 :suffix (if colon? ")" ""))
763 (pprint-exit-if-list-exhausted)
765 (output-object (pprint-pop) stream)
766 (pprint-exit-if-list-exhausted)
767 (write-char #\space stream)
768 (pprint-newline :linear stream))))
770 (defun pprint-tabular (stream list &optional (colon? t) atsign? tabsize)
772 "Output LIST to STREAM tabbing to the next column that is an even multiple
773 of TABSIZE (which defaults to 16) between each element. :FILL style
774 conditional newlines are also output between each element. If COLON? is
775 NIL (defaults to T), then no parens are printed around the output.
776 ATSIGN? is ignored (but allowed so that PPRINT-TABULAR can be used with
777 the ~/.../ format directive."
778 (declare (ignore atsign?))
779 (pprint-logical-block (stream list
780 :prefix (if colon? "(" "")
781 :suffix (if colon? ")" ""))
782 (pprint-exit-if-list-exhausted)
784 (output-object (pprint-pop) stream)
785 (pprint-exit-if-list-exhausted)
786 (write-char #\space stream)
787 (pprint-tab :section-relative 0 (or tabsize 16) stream)
788 (pprint-newline :fill stream))))
790 ;;;; pprint-dispatch tables
792 (defvar *initial-pprint-dispatch*)
793 (defvar *building-initial-table* nil)
795 (defstruct (pprint-dispatch-entry (:copier nil))
796 ;; the type specifier for this entry
797 (type (missing-arg) :type t)
798 ;; a function to test to see whether an object is of this time.
799 ;; Pretty must just (LAMBDA (OBJ) (TYPEP OBJECT TYPE)) except that
800 ;; we handle the CONS type specially so that (CONS (MEMBER FOO))
801 ;; works. We don't bother computing this for entries in the CONS
802 ;; hash table, because we don't need it.
803 (test-fn nil :type (or function null))
804 ;; the priority for this guy
805 (priority 0 :type real)
806 ;; T iff one of the original entries.
807 (initial-p *building-initial-table* :type (member t nil))
808 ;; and the associated function
809 (fun (missing-arg) :type callable))
810 (def!method print-object ((entry pprint-dispatch-entry) stream)
811 (print-unreadable-object (entry stream :type t)
812 (format stream "type=~S, priority=~S~@[ [initial]~]"
813 (pprint-dispatch-entry-type entry)
814 (pprint-dispatch-entry-priority entry)
815 (pprint-dispatch-entry-initial-p entry))))
817 (defun cons-type-specifier-p (spec)
819 (eq (car spec) 'cons)
822 (let ((car (cadr spec)))
824 (let ((carcar (car car)))
825 (or (eq carcar 'member)
828 (null (cddr car))))))
830 (defun entry< (e1 e2)
831 (declare (type pprint-dispatch-entry e1 e2))
832 (if (pprint-dispatch-entry-initial-p e1)
833 (if (pprint-dispatch-entry-initial-p e2)
834 (< (pprint-dispatch-entry-priority e1)
835 (pprint-dispatch-entry-priority e2))
837 (if (pprint-dispatch-entry-initial-p e2)
839 (< (pprint-dispatch-entry-priority e1)
840 (pprint-dispatch-entry-priority e2)))))
843 `(cons ',x (lambda (object) ,x))))
844 (defvar *precompiled-pprint-dispatch-funs*
845 (list (frob (typep object 'array))
846 (frob (and (consp object)
847 (symbolp (car object))
848 (fboundp (car object))))
849 (frob (typep object 'cons)))))
851 (defun compute-test-fn (type)
852 (let ((was-cons nil))
853 (labels ((compute-test-expr (type object)
859 (&optional (car nil car-p) (cdr nil cdr-p))
861 `(and (consp ,object)
863 `(,(compute-test-expr
864 car `(car ,object))))
866 `(,(compute-test-expr
867 cdr `(cdr ,object)))))))
869 (destructuring-bind (type) (cdr type)
870 `(not ,(compute-test-expr type object))))
872 `(and ,@(mapcar (lambda (type)
873 (compute-test-expr type object))
876 `(or ,@(mapcar (lambda (type)
877 (compute-test-expr type object))
880 `(typep ,object ',type)))
881 `(typep ,object ',type))))
882 (let ((expr (compute-test-expr type 'object)))
883 (cond ((cdr (assoc expr *precompiled-pprint-dispatch-funs*
886 (compile nil `(lambda (object) ,expr))))))))
888 (defun copy-pprint-dispatch (&optional (table *print-pprint-dispatch*))
889 (declare (type (or pprint-dispatch-table null) table))
890 (let* ((orig (or table *initial-pprint-dispatch*))
891 (new (make-pprint-dispatch-table
892 :entries (copy-list (pprint-dispatch-table-entries orig))))
893 (new-cons-entries (pprint-dispatch-table-cons-entries new)))
894 (maphash (lambda (key value)
895 (setf (gethash key new-cons-entries) value))
896 (pprint-dispatch-table-cons-entries orig))
899 (defun pprint-dispatch (object &optional (table *print-pprint-dispatch*))
900 (declare (type (or pprint-dispatch-table null) table))
901 (let* ((table (or table *initial-pprint-dispatch*))
904 (gethash (car object)
905 (pprint-dispatch-table-cons-entries table))))
907 (dolist (entry (pprint-dispatch-table-entries table) cons-entry)
908 (when (and cons-entry
909 (entry< entry cons-entry))
911 (when (funcall (pprint-dispatch-entry-test-fn entry) object)
914 (values (pprint-dispatch-entry-fun entry) t)
915 (values (lambda (stream object)
916 (output-ugly-object object stream))
919 (defun set-pprint-dispatch (type function &optional
920 (priority 0) (table *print-pprint-dispatch*))
921 (declare (type (or null callable) function)
923 (type pprint-dispatch-table table))
924 (/show0 "entering SET-PPRINT-DISPATCH, TYPE=...")
927 (if (cons-type-specifier-p type)
928 (setf (gethash (second (second type))
929 (pprint-dispatch-table-cons-entries table))
930 (make-pprint-dispatch-entry :type type
933 (let ((list (delete type (pprint-dispatch-table-entries table)
934 :key #'pprint-dispatch-entry-type
936 (entry (make-pprint-dispatch-entry
938 :test-fn (compute-test-fn type)
942 (next list (cdr next)))
945 (setf (cdr prev) (list entry))
946 (setf list (list entry))))
947 (when (entry< (car next) entry)
949 (setf (cdr prev) (cons entry next))
950 (setf list (cons entry next)))
952 (setf (pprint-dispatch-table-entries table) list)))
953 (if (cons-type-specifier-p type)
954 (remhash (second (second type))
955 (pprint-dispatch-table-cons-entries table))
956 (setf (pprint-dispatch-table-entries table)
957 (delete type (pprint-dispatch-table-entries table)
958 :key #'pprint-dispatch-entry-type
960 (/show0 "about to return NIL from SET-PPRINT-DISPATCH")
963 ;;;; standard pretty-printing routines
965 (defun pprint-array (stream array)
966 (cond ((or (and (null *print-array*) (null *print-readably*))
968 (bit-vector-p array))
969 (output-ugly-object array stream))
970 ((and *print-readably*
971 (not (array-readably-printable-p array)))
972 (let ((*print-readably* nil))
973 (error 'print-not-readable :object array)))
975 (pprint-vector stream array))
977 (pprint-multi-dim-array stream array))))
979 (defun pprint-vector (stream vector)
980 (pprint-logical-block (stream nil :prefix "#(" :suffix ")")
981 (dotimes (i (length vector))
983 (format stream " ~:_"))
985 (output-object (aref vector i) stream))))
987 (defun pprint-multi-dim-array (stream array)
988 (funcall (formatter "#~DA") stream (array-rank array))
989 (with-array-data ((data array) (start) (end))
990 (declare (ignore end))
991 (labels ((output-guts (stream index dimensions)
992 (if (null dimensions)
993 (output-object (aref data index) stream)
994 (pprint-logical-block
995 (stream nil :prefix "(" :suffix ")")
996 (let ((dim (car dimensions)))
998 (let* ((dims (cdr dimensions))
1000 (step (reduce #'* dims))
1004 (output-guts stream index dims)
1005 (when (= (incf count) dim)
1007 (write-char #\space stream)
1008 (pprint-newline (if dims :linear :fill)
1010 (incf index step)))))))))
1011 (output-guts stream start (array-dimensions array)))))
1013 (defun pprint-lambda-list (stream lambda-list &rest noise)
1014 (declare (ignore noise))
1015 (when (and (consp lambda-list)
1016 (member (car lambda-list) *backq-tokens*))
1017 ;; if this thing looks like a backquoty thing, then we don't want
1018 ;; to destructure it, we want to output it straight away. [ this
1019 ;; is the exception to the normal processing: if we did this
1020 ;; generally we would find lambda lists such as (FUNCTION FOO)
1021 ;; being printed as #'FOO ] -- CSR, 2003-12-07
1022 (output-object lambda-list stream)
1023 (return-from pprint-lambda-list nil))
1024 (pprint-logical-block (stream lambda-list :prefix "(" :suffix ")")
1025 (let ((state :required)
1028 (pprint-exit-if-list-exhausted)
1030 (write-char #\space stream))
1031 (let ((arg (pprint-pop)))
1035 (setf state :optional)
1036 (pprint-newline :linear stream))
1038 (setf state :required)
1039 (pprint-newline :linear stream))
1042 (pprint-newline :linear stream))
1044 (setf state :optional)
1045 (pprint-newline :linear stream))
1047 (pprint-newline :fill stream))))
1050 (pprint-lambda-list stream arg))
1052 (pprint-logical-block
1053 (stream arg :prefix "(" :suffix ")")
1054 (pprint-exit-if-list-exhausted)
1056 (pprint-logical-block
1057 (stream (pprint-pop) :prefix "(" :suffix ")")
1058 (pprint-exit-if-list-exhausted)
1059 (output-object (pprint-pop) stream)
1060 (pprint-exit-if-list-exhausted)
1061 (write-char #\space stream)
1062 (pprint-newline :fill stream)
1063 (pprint-lambda-list stream (pprint-pop))
1065 (pprint-exit-if-list-exhausted)
1066 (write-char #\space stream)
1067 (pprint-newline :fill stream)
1068 (output-object (pprint-pop) stream)))
1069 (pprint-lambda-list stream (pprint-pop)))
1071 (pprint-exit-if-list-exhausted)
1072 (write-char #\space stream)
1073 (pprint-newline :linear stream)
1074 (output-object (pprint-pop) stream))))))
1075 (setf first nil)))))
1077 (defun pprint-lambda (stream list &rest noise)
1078 (declare (ignore noise))
1080 ;; KLUDGE: This format string, and other format strings which also
1081 ;; refer to SB!PRETTY, rely on the current SBCL not-quite-ANSI
1082 ;; behavior of FORMATTER in order to make code which survives the
1083 ;; transition when SB!PRETTY is renamed to SB-PRETTY after cold
1084 ;; init. (ANSI says that the FORMATTER functions should be
1085 ;; equivalent to the format string, but the SBCL FORMATTER
1086 ;; functions contain references to package objects, not package
1087 ;; names, so they keep right on going if the packages are renamed.)
1088 ;; If our FORMATTER behavior is ever made more compliant, the code
1089 ;; here will have to change. -- WHN 19991207
1090 "~:<~^~W~^~3I ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1094 (defun pprint-block (stream list &rest noise)
1095 (declare (ignore noise))
1096 (funcall (formatter "~:<~^~W~^~3I ~:_~W~1I~@{ ~_~W~}~:>") stream list))
1098 (defun pprint-flet (stream list &rest noise)
1099 (declare (ignore noise))
1101 "~:<~^~W~^ ~@_~:<~@{~:<~^~W~^~3I ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~:@_~@{~W~^ ~_~}~:>~^ ~_~}~:>~1I~@:_~@{~W~^ ~_~}~:>")
1105 (defun pprint-let (stream list &rest noise)
1106 (declare (ignore noise))
1107 (funcall (formatter "~:<~^~W~^ ~@_~:<~@{~:<~^~W~@{ ~_~W~}~:>~^ ~_~}~:>~1I~:@_~@{~W~^ ~_~}~:>")
1111 (defun pprint-progn (stream list &rest noise)
1112 (declare (ignore noise))
1113 (funcall (formatter "~:<~^~W~@{ ~_~W~}~:>") stream list))
1115 (defun pprint-progv (stream list &rest noise)
1116 (declare (ignore noise))
1117 (funcall (formatter "~:<~^~W~^~3I ~_~W~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1120 (defun pprint-quote (stream list &rest noise)
1121 (declare (ignore noise))
1122 (if (and (consp list)
1127 (write-string "#'" stream)
1128 (output-object (cadr list) stream))
1130 (write-char #\' stream)
1131 (output-object (cadr list) stream))
1133 (pprint-fill stream list)))
1134 (pprint-fill stream list)))
1136 (defun pprint-setq (stream list &rest noise)
1137 (declare (ignore noise))
1138 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1139 (pprint-exit-if-list-exhausted)
1140 (output-object (pprint-pop) stream)
1141 (pprint-exit-if-list-exhausted)
1142 (write-char #\space stream)
1143 (pprint-newline :miser stream)
1144 (if (and (consp (cdr list)) (consp (cddr list)))
1146 (pprint-indent :current 2 stream)
1147 (output-object (pprint-pop) stream)
1148 (pprint-exit-if-list-exhausted)
1149 (write-char #\space stream)
1150 (pprint-newline :linear stream)
1151 (pprint-indent :current -2 stream)
1152 (output-object (pprint-pop) stream)
1153 (pprint-exit-if-list-exhausted)
1154 (write-char #\space stream)
1155 (pprint-newline :linear stream))
1157 (pprint-indent :current 0 stream)
1158 (output-object (pprint-pop) stream)
1159 (pprint-exit-if-list-exhausted)
1160 (write-char #\space stream)
1161 (pprint-newline :linear stream)
1162 (output-object (pprint-pop) stream)))))
1164 ;;; FIXME: could become SB!XC:DEFMACRO wrapped in EVAL-WHEN (COMPILE EVAL)
1165 (defmacro pprint-tagbody-guts (stream)
1167 (pprint-exit-if-list-exhausted)
1168 (write-char #\space ,stream)
1169 (let ((form-or-tag (pprint-pop)))
1170 (pprint-indent :block
1171 (if (atom form-or-tag) 0 1)
1173 (pprint-newline :linear ,stream)
1174 (output-object form-or-tag ,stream))))
1176 (defun pprint-tagbody (stream list &rest noise)
1177 (declare (ignore noise))
1178 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1179 (pprint-exit-if-list-exhausted)
1180 (output-object (pprint-pop) stream)
1181 (pprint-tagbody-guts stream)))
1183 (defun pprint-case (stream list &rest noise)
1184 (declare (ignore noise))
1186 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~:/SB!PRETTY:PPRINT-FILL/~^~@{ ~_~W~}~:>~}~:>")
1190 (defun pprint-defun (stream list &rest noise)
1191 (declare (ignore noise))
1193 "~:<~^~W~^ ~@_~:I~W~^ ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1197 (defun pprint-destructuring-bind (stream list &rest noise)
1198 (declare (ignore noise))
1200 "~:<~^~W~^~3I ~_~:/SB!PRETTY:PPRINT-LAMBDA-LIST/~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1203 (defun pprint-do (stream list &rest noise)
1204 (declare (ignore noise))
1205 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1206 (pprint-exit-if-list-exhausted)
1207 (output-object (pprint-pop) stream)
1208 (pprint-exit-if-list-exhausted)
1209 (write-char #\space stream)
1210 (pprint-indent :current 0 stream)
1211 (funcall (formatter "~:<~@{~:<~^~W~^ ~@_~:I~W~@{ ~_~W~}~:>~^~:@_~}~:>")
1214 (pprint-exit-if-list-exhausted)
1215 (write-char #\space stream)
1216 (pprint-newline :linear stream)
1217 (pprint-linear stream (pprint-pop))
1218 (pprint-tagbody-guts stream)))
1220 (defun pprint-dolist (stream list &rest noise)
1221 (declare (ignore noise))
1222 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1223 (pprint-exit-if-list-exhausted)
1224 (output-object (pprint-pop) stream)
1225 (pprint-exit-if-list-exhausted)
1226 (pprint-indent :block 3 stream)
1227 (write-char #\space stream)
1228 (pprint-newline :fill stream)
1229 (funcall (formatter "~:<~^~W~^ ~:_~:I~W~@{ ~_~W~}~:>")
1232 (pprint-tagbody-guts stream)))
1234 (defun pprint-typecase (stream list &rest noise)
1235 (declare (ignore noise))
1237 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~W~^~@{ ~_~W~}~:>~}~:>")
1241 (defun pprint-prog (stream list &rest noise)
1242 (declare (ignore noise))
1243 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1244 (pprint-exit-if-list-exhausted)
1245 (output-object (pprint-pop) stream)
1246 (pprint-exit-if-list-exhausted)
1247 (write-char #\space stream)
1248 (pprint-newline :miser stream)
1249 (pprint-fill stream (pprint-pop))
1250 (pprint-tagbody-guts stream)))
1252 (defun pprint-fun-call (stream list &rest noise)
1253 (declare (ignore noise))
1254 (funcall (formatter "~:<~^~W~^ ~:_~:I~@{~W~^ ~_~}~:>")
1258 ;;;; the interface seen by regular (ugly) printer and initialization routines
1260 ;;; OUTPUT-PRETTY-OBJECT is called by OUTPUT-OBJECT when
1261 ;;; *PRINT-PRETTY* is true.
1262 (defun output-pretty-object (object stream)
1263 (with-pretty-stream (stream)
1264 (funcall (pprint-dispatch object) stream object)))
1266 (defun !pprint-cold-init ()
1267 (/show0 "entering !PPRINT-COLD-INIT")
1268 (setf *initial-pprint-dispatch* (make-pprint-dispatch-table))
1269 (let ((*print-pprint-dispatch* *initial-pprint-dispatch*)
1270 (*building-initial-table* t))
1271 ;; printers for regular types
1272 (/show0 "doing SET-PPRINT-DISPATCH for regular types")
1273 (set-pprint-dispatch 'array #'pprint-array)
1274 (set-pprint-dispatch '(cons symbol)
1275 #'pprint-fun-call -1)
1276 (set-pprint-dispatch 'cons #'pprint-fill -2)
1277 ;; cons cells with interesting things for the car
1278 (/show0 "doing SET-PPRINT-DISPATCH for CONS with interesting CAR")
1280 (dolist (magic-form '((lambda pprint-lambda)
1283 (block pprint-block)
1284 (catch pprint-block)
1285 (eval-when pprint-block)
1287 (function pprint-quote)
1288 (labels pprint-flet)
1291 (locally pprint-progn)
1292 (macrolet pprint-flet)
1293 (multiple-value-call pprint-block)
1294 (multiple-value-prog1 pprint-block)
1295 (progn pprint-progn)
1296 (progv pprint-progv)
1297 (quote pprint-quote)
1298 (return-from pprint-block)
1300 (symbol-macrolet pprint-let)
1301 (tagbody pprint-tagbody)
1302 (throw pprint-block)
1303 (unwind-protect pprint-block)
1308 (ctypecase pprint-typecase)
1309 (defconstant pprint-block)
1310 (define-modify-macro pprint-defun)
1311 (define-setf-expander pprint-defun)
1312 (defmacro pprint-defun)
1313 (defparameter pprint-block)
1314 (defsetf pprint-defun)
1315 (defstruct pprint-block)
1316 (deftype pprint-defun)
1317 (defun pprint-defun)
1318 (defvar pprint-block)
1319 (destructuring-bind pprint-destructuring-bind)
1322 (do-all-symbols pprint-dolist)
1323 (do-external-symbols pprint-dolist)
1324 (do-symbols pprint-dolist)
1325 (dolist pprint-dolist)
1326 (dotimes pprint-dolist)
1328 (etypecase pprint-typecase)
1329 #+nil (handler-bind ...)
1330 #+nil (handler-case ...)
1332 (multiple-value-bind pprint-progv)
1333 (multiple-value-setq pprint-block)
1334 (pprint-logical-block pprint-block)
1335 (print-unreadable-object pprint-block)
1338 (prog1 pprint-block)
1339 (prog2 pprint-progv)
1342 #+nil (restart-bind ...)
1343 #+nil (restart-case ...)
1347 (typecase pprint-typecase)
1348 (unless pprint-block)
1350 (with-compilation-unit pprint-block)
1351 #+nil (with-condition-restarts ...)
1352 (with-hash-table-iterator pprint-block)
1353 (with-input-from-string pprint-block)
1354 (with-open-file pprint-block)
1355 (with-open-stream pprint-block)
1356 (with-output-to-string pprint-block)
1357 (with-package-iterator pprint-block)
1358 (with-simple-restart pprint-block)
1359 (with-standard-io-syntax pprint-progn)))
1361 (set-pprint-dispatch `(cons (eql ,(first magic-form)))
1362 (symbol-function (second magic-form))))
1364 ;; other pretty-print init forms
1365 (/show0 "about to call !BACKQ-PP-COLD-INIT")
1366 (sb!impl::!backq-pp-cold-init)
1367 (/show0 "leaving !PPRINT-COLD-INIT"))
1369 (setf *print-pprint-dispatch* (copy-pprint-dispatch nil))
1370 (setf *print-pretty* t))