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