0.8.14.20: Documentation madness, yet again
[sbcl.git] / src / code / early-pprint.lisp
1 ;;;; pretty printer stuff which has to be defined early (e.g. DEFMACROs)
2
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.
11
12 (in-package "SB!PRETTY")
13 \f
14 ;;;; utilities
15
16 (defmacro with-pretty-stream ((stream-var
17                                &optional (stream-expression stream-var))
18                               &body body)
19   (let ((flet-name (gensym "WITH-PRETTY-STREAM-")))
20     `(flet ((,flet-name (,stream-var)
21               ,@body))
22        (let ((stream ,stream-expression))
23          (if (pretty-stream-p stream)
24              (,flet-name stream)
25              (catch 'line-limit-abbreviation-happened
26                (let ((stream (make-pretty-stream stream)))
27                  (,flet-name stream)
28                  (force-pretty-output stream)))))
29        nil)))
30 \f
31 ;;;; user interface to the pretty printer
32
33 (defmacro pprint-logical-block ((stream-symbol
34                                  object
35                                  &key
36                                  (prefix nil prefixp)
37                                  (per-line-prefix nil per-line-prefix-p)
38                                  (suffix "" suffixp))
39                                 &body body)
40   #!+sb-doc
41   "Group some output into a logical block. STREAM-SYMBOL should be either a
42    stream, T (for *TERMINAL-IO*), or NIL (for *STANDARD-OUTPUT*). The printer
43    control variable *PRINT-LEVEL* is automatically handled."
44   (when (and prefixp per-line-prefix-p)
45     (error "cannot specify values for both PREFIX and PER-LINE-PREFIX."))
46   (multiple-value-bind (stream-var stream-expression)
47       (case stream-symbol
48         ((nil)
49          (values '*standard-output* '*standard-output*))
50         ((t)
51          (values '*terminal-io* '*terminal-io*))
52         (t
53          (values stream-symbol
54                  (once-only ((stream stream-symbol))
55                    `(case ,stream
56                       ((nil) *standard-output*)
57                       ((t) *terminal-io*)
58                       (t ,stream))))))
59     (let* ((object-var (if object (gensym) nil))
60            (block-name (gensym "PPRINT-LOGICAL-BLOCK-"))
61            (count-name (gensym "PPRINT-LOGICAL-BLOCK-LENGTH-"))
62            (pp-pop-name (gensym "PPRINT-POP-"))
63            (body
64             ;; FIXME: It looks as though PPRINT-LOGICAL-BLOCK might
65             ;; expand into a boatload of code, since DESCEND-INTO is a
66             ;; macro too. It might be worth looking at this to make
67             ;; sure it's not too bloated, since PPRINT-LOGICAL-BLOCK
68             ;; is called many times from system pretty-printing code.
69             ;;
70             ;; FIXME: I think pprint-logical-block is broken wrt
71             ;; argument order, multiple evaluation, etc. of its
72             ;; keyword (:PREFIX, :PER-LINE-PREFIX and :SUFFIX)
73             ;; arguments.  Dunno if that's legal.
74             `(descend-into (,stream-var)
75                (let ((,count-name 0))
76                  (declare (type index ,count-name) (ignorable ,count-name))
77                  ,@(when (or prefixp per-line-prefix-p)
78                      `((unless (typep ,(or prefix per-line-prefix) 'string)
79                          (error 'type-error
80                                 :datum ,(or prefix per-line-prefix)
81                                 :expected-type 'string))))
82                  ,@(when suffixp
83                      `((unless (typep ,suffix 'string)
84                          (error 'type-error
85                                 :datum ,suffix
86                                 :expected-type 'string))))
87                  (start-logical-block ,stream-var
88                                       ,(if (or prefixp per-line-prefix-p)
89                                            (or prefix per-line-prefix)
90                                            nil)
91                                       ,(if per-line-prefix-p t nil)
92                                       ,suffix)
93                  (block ,block-name
94                    (flet ((,pp-pop-name ()
95                             ,@(when object
96                                 `((unless (listp ,object-var)
97                                     (write-string ". " ,stream-var)
98                                     (output-object ,object-var ,stream-var)
99                                     (return-from ,block-name nil))))
100                             (when (and (not *print-readably*)
101                                        (eql ,count-name *print-length*))
102                               (write-string "..." ,stream-var)
103                               (return-from ,block-name nil))
104                             ,@(when object
105                                 `((when (and ,object-var
106                                              (plusp ,count-name)
107                                              (check-for-circularity
108                                               ,object-var))
109                                     (write-string ". " ,stream-var)
110                                     (output-object ,object-var ,stream-var)
111                                     (return-from ,block-name nil))))
112                             (incf ,count-name)
113                             ,@(when object
114                                 `((pop ,object-var)))))
115                      (locally
116                          (declare (disable-package-locks 
117                                    pprint-pop pprint-exit-if-list-exhausted))
118                        (macrolet ((pprint-pop ()
119                                     '(,pp-pop-name))
120                                   (pprint-exit-if-list-exhausted ()
121                                     ,(if object
122                                          `'(when (null ,object-var)
123                                             (return-from ,block-name nil))
124                                          `'(return-from ,block-name nil))))
125                          (declare (enable-package-locks
126                                    pprint-pop pprint-exit-if-list-exhausted))
127                          ,@body))))
128                  ;; FIXME: Don't we need UNWIND-PROTECT to ensure this
129                  ;; always gets executed?
130                  (end-logical-block ,stream-var)))))
131       (when object
132         (setf body
133               `(let ((,object-var ,object))
134                  (if (listp ,object-var)
135                      ,body
136                      (output-object ,object-var ,stream-var)))))
137       `(with-pretty-stream (,stream-var ,stream-expression)
138          ,body))))
139
140 (defmacro pprint-exit-if-list-exhausted ()
141   #!+sb-doc
142   "Cause the closest enclosing use of PPRINT-LOGICAL-BLOCK to return
143    if its list argument is exhausted. Can only be used inside
144    PPRINT-LOGICAL-BLOCK, and only when the LIST argument to
145    PPRINT-LOGICAL-BLOCK is supplied."
146   (error "PPRINT-EXIT-IF-LIST-EXHAUSTED must be lexically inside ~
147           PPRINT-LOGICAL-BLOCK."))
148
149 (defmacro pprint-pop ()
150   #!+sb-doc
151   "Return the next element from LIST argument to the closest enclosing
152    use of PPRINT-LOGICAL-BLOCK, automatically handling *PRINT-LENGTH*
153    and *PRINT-CIRCLE*. Can only be used inside PPRINT-LOGICAL-BLOCK.
154    If the LIST argument to PPRINT-LOGICAL-BLOCK was NIL, then nothing
155    is popped, but the *PRINT-LENGTH* testing still happens."
156   (error "PPRINT-POP must be lexically inside PPRINT-LOGICAL-BLOCK."))