3 ;;;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
20 ;;;; As a special exception, the Free Software Foundation gives permission
21 ;;;; for additional uses of the text contained in its release of GUILE.
23 ;;;; The exception is that, if you link the GUILE library with other files
24 ;;;; to produce an executable, this does not by itself cause the
25 ;;;; resulting executable to be covered by the GNU General Public License.
26 ;;;; Your use of that executable is in no way restricted on account of
27 ;;;; linking the GUILE library code into it.
29 ;;;; This exception does not however invalidate any other reasons why
30 ;;;; the executable file might be covered by the GNU General Public License.
32 ;;;; This exception applies only to the code released by the
33 ;;;; Free Software Foundation under the name GUILE. If you copy
34 ;;;; code from other Free Software Foundation releases into a copy of
35 ;;;; GUILE, as the General Public License permits, the exception does
36 ;;;; not apply to the code that you add in this way. To avoid misleading
37 ;;;; anyone as to the status of such modified files, you must delete
38 ;;;; this exception notice from them.
40 ;;;; If you write modifications of your own for GUILE, it is your choice
41 ;;;; whether to permit this exception to apply to your modifications.
42 ;;;; If you do not wish that, delete this exception notice.
44 (define-module (ice-9 pretty-print)
45 :use-module (ice-9 optargs)
46 :export (pretty-print))
50 ;;"genwrite.scm" generic write used by pretty-print and truncated-print.
51 ;; Copyright (c) 1991, Marc Feeley
52 ;; Author: Marc Feeley (feeley@iro.umontreal.ca)
53 ;; Distribution restrictions: none
55 (define genwrite:newline-str (make-string 1 #\newline))
57 (define (generic-write obj display? width per-line-prefix output)
59 (define (read-macro? l)
60 (define (length1? l) (and (pair? l) (null? (cdr l))))
61 (let ((head (car l)) (tail (cdr l)))
63 ((quote quasiquote unquote unquote-splicing) (length1? tail))
66 (define (read-macro-body l)
69 (define (read-macro-prefix l)
70 (let ((head (car l)) (tail (cdr l)))
75 ((unquote-splicing) ",@"))))
78 (and col (output str) (+ col (string-length str))))
82 (define (wr-expr expr col)
83 (if (read-macro? expr)
84 (wr (read-macro-body expr) (out (read-macro-prefix expr) col))
87 (define (wr-lst l col)
89 (let loop ((l (cdr l))
90 (col (and col (wr (car l) (out "(" col)))))
93 (loop (cdr l) (wr (car l) (out " " col))))
94 ((null? l) (out ")" col))
95 (else (out ")" (wr l (out " . " col))))))
98 (cond ((pair? obj) (wr-expr obj col))
99 ((null? obj) (wr-lst obj col))
100 ((vector? obj) (wr-lst (vector->list obj) (out "#" col)))
101 ((boolean? obj) (out (if obj "#t" "#f") col))
102 ((number? obj) (out (number->string obj) col))
103 ((symbol? obj) (out (symbol->string obj) col))
104 ((procedure? obj) (out "#[procedure]" col))
105 ((string? obj) (if display?
107 (let loop ((i 0) (j 0) (col (out "\"" col)))
108 (if (and col (< j (string-length obj)))
109 (let ((c (string-ref obj j)))
110 (if (or (char=? c #\\)
115 (out (substring obj i j)
117 (loop i (+ j 1) col)))
119 (out (substring obj i j) col))))))
120 ((char? obj) (if display?
121 (out (make-string 1 obj) col)
124 ((#\newline) "newline")
125 (else (make-string 1 obj)))
127 (else (out (object->string obj) col))))
131 (define (spaces n col)
134 (spaces (- n 8) (out " " col))
135 (out (substring " " 0 n) col))
138 (define (indent to col)
141 (and (out genwrite:newline-str col)
142 (out per-line-prefix 0)
144 (spaces (- to col) col))))
146 (define (pr obj col extra pp-pair)
147 (if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
149 (left (min (+ (- (- width col) extra) 1) max-expr-width)))
150 (generic-write obj display? #f ""
152 (set! result (cons str result))
153 (set! left (- left (string-length str)))
155 (if (> left 0) ; all can be printed on one line
156 (out (reverse-string-append result) col)
158 (pp-pair obj col extra)
159 (pp-list (vector->list obj) (out "#" col) extra pp-expr))))
162 (define (pp-expr expr col extra)
163 (if (read-macro? expr)
164 (pr (read-macro-body expr)
165 (out (read-macro-prefix expr) col)
168 (let ((head (car expr)))
170 (let ((proc (style head)))
172 (proc expr col extra)
173 (if (> (string-length (symbol->string head))
175 (pp-general expr col extra #f #f #f pp-expr)
176 (pp-call expr col extra pp-expr))))
177 (pp-list expr col extra pp-expr)))))
182 (define (pp-call expr col extra pp-item)
183 (let ((col* (wr (car expr) (out "(" col))))
185 (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))
190 (define (pp-list l col extra pp-item)
191 (let ((col (out "(" col)))
192 (pp-down l col col extra pp-item)))
194 (define (pp-down l col1 col2 extra pp-item)
195 (let loop ((l l) (col col1))
198 (let ((rest (cdr l)))
199 (let ((extra (if (null? rest) (+ extra 1) 0)))
201 (pr (car l) (indent col2 col) extra pp-item)))))
207 (indent col2 (out "." (indent col2 col)))
211 (define (pp-general expr col extra named? pp-1 pp-2 pp-3)
213 (define (tail1 rest col1 col2 col3)
214 (if (and pp-1 (pair? rest))
215 (let* ((val1 (car rest))
217 (extra (if (null? rest) (+ extra 1) 0)))
218 (tail2 rest col1 (pr val1 (indent col3 col2) extra pp-1) col3))
219 (tail2 rest col1 col2 col3)))
221 (define (tail2 rest col1 col2 col3)
222 (if (and pp-2 (pair? rest))
223 (let* ((val1 (car rest))
225 (extra (if (null? rest) (+ extra 1) 0)))
226 (tail3 rest col1 (pr val1 (indent col3 col2) extra pp-2)))
227 (tail3 rest col1 col2)))
229 (define (tail3 rest col1 col2)
230 (pp-down rest col2 col1 extra pp-3))
232 (let* ((head (car expr))
234 (col* (wr head (out "(" col))))
235 (if (and named? (pair? rest))
236 (let* ((name (car rest))
238 (col** (wr name (out " " col*))))
239 (tail1 rest (+ col indent-general) col** (+ col** 1)))
240 (tail1 rest (+ col indent-general) col* (+ col* 1)))))
242 (define (pp-expr-list l col extra)
243 (pp-list l col extra pp-expr))
245 (define (pp-LAMBDA expr col extra)
246 (pp-general expr col extra #f pp-expr-list #f pp-expr))
248 (define (pp-IF expr col extra)
249 (pp-general expr col extra #f pp-expr #f pp-expr))
251 (define (pp-COND expr col extra)
252 (pp-call expr col extra pp-expr-list))
254 (define (pp-CASE expr col extra)
255 (pp-general expr col extra #f pp-expr #f pp-expr-list))
257 (define (pp-AND expr col extra)
258 (pp-call expr col extra pp-expr))
260 (define (pp-LET expr col extra)
261 (let* ((rest (cdr expr))
262 (named? (and (pair? rest) (symbol? (car rest)))))
263 (pp-general expr col extra named? pp-expr-list #f pp-expr)))
265 (define (pp-BEGIN expr col extra)
266 (pp-general expr col extra #f #f #f pp-expr))
268 (define (pp-DO expr col extra)
269 (pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))
271 ; define formatting style (change these to suit your style)
273 (define indent-general 2)
275 (define max-call-head-width 5)
277 (define max-expr-width 50)
281 ((lambda let* letrec define) pp-LAMBDA)
291 (pr obj col 0 pp-expr))
293 (out per-line-prefix 0)
295 (out genwrite:newline-str (pp obj 0))
297 ;; Return `unspecified'
300 ; (reverse-string-append l) = (apply string-append (reverse l))
302 (define (reverse-string-append l)
304 (define (rev-string-append l i)
307 (len (string-length str))
308 (result (rev-string-append (cdr l) (+ i len))))
309 (let loop ((j 0) (k (- (- (string-length result) i) len)))
312 (string-set! result k (string-ref str j))
313 (loop (+ j 1) (+ k 1)))
317 (rev-string-append l 0))
319 (define (pretty-print obj . opts)
320 "Pretty-print OBJ on PORT, which is a keyword argument defaulting to
321 the current output port. Formatting can be controlled by a number of
322 keyword arguments: Each line in the output is preceded by the string
323 PER-LINE-PREFIX, which is empty by default. The output lines will be
324 at most WIDTH characters wide; the default is 79. If DISPLAY? is
325 true, display rather than write representation will be used.
327 Instead of with a keyword argument, you can also specify the output
328 port directly after OBJ, like (pretty-print OBJ PORT)."
330 (if (keyword? (car opts))
331 (apply pretty-print-with-keys obj opts)
332 (apply pretty-print-with-keys obj #:port (car opts) (cdr opts)))
333 (pretty-print-with-keys obj)))
335 (define* (pretty-print-with-keys obj
337 (port (current-output-port))
340 (per-line-prefix ""))
341 (generic-write obj display?
342 (- width (string-length per-line-prefix))
344 (lambda (s) (display s port) #t)))