Declare EXPLICIT-CHECK on CONCATENATE, MAKE-STRING, SET-PPRINT-DISPATCH.
[sbcl.git] / src / code / pprint.lisp
bloba5b9930235509d73c67558bb31db1d1c3c46417a
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 ;;;; pretty streams
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
20 ;;; the output buffer
21 (deftype column ()
22 '(and fixnum unsigned-byte))
23 ;;; The INDEX type is picked up from the kernel package.
24 (deftype posn ()
25 'fixnum)
27 (defconstant initial-buffer-size 128)
29 (defconstant default-line-length 80)
31 ;; We're allowed to DXify the pretty-stream used by PPRINT-LOGICAL-BLOCK.
32 ;; "pprint-logical-block and the pretty printing stream it creates have
33 ;; dynamic extent. The consequences are undefined if, outside of this
34 ;; extent, output is attempted to the pretty printing stream it creates."
35 ;; However doing that is slightly dangerous since there are a zillion ways
36 ;; for users to get a hold of the stream and stash it somewhere.
37 ;; Anyway, just a thought...
38 (declaim (maybe-inline make-pretty-stream))
39 (defstruct (pretty-stream (:include ansi-stream
40 (out #'pretty-out)
41 (sout #'pretty-sout)
42 (misc #'pretty-misc))
43 (:constructor make-pretty-stream (target))
44 (:copier nil))
45 ;; Where the output is going to finally go.
46 (target (missing-arg) :type stream :read-only t)
47 ;; Line length we should format to. Cached here so we don't have to keep
48 ;; extracting it from the target stream.
49 (line-length (or *print-right-margin*
50 (sb!impl::line-length target)
51 default-line-length)
52 :type column
53 :read-only t)
54 ;; If non-nil, a function to call before performing OUT or SOUT
55 (char-out-oneshot-hook nil :type (or null function))
56 ;; A simple string holding all the text that has been output but not yet
57 ;; printed.
58 (buffer (make-string initial-buffer-size) :type (simple-array character (*)))
59 ;; The index into BUFFER where more text should be put.
60 (buffer-fill-pointer 0 :type index)
61 ;; Whenever we output stuff from the buffer, we shift the remaining noise
62 ;; over. This makes it difficult to keep references to locations in
63 ;; the buffer. Therefore, we have to keep track of the total amount of
64 ;; stuff that has been shifted out of the buffer.
65 (buffer-offset 0 :type posn)
66 ;; The column the first character in the buffer will appear in. Normally
67 ;; zero, but if we end up with a very long line with no breaks in it we
68 ;; might have to output part of it. Then this will no longer be zero.
69 (buffer-start-column (or (sb!impl::charpos target) 0) :type column)
70 ;; The line number we are currently on. Used for *PRINT-LINES*
71 ;; abbreviations and to tell when sections have been split across
72 ;; multiple lines.
73 (line-number 0 :type index)
74 ;; the value of *PRINT-LINES* captured at object creation time. We
75 ;; use this, instead of the dynamic *PRINT-LINES*, to avoid
76 ;; weirdness like
77 ;; (let ((*print-lines* 50))
78 ;; (pprint-logical-block ..
79 ;; (dotimes (i 10)
80 ;; (let ((*print-lines* 8))
81 ;; (print (aref possiblybigthings i) prettystream)))))
82 ;; terminating the output of the entire logical blockafter 8 lines.
83 (print-lines *print-lines* :type (or index null) :read-only t)
84 ;; Stack of logical blocks in effect at the buffer start.
85 (blocks (list (make-logical-block)) :type list)
86 ;; Buffer holding the per-line prefix active at the buffer start.
87 ;; Indentation is included in this. The length of this is stored
88 ;; in the logical block stack.
89 (prefix (make-string initial-buffer-size) :type (simple-array character (*)))
90 ;; Buffer holding the total remaining suffix active at the buffer start.
91 ;; The characters are right-justified in the buffer to make it easier
92 ;; to output the buffer. The length is stored in the logical block
93 ;; stack.
94 (suffix (make-string initial-buffer-size) :type (simple-array character (*)))
95 ;; Queue of pending operations. When empty, HEAD=TAIL=NIL. Otherwise,
96 ;; TAIL holds the first (oldest) cons and HEAD holds the last (newest)
97 ;; cons. Adding things to the queue is basically (setf (cdr head) (list
98 ;; new)) and removing them is basically (pop tail) [except that care must
99 ;; be taken to handle the empty queue case correctly.]
100 (queue-tail nil :type list)
101 (queue-head nil :type list)
102 ;; Block-start queue entries in effect at the queue head.
103 (pending-blocks nil :type list))
104 (def!method print-object ((pstream pretty-stream) stream)
105 ;; FIXME: CMU CL had #+NIL'ed out this code and done a hand-written
106 ;; FORMAT hack instead. Make sure that this code actually works instead
107 ;; of falling into infinite regress or something.
108 (print-unreadable-object (pstream stream :type t :identity t)))
110 #!-sb-fluid (declaim (inline index-posn posn-index posn-column))
111 (defun index-posn (index stream)
112 (declare (type index index) (type pretty-stream stream)
113 (values posn))
114 (+ index (pretty-stream-buffer-offset stream)))
115 (defun posn-index (posn stream)
116 (declare (type posn posn) (type pretty-stream stream)
117 (values index))
118 (- posn (pretty-stream-buffer-offset stream)))
119 (defun posn-column (posn stream)
120 (declare (type posn posn) (type pretty-stream stream)
121 (values posn))
122 (index-column (posn-index posn stream) stream))
124 ;;; Is it OK to do pretty printing on this stream at this time?
125 (defun print-pretty-on-stream-p (stream)
126 (and (pretty-stream-p stream)
127 *print-pretty*))
129 ;;;; stream interface routines
131 (defun pretty-out (stream char)
132 (declare (type pretty-stream stream)
133 (type character char))
134 (let ((f (pretty-stream-char-out-oneshot-hook stream)))
135 (when f
136 (setf (pretty-stream-char-out-oneshot-hook stream) nil)
137 (funcall f stream char)))
138 (cond ((char= char #\newline)
139 (enqueue-newline stream :literal))
141 (ensure-space-in-buffer stream 1)
142 (let ((fill-pointer (pretty-stream-buffer-fill-pointer stream)))
143 (setf (schar (pretty-stream-buffer stream) fill-pointer) char)
144 (setf (pretty-stream-buffer-fill-pointer stream)
145 (1+ fill-pointer))))))
147 (defun pretty-sout (stream string start end)
148 (declare (type pretty-stream stream)
149 (type simple-string string)
150 (type index start)
151 (type (or index null) end))
152 (let* ((end (or end (length string))))
153 (unless (= start end)
154 (sb!impl::string-dispatch (simple-base-string
155 #!+sb-unicode
156 (simple-array character (*)))
157 string
158 ;; For POSITION transform
159 (declare (optimize (speed 2)))
160 (let ((f (pretty-stream-char-out-oneshot-hook stream)))
161 (when f
162 (setf (pretty-stream-char-out-oneshot-hook stream) nil)
163 (funcall f stream (aref string start))))
164 (let ((newline (position #\newline string :start start :end end)))
165 (cond
166 (newline
167 (pretty-sout stream string start newline)
168 (enqueue-newline stream :literal)
169 (pretty-sout stream string (1+ newline) end))
171 (let ((chars (- end start)))
172 (loop
173 (let* ((available (ensure-space-in-buffer stream chars))
174 (count (min available chars))
175 (fill-pointer (pretty-stream-buffer-fill-pointer
176 stream))
177 (new-fill-ptr (+ fill-pointer count)))
178 (declare (fixnum available count))
179 (if (typep string 'simple-base-string)
180 ;; FIXME: Reimplementing REPLACE, since it
181 ;; can't be inlined and we don't have a
182 ;; generic "simple-array -> simple-array"
183 ;; transform for it.
184 (loop for i from fill-pointer below new-fill-ptr
185 for j from start
186 with target = (pretty-stream-buffer stream)
187 do (setf (aref target i)
188 (aref string j)))
189 (replace (pretty-stream-buffer stream)
190 string
191 :start1 fill-pointer :end1 new-fill-ptr
192 :start2 start))
193 (setf (pretty-stream-buffer-fill-pointer stream)
194 new-fill-ptr)
195 (decf chars count)
196 (when (zerop count)
197 (return))
198 (incf start count)))))))))))
200 (defun pretty-misc (stream op &optional arg1 arg2)
201 (declare (ignore stream op arg1 arg2)))
203 ;;;; logical blocks
205 (defstruct (logical-block (:copier nil))
206 ;; The column this logical block started in.
207 (start-column 0 :type column)
208 ;; The column the current section started in.
209 (section-column 0 :type column)
210 ;; The length of the per-line prefix. We can't move the indentation
211 ;; left of this.
212 (per-line-prefix-end 0 :type index)
213 ;; The overall length of the prefix, including any indentation.
214 (prefix-length 0 :type index)
215 ;; The overall length of the suffix.
216 (suffix-length 0 :type index)
217 ;; The line number
218 (section-start-line 0 :type index))
220 (defun really-start-logical-block (stream column prefix suffix)
221 (let* ((blocks (pretty-stream-blocks stream))
222 (prev-block (car blocks))
223 (per-line-end (logical-block-per-line-prefix-end prev-block))
224 (prefix-length (logical-block-prefix-length prev-block))
225 (suffix-length (logical-block-suffix-length prev-block))
226 (block (make-logical-block
227 :start-column column
228 :section-column column
229 :per-line-prefix-end per-line-end
230 :prefix-length prefix-length
231 :suffix-length suffix-length
232 :section-start-line (pretty-stream-line-number stream))))
233 (setf (pretty-stream-blocks stream) (cons block blocks))
234 (set-indentation stream column)
235 (when prefix
236 (setf (logical-block-per-line-prefix-end block) column)
237 (replace (pretty-stream-prefix stream) prefix
238 :start1 (- column (length prefix)) :end1 column))
239 (when suffix
240 (let* ((total-suffix (pretty-stream-suffix stream))
241 (total-suffix-len (length total-suffix))
242 (additional (length suffix))
243 (new-suffix-len (+ suffix-length additional)))
244 (when (> new-suffix-len total-suffix-len)
245 (let ((new-total-suffix-len
246 (max (* total-suffix-len 2)
247 (+ suffix-length
248 (floor (* additional 5) 4)))))
249 (setf total-suffix
250 (replace (make-string new-total-suffix-len) total-suffix
251 :start1 (- new-total-suffix-len suffix-length)
252 :start2 (- total-suffix-len suffix-length)))
253 (setf total-suffix-len new-total-suffix-len)
254 (setf (pretty-stream-suffix stream) total-suffix)))
255 (replace total-suffix suffix
256 :start1 (- total-suffix-len new-suffix-len)
257 :end1 (- total-suffix-len suffix-length))
258 (setf (logical-block-suffix-length block) new-suffix-len))))
259 nil)
261 (defun set-indentation (stream column)
262 (let* ((prefix (pretty-stream-prefix stream))
263 (prefix-len (length prefix))
264 (block (car (pretty-stream-blocks stream)))
265 (current (logical-block-prefix-length block))
266 (minimum (logical-block-per-line-prefix-end block))
267 (column (max minimum column)))
268 (when (> column prefix-len)
269 (setf prefix
270 (replace (make-string (max (* prefix-len 2)
271 (+ prefix-len
272 (floor (* (- column prefix-len) 5)
273 4))))
274 prefix
275 :end1 current))
276 (setf (pretty-stream-prefix stream) prefix))
277 (when (> column current)
278 (fill prefix #\space :start current :end column))
279 (setf (logical-block-prefix-length block) column)))
281 (defun really-end-logical-block (stream)
282 (let* ((old (pop (pretty-stream-blocks stream)))
283 (old-indent (logical-block-prefix-length old))
284 (new (car (pretty-stream-blocks stream)))
285 (new-indent (logical-block-prefix-length new)))
286 (when (> new-indent old-indent)
287 (fill (pretty-stream-prefix stream) #\space
288 :start old-indent :end new-indent)))
289 nil)
291 ;;;; the pending operation queue
293 (defstruct (queued-op (:constructor nil)
294 (:copier nil))
295 (posn 0 :type posn))
297 (defmacro enqueue (stream type &rest args)
298 (let ((constructor (symbolicate "MAKE-" type)))
299 (once-only ((stream stream)
300 (entry `(,constructor :posn
301 (index-posn
302 (pretty-stream-buffer-fill-pointer
303 ,stream)
304 ,stream)
305 ,@args))
306 (op `(list ,entry))
307 (head `(pretty-stream-queue-head ,stream)))
308 `(progn
309 (if ,head
310 (setf (cdr ,head) ,op)
311 (setf (pretty-stream-queue-tail ,stream) ,op))
312 (setf (pretty-stream-queue-head ,stream) ,op)
313 ,entry))))
315 (defstruct (section-start (:include queued-op)
316 (:constructor nil)
317 (:copier nil))
318 (depth 0 :type index)
319 (section-end nil :type (or null newline block-end)))
321 (defstruct (newline (:include section-start)
322 (:copier nil))
323 (kind (missing-arg)
324 :type (member :linear :fill :miser :literal :mandatory)))
326 (defun enqueue-newline (stream kind)
327 (let* ((depth (length (pretty-stream-pending-blocks stream)))
328 (newline (enqueue stream newline :kind kind :depth depth)))
329 (dolist (entry (pretty-stream-queue-tail stream))
330 (when (and (not (eq newline entry))
331 (section-start-p entry)
332 (null (section-start-section-end entry))
333 (<= depth (section-start-depth entry)))
334 (setf (section-start-section-end entry) newline))))
335 (maybe-output stream (or (eq kind :literal) (eq kind :mandatory))))
337 (defstruct (indentation (:include queued-op)
338 (:copier nil))
339 (kind (missing-arg) :type (member :block :current))
340 (amount 0 :type fixnum))
342 (defun enqueue-indent (stream kind amount)
343 (enqueue stream indentation :kind kind :amount amount))
345 (defstruct (block-start (:include section-start)
346 (:copier nil))
347 (block-end nil :type (or null block-end))
348 (prefix nil :type (or null simple-string))
349 (suffix nil :type (or null simple-string)))
351 (defun start-logical-block (stream prefix per-line-p suffix)
352 ;; (In the PPRINT-LOGICAL-BLOCK form which calls us,
353 ;; :PREFIX and :PER-LINE-PREFIX have hairy defaulting behavior,
354 ;; and might end up being NIL.)
355 (declare (type (or null string) prefix))
356 ;; (But the defaulting behavior of PPRINT-LOGICAL-BLOCK :SUFFIX is
357 ;; trivial, so it should always be a string.)
358 (declare (type string suffix))
359 (when prefix
360 (unless (typep prefix 'simple-string)
361 (setq prefix (coerce prefix '(simple-array character (*)))))
362 (pretty-sout stream prefix 0 (length prefix)))
363 (unless (typep suffix 'simple-string)
364 (setq suffix (coerce suffix '(simple-array character (*)))))
365 (let* ((pending-blocks (pretty-stream-pending-blocks stream))
366 (start (enqueue stream block-start
367 :prefix (and per-line-p prefix)
368 :suffix suffix
369 :depth (length pending-blocks))))
370 (setf (pretty-stream-pending-blocks stream)
371 (cons start pending-blocks))))
373 (defstruct (block-end (:include queued-op)
374 (:copier nil))
375 (suffix nil :type (or null simple-string)))
377 (defun end-logical-block (stream)
378 (let* ((start (pop (pretty-stream-pending-blocks stream)))
379 (suffix (block-start-suffix start))
380 (end (enqueue stream block-end :suffix suffix)))
381 (when suffix
382 (pretty-sout stream suffix 0 (length suffix)))
383 (setf (block-start-block-end start) end)))
385 (defstruct (tab (:include queued-op)
386 (:copier nil))
387 (sectionp nil :type (member t nil))
388 (relativep nil :type (member t nil))
389 (colnum 0 :type column)
390 (colinc 0 :type column))
392 (defun enqueue-tab (stream kind colnum colinc)
393 (multiple-value-bind (sectionp relativep)
394 (ecase kind
395 (:line (values nil nil))
396 (:line-relative (values nil t))
397 (:section (values t nil))
398 (:section-relative (values t t)))
399 (enqueue stream tab :sectionp sectionp :relativep relativep
400 :colnum colnum :colinc colinc)))
402 ;;;; tab support
404 (defun compute-tab-size (tab section-start column)
405 (let* ((origin (if (tab-sectionp tab) section-start 0))
406 (colnum (tab-colnum tab))
407 (colinc (tab-colinc tab))
408 (position (- column origin)))
409 (cond ((tab-relativep tab)
410 (unless (<= colinc 1)
411 (let ((newposn (+ position colnum)))
412 (let ((rem (rem newposn colinc)))
413 (unless (zerop rem)
414 (incf colnum (- colinc rem))))))
415 colnum)
416 ((< position colnum)
417 (- colnum position))
418 ((zerop colinc) 0)
420 (- colinc
421 (rem (- position colnum) colinc))))))
423 (defun index-column (index stream)
424 (let ((column (pretty-stream-buffer-start-column stream))
425 (section-start (logical-block-section-column
426 (first (pretty-stream-blocks stream))))
427 (end-posn (index-posn index stream)))
428 (dolist (op (pretty-stream-queue-tail stream))
429 (when (>= (queued-op-posn op) end-posn)
430 (return))
431 (typecase op
432 (tab
433 (incf column
434 (compute-tab-size op
435 section-start
436 (+ column
437 (posn-index (tab-posn op)
438 stream)))))
439 ((or newline block-start)
440 (setf section-start
441 (+ column (posn-index (queued-op-posn op)
442 stream))))))
443 (+ column index)))
445 (defun expand-tabs (stream through)
446 (let ((insertions nil)
447 (additional 0)
448 (column (pretty-stream-buffer-start-column stream))
449 (section-start (logical-block-section-column
450 (first (pretty-stream-blocks stream)))))
451 (dolist (op (pretty-stream-queue-tail stream))
452 (typecase op
453 (tab
454 (let* ((index (posn-index (tab-posn op) stream))
455 (tabsize (compute-tab-size op
456 section-start
457 (+ column index))))
458 (unless (zerop tabsize)
459 (push (cons index tabsize) insertions)
460 (incf additional tabsize)
461 (incf column tabsize))))
462 ((or newline block-start)
463 (setf section-start
464 (+ column (posn-index (queued-op-posn op) stream)))))
465 (when (eq op through)
466 (return)))
467 (when insertions
468 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
469 (new-fill-ptr (+ fill-ptr additional))
470 (buffer (pretty-stream-buffer stream))
471 (new-buffer buffer)
472 (length (length buffer))
473 (end fill-ptr))
474 (when (> new-fill-ptr length)
475 (let ((new-length (max (* length 2)
476 (+ fill-ptr
477 (floor (* additional 5) 4)))))
478 (setf new-buffer (make-string new-length))
479 (setf (pretty-stream-buffer stream) new-buffer)))
480 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
481 (decf (pretty-stream-buffer-offset stream) additional)
482 (dolist (insertion insertions)
483 (let* ((srcpos (car insertion))
484 (amount (cdr insertion))
485 (dstpos (+ srcpos additional)))
486 (replace new-buffer buffer :start1 dstpos :start2 srcpos :end2 end)
487 (fill new-buffer #\space :start (- dstpos amount) :end dstpos)
488 (decf additional amount)
489 (setf end srcpos)))
490 (unless (eq new-buffer buffer)
491 (replace new-buffer buffer :end1 end :end2 end))))))
493 ;;;; stuff to do the actual outputting
495 (defun ensure-space-in-buffer (stream want)
496 (declare (type pretty-stream stream)
497 (type index want))
498 (let* ((buffer (pretty-stream-buffer stream))
499 (length (length buffer))
500 (fill-ptr (pretty-stream-buffer-fill-pointer stream))
501 (available (- length fill-ptr)))
502 (cond ((plusp available)
503 available)
504 ((> fill-ptr (pretty-stream-line-length stream))
505 (unless (maybe-output stream nil)
506 (output-partial-line stream))
507 (ensure-space-in-buffer stream want))
509 (let* ((new-length (max (* length 2)
510 (+ length
511 (floor (* want 5) 4))))
512 (new-buffer (make-string new-length)))
513 (setf (pretty-stream-buffer stream) new-buffer)
514 (replace new-buffer buffer :end1 fill-ptr)
515 (- new-length fill-ptr))))))
517 (defun maybe-output (stream force-newlines-p)
518 (declare (type pretty-stream stream))
519 (let ((tail (pretty-stream-queue-tail stream))
520 (output-anything nil))
521 (loop
522 (unless tail
523 (setf (pretty-stream-queue-head stream) nil)
524 (return))
525 (let ((next (pop tail)))
526 (etypecase next
527 (newline
528 (when (ecase (newline-kind next)
529 ((:literal :mandatory :linear) t)
530 (:miser (misering-p stream))
531 (:fill
532 (or (misering-p stream)
533 (> (pretty-stream-line-number stream)
534 (logical-block-section-start-line
535 (first (pretty-stream-blocks stream))))
536 (ecase (fits-on-line-p stream
537 (newline-section-end next)
538 force-newlines-p)
539 ((t) nil)
540 ((nil) t)
541 (:dont-know
542 (return))))))
543 (setf output-anything t)
544 (output-line stream next)))
545 (indentation
546 (unless (misering-p stream)
547 (set-indentation stream
548 (+ (ecase (indentation-kind next)
549 (:block
550 (logical-block-start-column
551 (car (pretty-stream-blocks stream))))
552 (:current
553 (posn-column
554 (indentation-posn next)
555 stream)))
556 (indentation-amount next)))))
557 (block-start
558 (ecase (fits-on-line-p stream (block-start-section-end next)
559 force-newlines-p)
560 ((t)
561 ;; Just nuke the whole logical block and make it look
562 ;; like one nice long literal.
563 (let ((end (block-start-block-end next)))
564 (expand-tabs stream end)
565 (setf tail (cdr (member end tail)))))
566 ((nil)
567 (really-start-logical-block
568 stream
569 (posn-column (block-start-posn next) stream)
570 (block-start-prefix next)
571 (block-start-suffix next)))
572 (:dont-know
573 (return))))
574 (block-end
575 (really-end-logical-block stream))
576 (tab
577 (expand-tabs stream next))))
578 (setf (pretty-stream-queue-tail stream) tail))
579 output-anything))
581 (defun misering-p (stream)
582 (declare (type pretty-stream stream))
583 (and *print-miser-width*
584 (<= (- (pretty-stream-line-length stream)
585 (logical-block-start-column (car (pretty-stream-blocks stream))))
586 *print-miser-width*)))
588 (defun fits-on-line-p (stream until force-newlines-p)
589 (let ((available (pretty-stream-line-length stream)))
590 (when (and (not *print-readably*)
591 (pretty-stream-print-lines stream)
592 (= (pretty-stream-print-lines stream)
593 (pretty-stream-line-number stream)))
594 (decf available 3) ; for the `` ..''
595 (decf available (logical-block-suffix-length
596 (car (pretty-stream-blocks stream)))))
597 (cond (until
598 (<= (posn-column (queued-op-posn until) stream) available))
599 (force-newlines-p nil)
600 ((> (index-column (pretty-stream-buffer-fill-pointer stream) stream)
601 available)
602 nil)
604 :dont-know))))
606 (defun output-line (stream until)
607 (declare (type pretty-stream stream)
608 (type newline until))
609 (let* ((target (pretty-stream-target stream))
610 (buffer (pretty-stream-buffer stream))
611 (kind (newline-kind until))
612 (literal-p (eq kind :literal))
613 (amount-to-consume (posn-index (newline-posn until) stream))
614 (amount-to-print
615 (if literal-p
616 amount-to-consume
617 (let ((last-non-blank
618 (position #\space buffer :end amount-to-consume
619 :from-end t :test #'char/=)))
620 (if last-non-blank
621 (1+ last-non-blank)
622 0)))))
623 (write-string buffer target :end amount-to-print)
624 (let ((line-number (pretty-stream-line-number stream)))
625 (incf line-number)
626 (when (and (not *print-readably*)
627 (pretty-stream-print-lines stream)
628 (>= line-number (pretty-stream-print-lines stream)))
629 (write-string " .." target)
630 (let ((suffix-length (logical-block-suffix-length
631 (car (pretty-stream-blocks stream)))))
632 (unless (zerop suffix-length)
633 (let* ((suffix (pretty-stream-suffix stream))
634 (len (length suffix)))
635 (write-string suffix target
636 :start (- len suffix-length)
637 :end len))))
638 (throw 'line-limit-abbreviation-happened t))
639 (setf (pretty-stream-line-number stream) line-number)
640 (write-char #\newline target)
641 (setf (pretty-stream-buffer-start-column stream) 0)
642 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
643 (block (first (pretty-stream-blocks stream)))
644 (prefix-len
645 (if literal-p
646 (logical-block-per-line-prefix-end block)
647 (logical-block-prefix-length block)))
648 (shift (- amount-to-consume prefix-len))
649 (new-fill-ptr (- fill-ptr shift))
650 (new-buffer buffer)
651 (buffer-length (length buffer)))
652 (when (> new-fill-ptr buffer-length)
653 (setf new-buffer
654 (make-string (max (* buffer-length 2)
655 (+ buffer-length
656 (floor (* (- new-fill-ptr buffer-length)
658 4)))))
659 (setf (pretty-stream-buffer stream) new-buffer))
660 (replace new-buffer buffer
661 :start1 prefix-len :start2 amount-to-consume :end2 fill-ptr)
662 (replace new-buffer (pretty-stream-prefix stream)
663 :end1 prefix-len)
664 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
665 (incf (pretty-stream-buffer-offset stream) shift)
666 (unless literal-p
667 (setf (logical-block-section-column block) prefix-len)
668 (setf (logical-block-section-start-line block) line-number))))))
670 (defun output-partial-line (stream)
671 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
672 (tail (pretty-stream-queue-tail stream))
673 (count
674 (if tail
675 (posn-index (queued-op-posn (car tail)) stream)
676 fill-ptr))
677 (new-fill-ptr (- fill-ptr count))
678 (buffer (pretty-stream-buffer stream)))
679 (when (zerop count)
680 (error "Output-partial-line called when nothing can be output."))
681 (write-string buffer (pretty-stream-target stream)
682 :start 0 :end count)
683 (incf (pretty-stream-buffer-start-column stream) count)
684 (replace buffer buffer :end1 new-fill-ptr :start2 count :end2 fill-ptr)
685 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
686 (incf (pretty-stream-buffer-offset stream) count)))
688 (defun force-pretty-output (stream)
689 (maybe-output stream nil)
690 (expand-tabs stream nil)
691 (write-string (pretty-stream-buffer stream)
692 (pretty-stream-target stream)
693 :end (pretty-stream-buffer-fill-pointer stream)))
695 ;;;; user interface to the pretty printer
697 (defun pprint-newline (kind &optional stream)
698 #!+sb-doc
699 "Output a conditional newline to STREAM (which defaults to
700 *STANDARD-OUTPUT*) if it is a pretty-printing stream, and do
701 nothing if not. KIND can be one of:
702 :LINEAR - A line break is inserted if and only if the immediately
703 containing section cannot be printed on one line.
704 :MISER - Same as LINEAR, but only if ``miser-style'' is in effect.
705 (See *PRINT-MISER-WIDTH*.)
706 :FILL - A line break is inserted if and only if either:
707 (a) the following section cannot be printed on the end of the
708 current line,
709 (b) the preceding section was not printed on a single line, or
710 (c) the immediately containing section cannot be printed on one
711 line and miser-style is in effect.
712 :MANDATORY - A line break is always inserted.
713 When a line break is inserted by any type of conditional newline, any
714 blanks that immediately precede the conditional newline are omitted
715 from the output and indentation is introduced at the beginning of the
716 next line. (See PPRINT-INDENT.)"
717 (declare (type (member :linear :miser :fill :mandatory) kind)
718 (type stream-designator stream)
719 (values null))
720 (let ((stream (out-synonym-of stream)))
721 (when (print-pretty-on-stream-p stream)
722 (enqueue-newline stream kind)))
723 nil)
725 (defun pprint-indent (relative-to n &optional stream)
726 #!+sb-doc
727 "Specify the indentation to use in the current logical block if
728 STREAM \(which defaults to *STANDARD-OUTPUT*) is a pretty-printing
729 stream and do nothing if not. (See PPRINT-LOGICAL-BLOCK.) N is the
730 indentation to use (in ems, the width of an ``m'') and RELATIVE-TO can
731 be either:
733 :BLOCK - Indent relative to the column the current logical block
734 started on.
736 :CURRENT - Indent relative to the current column.
738 The new indentation value does not take effect until the following
739 line break."
740 (declare (type (member :block :current) relative-to)
741 (type real n)
742 (type stream-designator stream)
743 (values null))
744 (let ((stream (out-synonym-of stream)))
745 (when (print-pretty-on-stream-p stream)
746 (enqueue-indent stream relative-to (truncate n))))
747 nil)
749 (defun pprint-tab (kind colnum colinc &optional stream)
750 #!+sb-doc
751 "If STREAM (which defaults to *STANDARD-OUTPUT*) is a pretty-printing
752 stream, perform tabbing based on KIND, otherwise do nothing. KIND can
753 be one of:
754 :LINE - Tab to column COLNUM. If already past COLNUM tab to the next
755 multiple of COLINC.
756 :SECTION - Same as :LINE, but count from the start of the current
757 section, not the start of the line.
758 :LINE-RELATIVE - Output COLNUM spaces, then tab to the next multiple of
759 COLINC.
760 :SECTION-RELATIVE - Same as :LINE-RELATIVE, but count from the start
761 of the current section, not the start of the line."
762 (declare (type (member :line :section :line-relative :section-relative) kind)
763 (type unsigned-byte colnum colinc)
764 (type stream-designator stream)
765 (values null))
766 (let ((stream (out-synonym-of stream)))
767 (when (print-pretty-on-stream-p stream)
768 (enqueue-tab stream kind colnum colinc)))
769 nil)
771 (defun pprint-fill (stream list &optional (colon? t) atsign?)
772 #!+sb-doc
773 "Output LIST to STREAM putting :FILL conditional newlines between each
774 element. If COLON? is NIL (defaults to T), then no parens are printed
775 around the output. ATSIGN? is ignored (but allowed so that PPRINT-FILL
776 can be used with the ~/.../ format directive."
777 (declare (ignore atsign?))
778 (pprint-logical-block (stream list
779 :prefix (if colon? "(" "")
780 :suffix (if colon? ")" ""))
781 (pprint-exit-if-list-exhausted)
782 (loop
783 (output-object (pprint-pop) stream)
784 (pprint-exit-if-list-exhausted)
785 (write-char #\space stream)
786 (pprint-newline :fill stream))))
788 (defun pprint-linear (stream list &optional (colon? t) atsign?)
789 #!+sb-doc
790 "Output LIST to STREAM putting :LINEAR conditional newlines between each
791 element. If COLON? is NIL (defaults to T), then no parens are printed
792 around the output. ATSIGN? is ignored (but allowed so that PPRINT-LINEAR
793 can be used with the ~/.../ format directive."
794 (declare (ignore atsign?))
795 (pprint-logical-block (stream list
796 :prefix (if colon? "(" "")
797 :suffix (if colon? ")" ""))
798 (pprint-exit-if-list-exhausted)
799 (loop
800 (output-object (pprint-pop) stream)
801 (pprint-exit-if-list-exhausted)
802 (write-char #\space stream)
803 (pprint-newline :linear stream))))
805 (defun pprint-tabular (stream list &optional (colon? t) atsign? tabsize)
806 #!+sb-doc
807 "Output LIST to STREAM tabbing to the next column that is an even multiple
808 of TABSIZE (which defaults to 16) between each element. :FILL style
809 conditional newlines are also output between each element. If COLON? is
810 NIL (defaults to T), then no parens are printed around the output.
811 ATSIGN? is ignored (but allowed so that PPRINT-TABULAR can be used with
812 the ~/.../ format directive."
813 (declare (ignore atsign?))
814 (pprint-logical-block (stream list
815 :prefix (if colon? "(" "")
816 :suffix (if colon? ")" ""))
817 (pprint-exit-if-list-exhausted)
818 (loop
819 (output-object (pprint-pop) stream)
820 (pprint-exit-if-list-exhausted)
821 (write-char #\space stream)
822 (pprint-tab :section-relative 0 (or tabsize 16) stream)
823 (pprint-newline :fill stream))))
825 ;;;; pprint-dispatch tables
827 (defvar *initial-pprint-dispatch-table*)
829 (defstruct (pprint-dispatch-entry (:copier nil) (:predicate nil))
830 ;; the type specifier for this entry
831 (type (missing-arg) :type t :read-only t)
832 ;; a function to test to see whether an object is of this type,
833 ;; either (LAMBDA (OBJ) (TYPEP OBJECT TYPE)) or a builtin predicate.
834 ;; We don't bother computing this for entries in the CONS
835 ;; hash table, because we don't need it.
836 (test-fn nil :type (or function null))
837 ;; the priority for this guy
838 (priority 0 :type real :read-only t)
839 ;; T iff one of the original entries.
840 (initial-p (eq *initial-pprint-dispatch-table* nil)
841 :type (member t nil) :read-only t)
842 ;; and the associated function
843 (fun (missing-arg) :type callable :read-only t))
844 (def!method print-object ((entry pprint-dispatch-entry) stream)
845 (print-unreadable-object (entry stream :type t)
846 (format stream "type=~S, priority=~S~@[ [initial]~]"
847 (pprint-dispatch-entry-type entry)
848 (pprint-dispatch-entry-priority entry)
849 (pprint-dispatch-entry-initial-p entry))))
851 ;; Return T iff E1 is strictly less preferable than E2.
852 (defun entry< (e1 e2)
853 (declare (type pprint-dispatch-entry e1 e2))
854 (if (pprint-dispatch-entry-initial-p e1)
855 (if (pprint-dispatch-entry-initial-p e2)
856 (< (pprint-dispatch-entry-priority e1)
857 (pprint-dispatch-entry-priority e2))
859 (if (pprint-dispatch-entry-initial-p e2)
861 (< (pprint-dispatch-entry-priority e1)
862 (pprint-dispatch-entry-priority e2)))))
864 ;; Return the predicate for CTYPE, equivalently TYPE-SPEC.
865 ;; This used to involve rewriting into a sexpr if CONS was involved,
866 ;; since it was not an official specifier. But now it is.
867 (defun compute-test-fn (ctype type-spec function)
868 (declare (special sb!c::*backend-type-predicates*))
869 ;; Avoid compiling code for an existing structure predicate
870 (or (and (eq (info :type :kind type-spec) :instance)
871 (let ((layout (info :type :compiler-layout type-spec)))
872 (and layout
873 (let ((info (layout-info layout)))
874 (and info
875 (let ((pred (dd-predicate-name info)))
876 (and pred (fboundp pred)
877 (symbol-function pred))))))))
878 ;; avoid compiling code for CONS, ARRAY, VECTOR, etc
879 (awhen (assoc ctype sb!c::*backend-type-predicates* :test #'type=)
880 (symbol-function (cdr it)))
881 ;; OK, compile something
882 (let ((name
883 ;; Keep name as a string, because NAMED-LAMBDA with a symbol
884 ;; affects the global environment, when all you want
885 ;; is to give the lambda a human-readable label.
886 (format nil "~A-P"
887 (cond ((symbolp type-spec) type-spec)
888 ((symbolp function) function)
889 ((%fun-name function))
891 (write-to-string type-spec :pretty nil :escape nil
892 :readably nil))))))
893 (compile nil
894 `(named-lambda ,name (object) (typep object ',type-spec))))))
896 (defun copy-pprint-dispatch (&optional (table *print-pprint-dispatch*))
897 (declare (type (or pprint-dispatch-table null) table))
898 (let* ((orig (or table *initial-pprint-dispatch-table*))
899 (new (make-pprint-dispatch-table
900 :entries (copy-list (pprint-dispatch-table-entries orig)))))
901 (replace/eql-hash-table (pprint-dispatch-table-cons-entries new)
902 (pprint-dispatch-table-cons-entries orig))
903 new))
905 (defun pprint-dispatch (object &optional (table *print-pprint-dispatch*))
906 (declare (type (or pprint-dispatch-table null) table))
907 (let* ((table (or table *initial-pprint-dispatch-table*))
908 (cons-entry
909 (and (consp object)
910 (gethash (car object)
911 (pprint-dispatch-table-cons-entries table))))
912 (entry
913 (dolist (entry (pprint-dispatch-table-entries table) cons-entry)
914 (when (and cons-entry
915 (entry< entry cons-entry))
916 (return cons-entry))
917 (when (funcall (pprint-dispatch-entry-test-fn entry) object)
918 (return entry)))))
919 (if entry
920 (values (pprint-dispatch-entry-fun entry) t)
921 (values (lambda (stream object)
922 (output-ugly-object object stream))
923 nil))))
925 (defun assert-not-standard-pprint-dispatch-table (pprint-dispatch operation)
926 (when (eq pprint-dispatch *standard-pprint-dispatch-table*)
927 (cerror "Frob it anyway!" 'standard-pprint-dispatch-table-modified-error
928 :operation operation)))
930 ;; Similar to (NOT CONTAINS-UNKNOWN-TYPE-P), but this is for when you
931 ;; want to pre-verify that TYPEP won't outright croak, given that you're
932 ;; going to call it really soon.
933 ;; Granted, certain checks could pass or fail by short-circuiting,
934 ;; such as (TYPEP 3 '(OR NUMBER (SATISFIES NO-SUCH-FUN))
935 ;; but this has to be maximally conservative.
936 (defun testable-type-p (ctype)
937 (typecase ctype
938 (unknown-type nil) ; must precede HAIRY because an unknown is HAIRY
939 (hairy-type
940 (let ((spec (hairy-type-specifier ctype)))
941 ;; Anything other than (SATISFIES ...) is testable
942 ;; because there's no reason to suppose that it isn't.
943 (or (neq (car spec) 'satisfies) (fboundp (cadr spec)))))
944 (compound-type (every #'testable-type-p (compound-type-types ctype)))
945 (negation-type (testable-type-p (negation-type-type ctype)))
946 (cons-type (and (testable-type-p (cons-type-car-type ctype))
947 (testable-type-p (cons-type-cdr-type ctype))))
948 (array-type (testable-type-p (array-type-element-type ctype)))
949 (t t)))
951 (defun defer-type-checker (entry)
952 (let ((saved-nonce sb!c::*type-cache-nonce*))
953 (lambda (obj)
954 (let ((nonce sb!c::*type-cache-nonce*))
955 (if (eq nonce saved-nonce)
957 (let ((ctype (specifier-type (pprint-dispatch-entry-type entry))))
958 (setq saved-nonce nonce)
959 (if (testable-type-p ctype)
960 (funcall (setf (pprint-dispatch-entry-test-fn entry)
961 (compute-test-fn
962 ctype
963 (pprint-dispatch-entry-type entry)
964 (pprint-dispatch-entry-fun entry)))
965 obj)
966 nil)))))))
968 ;; The dispatch mechanism is not quite sophisticated enough to have a guard
969 ;; condition on CONS entries. One place this would impact is that you could
970 ;; write the full matcher for QUOTE as just a type-specifier. It can be done
971 ;; now, but using the non-cons table entails linear scan.
972 ;; A test-fn in the cons table would require storing multiple entries per
973 ;; key though because any might fail. Conceivably you could have
974 ;; (cons (eql foo) cons) and (cons (eql foo) bit-vector) as two FOO entries.
975 (defun set-pprint-dispatch (type function &optional
976 (priority 0) (table *print-pprint-dispatch*))
977 (declare (type (or null callable) function)
978 (type real priority)
979 (type pprint-dispatch-table table))
980 (/show0 "entering SET-PPRINT-DISPATCH, TYPE=...")
981 (/hexstr type)
982 (assert-not-standard-pprint-dispatch-table table 'set-pprint-dispatch)
983 (let* ((ctype (or (handler-bind
984 ((parse-unknown-type
985 (lambda (c)
986 (warn "~S is not a recognized type specifier"
987 (parse-unknown-type-specifier c)))))
988 (sb!c::careful-specifier-type type))
989 (error "~S is not a valid type-specifier" type)))
990 (consp (and (cons-type-p ctype)
991 (eq (cons-type-cdr-type ctype) *universal-type*)
992 (member-type-p (cons-type-car-type ctype))))
993 (disabled-p (not (testable-type-p ctype)))
994 (entry (if function
995 (make-pprint-dispatch-entry
996 :type type
997 :test-fn (unless (or consp disabled-p)
998 (compute-test-fn ctype type function))
999 :priority priority :fun function))))
1000 (when (and function disabled-p)
1001 ;; a DISABLED-P test function has to close over the ENTRY
1002 (setf (pprint-dispatch-entry-test-fn entry) (defer-type-checker entry))
1003 (unless (unknown-type-p ctype) ; already warned in this case
1004 ;; But (OR KNOWN UNKNOWN) did not signal - actually it is indeterminate
1005 ;; - depending on whather it was cached. I think we should not cache
1006 ;; any specifier that contains any unknown anywhere within it.
1007 (warn "~S contains an unrecognized type specifier" type)))
1008 (if consp
1009 (let ((hashtable (pprint-dispatch-table-cons-entries table)))
1010 (dolist (key (member-type-members (cons-type-car-type ctype)))
1011 (if function
1012 (setf (gethash key hashtable) entry)
1013 (remhash key hashtable))))
1014 (setf (pprint-dispatch-table-entries table)
1015 (let ((list (delete type (pprint-dispatch-table-entries table)
1016 :key #'pprint-dispatch-entry-type
1017 :test #'equal)))
1018 (if function
1019 ;; ENTRY< is T if lower in priority, which should sort to
1020 ;; the end, but MERGE's predicate wants T for the (a,b) pair
1021 ;; if 'a' should go in front of 'b', so swap them.
1022 ;; (COMPLEMENT #'entry<) is unstable wrt insertion order.
1023 (merge 'list list (list entry) (lambda (a b) (entry< b a)))
1024 list)))))
1025 (/show0 "about to return NIL from SET-PPRINT-DISPATCH")
1026 nil)
1028 ;;;; standard pretty-printing routines
1030 (defun pprint-array (stream array)
1031 (cond ((and (null *print-array*) (null *print-readably*))
1032 (output-ugly-object array stream))
1033 ((and *print-readably*
1034 (not (array-readably-printable-p array)))
1035 (if *read-eval*
1036 (if (vectorp array)
1037 (sb!impl::output-unreadable-vector-readably array stream)
1038 (sb!impl::output-unreadable-array-readably array stream))
1039 (print-not-readable-error array stream)))
1040 ((vectorp array)
1041 (pprint-vector stream array))
1043 (pprint-multi-dim-array stream array))))
1045 (defun pprint-vector (stream vector)
1046 (pprint-logical-block (stream nil :prefix "#(" :suffix ")")
1047 (dotimes (i (length vector))
1048 (unless (zerop i)
1049 (format stream " ~:_"))
1050 (pprint-pop)
1051 (output-object (aref vector i) stream))))
1053 (defun pprint-multi-dim-array (stream array)
1054 (funcall (formatter "#~DA") stream (array-rank array))
1055 (with-array-data ((data array) (start) (end))
1056 (declare (ignore end))
1057 (labels ((output-guts (stream index dimensions)
1058 (if (null dimensions)
1059 (output-object (aref data index) stream)
1060 (pprint-logical-block
1061 (stream nil :prefix "(" :suffix ")")
1062 (let ((dim (car dimensions)))
1063 (unless (zerop dim)
1064 (let* ((dims (cdr dimensions))
1065 (index index)
1066 (step (reduce #'* dims))
1067 (count 0))
1068 (loop
1069 (pprint-pop)
1070 (output-guts stream index dims)
1071 (when (= (incf count) dim)
1072 (return))
1073 (write-char #\space stream)
1074 (pprint-newline (if dims :linear :fill)
1075 stream)
1076 (incf index step)))))))))
1077 (output-guts stream start (array-dimensions array)))))
1079 (defun pprint-lambda-list (stream lambda-list &rest noise)
1080 (declare (ignore noise))
1081 (pprint-logical-block (stream lambda-list :prefix "(" :suffix ")")
1082 (let ((state :required)
1083 (first t))
1084 (loop
1085 (pprint-exit-if-list-exhausted)
1086 (unless first
1087 (write-char #\space stream))
1088 (let ((arg (pprint-pop)))
1089 (unless first
1090 (case arg
1091 (&optional
1092 (setf state :optional)
1093 (pprint-newline :linear stream))
1094 ((&rest &body)
1095 (setf state :required)
1096 (pprint-newline :linear stream))
1097 (&key
1098 (setf state :key)
1099 (pprint-newline :linear stream))
1100 (&aux
1101 (setf state :optional)
1102 (pprint-newline :linear stream))
1104 (pprint-newline :fill stream))))
1105 (ecase state
1106 (:required
1107 (pprint-lambda-list stream arg))
1108 ((:optional :key)
1109 (pprint-logical-block
1110 (stream arg :prefix "(" :suffix ")")
1111 (pprint-exit-if-list-exhausted)
1112 (if (eq state :key)
1113 (pprint-logical-block
1114 (stream (pprint-pop) :prefix "(" :suffix ")")
1115 (pprint-exit-if-list-exhausted)
1116 (output-object (pprint-pop) stream)
1117 (pprint-exit-if-list-exhausted)
1118 (write-char #\space stream)
1119 (pprint-newline :fill stream)
1120 (pprint-lambda-list stream (pprint-pop))
1121 (loop
1122 (pprint-exit-if-list-exhausted)
1123 (write-char #\space stream)
1124 (pprint-newline :fill stream)
1125 (output-object (pprint-pop) stream)))
1126 (pprint-lambda-list stream (pprint-pop)))
1127 (loop
1128 (pprint-exit-if-list-exhausted)
1129 (write-char #\space stream)
1130 (pprint-newline :linear stream)
1131 (output-object (pprint-pop) stream))))))
1132 (setf first nil)))))
1134 (defun pprint-lambda (stream list &rest noise)
1135 (declare (ignore noise))
1136 (funcall (formatter
1137 ;; KLUDGE: This format string, and other format strings which also
1138 ;; refer to SB!PRETTY, rely on the current SBCL not-quite-ANSI
1139 ;; behavior of FORMATTER in order to make code which survives the
1140 ;; transition when SB!PRETTY is renamed to SB-PRETTY after cold
1141 ;; init. (ANSI says that the FORMATTER functions should be
1142 ;; equivalent to the format string, but the SBCL FORMATTER
1143 ;; functions contain references to package objects, not package
1144 ;; names, so they keep right on going if the packages are renamed.)
1145 ;; If our FORMATTER behavior is ever made more compliant, the code
1146 ;; here will have to change. -- WHN 19991207
1147 "~:<~^~W~^~3I ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1148 stream
1149 list))
1151 (defun pprint-block (stream list &rest noise)
1152 (declare (ignore noise))
1153 (funcall (formatter "~:<~^~W~^~3I ~:_~W~1I~@{ ~_~W~}~:>") stream list))
1155 (defun pprint-flet (stream list &rest noise)
1156 (declare (ignore noise))
1157 (if (and (consp list)
1158 (consp (cdr list))
1159 (cddr list)
1160 ;; Filter out (FLET FOO :IN BAR) names.
1161 (and (consp (cddr list))
1162 (not (eq :in (third list)))))
1163 (funcall (formatter
1164 "~:<~^~W~^ ~@_~:<~@{~:<~^~W~^~3I ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~:@_~@{~W~^ ~_~}~:>~^ ~_~}~:>~1I~@:_~@{~W~^ ~_~}~:>")
1165 stream
1166 list)
1167 ;; for printing function names like (flet foo)
1168 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1169 (pprint-exit-if-list-exhausted)
1170 (write (pprint-pop) :stream stream)
1171 (loop
1172 (pprint-exit-if-list-exhausted)
1173 (write-char #\space stream)
1174 (write (pprint-pop) :stream stream)))))
1176 (defun pprint-let (stream list &rest noise)
1177 (declare (ignore noise))
1178 (funcall (formatter "~:<~^~W~^ ~@_~:<~@{~:<~^~W~@{ ~_~W~}~:>~^ ~_~}~:>~1I~:@_~@{~W~^ ~_~}~:>")
1179 stream
1180 list))
1182 (defun pprint-progn (stream list &rest noise)
1183 (declare (ignore noise))
1184 (pprint-linear stream list))
1186 (defun pprint-progv (stream list &rest noise)
1187 (declare (ignore noise))
1188 (funcall (formatter "~:<~^~W~^~3I ~_~W~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1189 stream list))
1191 (defun pprint-prog2 (stream list &rest noise)
1192 (declare (ignore noise))
1193 (funcall (formatter "~:<~^~W~^~3I ~:_~W~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1194 stream list))
1196 (defun pprint-unquoting-comma (stream obj &rest noise)
1197 (declare (ignore noise))
1198 (write-string (svref #("," ",." ",@") (comma-kind obj)) stream)
1199 (when (eql (comma-kind obj) 0)
1200 ;; Ensure a space is written before any output that would change the meaning
1201 ;; of the preceding the comma to ",." or ",@" such as a symbol named "@BAR".
1202 (setf (pretty-stream-char-out-oneshot-hook stream)
1203 (lambda (stream char)
1204 (when (member char '(#\. #\@))
1205 (write-char #\Space stream)))))
1206 (output-object (comma-expr obj) stream))
1208 (defvar *pprint-quote-with-syntactic-sugar* t)
1210 (defun pprint-quote (stream list &rest noise)
1211 (declare (ignore noise))
1212 (when (and (listp list) (singleton-p (cdr list)))
1213 (let* ((pretty-p nil)
1214 (sigil (case (car list)
1215 (function "#'")
1216 ;; QUASIQUOTE can't choose not to print prettily.
1217 ;; Wrongly nested commas beget unreadable sexprs.
1218 (quasiquote (setq pretty-p t) "`")
1219 (t "'")))) ; ordinary QUOTE
1220 (when (or pretty-p *pprint-quote-with-syntactic-sugar*)
1221 (write-string sigil stream)
1222 (return-from pprint-quote (output-object (cadr list) stream)))))
1223 (pprint-fill stream list))
1225 (defun pprint-declare (stream list &rest noise)
1226 (declare (ignore noise))
1227 ;; Make sure to print (DECLARE (FUNCTION F)) not (DECLARE #'A).
1228 (let ((*pprint-quote-with-syntactic-sugar* nil))
1229 (pprint-spread-fun-call stream list)))
1231 ;;; Try to print every variable-value pair on one line; if that doesn't
1232 ;;; work print the value indented by 2 spaces:
1234 ;;; (setq foo bar
1235 ;;; quux xoo)
1236 ;;; vs.
1237 ;;; (setf foo
1238 ;;; (long form ...)
1239 ;;; quux xoo)
1240 (defun pprint-setq (stream list &rest noise)
1241 (declare (ignore noise))
1242 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1243 (pprint-exit-if-list-exhausted)
1244 (output-object (pprint-pop) stream)
1245 (pprint-exit-if-list-exhausted)
1246 (write-char #\space stream)
1247 (unless (listp (cdr list))
1248 (write-string ". " stream))
1249 (pprint-newline :miser stream)
1250 (pprint-logical-block (stream (cdr list) :prefix "" :suffix "")
1251 (loop
1252 (pprint-indent :block 2 stream)
1253 (output-object (pprint-pop) stream)
1254 (pprint-exit-if-list-exhausted)
1255 (write-char #\space stream)
1256 (pprint-newline :fill stream)
1257 (pprint-indent :block 0 stream)
1258 (output-object (pprint-pop) stream)
1259 (pprint-exit-if-list-exhausted)
1260 (write-char #\space stream)
1261 (pprint-newline :mandatory stream)))))
1263 (eval-when (:compile-toplevel :execute)
1264 (sb!xc:defmacro pprint-tagbody-guts (stream)
1265 `(loop
1266 (pprint-exit-if-list-exhausted)
1267 (write-char #\space ,stream)
1268 (let ((form-or-tag (pprint-pop)))
1269 (pprint-indent :block
1270 (if (atom form-or-tag) 0 1)
1271 ,stream)
1272 (pprint-newline :linear ,stream)
1273 (output-object form-or-tag ,stream)))))
1275 (defun pprint-tagbody (stream list &rest noise)
1276 (declare (ignore noise))
1277 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1278 (pprint-exit-if-list-exhausted)
1279 (output-object (pprint-pop) stream)
1280 (pprint-tagbody-guts stream)))
1282 (defun pprint-case (stream list &rest noise)
1283 (declare (ignore noise))
1284 (funcall (formatter
1285 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~:/SB!PRETTY:PPRINT-FILL/~^~@{ ~_~W~}~:>~}~:>")
1286 stream
1287 list))
1289 (defun pprint-defun (stream list &rest noise)
1290 (declare (ignore noise))
1291 (funcall (formatter
1292 "~:<~^~W~^ ~@_~:I~W~^ ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1293 stream
1294 list))
1296 (defun pprint-defmethod (stream list &rest noise)
1297 (declare (ignore noise))
1298 (if (and (consp (cdr list))
1299 (consp (cddr list))
1300 (consp (third list)))
1301 (pprint-defun stream list)
1302 (funcall (formatter
1303 "~:<~^~W~^ ~@_~:I~W~^ ~W~^ ~:_~/SB!PRETTY:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1304 stream
1305 list)))
1307 (defun pprint-defpackage (stream list &rest noise)
1308 (declare (ignore noise))
1309 (funcall (formatter
1310 "~:<~W~^ ~3I~:_~W~^~1I~@{~:@_~:<~^~W~^ ~:I~@_~@{~W~^ ~_~}~:>~}~:>")
1311 stream
1312 list))
1314 (defun pprint-destructuring-bind (stream list &rest noise)
1315 (declare (ignore noise))
1316 (funcall (formatter
1317 "~:<~^~W~^~3I ~_~:/SB!PRETTY:PPRINT-LAMBDA-LIST/~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1318 stream list))
1320 (defun pprint-do (stream list &rest noise)
1321 (declare (ignore noise))
1322 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1323 (pprint-exit-if-list-exhausted)
1324 (output-object (pprint-pop) stream)
1325 (pprint-exit-if-list-exhausted)
1326 (write-char #\space stream)
1327 (pprint-indent :current 0 stream)
1328 (funcall (formatter "~:<~@{~:<~^~W~^ ~@_~:I~W~@{ ~_~W~}~:>~^~:@_~}~:>")
1329 stream
1330 (pprint-pop))
1331 (pprint-exit-if-list-exhausted)
1332 (write-char #\space stream)
1333 (pprint-newline :linear stream)
1334 (pprint-linear stream (pprint-pop))
1335 (pprint-tagbody-guts stream)))
1337 (defun pprint-dolist (stream list &rest noise)
1338 (declare (ignore noise))
1339 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1340 (pprint-exit-if-list-exhausted)
1341 (output-object (pprint-pop) stream)
1342 (pprint-exit-if-list-exhausted)
1343 (pprint-indent :block 3 stream)
1344 (write-char #\space stream)
1345 (pprint-newline :fill stream)
1346 (funcall (formatter "~:<~^~W~^ ~:_~:I~W~@{ ~_~W~}~:>")
1347 stream
1348 (pprint-pop))
1349 (pprint-tagbody-guts stream)))
1351 (defun pprint-typecase (stream list &rest noise)
1352 (declare (ignore noise))
1353 (funcall (formatter
1354 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~W~^~@{ ~_~W~}~:>~}~:>")
1355 stream
1356 list))
1358 (defun pprint-prog (stream list &rest noise)
1359 (declare (ignore noise))
1360 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1361 (pprint-exit-if-list-exhausted)
1362 (output-object (pprint-pop) stream)
1363 (pprint-exit-if-list-exhausted)
1364 (write-char #\space stream)
1365 (pprint-newline :miser stream)
1366 (pprint-fill stream (pprint-pop))
1367 (pprint-tagbody-guts stream)))
1369 ;;; Each clause in this list will get its own line.
1370 ;;; FIXME: (LOOP for x in list summing (f x) into count finally ...)
1371 ;;; puts a newline in between INTO and COUNT.
1372 ;;; It would be awesome to have code in common with the macro
1373 ;;; the properly represents each clauses.
1374 (defvar *loop-seperating-clauses*
1375 '(:and
1376 :with :for
1377 :initially :finally
1378 :do :doing
1379 :collect :collecting
1380 :append :appending
1381 :nconc :nconcing
1382 :count :counting
1383 :sum :summing
1384 :maximize :maximizing
1385 :minimize :minimizing
1386 :if :when :unless :end
1387 :for :while :until :repeat :always :never :thereis
1390 (defun pprint-extended-loop (stream list)
1391 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1392 (output-object (pprint-pop) stream)
1393 (pprint-exit-if-list-exhausted)
1394 (write-char #\space stream)
1395 (pprint-indent :current 0 stream)
1396 (output-object (pprint-pop) stream)
1397 (pprint-exit-if-list-exhausted)
1398 (write-char #\space stream)
1399 (loop for thing = (pprint-pop)
1400 when (and (symbolp thing)
1401 (member thing *loop-seperating-clauses* :test #'string=))
1402 do (pprint-newline :mandatory stream)
1403 do (output-object thing stream)
1404 do (pprint-exit-if-list-exhausted)
1405 do (write-char #\space stream))))
1407 (defun pprint-loop (stream list &rest noise)
1408 (declare (ignore noise))
1409 (destructuring-bind (loop-symbol . clauses) list
1410 (declare (ignore loop-symbol))
1411 (if (or (atom clauses) (consp (car clauses)))
1412 (pprint-spread-fun-call stream list)
1413 (pprint-extended-loop stream list))))
1415 (defun pprint-if (stream list &rest noise)
1416 (declare (ignore noise))
1417 ;; Indent after the ``predicate'' form, and the ``then'' form.
1418 (funcall (formatter "~:<~^~W~^ ~:I~W~^ ~:@_~@{~W~^ ~:@_~}~:>")
1419 stream
1420 list))
1422 (defun pprint-fun-call (stream list &rest noise)
1423 (declare (ignore noise))
1424 (funcall (formatter "~:<~^~W~^ ~:_~:I~@{~W~^ ~:_~}~:>")
1425 stream
1426 list))
1428 (defun pprint-spread-fun-call (stream list &rest noise)
1429 (declare (ignore noise))
1430 ;; Similiar to PPRINT-FUN-CALL but emit a mandatory newline after
1431 ;; each parameter. I.e. spread out each parameter on its own line.
1432 (funcall (formatter "~:<~^~W~^ ~:_~:I~@{~W~^ ~:@_~}~:>")
1433 stream
1434 list))
1436 (defun pprint-data-list (stream list &rest noise)
1437 (declare (ignore noise))
1438 (pprint-fill stream list))
1440 ;;; Returns an Emacs-style indent spec: an integer N, meaning indent
1441 ;;; the first N arguments specially then indent any further arguments
1442 ;;; like a body.
1443 (defun macro-indentation (name)
1444 (labels ((clean-arglist (arglist)
1445 ;; FIXME: for purposes of introspection, we should never "leak"
1446 ;; that a macro uses an &AUX variable, that it takes &WHOLE,
1447 ;; or that it cares about its lexenv (though that's debatable).
1448 ;; Certainly the first two aspects are not part of the macro's
1449 ;; interface, and as such, should not be stored at all.
1450 "Remove &whole, &enviroment, and &aux elements from ARGLIST."
1451 (cond ((null arglist) '())
1452 ((member (car arglist) '(&whole &environment))
1453 (clean-arglist (cddr arglist)))
1454 ((eq (car arglist) '&aux)
1455 '())
1456 (t (cons (car arglist) (clean-arglist (cdr arglist)))))))
1457 (let ((arglist (%fun-lambda-list (macro-function name))))
1458 (if (proper-list-p arglist) ; guard against dotted arglists
1459 (position '&body (remove '&optional (clean-arglist arglist)))
1460 nil))))
1462 ;;; Pretty-Print macros by looking where &BODY appears in a macro's
1463 ;;; lambda-list.
1464 (defun pprint-macro-call (stream list &rest noise)
1465 (declare (ignore noise))
1466 (let ((indentation (and (car list) (macro-indentation (car list)))))
1467 (unless indentation
1468 (return-from pprint-macro-call
1469 (pprint-fun-call stream list)))
1470 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1471 (output-object (pprint-pop) stream)
1472 (pprint-exit-if-list-exhausted)
1473 (write-char #\space stream)
1474 (loop for indent from 0 below indentation do
1475 (cond
1476 ;; Place the very first argument next to the macro name
1477 ((zerop indent)
1478 (output-object (pprint-pop) stream)
1479 (pprint-exit-if-list-exhausted))
1480 ;; Indent any other non-body argument by the same
1481 ;; amount. It's what Emacs seems to do, too.
1483 (pprint-indent :block 3 stream)
1484 (pprint-newline :mandatory stream)
1485 (output-object (pprint-pop) stream)
1486 (pprint-exit-if-list-exhausted))))
1487 ;; Indent back for the body.
1488 (pprint-indent :block 1 stream)
1489 (pprint-newline :mandatory stream)
1490 (loop
1491 (output-object (pprint-pop) stream)
1492 (pprint-exit-if-list-exhausted)
1493 (pprint-newline :mandatory stream)))))
1495 ;;;; the interface seen by regular (ugly) printer and initialization routines
1497 (eval-when (:compile-toplevel :execute)
1498 (sb!xc:defmacro with-pretty-stream ((stream-var
1499 &optional (stream-expression stream-var))
1500 &body body)
1501 (let ((flet-name (sb!xc:gensym "WITH-PRETTY-STREAM")))
1502 `(flet ((,flet-name (,stream-var)
1503 ,@body))
1504 (let ((stream ,stream-expression))
1505 (if (pretty-stream-p stream)
1506 (,flet-name stream)
1507 (catch 'line-limit-abbreviation-happened
1508 (let ((stream (make-pretty-stream stream)))
1509 (,flet-name stream)
1510 (force-pretty-output stream)))))
1511 nil))))
1513 ;;; OUTPUT-PRETTY-OBJECT is called by OUTPUT-OBJECT when
1514 ;;; *PRINT-PRETTY* is true.
1515 (defun output-pretty-object (object stream)
1516 (multiple-value-bind (fun pretty) (pprint-dispatch object)
1517 (if pretty
1518 (with-pretty-stream (stream)
1519 (funcall fun stream object))
1520 ;; No point in consing up a pretty stream if we are not using pretty
1521 ;; printing the object after all.
1522 (output-ugly-object object stream))))
1524 (defun call-logical-block-printer (proc stream prefix per-line-p suffix
1525 &optional (object nil obj-supplied-p))
1526 ;; PREFIX and SUFFIX will be checked for stringness by START-LOGICAL-BLOCK.
1527 ;; Doing it here would be more strict, but I really don't think it's worth
1528 ;; an extra check. The only observable difference would occur when you have
1529 ;; a non-list object which bypasses START-LOGICAL-BLOCK.
1530 ;; Also, START-LOGICAL-BLOCK could become an FLET inside here.
1531 (declare (function proc))
1532 (with-pretty-stream (stream (out-synonym-of stream))
1533 (if (or (not (listp object)) ; implies obj-supplied-p
1534 (and (eq (car object) 'quasiquote)
1535 ;; We can only bail out from printing this logical block
1536 ;; if the quasiquote printer would *NOT* punt.
1537 ;; If it would punt, then we have to forge ahead.
1538 (singleton-p (cdr object))))
1539 ;; the spec says "If object is not a list, it is printed using WRITE"
1540 ;; but I guess this is close enough.
1541 (output-object object stream)
1542 (dx-let ((state (cons 0 stream)))
1543 (if obj-supplied-p
1544 (with-circularity-detection (object stream)
1545 (descend-into (stream)
1546 (start-logical-block stream prefix per-line-p suffix)
1547 (funcall proc object state stream)
1548 ;; Comment preserved for posterity:
1549 ;; FIXME: Don't we need UNWIND-PROTECT to ensure this
1550 ;; always gets executed?
1551 ;; I think not because I wouldn't characterize this as
1552 ;; "cleanup" code. If and only if you follow the accepted
1553 ;; protocol for defining and using print functions should
1554 ;; the behavior be expected to be reasonable and predictable.
1555 ;; Throwing to LINE-LIMIT-ABBREVIATION-HAPPENED is designed
1556 ;; to do the right thing, and printing should not generally
1557 ;; continue to have side-effects if the user felt it necessary
1558 ;; to nonlocally exit in an unexpected way for other reasons.
1559 (end-logical-block stream)))
1560 (descend-into (stream)
1561 (start-logical-block stream prefix per-line-p suffix)
1562 (funcall proc state stream)
1563 (end-logical-block stream)))))))
1565 ;; Return non-nil if we should keep printing within the logical-block,
1566 ;; or NIL to stop printing due to non-list, length cutoff, or circularity.
1567 (defun pprint-length-check (obj state)
1568 (let ((stream (cdr state)))
1569 (cond ((or (not (listp obj))
1570 ;; Consider (A . `(,B C)) = (A QUASIQUOTE ,B C)
1571 ;; We have to detect this and print as the form on the left,
1572 ;; since pretty commas with no containing #\` will be unreadable
1573 ;; due to a nesting error.
1574 (and (eq (car obj) 'quasiquote) (singleton-p (cdr obj))))
1575 (write-string ". " stream)
1576 (output-object obj stream)
1577 nil)
1578 ((and (not *print-readably*) (eql (car state) *print-length*))
1579 (write-string "..." stream)
1580 nil)
1581 ((and obj
1582 (plusp (car state))
1583 (check-for-circularity obj nil :logical-block))
1584 (write-string ". " stream)
1585 (output-object obj stream)
1586 nil)
1588 (incf (car state))))))
1590 ;; As above, but for logical blocks with an unspecific object.
1591 (defun pprint-length-check* (state)
1592 (let ((stream (cdr state)))
1593 (cond ((and (not *print-readably*) (eql (car state) *print-length*))
1594 (write-string "..." stream)
1595 nil)
1597 (incf (car state))))))
1599 (defun !pprint-cold-init ()
1600 (/show0 "entering !PPRINT-COLD-INIT")
1601 ;; Kludge: We set *STANDARD-PP-D-TABLE* to a new table even though
1602 ;; it's going to be set to a copy of *INITIAL-PP-D-T* below because
1603 ;; it's used in WITH-STANDARD-IO-SYNTAX, and condition reportery
1604 ;; possibly performed in the following extent may use W-S-IO-SYNTAX.
1605 (setf *standard-pprint-dispatch-table* (make-pprint-dispatch-table))
1606 (setf *initial-pprint-dispatch-table* nil)
1607 (let ((*print-pprint-dispatch* (make-pprint-dispatch-table)))
1608 (/show0 "doing SET-PPRINT-DISPATCH for regular types")
1609 (set-pprint-dispatch '(and array (not (or string bit-vector))) #'pprint-array)
1610 ;; MACRO-FUNCTION must have effectively higher priority than FBOUNDP.
1611 ;; The implementation happens to check identical priorities in the order added,
1612 ;; but that's unspecified behavior. Both must be _strictly_ lower than the
1613 ;; default cons entries though.
1614 (set-pprint-dispatch '(cons (and symbol (satisfies macro-function)))
1615 #'pprint-macro-call -1)
1616 (set-pprint-dispatch '(cons (and symbol (satisfies fboundp)))
1617 #'pprint-fun-call -1)
1618 (set-pprint-dispatch '(cons symbol)
1619 #'pprint-data-list -2)
1620 (set-pprint-dispatch 'cons #'pprint-fill -2)
1621 (set-pprint-dispatch 'sb!impl::comma #'pprint-unquoting-comma -3)
1622 ;; cons cells with interesting things for the car
1623 (/show0 "doing SET-PPRINT-DISPATCH for CONS with interesting CAR")
1625 (dolist (magic-form '((lambda pprint-lambda)
1626 (declare pprint-declare)
1628 ;; special forms
1629 (block pprint-block)
1630 (catch pprint-block)
1631 (eval-when pprint-block)
1632 (flet pprint-flet)
1633 (function pprint-quote)
1634 (if pprint-if)
1635 (labels pprint-flet)
1636 ((let let*) pprint-let)
1637 (locally pprint-progn)
1638 (macrolet pprint-flet)
1639 (multiple-value-call pprint-block)
1640 (multiple-value-prog1 pprint-block)
1641 (progn pprint-progn)
1642 (progv pprint-progv)
1643 ((quasiquote quote) pprint-quote)
1644 (return-from pprint-block)
1645 ((setq psetq setf psetf) pprint-setq)
1646 (symbol-macrolet pprint-let)
1647 (tagbody pprint-tagbody)
1648 (throw pprint-block)
1649 (unwind-protect pprint-block)
1651 ;; macros
1652 ((case ccase ecase) pprint-case)
1653 ((ctypecase etypecase typecase) pprint-typecase)
1654 (declaim pprint-declare)
1655 (defconstant pprint-block)
1656 (define-modify-macro pprint-defun)
1657 (define-setf-expander pprint-defun)
1658 (defmacro pprint-defun)
1659 (defmethod pprint-defmethod)
1660 (defpackage pprint-defpackage)
1661 (defparameter pprint-block)
1662 (defsetf pprint-defun)
1663 (defstruct pprint-block)
1664 (deftype pprint-defun)
1665 (defun pprint-defun)
1666 (defvar pprint-block)
1667 (destructuring-bind pprint-destructuring-bind)
1668 ((do do*) pprint-do)
1669 ((do-all-symbols do-external-symbols do-symbols
1670 dolist dotimes) pprint-dolist)
1671 #+nil (handler-bind ...)
1672 #+nil (handler-case ...)
1673 (loop pprint-loop)
1674 (multiple-value-bind pprint-prog2)
1675 (multiple-value-setq pprint-block)
1676 (pprint-logical-block pprint-block)
1677 (print-unreadable-object pprint-block)
1678 ((prog prog*) pprint-prog)
1679 (prog1 pprint-block)
1680 (prog2 pprint-prog2)
1681 #+nil (restart-bind ...)
1682 #+nil (restart-case ...)
1683 (step pprint-progn)
1684 (time pprint-progn)
1685 ((unless when) pprint-block)
1686 (with-compilation-unit pprint-block)
1687 #+nil (with-condition-restarts ...)
1688 (with-hash-table-iterator pprint-block)
1689 (with-input-from-string pprint-block)
1690 (with-open-file pprint-block)
1691 (with-open-stream pprint-block)
1692 (with-output-to-string pprint-block)
1693 (with-package-iterator pprint-block)
1694 (with-simple-restart pprint-block)
1695 (with-standard-io-syntax pprint-progn)
1697 ;; sbcl specific
1698 (sb!int:dx-flet pprint-flet)
1701 ;; Grouping some symbols together in the above list looks pretty.
1702 ;; The sharing of dispatch entries is inconsequential.
1703 (set-pprint-dispatch (let ((thing (first magic-form)))
1704 `(cons (member
1705 ,@(if (consp thing) thing (list thing)))))
1706 (symbol-function (second magic-form))))
1707 (setf *initial-pprint-dispatch-table* *print-pprint-dispatch*))
1709 (setf *standard-pprint-dispatch-table*
1710 (copy-pprint-dispatch *initial-pprint-dispatch-table*))
1711 (setf *print-pprint-dispatch*
1712 (copy-pprint-dispatch *initial-pprint-dispatch-table*))
1713 (setf *print-pretty* t))