3 ;;;; Copyright (C) 2001, 2004, 2006 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 2.1 of the License, or (at your option) any later version.
10 ;;;; This library 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 GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (ice-9 pretty-print)
20 :use-module (ice-9 optargs)
21 :export (pretty-print))
25 ;;"genwrite.scm" generic write used by pretty-print and truncated-print.
26 ;; Copyright (c) 1991, Marc Feeley
27 ;; Author: Marc Feeley (feeley@iro.umontreal.ca)
28 ;; Distribution restrictions: none
30 (define genwrite:newline-str (make-string 1 #\newline))
32 (define (generic-write obj display? width per-line-prefix output)
34 (define (read-macro? l)
35 (define (length1? l) (and (pair? l) (null? (cdr l))))
36 (let ((head (car l)) (tail (cdr l)))
38 ((quote quasiquote unquote unquote-splicing) (length1? tail))
41 (define (read-macro-body l)
44 (define (read-macro-prefix l)
50 ((unquote-splicing) ",@"))))
53 (and col (output str) (+ col (string-length str))))
56 (cond ((and (pair? obj)
58 (wr (read-macro-body obj)
59 (out (read-macro-prefix obj) col)))
61 (out (object->string obj (if display? display write)) col))))
65 (define (spaces n col)
68 (spaces (- n 8) (out " " col))
69 (out (substring " " 0 n) col))
72 (define (indent to col)
75 (and (out genwrite:newline-str col)
76 (out per-line-prefix 0)
78 (spaces (- to col) col))))
80 (define (pr obj col extra pp-pair)
81 (if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
83 (left (min (+ (- (- width col) extra) 1) max-expr-width)))
84 (generic-write obj display? #f ""
86 (set! result (cons str result))
87 (set! left (- left (string-length str)))
89 (if (> left 0) ; all can be printed on one line
90 (out (reverse-string-append result) col)
92 (pp-pair obj col extra)
93 (pp-list (vector->list obj) (out "#" col) extra pp-expr))))
96 (define (pp-expr expr col extra)
97 (if (read-macro? expr)
98 (pr (read-macro-body expr)
99 (out (read-macro-prefix expr) col)
102 (let ((head (car expr)))
104 (let ((proc (style head)))
106 (proc expr col extra)
107 (if (> (string-length (symbol->string head))
109 (pp-general expr col extra #f #f #f pp-expr)
110 (pp-call expr col extra pp-expr))))
111 (pp-list expr col extra pp-expr)))))
116 (define (pp-call expr col extra pp-item)
117 (let ((col* (wr (car expr) (out "(" col))))
119 (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))
124 (define (pp-list l col extra pp-item)
125 (let ((col (out "(" col)))
126 (pp-down l col col extra pp-item)))
128 (define (pp-down l col1 col2 extra pp-item)
129 (let loop ((l l) (col col1))
132 (let ((rest (cdr l)))
133 (let ((extra (if (null? rest) (+ extra 1) 0)))
135 (pr (car l) (indent col2 col) extra pp-item)))))
141 (indent col2 (out "." (indent col2 col)))
145 (define (pp-general expr col extra named? pp-1 pp-2 pp-3)
147 (define (tail1 rest col1 col2 col3)
148 (if (and pp-1 (pair? rest))
149 (let* ((val1 (car rest))
151 (extra (if (null? rest) (+ extra 1) 0)))
152 (tail2 rest col1 (pr val1 (indent col3 col2) extra pp-1) col3))
153 (tail2 rest col1 col2 col3)))
155 (define (tail2 rest col1 col2 col3)
156 (if (and pp-2 (pair? rest))
157 (let* ((val1 (car rest))
159 (extra (if (null? rest) (+ extra 1) 0)))
160 (tail3 rest col1 (pr val1 (indent col3 col2) extra pp-2)))
161 (tail3 rest col1 col2)))
163 (define (tail3 rest col1 col2)
164 (pp-down rest col2 col1 extra pp-3))
166 (let* ((head (car expr))
168 (col* (wr head (out "(" col))))
169 (if (and named? (pair? rest))
170 (let* ((name (car rest))
172 (col** (wr name (out " " col*))))
173 (tail1 rest (+ col indent-general) col** (+ col** 1)))
174 (tail1 rest (+ col indent-general) col* (+ col* 1)))))
176 (define (pp-expr-list l col extra)
177 (pp-list l col extra pp-expr))
179 (define (pp-LAMBDA expr col extra)
180 (pp-general expr col extra #f pp-expr-list #f pp-expr))
182 (define (pp-IF expr col extra)
183 (pp-general expr col extra #f pp-expr #f pp-expr))
185 (define (pp-COND expr col extra)
186 (pp-call expr col extra pp-expr-list))
188 (define (pp-CASE expr col extra)
189 (pp-general expr col extra #f pp-expr #f pp-expr-list))
191 (define (pp-AND expr col extra)
192 (pp-call expr col extra pp-expr))
194 (define (pp-LET expr col extra)
195 (let* ((rest (cdr expr))
196 (named? (and (pair? rest) (symbol? (car rest)))))
197 (pp-general expr col extra named? pp-expr-list #f pp-expr)))
199 (define (pp-BEGIN expr col extra)
200 (pp-general expr col extra #f #f #f pp-expr))
202 (define (pp-DO expr col extra)
203 (pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))
205 ; define formatting style (change these to suit your style)
207 (define indent-general 2)
209 (define max-call-head-width 5)
211 (define max-expr-width 50)
215 ((lambda let* letrec define) pp-LAMBDA)
225 (pr obj col 0 pp-expr))
227 (out per-line-prefix 0)
229 (out genwrite:newline-str (pp obj 0))
231 ;; Return `unspecified'
234 ; (reverse-string-append l) = (apply string-append (reverse l))
236 (define (reverse-string-append l)
238 (define (rev-string-append l i)
241 (len (string-length str))
242 (result (rev-string-append (cdr l) (+ i len))))
243 (let loop ((j 0) (k (- (- (string-length result) i) len)))
246 (string-set! result k (string-ref str j))
247 (loop (+ j 1) (+ k 1)))
251 (rev-string-append l 0))
253 (define (pretty-print obj . opts)
254 "Pretty-print OBJ on PORT, which is a keyword argument defaulting to
255 the current output port. Formatting can be controlled by a number of
256 keyword arguments: Each line in the output is preceded by the string
257 PER-LINE-PREFIX, which is empty by default. The output lines will be
258 at most WIDTH characters wide; the default is 79. If DISPLAY? is
259 true, display rather than write representation will be used.
261 Instead of with a keyword argument, you can also specify the output
262 port directly after OBJ, like (pretty-print OBJ PORT)."
264 (if (keyword? (car opts))
265 (apply pretty-print-with-keys obj opts)
266 (apply pretty-print-with-keys obj #:port (car opts) (cdr opts)))
267 (pretty-print-with-keys obj)))
269 (define* (pretty-print-with-keys obj
271 (port (current-output-port))
274 (per-line-prefix ""))
275 (generic-write obj display?
276 (- width (string-length per-line-prefix))
278 (lambda (s) (display s port) #t)))