Debian Codegraph @

src/lsp/pprint.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;; -*- Package: PRETTY-PRINT -*-
6 ;;;
7 ;;; **********************************************************************
8 ;;; This code was written as part of the CMU Common Lisp project at
9 ;;; Carnegie Mellon University, and has been placed in the public domain.
10 ;;;
11 ;;; CMU Common Lisp pretty printer.
12 ;;; Written by William Lott. Algorithm stolen from Richard Waters' XP.
13 ;;;
14
15 (in-package "SI")
16
17 (declaim #.+ecl-unsafe-declarations+)
18
19 ;;;; Pretty streams
20
21 ;;; There are three different units for measuring character positions:
22 ;;; COLUMN - offset (if characters) from the start of the current line.
23 ;;; INDEX - index into the output buffer.
24 ;;; POSN - some position in the stream of characters cycling through
25 ;;; the output buffer.
26 ;;;
27 (deftype column ()
28 '(and fixnum unsigned-byte))
29 ;;; The INDEX type is picked up from the kernel package.
30 (deftype posn ()
31 'fixnum)
32
33 (defconstant initial-buffer-size 128)
34
35 (defclass pretty-stream (gray:fundamental-character-output-stream) (
36 ;;
37 ;; Where the output is going to finally go.
38 ;;
39 (target :initarg :target :initform t :type stream
40 :accessor pretty-stream-target)
41 ;;
42 ;; Line length we should format to. Cached here so we don't have to keep
43 ;; extracting it from the target stream.
44 (line-length :initarg :line-length
45 :initform default-line-length
46 :type column
47 :accessor pretty-stream-line-length)
48 ;;
49 ;; A simple string holding all the text that has been output but not yet
50 ;; printed.
51 (buffer :initform (make-string initial-buffer-size) :type simple-string
52 :accessor pretty-stream-buffer)
53 ;;
54 ;; The index into BUFFER where more text should be put.
55 (buffer-fill-pointer :initform 0 :type index :accessor pretty-stream-buffer-fill-pointer)
56 ;;
57 ;; Whenever we output stuff from the buffer, we shift the remaining noise
58 ;; over. This makes it difficult to keep references to locations in
59 ;; the buffer. Therefore, we have to keep track of the total amount of
60 ;; stuff that has been shifted out of the buffer.
61 (buffer-offset :initform 0 :type posn :accessor pretty-stream-buffer-offset)
62 ;;
63 ;; The column the first character in the buffer will appear in. Normally
64 ;; zero, but if we end up with a very long line with no breaks in it we
65 ;; might have to output part of it. Then this will no longer be zero.
66 (buffer-start-column :initarg :buffer-start-column :type column
67 :accessor pretty-stream-buffer-start-column)
68 ;;
69 ;; The line number we are currently on. Used for *print-lines* abrevs and
70 ;; to tell when sections have been split across multiple lines.
71 (line-number :initform 0 :type index
72 :accessor pretty-stream-line-number)
73 ;;
74 ;; Stack of logical blocks in effect at the buffer start.
75 (blocks :initform (list (make-logical-block)) :type list
76 :accessor pretty-stream-blocks)
77 ;;
78 ;; Buffer holding the per-line prefix active at the buffer start.
79 ;; Indentation is included in this. The length of this is stored
80 ;; in the logical block stack.
81 (prefix :initform (make-string initial-buffer-size) :type string
82 :accessor pretty-stream-prefix)
83 ;;
84 ;; Buffer holding the total remaining suffix active at the buffer start.
85 ;; The characters are right-justified in the buffer to make it easier
86 ;; to output the buffer. The length is stored in the logical block
87 ;; stack.
88 (suffix :initform (make-string initial-buffer-size) :type string
89 :accessor pretty-stream-suffix)
90 ;;
91 ;; Queue of pending operations. When empty, HEAD=TAIL=NIL. Otherwise,
92 ;; TAIL holds the first (oldest) cons and HEAD holds the last (newest)
93 ;; cons. Adding things to the queue is basically (setf (cdr head) (list
94 ;; new)) and removing them is basically (pop tail) [except that care must
95 ;; be taken to handle the empty queue case correctly.]
96 (queue-tail :initform nil :type list :accessor pretty-stream-queue-tail)
97 (queue-head :initform nil :type list :accessor pretty-stream-queue-head)
98 ;;
99 ;; Block-start queue entries in effect at the queue head.
100 (pending-blocks :initform nil :type list :accessor pretty-stream-pending-blocks)
101 )
102 (:sealedp t)
103 )
104
105 (defun pretty-stream-p (stream)
106 (typep stream 'pretty-stream))
107
108 (defun make-pretty-stream (target)
109 (make-instance 'pretty-stream :target target
110 :buffer-start-column (or (file-column target) 0)
111 :line-length (or *print-right-margin*
112 (gray:stream-line-length target)
113 default-line-length)))
114
115 (defmethod print-object ((pretty-stream pretty-stream) stream)
116 (print-unreadable-object (pretty-stream stream :type t :identity t))
117 #+nil
118 (format stream "#<pretty stream {~8,'0X}>"
119 (kernel:get-lisp-obj-address pretty-stream)))
120
121 (declaim (inline index-posn posn-index posn-column))
122 (defun index-posn (index stream)
123 (declare (type index index) (type pretty-stream stream))
124 (+ index (pretty-stream-buffer-offset stream)))
125 (defun posn-index (posn stream)
126 (declare (type posn posn) (type pretty-stream stream))
127 (- posn (pretty-stream-buffer-offset stream)))
128 (defun posn-column (posn stream)
129 (declare (type posn posn) (type pretty-stream stream))
130 (index-column (posn-index posn stream) stream))
131
132
133 ;;;; Stream interface routines.
134
135 (defmethod gray:stream-line-length ((stream pretty-stream))
136 (pretty-stream-line-length stream))
137
138 (defmethod gray::stream-write-char ((stream pretty-stream) char)
139 (pretty-out stream char))
140
141 (defmethod gray::stream-force-output ((stream pretty-stream))
142 (declare (ignore stream))
143 ;(force-pretty-output stream)
144 )
145
146 (defmethod gray::stream-clear-output ((stream pretty-stream))
147 (declare (type pretty-stream stream))
148 (clear-output (pretty-stream-target stream)))
149
150 (defun pretty-out (stream char)
151 (declare (type pretty-stream stream)
152 (type character char)
153 (si::c-local))
154 (cond ((char= char #\newline)
155 (enqueue-newline stream :literal))
156 (t
157 (assure-space-in-buffer stream 1)
158 (let ((fill-pointer (pretty-stream-buffer-fill-pointer stream)))
159 (setf (schar (pretty-stream-buffer stream) fill-pointer) char)
160 (setf (pretty-stream-buffer-fill-pointer stream)
161 (1+ fill-pointer))))))
162
163 (defun pretty-sout (stream string start end)
164 (declare (type pretty-stream stream)
165 (type string string)
166 (type index start)
167 (type (or index null) end)
168 (si::c-local))
169 (let ((end (or end (length string))))
170 (unless (= start end)
171 (let ((newline (position #\newline string :start start :end end)))
172 (cond
173 (newline
174 (pretty-sout stream string start newline)
175 (enqueue-newline stream :literal)
176 (pretty-sout stream string (1+ newline) end))
177 (t
178 (let ((chars (- end start)))
179 (loop
180 (let* ((available (assure-space-in-buffer stream chars))
181 (count (min available chars))
182 (fill-pointer (pretty-stream-buffer-fill-pointer stream))
183 (new-fill-ptr (+ fill-pointer count)))
184 (replace (pretty-stream-buffer stream)
185 string
186 :start1 fill-pointer :end1 new-fill-ptr
187 :start2 start)
188 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
189 (decf chars count)
190 (when (zerop count)
191 (return))
192 (incf start count))))))))))
193
194
195 ;;;; Logical blocks.
196
197 (defstruct logical-block
198 ;;
199 ;; The column this logical block started in.
200 (start-column 0 :type column)
201 ;;
202 ;; The column the current section started in.
203 (section-column 0 :type column)
204 ;;
205 ;; The length of the per-line prefix. We can't move the indentation
206 ;; left of this.
207 (per-line-prefix-end 0 :type index)
208 ;;
209 ;; The overall length of the prefix, including any indentation.
210 (prefix-length 0 :type index)
211 ;;
212 ;; The overall length of the suffix.
213 (suffix-length 0 :type index)
214 ;;
215 ;; The line number
216 (section-start-line 0 :type index))
217
218 (defun really-start-logical-block (stream column prefix suffix)
219 (declare (si::c-local)
220 (type pretty-stream stream))
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)
260
261 (defun set-indentation (stream column)
262 (declare (si::c-local)
263 (type pretty-stream stream))
264 (let* ((prefix (pretty-stream-prefix stream))
265 (prefix-len (length prefix))
266 (block (car (pretty-stream-blocks stream)))
267 (current (logical-block-prefix-length block))
268 (minimum (logical-block-per-line-prefix-end block))
269 (column (max minimum column)))
270 (when (> column prefix-len)
271 (setf prefix
272 (replace (make-string (max (* prefix-len 2)
273 (+ prefix-len
274 (floor (* (- column prefix-len) 5)
275 4))))
276 prefix
277 :end1 current))
278 (setf (pretty-stream-prefix stream) prefix))
279 (when (> column current)
280 (fill prefix #\space :start current :end column))
281 (setf (logical-block-prefix-length block) column)))
282
283 (defun really-end-logical-block (stream)
284 (declare (si::c-local)
285 (type pretty-stream stream))
286 (let* ((old (pop (pretty-stream-blocks stream)))
287 (old-indent (logical-block-prefix-length old))
288 (new (car (pretty-stream-blocks stream)))
289 (new-indent (logical-block-prefix-length new)))
290 (when (> new-indent old-indent)
291 (fill (pretty-stream-prefix stream) #\space
292 :start old-indent :end new-indent)))
293 nil)
294
295
296 ;;;; The pending operation queue.
297
298 (defstruct queued-op
299 (posn 0 :type posn))
300
301 (eval-when (:compile-toplevel :execute)
302 (defmacro enqueue (stream type &rest args)
303 (let ((constructor (intern (concatenate 'string
304 "MAKE-"
305 (symbol-name type)))))
306 (once-only ((stream stream)
307 (entry `(,constructor :posn
308 (index-posn
309 (pretty-stream-buffer-fill-pointer
310 (truly-the pretty-stream ,stream))
311 ,stream)
312 ,@args))
313 (op `(list ,entry))
314 (head `(pretty-stream-queue-head (truly-the pretty-stream ,stream))))
315 `(progn
316 (if ,head
317 (setf (cdr ,head) ,op)
318 (setf (pretty-stream-queue-tail (truly-the pretty-stream ,stream)) ,op))
319 (setf (pretty-stream-queue-head (truly-the pretty-stream ,stream)) ,op)
320 ,entry))))
321 )
322
323 (defstruct (section-start
324 (:include queued-op))
325 (depth 0 :type index)
326 (section-end nil :type (or null newline block-end)))
327
328 (defstruct (newline
329 (:include section-start))
330 (kind (required-argument)
331 :type (member :linear :fill :miser :literal :mandatory)))
332
333 (defun enqueue-newline (stream kind)
334 (declare (si::c-local)
335 (type pretty-stream stream))
336 (let* ((depth (length (pretty-stream-pending-blocks stream)))
337 (newline (enqueue stream newline :kind kind :depth depth)))
338 (dolist (entry (pretty-stream-queue-tail stream))
339 (when (and (not (eq newline entry))
340 (section-start-p entry)
341 (null (section-start-section-end entry))
342 (<= depth (section-start-depth entry)))
343 (setf (section-start-section-end entry) newline))))
344 (maybe-output stream (or (eq kind :literal) (eq kind :mandatory))))
345
346 (defstruct (indentation
347 (:include queued-op))
348 (kind (required-argument) :type (member :block :current))
349 (amount 0 :type fixnum))
350
351 (defun enqueue-indent (stream kind amount)
352 (declare (si::c-local))
353 (enqueue stream indentation :kind kind :amount amount))
354
355 (defstruct (block-start
356 (:include section-start))
357 (block-end nil :type (or null block-end))
358 (prefix nil :type (or null string))
359 (suffix nil :type (or null string)))
360
361 (defun start-logical-block (stream prefix per-line-p suffix)
362 (declare (si::c-local)
363 (type string prefix suffix)
364 (type pretty-stream stream)
365 (ext:check-arguments-type))
366 (let ((prefix-len (length prefix)))
367 (when (plusp prefix-len)
368 (pretty-sout stream prefix 0 prefix-len))
369 (let* ((pending-blocks (pretty-stream-pending-blocks stream))
370 (start (enqueue stream block-start
371 :prefix (and (plusp prefix-len) per-line-p prefix)
372 :suffix (and (plusp (length suffix)) suffix)
373 :depth (length pending-blocks))))
374 (setf (pretty-stream-pending-blocks stream)
375 (cons start pending-blocks)))))
376
377 (defstruct (block-end
378 (:include queued-op))
379 (suffix nil :type (or null string)))
380
381 (defun end-logical-block (stream)
382 (declare (si::c-local)
383 (type pretty-stream stream))
384 (let* ((start (pop (pretty-stream-pending-blocks stream)))
385 (suffix (block-start-suffix start))
386 (end (enqueue stream block-end :suffix suffix)))
387 (when suffix
388 (pretty-sout stream suffix 0 (length suffix)))
389 (setf (block-start-block-end start) end)))
390
391 (defstruct (tab
392 (:include queued-op))
393 (sectionp nil :type (member t nil))
394 (relativep nil :type (member t nil))
395 (colnum 0 :type column)
396 (colinc 0 :type column))
397
398 (defun enqueue-tab (stream kind colnum colinc)
399 (declare (si::c-local))
400 (multiple-value-bind
401 (sectionp relativep)
402 (ecase kind
403 (:line (values nil nil))
404 (:line-relative (values nil t))
405 (:section (values t nil))
406 (:section-relative (values t t)))
407 (enqueue stream tab :sectionp sectionp :relativep relativep
408 :colnum colnum :colinc colinc)))
409
410
411 ;;;; Tab support.
412
413 (defun compute-tab-size (tab section-start column)
414 (declare (si::c-local))
415 (let ((colnum (tab-colnum tab))
416 (colinc (tab-colinc tab)))
417 (when (tab-sectionp tab)
418 (setf column (- column section-start)))
419 (cond ((tab-relativep tab)
420 (unless (<= colinc 1)
421 (let ((newposn (+ column colnum)))
422 (let ((rem (rem newposn colinc)))
423 (unless (zerop rem)
424 (incf colnum (- colinc rem))))))
425 colnum)
426 ((< column colnum)
427 (- colnum column))
428 ((= column colnum)
429 colinc)
430 ((plusp colinc)
431 (- colinc (rem (- column colnum) colinc)))
432 (t
433 0))))
434
435 (defun index-column (index stream)
436 (declare (si::c-local)
437 (type pretty-stream stream))
438 (let ((column (pretty-stream-buffer-start-column stream))
439 (section-start (logical-block-section-column
440 (first (pretty-stream-blocks stream))))
441 (end-posn (index-posn index stream)))
442 (dolist (op (pretty-stream-queue-tail stream))
443 (when (>= (queued-op-posn op) end-posn)
444 (return))
445 (typecase op
446 (tab
447 (incf column
448 (compute-tab-size op
449 section-start
450 (+ column
451 (posn-index (tab-posn op)
452 stream)))))
453 ((or newline block-start)
454 (setf section-start
455 (+ column (posn-index (queued-op-posn op)
456 stream))))))
457 (+ column index)))
458
459 (defun expand-tabs (stream through)
460 (declare (si::c-local)
461 (type pretty-stream stream))
462 (let ((insertions nil)
463 (additional 0)
464 (column (pretty-stream-buffer-start-column stream))
465 (section-start (logical-block-section-column
466 (first (pretty-stream-blocks stream)))))
467 (dolist (op (pretty-stream-queue-tail stream))
468 (typecase op
469 (tab
470 (let* ((index (posn-index (tab-posn op) stream))
471 (tabsize (compute-tab-size op
472 section-start
473 (+ column index))))
474 (unless (zerop tabsize)
475 (push (cons index tabsize) insertions)
476 (incf additional tabsize)
477 (incf column tabsize))))
478 ((or newline block-start)
479 (setf section-start
480 (+ column (posn-index (queued-op-posn op) stream)))))
481 (when (eq op through)
482 (return)))
483 (when insertions
484 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
485 (new-fill-ptr (+ fill-ptr additional))
486 (buffer (pretty-stream-buffer stream))
487 (new-buffer buffer)
488 (length (length buffer))
489 (end fill-ptr))
490 (when (> new-fill-ptr length)
491 (let ((new-length (max (* length 2)
492 (+ fill-ptr
493 (floor (* additional 5) 4)))))
494 (setf new-buffer (make-string new-length))
495 (setf (pretty-stream-buffer stream) new-buffer)))
496 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
497 (decf (pretty-stream-buffer-offset stream) additional)
498 (dolist (insertion insertions)
499 (let* ((srcpos (car insertion))
500 (amount (cdr insertion))
501 (dstpos (+ srcpos additional)))
502 (replace new-buffer buffer :start1 dstpos :start2 srcpos :end2 end)
503 (fill new-buffer #\space :start (- dstpos amount) :end dstpos)
504 (decf additional amount)
505 (setf end srcpos)))
506 (unless (eq new-buffer buffer)
507 (replace new-buffer buffer :end1 end :end2 end))))))
508
509
510 ;;;; Stuff to do the actual outputting.
511
512 (defun assure-space-in-buffer (stream want)
513 (declare (type pretty-stream stream)
514 (type index want)
515 (si::c-local))
516 (let* ((buffer (pretty-stream-buffer stream))
517 (length (length buffer))
518 (fill-ptr (pretty-stream-buffer-fill-pointer stream))
519 (available (- length fill-ptr)))
520 (cond ((plusp available)
521 available)
522 ((> fill-ptr (pretty-stream-line-length stream))
523 (unless (maybe-output stream nil)
524 (output-partial-line stream))
525 (assure-space-in-buffer stream want))
526 (t
527 (let* ((new-length (max (* length 2)
528 (+ length
529 (floor (* want 5) 4))))
530 (new-buffer (make-string new-length)))
531 (setf (pretty-stream-buffer stream) new-buffer)
532 (replace new-buffer buffer :end1 fill-ptr)
533 (- new-length fill-ptr))))))
534
535 (defun maybe-output (stream force-newlines-p)
536 (declare (type pretty-stream stream)
537 (si::c-local))
538 (let ((tail (pretty-stream-queue-tail stream))
539 (output-anything nil))
540 (loop
541 (unless tail
542 (setf (pretty-stream-queue-head stream) nil)
543 (return))
544 (let ((next (pop tail)))
545 (etypecase next
546 (newline
547 (when (ecase (newline-kind next)
548 ((:literal :mandatory :linear) t)
549 (:miser (misering-p stream))
550 (:fill
551 (or (misering-p stream)
552 (> (pretty-stream-line-number stream)
553 (logical-block-section-start-line
554 (first (pretty-stream-blocks stream))))
555 (ecase (fits-on-line-p stream
556 (newline-section-end next)
557 force-newlines-p)
558 ((t) nil)
559 ((nil) t)
560 (:dont-know
561 (return))))))
562 (setf output-anything t)
563 (output-line stream next)))
564 (indentation
565 (unless (misering-p stream)
566 (set-indentation stream
567 (+ (ecase (indentation-kind next)
568 (:block
569 (logical-block-start-column
570 (car (pretty-stream-blocks stream))))
571 (:current
572 (posn-column
573 (indentation-posn next)
574 stream)))
575 (indentation-amount next)))))
576 (block-start
577 (ecase (fits-on-line-p stream (block-start-section-end next)
578 force-newlines-p)
579 ((t)
580 ;; Just nuke the whole logical block and make it look like one
581 ;; nice long literal.
582 (let ((end (block-start-block-end next)))
583 (expand-tabs stream end)
584 (setf tail (cdr (member end tail)))))
585 ((nil)
586 (really-start-logical-block
587 stream
588 (posn-column (block-start-posn next) stream)
589 (block-start-prefix next)
590 (block-start-suffix next)))
591 (:dont-know
592 (return))))
593 (block-end
594 (really-end-logical-block stream))
595 (tab
596 (expand-tabs stream next))))
597 (setf (pretty-stream-queue-tail stream) tail))
598 output-anything))
599
600 (defun misering-p (stream)
601 (declare (type pretty-stream stream)
602 (si::c-local))
603 (and *print-miser-width*
604 (<= (- (pretty-stream-line-length stream)
605 (logical-block-start-column (car (pretty-stream-blocks stream))))
606 *print-miser-width*)))
607
608 (defun fits-on-line-p (stream until force-newlines-p)
609 (declare (si::c-local)
610 (type pretty-stream stream))
611 (let ((available (pretty-stream-line-length stream)))
612 (when (and (not *print-readably*) *print-lines*
613 (= *print-lines* (pretty-stream-line-number stream)))
614 (decf available 3) ; for the `` ..''
615 (decf available (logical-block-suffix-length
616 (car (pretty-stream-blocks stream)))))
617 (cond (until
618 (<= (posn-column (queued-op-posn until) stream) available))
619 (force-newlines-p nil)
620 ((> (index-column (pretty-stream-buffer-fill-pointer stream) stream)
621 available)
622 nil)
623 (t
624 :dont-know))))
625
626 (defun output-line (stream until)
627 (declare (type pretty-stream stream)
628 (type newline until)
629 (si::c-local))
630 (let* ((target (pretty-stream-target stream))
631 (buffer (pretty-stream-buffer stream))
632 (kind (newline-kind until))
633 (literal-p (eq kind :literal))
634 (amount-to-consume (posn-index (newline-posn until) stream))
635 (amount-to-print
636 (if literal-p
637 amount-to-consume
638 (let ((last-non-blank
639 (position #\space buffer :end amount-to-consume
640 :from-end t :test #'char/=)))
641 (if last-non-blank
642 (1+ last-non-blank)
643 0)))))
644 (write-string buffer target :end amount-to-print)
645 (let ((line-number (pretty-stream-line-number stream)))
646 (incf line-number)
647 (when (and (not *print-readably*)
648 *print-lines* (>= line-number *print-lines*))
649 (write-string " .." target)
650 (let ((suffix-length (logical-block-suffix-length
651 (car (pretty-stream-blocks stream)))))
652 (unless (zerop suffix-length)
653 (let* ((suffix (pretty-stream-suffix stream))
654 (len (length suffix)))
655 (write-string suffix target
656 :start (- len suffix-length)
657 :end len))))
658 (throw 'line-limit-abbreviation-happened t))
659 (setf (pretty-stream-line-number stream) line-number)
660 (write-char #\newline target)
661 (setf (pretty-stream-buffer-start-column stream) 0)
662 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
663 (block (first (pretty-stream-blocks stream)))
664 (prefix-len
665 (if literal-p
666 (logical-block-per-line-prefix-end block)
667 (logical-block-prefix-length block)))
668 (shift (- amount-to-consume prefix-len))
669 (new-fill-ptr (- fill-ptr shift))
670 (new-buffer buffer)
671 (buffer-length (length buffer)))
672 (when (> new-fill-ptr buffer-length)
673 (setf new-buffer
674 (make-string (max (* buffer-length 2)
675 (+ buffer-length
676 (floor (* (- new-fill-ptr buffer-length)
677 5)
678 4)))))
679 (setf (pretty-stream-buffer stream) new-buffer))
680 (replace new-buffer buffer
681 :start1 prefix-len :start2 amount-to-consume :end2 fill-ptr)
682 (replace new-buffer (pretty-stream-prefix stream)
683 :end1 prefix-len)
684 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
685 (incf (pretty-stream-buffer-offset stream) shift)
686 (unless literal-p
687 (setf (logical-block-section-column block) prefix-len)
688 (setf (logical-block-section-start-line block) line-number))))))
689
690 (defun output-partial-line (stream)
691 (declare (si::c-local)
692 (type pretty-stream stream))
693 (let* ((fill-ptr (pretty-stream-buffer-fill-pointer stream))
694 (tail (pretty-stream-queue-tail stream))
695 (count
696 (if tail
697 (posn-index (queued-op-posn (car tail)) stream)
698 fill-ptr))
699 (new-fill-ptr (- fill-ptr count))
700 (buffer (pretty-stream-buffer stream)))
701 (when (zerop count)
702 (error "Output-partial-line called when nothing can be output."))
703 (write-string buffer (pretty-stream-target stream)
704 :start 0 :end count)
705 (incf (pretty-stream-buffer-start-column stream) count)
706 (replace buffer buffer :end1 new-fill-ptr :start2 count :end2 fill-ptr)
707 (setf (pretty-stream-buffer-fill-pointer stream) new-fill-ptr)
708 (incf (pretty-stream-buffer-offset stream) count)))
709
710 (defun force-pretty-output (stream)
711 (declare (si::c-local)
712 (type pretty-stream stream))
713 (maybe-output stream nil)
714 (expand-tabs stream nil)
715 (write-string (pretty-stream-buffer stream)
716 (pretty-stream-target stream)
717 :end (pretty-stream-buffer-fill-pointer stream)))
718
719
720 ;;;; Utilities.
721
722 (defun pprint-pop-helper (object count stream &aux code)
723 (cond ((not (listp object))
724 (write-string ". " stream)
725 (write-object object stream)
726 nil)
727 ((and (not *print-readably*)
728 (eql count *print-length*))
729 (write-string "..." stream)
730 nil)
731 ((or (null object)
732 (zerop count)
733 (null *circle-counter*))
734 t)
735 ((eql 'NULL (setf code (gethash object *circle-stack* 'NULL)))
736 ;; We visit this part of the list for the first time and thus we must
737 ;; register it in the hash, or we are on the second pass and have
738 ;; found a completely new list. This should not happen, but anyway
739 ;; we try to print it.
740 (search-print-circle object)
741 t)
742 ((and (null code) (integerp *circle-counter*))
743 ;; This object is not visited twice.
744 t)
745 (t
746 ;; In all other cases, WRITE-OBJECT
747 (write-string ". " stream)
748 (write-object object stream)
749 nil)))
750
751 ;;;; User interface to the pretty printer.
752
753 (defun check-print-level ()
754 (declare (si::c-local))
755 "Automatically handle *print-level* abbreviation. If we are too deep, then
756 a # is printed to STREAM and BODY is ignored."
757 (cond ((or *print-readably* (null *print-level*))
758 t)
759 ((zerop *print-level*)
760 nil)
761 (t
762 (setf *print-level* (1- *print-level*)))))
763
764 (defun do-pprint-logical-block (function object stream prefix
765 per-line-prefix-p suffix)
766 (declare (si::c-local))
767 (unless (listp object)
768 (write-object object stream)
769 (return-from do-pprint-logical-block nil))
770 (when (and (not *print-readably*) (eql *print-level* 0))
771 (write-char #\# stream)
772 (return-from do-pprint-logical-block nil))
773 (write-object-with-circle
774 object stream
775 #'(lambda (object s)
776 (unless (pretty-stream-p s)
777 (setf s (make-pretty-stream s)))
778 (let ((*print-level* (and (not *print-readably*)
779 *print-level*
780 (1- *print-level*))))
781 (start-logical-block s prefix per-line-prefix-p suffix)
782 (funcall function object s)
783 (end-logical-block s))))
784 nil)
785
786 (defun pprint-logical-block-helper (function object stream prefix
787 per-line-prefix-p suffix)
788 (setf stream (case stream
789 ((nil) *standard-output*)
790 ((t) *terminal-io*)
791 (t stream)))
792 (if (pretty-stream-p stream)
793 (do-pprint-logical-block function object stream prefix
794 per-line-prefix-p suffix)
795 (let ((stream (make-pretty-stream stream)))
796 (catch 'line-limit-abbreviation-happened
797 (do-pprint-logical-block function object stream prefix
798 per-line-prefix-p suffix)
799 (force-pretty-output stream))
800 nil)))
801
802 (defmacro pprint-logical-block
803 ((stream-symbol object &key (prefix "" prefix-p)
804 (per-line-prefix "" per-line-prefix-p)
805 (suffix "" suffix-p))
806 &body body)
807 "Group some output into a logical block. STREAM-SYMBOL should be either a
808 stream, T (for *TERMINAL-IO*), or NIL (for *STANDARD-OUTPUT*). The printer
809 control variable *PRINT-LEVEL* is automatically handled."
810 (declare #.+ecl-safe-declarations+)
811 (when per-line-prefix-p
812 (when prefix-p
813 (error "Cannot specify both a prefix and a per-line-prefix."))
814 (setf prefix per-line-prefix))
815 (let* ((object-var (gensym))
816 (block-name (gensym "PPRINT-LOGICAL-BLOCK-"))
817 (count-name (gensym "PPRINT-LOGICAL-BLOCK-LENGTH-"))
818 (stream-var (case stream-symbol
819 ((nil) '*standard-output*)
820 ((t) '*terminal-io*)
821 (t stream-symbol)))
822 (function
823 `(ext::lambda-block ,block-name (,object-var ,stream-var
824 &aux (,count-name 0))
825 (declare (ignorable ,object-var ,stream-var ,count-name))
826 (macrolet ((pprint-pop ()
827 '(progn
828 (unless (pprint-pop-helper ,object-var ,count-name
829 ,stream-var)
830 (return-from ,block-name nil))
831 (incf ,count-name)
832 ,(if object `(pop ,object-var) nil)))
833 (pprint-exit-if-list-exhausted ()
834 ,(if object
835 `'(when (null ,object-var)
836 (return-from ,block-name nil))
837 `'(return-from ,block-name nil))))
838 ,@body))))
839 `(pprint-logical-block-helper #',function ,object ,stream-symbol
840 ,prefix ,per-line-prefix-p ,suffix)))
841
842 (defmacro pprint-exit-if-list-exhausted ()
843 "Cause the closest enclosing use of PPRINT-LOGICAL-BLOCK to return
844 if it's list argument is exhausted. Can only be used inside
845 PPRINT-LOGICAL-BLOCK, and only when the LIST argument to
846 PPRINT-LOGICAL-BLOCK is supplied."
847 (declare #.+ecl-safe-declarations+)
848 (error "PPRINT-EXIT-IF-LIST-EXHAUSTED must be lexically inside ~
849 PPRINT-LOGICAL-BLOCK."))
850
851 (defmacro pprint-pop ()
852 "Return the next element from LIST argument to the closest enclosing
853 use of PPRINT-LOGICAL-BLOCK, automatically handling *PRINT-LENGTH*
854 and *PRINT-CIRCLE*. Can only be used inside PPRINT-LOGICAL-BLOCK.
855 If the LIST argument to PPRINT-LOGICAL-BLOCK was NIL, then nothing
856 is poped, but the *PRINT-LENGTH* testing still happens."
857 (declare #.+ecl-safe-declarations+)
858 (error "PPRINT-POP must be lexically inside PPRINT-LOGICAL-BLOCK."))
859
860 (defun pprint-newline (kind &optional stream)
861 "Output a conditional newline to STREAM (which defaults to
862 *STANDARD-OUTPUT*) if it is a pretty-printing stream, and do
863 nothing if not. KIND can be one of:
864 :LINEAR - A line break is inserted if and only if the immediatly
865 containing section cannot be printed on one line.
866 :MISER - Same as LINEAR, but only if ``miser-style'' is in effect.
867 (See *PRINT-MISER-WIDTH*.)
868 :FILL - A line break is inserted if and only if either:
869 (a) the following section cannot be printed on the end of the
870 current line,
871 (b) the preceding section was not printed on a single line, or
872 (c) the immediately containing section cannot be printed on one
873 line and miser-style is in effect.
874 :MANDATORY - A line break is always inserted.
875 When a line break is inserted by any type of conditional newline, any
876 blanks that immediately precede the conditional newline are ommitted
877 from the output and indentation is introduced at the beginning of the
878 next line. (See PPRINT-INDENT.)"
879 (declare (type (member :linear :miser :fill :mandatory) kind)
880 (type (or stream (member t nil)) stream)
881 (ext:check-arguments-type)
882 #.+ecl-safe-declarations+)
883 (let ((stream (case stream
884 ((t) *terminal-io*)
885 ((nil) *standard-output*)
886 (t stream))))
887 (when (and (pretty-stream-p stream) *print-pretty*)
888 (enqueue-newline stream kind)))
889 nil)
890
891 (defun pprint-indent (relative-to n &optional stream)
892 "Specify the indentation to use in the current logical block if STREAM
893 (which defaults to *STANDARD-OUTPUT*) is it is a pretty-printing stream
894 and do nothing if not. (See PPRINT-LOGICAL-BLOCK.) N is the indention
895 to use (in ems, the width of an ``m'') and RELATIVE-TO can be either:
896 :BLOCK - Indent relative to the column the current logical block
897 started on.
898 :CURRENT - Indent relative to the current column.
899 The new indention value does not take effect until the following line
900 break."
901 (declare (type (member :block :current) relative-to)
902 (type real n)
903 (type (or stream (member t nil)) stream)
904 (ext:check-arguments-type)
905 #.+ecl-safe-declarations+)
906 (let ((stream (case stream
907 ((t) *terminal-io*)
908 ((nil) *standard-output*)
909 (t stream))))
910 (when (and (pretty-stream-p stream) *print-pretty*)
911 (enqueue-indent stream relative-to (round n))))
912 nil)
913
914 (defun pprint-tab (kind colnum colinc &optional stream)
915 "If STREAM (which defaults to *STANDARD-OUTPUT*) is a pretty-printing
916 stream, perform tabbing based on KIND, otherwise do nothing. KIND can
917 be one of:
918 :LINE - Tab to column COLNUM. If already past COLNUM tab to the next
919 multiple of COLINC.
920 :SECTION - Same as :LINE, but count from the start of the current
921 section, not the start of the line.
922 :LINE-RELATIVE - Output COLNUM spaces, then tab to the next multiple of
923 COLINC.
924 :SECTION-RELATIVE - Same as :LINE-RELATIVE, but count from the start
925 of the current section, not the start of the line."
926 (declare (type (member :line :section :line-relative :section-relative) kind)
927 (type unsigned-byte colnum colinc)
928 (type (or stream (member t nil)) stream)
929 (ext:check-arguments-type)
930 #.+ecl-safe-declarations+)
931 (let ((stream (case stream
932 ((t) *terminal-io*)
933 ((nil) *standard-output*)
934 (t stream))))
935 (when (and (pretty-stream-p stream) *print-pretty*)
936 (enqueue-tab stream kind colnum colinc)))
937 nil)
938
939 (defun pprint-fill (stream list &optional (colon? t) atsign?)
940 "Output LIST to STREAM putting :FILL conditional newlines between each
941 element. If COLON? is NIL (defaults to T), then no parens are printed
942 around the output. ATSIGN? is ignored (but allowed so that PPRINT-FILL
943 can be used with the ~/.../ format directive."
944 (declare (ignore atsign?)
945 (type (or stream (member t nil)) stream)
946 (ext:check-arguments-type)
947 #.+ecl-safe-declarations+)
948 (pprint-logical-block (stream list
949 :prefix (if colon? "(" "")
950 :suffix (if colon? ")" ""))
951 (pprint-exit-if-list-exhausted)
952 (loop
953 (write-object (pprint-pop) stream)
954 (pprint-exit-if-list-exhausted)
955 (write-char #\space stream)
956 (pprint-newline :fill stream))))
957
958 (defun pprint-linear (stream list &optional (colon? t) atsign?)
959 "Output LIST to STREAM putting :LINEAR conditional newlines between each
960 element. If COLON? is NIL (defaults to T), then no parens are printed
961 around the output. ATSIGN? is ignored (but allowed so that PPRINT-LINEAR
962 can be used with the ~/.../ format directive."
963 (declare (ignore atsign?)
964 (type (or stream (member t nil)) stream)
965 (ext:check-arguments-type)
966 #.+ecl-safe-declarations+)
967 (pprint-logical-block (stream list
968 :prefix (if colon? "(" "")
969 :suffix (if colon? ")" ""))
970 (pprint-exit-if-list-exhausted)
971 (loop
972 (write-object (pprint-pop) stream)
973 (pprint-exit-if-list-exhausted)
974 (write-char #\space stream)
975 (pprint-newline :linear stream))))
976
977 (defun pprint-tabular (stream list &optional (colon? t) atsign? (tabsize 16))
978 "Output LIST to STREAM tabbing to the next column that is an even multiple
979 of TABSIZE (which defaults to 16) between each element. :FILL style
980 conditional newlines are also output between each element. If COLON? is
981 NIL (defaults to T), then no parens are printed around the output.
982 ATSIGN? is ignored (but allowed so that PPRINT-TABULAR can be used with
983 the ~/.../ format directive."
984 (declare (ignore atsign?)
985 (type (or stream (member t nil)) stream)
986 (type (or unsigned-byte null) tabsize)
987 (ext:check-arguments-type)
988 #.+ecl-safe-declarations+)
989 (pprint-logical-block (stream list
990 :prefix (if colon? "(" "")
991 :suffix (if colon? ")" ""))
992 (pprint-exit-if-list-exhausted)
993 (loop
994 (write-object (pprint-pop) stream)
995 (pprint-exit-if-list-exhausted)
996 (write-char #\space stream)
997 (pprint-tab :section-relative 0 (or tabsize 16) stream)
998 (pprint-newline :fill stream))))
999
1000
1001 ;;;; Pprint-dispatch tables.
1002
1003 (defvar *standard-pprint-dispatch*)
1004 (defvar *initial-pprint-dispatch*)
1005
1006 (defstruct (pprint-dispatch-entry
1007 (:print-function %print-pprint-dispatch-entry))
1008 ;;
1009 ;; The type specifier for this entry.
1010 (type (required-argument) :type t)
1011 ;;
1012 ;; The priority for this guy.
1013 (priority 0 :type real)
1014 ;;
1015 ;; T iff one of the original entries.
1016 (initial-p (not (boundp '*initial-pprint-dispatch*)) :type (member t nil))
1017 ;;
1018 ;; And the associated function.
1019 (function (required-argument) :type (or function symbol)))
1020
1021 (defun %print-pprint-dispatch-entry (entry stream depth)
1022 (declare (ignore depth))
1023 (print-unreadable-object (entry stream :type t)
1024 (format stream "Type=~S, priority=~S~@[ [Initial]~]"
1025 (pprint-dispatch-entry-type entry)
1026 (pprint-dispatch-entry-priority entry)
1027 (pprint-dispatch-entry-initial-p entry))))
1028
1029 (defstruct (pprint-dispatch-table
1030 (:print-function %print-pprint-dispatch-table))
1031 ;; Are we allowed to modify this table?
1032 (read-only-p nil)
1033 ;;
1034 ;; A list of all the entries (except for CONS entries below) in highest
1035 ;; to lowest priority.
1036 (entries nil :type list)
1037 ;;
1038 ;; A hash table mapping things to entries for type specifiers of the
1039 ;; form (CONS (MEMBER <thing>)). If the type specifier is of this form,
1040 ;; we put it in this hash table instead of the regular entries table.
1041 (cons-entries (make-hash-table :test #'eql)))
1042
1043 (defun %print-pprint-dispatch-table (table stream depth)
1044 (declare (ignore depth))
1045 (print-unreadable-object (table stream :type t :identity t)))
1046
1047 (defun cons-type-specifier-p (spec)
1048 (declare (si::c-local))
1049 (and (consp spec)
1050 (eq (car spec) 'cons)
1051 (cdr spec)
1052 (null (cddr spec))
1053 (let ((car (cadr spec)))
1054 (and (consp car)
1055 (let ((carcar (car car)))
1056 (or (eq carcar 'member)
1057 (eq carcar 'eql)))
1058 (cdr car)
1059 (null (cddr car))))))
1060
1061 (defun entry< (e1 e2)
1062 (declare (type pprint-dispatch-entry e1 e2)
1063 (si::c-local))
1064 (if (pprint-dispatch-entry-initial-p e1)
1065 (if (pprint-dispatch-entry-initial-p e2)
1066 (< (pprint-dispatch-entry-priority e1)
1067 (pprint-dispatch-entry-priority e2))
1068 t)
1069 (if (pprint-dispatch-entry-initial-p e2)
1070 nil
1071 (< (pprint-dispatch-entry-priority e1)
1072 (pprint-dispatch-entry-priority e2)))))
1073
1074
1075 (defun copy-pprint-dispatch (&optional (table *print-pprint-dispatch*))
1076 (declare (type (or pprint-dispatch-table null) table)
1077 #.+ecl-safe-declarations+)
1078 (let* ((orig (or table *initial-pprint-dispatch*)))
1079 (let* ((new (make-pprint-dispatch-table
1080 :entries (copy-list (pprint-dispatch-table-entries orig))))
1081 (new-cons-entries (pprint-dispatch-table-cons-entries new)))
1082 (maphash #'(lambda (key value)
1083 (setf (gethash key new-cons-entries) value))
1084 (pprint-dispatch-table-cons-entries orig))
1085 new)))
1086
1087 (defun default-pprint-dispatch (stream object)
1088 (write-ugly-object object stream))
1089
1090 (defun pprint-dispatch (object &optional (table *print-pprint-dispatch*))
1091 (declare (type (or pprint-dispatch-table null) table)
1092 (ext:check-arguments-type)
1093 #.+ecl-safe-declarations+)
1094 (let* ((table (or table *initial-pprint-dispatch*))
1095 (cons-entry
1096 (and (consp object)
1097 (gethash (car object)
1098 (pprint-dispatch-table-cons-entries table))))
1099 (entry
1100 (dolist (entry (pprint-dispatch-table-entries table) cons-entry)
1101 (when (and cons-entry
1102 (entry< entry cons-entry))
1103 (return cons-entry))
1104 (when (typep object (pprint-dispatch-entry-type entry))
1105 (return entry)))))
1106 (if entry
1107 (values (pprint-dispatch-entry-function entry) t)
1108 (values #'default-pprint-dispatch nil))))
1109
1110 (defun set-pprint-dispatch (type function &optional
1111 (priority 0) (table *print-pprint-dispatch*))
1112 (declare (type t type)
1113 (type (or null function symbol) function)
1114 (type real priority)
1115 (type pprint-dispatch-table table)
1116 #.+ecl-safe-declarations+)
1117 (when (pprint-dispatch-table-read-only-p table)
1118 (cerror "Ignore and continue"
1119 "Tried to modify a read-only pprint dispatch table: ~A"
1120 table))
1121 ;; FIXME! This check should be automatically generated when compiling
1122 ;; with high enough safety mode.
1123 (unless (typep priority 'real)
1124 (error 'simple-type-error
1125 :format-control "Not a valid priority for set-pprint-dispatch: ~A"
1126 :format-arguments (list priority)
1127 :expected-type 'real
1128 :datum priority))
1129 (if function
1130 (if (cons-type-specifier-p type)
1131 (setf (gethash (second (second type))
1132 (pprint-dispatch-table-cons-entries table))
1133 (make-pprint-dispatch-entry :type type :priority priority
1134 :function function))
1135 (let ((list (delete type (pprint-dispatch-table-entries table)
1136 :key #'pprint-dispatch-entry-type
1137 :test #'equal))
1138 (entry (make-pprint-dispatch-entry
1139 :type type
1140 :priority priority :function function)))
1141 (do ((prev nil next)
1142 (next list (cdr next)))
1143 ((null next)
1144 (if prev
1145 (setf (cdr prev) (list entry))
1146 (setf list (list entry))))
1147 (when (entry< (car next) entry)
1148 (if prev
1149 (setf (cdr prev) (cons entry next))
1150 (setf list (cons entry next)))
1151 (return)))
1152 (setf (pprint-dispatch-table-entries table) list)))
1153 (if (cons-type-specifier-p type)
1154 (remhash (second (second type))
1155 (pprint-dispatch-table-cons-entries table))
1156 (setf (pprint-dispatch-table-entries table)
1157 (delete type (pprint-dispatch-table-entries table)
1158 :key #'pprint-dispatch-entry-type
1159 :test #'equal))))
1160 nil)
1161
1162
1163 ;;;; Standard pretty-printing routines.
1164
1165 (defun pprint-array (stream array)
1166 (cond ((or (and (null *print-array*) (null *print-readably*))
1167 (stringp array)
1168 (bit-vector-p array))
1169 (write-ugly-object array stream))
1170 (*print-readably*
1171 (pprint-raw-array stream array))
1172 ((vectorp array)
1173 (pprint-vector stream array))
1174 (t
1175 (pprint-multi-dim-array stream array))))
1176
1177 (defun pprint-vector (stream vector)
1178 (write-object-with-circle
1179 vector stream
1180 #'(lambda (vector stream)
1181 (pprint-logical-block (stream nil :prefix "#(" :suffix ")")
1182 (dotimes (i (length vector))
1183 (unless (zerop i)
1184 (write-char #\space stream)
1185 (pprint-newline :fill stream))
1186 (pprint-pop)
1187 (write-object (aref vector i) stream))))))
1188
1189 (defun pprint-array-contents (stream array)
1190 (declare (si::c-local)
1191 (array array))
1192 (labels ((output-guts (stream index dimensions)
1193 (if (null dimensions)
1194 (write-object (row-major-aref array index) stream)
1195 (pprint-logical-block
1196 (stream nil :prefix "(" :suffix ")")
1197 (let ((dim (car dimensions)))
1198 (unless (zerop dim)
1199 (let* ((dims (cdr dimensions))
1200 (index index)
1201 (step (reduce #'* dims))
1202 (count 0))
1203 (loop
1204 (pprint-pop)
1205 (output-guts stream index dims)
1206 (when (= (incf count) dim)
1207 (return))
1208 (write-char #\space stream)
1209 (pprint-newline (if dims :linear :fill)
1210 stream)
1211 (incf index step)))))))))
1212 (output-guts stream 0 (array-dimensions array))))
1213
1214 (defun pprint-multi-dim-array (stream array)
1215 (declare (si::c-local))
1216 (write-object-with-circle
1217 array stream
1218 #'(lambda (array stream)
1219 (funcall (formatter "#~DA") stream (array-rank array))
1220 (pprint-array-contents stream array))))
1221
1222 (defun pprint-raw-array (stream array)
1223 (declare (si::c-local))
1224 (write-object-with-circle
1225 array stream
1226 #'(lambda (array stream)
1227 (pprint-logical-block (stream nil :prefix "#A(" :suffix ")")
1228 (write-object (array-element-type array) stream)
1229 (write-char #\Space stream)
1230 (pprint-newline :fill stream)
1231 (write-object (array-dimensions array) stream)
1232 (write-char #\Space stream)
1233 (pprint-newline :fill stream)
1234 (pprint-array-contents stream array)))))
1235
1236 (defun pprint-lambda-list (stream lambda-list &rest noise)
1237 (declare (ignore noise))
1238 (pprint-logical-block (stream lambda-list :prefix "(" :suffix ")")
1239 (let ((state :required)
1240 (first t))
1241 (loop
1242 (pprint-exit-if-list-exhausted)
1243 (unless first
1244 (write-char #\space stream))
1245 (let ((arg (pprint-pop)))
1246 (unless first
1247 (case arg
1248 (&optional
1249 (setf state :optional)
1250 (pprint-newline :linear stream))
1251 ((&rest &body)
1252 (setf state :required)
1253 (pprint-newline :linear stream))
1254 (&key
1255 (setf state :key)
1256 (pprint-newline :linear stream))
1257 (&aux
1258 (setf state :optional)
1259 (pprint-newline :linear stream))
1260 (t
1261 (pprint-newline :fill stream))))
1262 (ecase state
1263 (:required
1264 (pprint-lambda-list stream arg))
1265 ((:optional :key)
1266 (pprint-logical-block
1267 (stream arg :prefix "(" :suffix ")")
1268 (pprint-exit-if-list-exhausted)
1269 (if (eq state :key)
1270 (pprint-logical-block
1271 (stream (pprint-pop) :prefix "(" :suffix ")")
1272 (pprint-exit-if-list-exhausted)
1273 (write-object (pprint-pop) stream)
1274 (pprint-exit-if-list-exhausted)
1275 (write-char #\space stream)
1276 (pprint-newline :fill stream)
1277 (pprint-lambda-list stream (pprint-pop))
1278 (loop
1279 (pprint-exit-if-list-exhausted)
1280 (write-char #\space stream)
1281 (pprint-newline :fill stream)
1282 (write-object (pprint-pop) stream)))
1283 (pprint-lambda-list stream (pprint-pop)))
1284 (loop
1285 (pprint-exit-if-list-exhausted)
1286 (write-char #\space stream)
1287 (pprint-newline :linear stream)
1288 (write-object (pprint-pop) stream))))))
1289 (setf first nil)))))
1290
1291 (defun pprint-lambda (stream list &rest noise)
1292 (declare (ignore noise))
1293 (funcall (formatter
1294 "~:<~^~W~^~3I ~:_~/SI:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1295 stream list))
1296
1297 (defun pprint-block (stream list &rest noise)
1298 (declare (ignore noise))
1299 (funcall (formatter "~:<~^~W~^~3I ~:_~W~1I~@{ ~_~W~}~:>") stream list))
1300
1301 (defun pprint-flet (stream list &rest noise)
1302 (declare (ignore noise))
1303 (funcall (formatter
1304 "~:<~^~W~^ ~@_~:<~@{~:<~^~W~^~3I ~:_~/SI:PPRINT-LAMBDA-LIST/~1I~:@_~@{~W~^ ~_~}~:>~^ ~_~}~:>~1I~@:_~@{~W~^ ~_~}~:>")
1305 stream
1306 list))
1307
1308 (defun pprint-let (stream list &rest noise)
1309 (declare (ignore noise))
1310 (funcall (formatter "~:<~^~W~^ ~@_~:<~@{~:<~^~W~@{ ~_~W~}~:>~^ ~_~}~:>~1I~:@_~@{~W~^ ~_~}~:>")
1311 stream
1312 list))
1313
1314 (defun pprint-progn (stream list &rest noise)
1315 (declare (ignore noise))
1316 (funcall (formatter "~:<~^~W~@{ ~_~W~}~:>") stream list))
1317
1318 (defun pprint-progv (stream list &rest noise)
1319 (declare (ignore noise))
1320 (funcall (formatter "~:<~^~W~^~3I ~_~W~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1321 stream list))
1322
1323 (defun pprint-quote (stream list &rest noise)
1324 (declare (ignore noise))
1325 (if (and (consp list)
1326 (consp (cdr list))
1327 (null (cddr list)))
1328 (case (car list)
1329 (function
1330 (write-string "#'" stream)
1331 (write-object (cadr list) stream))
1332 (quote
1333 (write-char #\' stream)
1334 (write-object (cadr list) stream))
1335 (si:quasiquote
1336 (write-char #\` stream)
1337 (write-object (cadr list) stream))
1338 (si:unquote
1339 (write-char #\, stream)
1340 (write-object (cadr list) stream))
1341 (si:unquote-splice
1342 (write-string ",@" stream)
1343 (write-object (cadr list) stream))
1344 (si:unquote-nsplice
1345 (write-string ",." stream)
1346 (write-object (cadr list) stream))
1347 (t
1348 (pprint-fill stream list)))
1349 (pprint-fill stream list)))
1350
1351 (defun pprint-setq (stream list &rest noise)
1352 (declare (ignore noise))
1353 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1354 (pprint-exit-if-list-exhausted)
1355 (write-object (pprint-pop) stream)
1356 (pprint-exit-if-list-exhausted)
1357 (write-char #\space stream)
1358 (pprint-newline :miser stream)
1359 (if (and (consp (cdr list)) (consp (cddr list)))
1360 (loop
1361 (pprint-indent :current 2 stream)
1362 (write-object (pprint-pop) stream)
1363 (pprint-exit-if-list-exhausted)
1364 (write-char #\space stream)
1365 (pprint-newline :linear stream)
1366 (pprint-indent :current -2 stream)
1367 (write-object (pprint-pop) stream)
1368 (pprint-exit-if-list-exhausted)
1369 (write-char #\space stream)
1370 (pprint-newline :linear stream))
1371 (progn
1372 (pprint-indent :current 0 stream)
1373 (write-object (pprint-pop) stream)
1374 (pprint-exit-if-list-exhausted)
1375 (write-char #\space stream)
1376 (pprint-newline :linear stream)
1377 (write-object (pprint-pop) stream)))))
1378
1379 #+ecl-min
1380 (defmacro pprint-tagbody-guts (stream)
1381 `(loop
1382 (pprint-exit-if-list-exhausted)
1383 (write-char #\space ,stream)
1384 (let ((form-or-tag (pprint-pop)))
1385 (pprint-indent :block
1386 (if (atom form-or-tag) 0 1)
1387 ,stream)
1388 (pprint-newline :linear ,stream)
1389 (write-object form-or-tag ,stream))))
1390
1391 (defun pprint-tagbody (stream list &rest noise)
1392 (declare (ignore noise))
1393 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1394 (pprint-exit-if-list-exhausted)
1395 (write-object (pprint-pop) stream)
1396 (pprint-tagbody-guts stream)))
1397
1398 (defun pprint-case (stream list &rest noise)
1399 (declare (ignore noise))
1400 (funcall (formatter
1401 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~:/SI:PPRINT-FILL/~^~@{ ~_~W~}~:>~}~:>")
1402 stream
1403 list))
1404
1405 (defun pprint-defun (stream list &rest noise)
1406 (declare (ignore noise))
1407 (funcall (formatter
1408 "~:<~^~W~^ ~@_~:I~W~^ ~:_~/SI:PPRINT-LAMBDA-LIST/~1I~@{ ~_~W~}~:>")
1409 stream
1410 list))
1411
1412 (defun pprint-destructuring-bind (stream list &rest noise)
1413 (declare (ignore noise))
1414 (funcall (formatter
1415 "~:<~^~W~^~3I ~_~:/SI:PPRINT-LAMBDA-LIST/~^ ~_~W~^~1I~@{ ~_~W~}~:>")
1416 stream list))
1417
1418 (defun pprint-do (stream list &rest noise)
1419 (declare (ignore noise))
1420 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1421 (pprint-exit-if-list-exhausted)
1422 (write-object (pprint-pop) stream)
1423 (pprint-exit-if-list-exhausted)
1424 (write-char #\space stream)
1425 (pprint-indent :current 0 stream)
1426 (funcall (formatter "~:<~@{~:<~^~W~^ ~@_~:I~W~@{ ~_~W~}~:>~^~:@_~}~:>")
1427 stream
1428 (pprint-pop))
1429 (pprint-exit-if-list-exhausted)
1430 (write-char #\space stream)
1431 (pprint-newline :linear stream)
1432 (pprint-linear stream (pprint-pop))
1433 (pprint-tagbody-guts stream)))
1434
1435 (defun pprint-dolist (stream list &rest noise)
1436 (declare (ignore noise))
1437 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1438 (pprint-exit-if-list-exhausted)
1439 (write-object (pprint-pop) stream)
1440 (pprint-exit-if-list-exhausted)
1441 (pprint-indent :block 3 stream)
1442 (write-char #\space stream)
1443 (pprint-newline :fill stream)
1444 (funcall (formatter "~:<~^~W~^ ~:_~:I~W~@{ ~_~W~}~:>")
1445 stream
1446 (pprint-pop))
1447 (pprint-tagbody-guts stream)))
1448
1449 (defun pprint-typecase (stream list &rest noise)
1450 (declare (ignore noise))
1451 (funcall (formatter
1452 "~:<~^~W~^ ~3I~:_~W~1I~@{ ~_~:<~^~W~^~@{ ~_~W~}~:>~}~:>")
1453 stream
1454 list))
1455
1456 (defun pprint-prog (stream list &rest noise)
1457 (declare (ignore noise))
1458 (pprint-logical-block (stream list :prefix "(" :suffix ")")
1459 (pprint-exit-if-list-exhausted)
1460 (write-object (pprint-pop) stream)
1461 (pprint-exit-if-list-exhausted)
1462 (write-char #\space stream)
1463 (pprint-newline :miser stream)
1464 (pprint-fill stream (pprint-pop))
1465 (pprint-tagbody-guts stream)))
1466
1467 (defun pprint-function-call (stream list &rest noise)
1468 (declare (ignore noise))
1469 (funcall (formatter "~:<~^~W~^ ~:_~:I~@{~W~^ ~_~}~:>")
1470 stream
1471 list))
1472
1473
1474 ;;;; Interface seen by regular (ugly) printer and initialization routines.
1475
1476 (eval-when (:compile-toplevel :execute)
1477 (defconstant +magic-forms+
1478 '((lambda pprint-lambda)
1479 ;; Special forms.
1480 (block pprint-block)
1481 (catch pprint-block)
1482 (compiler-let pprint-let)
1483 (eval-when pprint-block)
1484 (flet pprint-flet)
1485 (function pprint-quote)
1486 (labels pprint-flet)
1487 (let pprint-let)
1488 (let* pprint-let)
1489 (locally pprint-progn)
1490 (macrolet pprint-flet)
1491 (multiple-value-call pprint-block)
1492 (multiple-value-prog1 pprint-block)
1493 (progn pprint-progn)
1494 (progv pprint-progv)
1495 (si:quasiquote pprint-quote)
1496 (quote pprint-quote)
1497 (return-from pprint-block)
1498 (setq pprint-setq)
1499 (symbol-macrolet pprint-let)
1500 (tagbody pprint-tagbody)
1501 (throw pprint-block)
1502 (si:unquote pprint-quote)
1503 (si:unquote-nsplice pprint-quote)
1504 (si:unquote-splice pprint-quote)
1505 (unwind-protect pprint-block)
1506
1507 ;; Macros.
1508 (case pprint-case)
1509 (ccase pprint-case)
1510 (ctypecase pprint-typecase)
1511 (defconstant pprint-block)
1512 (define-modify-macro pprint-defun)
1513 (define-setf-expander pprint-defun)
1514 (defmacro pprint-defun)
1515 (defparameter pprint-block)
1516 (defsetf pprint-defun)
1517 (defstruct pprint-block)
1518 (deftype pprint-defun)
1519 (defun pprint-defun)
1520 (defvar pprint-block)
1521 (destructuring-bind pprint-destructuring-bind)
1522 (do pprint-do)
1523 (do* pprint-do)
1524 (do-all-symbols pprint-dolist)
1525 (do-external-symbols pprint-dolist)
1526 (do-symbols pprint-dolist)
1527 (dolist pprint-dolist)
1528 (dotimes pprint-dolist)
1529 (ecase pprint-case)
1530 (etypecase pprint-typecase)
1531 #+nil (handler-bind ...)
1532 #+nil (handler-case ...)
1533 #+nil (loop ...)
1534 (multiple-value-bind pprint-progv)
1535 (multiple-value-setq pprint-block)
1536 (pprint-logical-block pprint-block)
1537 (print-unreadable-object pprint-block)
1538 (prog pprint-prog)
1539 (prog* pprint-prog)
1540 (prog1 pprint-block)
1541 (prog2 pprint-progv)
1542 (psetf pprint-setq)
1543 (psetq pprint-setq)
1544 #+nil (restart-bind ...)
1545 #+nil (restart-case ...)
1546 (setf pprint-setq)
1547 (step pprint-progn)
1548 (time pprint-progn)
1549 (typecase pprint-typecase)
1550 (unless pprint-block)
1551 (when pprint-block)
1552 (with-compilation-unit pprint-block)
1553 #+nil (with-condition-restarts ...)
1554 (with-hash-table-iterator pprint-block)
1555 (with-input-from-string pprint-block)
1556 (with-open-file pprint-block)
1557 (with-open-stream pprint-block)
1558 (with-output-to-string pprint-block)
1559 (with-package-iterator pprint-block)
1560 (with-simple-restart pprint-block)
1561 (with-standard-io-syntax pprint-progn))))
1562
1563 (progn
1564 (let ((*print-pprint-dispatch* (make-pprint-dispatch-table)))
1565 ;; Printers for regular types.
1566 (set-pprint-dispatch 'array #'pprint-array)
1567 (set-pprint-dispatch '(cons (and symbol (satisfies fboundp)))
1568 #'pprint-function-call -1)
1569 (set-pprint-dispatch 'cons #'pprint-fill -2)
1570 ;; Cons cells with interesting things for the car.
1571 (dolist (magic-form '#.+magic-forms+)
1572 (set-pprint-dispatch `(cons (eql ,(first magic-form)))
1573 (symbol-function (second magic-form))))
1574 (setf *initial-pprint-dispatch* *print-pprint-dispatch*)
1575 )
1576 (setf *print-pprint-dispatch* (copy-pprint-dispatch nil)
1577 *standard-pprint-dispatch* *initial-pprint-dispatch*)
1578 (setf (pprint-dispatch-table-read-only-p *standard-pprint-dispatch*) t)
1579 (setf (first (cdr si::+io-syntax-progv-list+)) *standard-pprint-dispatch*)
1580 (setf (first (cdr si::+ecl-syntax-progv-list+)) *standard-pprint-dispatch*)
1581 #-ecl-min
1582 (setf *print-pretty* t))