format's first arg is "destination"
[bpt/guile.git] / module / ice-9 / pretty-print.scm
CommitLineData
c5e05a1c 1;;;; -*- coding: utf-8; mode: scheme -*-
a482f2cc 2;;;;
c5e05a1c 3;;;; Copyright (C) 2001, 2004, 2006, 2009, 2010 Free Software Foundation, Inc.
a482f2cc 4;;;;
73be1d9e
MV
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
53befeb7 8;;;; version 3 of the License, or (at your option) any later version.
a482f2cc 9;;;;
73be1d9e 10;;;; This library is distributed in the hope that it will be useful,
a482f2cc 11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;;;; Lesser General Public License for more details.
a482f2cc 14;;;;
73be1d9e
MV
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
92205699 17;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a482f2cc 18;;;;
1a179b03 19(define-module (ice-9 pretty-print)
5bae880e
LC
20 #:use-module (ice-9 match)
21 #:use-module (srfi srfi-1)
8c6eea2f
AW
22 #:export (pretty-print
23 truncated-print))
c1ff4aa7 24
b337528f
MV
25
26;; From SLIB.
27
28;;"genwrite.scm" generic write used by pretty-print and truncated-print.
29;; Copyright (c) 1991, Marc Feeley
30;; Author: Marc Feeley (feeley@iro.umontreal.ca)
31;; Distribution restrictions: none
32
33(define genwrite:newline-str (make-string 1 #\newline))
34
f5259dd3 35(define (generic-write obj display? width per-line-prefix output)
b337528f
MV
36
37 (define (read-macro? l)
38 (define (length1? l) (and (pair? l) (null? (cdr l))))
39 (let ((head (car l)) (tail (cdr l)))
40 (case head
41 ((quote quasiquote unquote unquote-splicing) (length1? tail))
42 (else #f))))
43
44 (define (read-macro-body l)
45 (cadr l))
46
47 (define (read-macro-prefix l)
5bc8bc69 48 (let ((head (car l)))
b337528f
MV
49 (case head
50 ((quote) "'")
51 ((quasiquote) "`")
52 ((unquote) ",")
53 ((unquote-splicing) ",@"))))
54
55 (define (out str col)
56 (and col (output str) (+ col (string-length str))))
57
58 (define (wr obj col)
5bae880e
LC
59 (let loop ((obj obj)
60 (col col))
61 (match obj
62 (((or 'quote 'quasiquote 'unquote 'unquote-splicing) body)
63 (wr body (out (read-macro-prefix obj) col)))
64 ((head . (rest ...))
65 ;; A proper list: do our own list printing so as to catch read
66 ;; macros that appear in the middle of the list.
67 (let ((col (loop head (out "(" col))))
68 (out ")"
69 (fold (lambda (i col)
70 (loop i (out " " col)))
71 col rest))))
72 (_
73 (out (object->string obj (if display? display write)) col)))))
b337528f
MV
74
75 (define (pp obj col)
76
77 (define (spaces n col)
78 (if (> n 0)
79 (if (> n 7)
80 (spaces (- n 8) (out " " col))
81 (out (substring " " 0 n) col))
82 col))
83
84 (define (indent to col)
85 (and col
86 (if (< to col)
f5259dd3
MV
87 (and (out genwrite:newline-str col)
88 (out per-line-prefix 0)
89 (spaces to 0))
b337528f
MV
90 (spaces (- to col) col))))
91
92 (define (pr obj col extra pp-pair)
93 (if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
94 (let ((result '())
95 (left (min (+ (- (- width col) extra) 1) max-expr-width)))
f5259dd3 96 (generic-write obj display? #f ""
b337528f
MV
97 (lambda (str)
98 (set! result (cons str result))
99 (set! left (- left (string-length str)))
100 (> left 0)))
101 (if (> left 0) ; all can be printed on one line
102 (out (reverse-string-append result) col)
103 (if (pair? obj)
104 (pp-pair obj col extra)
105 (pp-list (vector->list obj) (out "#" col) extra pp-expr))))
106 (wr obj col)))
107
108 (define (pp-expr expr col extra)
109 (if (read-macro? expr)
110 (pr (read-macro-body expr)
111 (out (read-macro-prefix expr) col)
112 extra
113 pp-expr)
114 (let ((head (car expr)))
115 (if (symbol? head)
116 (let ((proc (style head)))
117 (if proc
118 (proc expr col extra)
119 (if (> (string-length (symbol->string head))
120 max-call-head-width)
121 (pp-general expr col extra #f #f #f pp-expr)
122 (pp-call expr col extra pp-expr))))
123 (pp-list expr col extra pp-expr)))))
124
125 ; (head item1
126 ; item2
127 ; item3)
128 (define (pp-call expr col extra pp-item)
129 (let ((col* (wr (car expr) (out "(" col))))
130 (and col
131 (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))
132
133 ; (item1
134 ; item2
135 ; item3)
136 (define (pp-list l col extra pp-item)
137 (let ((col (out "(" col)))
138 (pp-down l col col extra pp-item)))
139
140 (define (pp-down l col1 col2 extra pp-item)
141 (let loop ((l l) (col col1))
142 (and col
143 (cond ((pair? l)
144 (let ((rest (cdr l)))
145 (let ((extra (if (null? rest) (+ extra 1) 0)))
146 (loop rest
147 (pr (car l) (indent col2 col) extra pp-item)))))
148 ((null? l)
149 (out ")" col))
150 (else
151 (out ")"
152 (pr l
153 (indent col2 (out "." (indent col2 col)))
154 (+ extra 1)
155 pp-item)))))))
156
157 (define (pp-general expr col extra named? pp-1 pp-2 pp-3)
158
159 (define (tail1 rest col1 col2 col3)
160 (if (and pp-1 (pair? rest))
161 (let* ((val1 (car rest))
162 (rest (cdr rest))
163 (extra (if (null? rest) (+ extra 1) 0)))
164 (tail2 rest col1 (pr val1 (indent col3 col2) extra pp-1) col3))
165 (tail2 rest col1 col2 col3)))
166
167 (define (tail2 rest col1 col2 col3)
168 (if (and pp-2 (pair? rest))
169 (let* ((val1 (car rest))
170 (rest (cdr rest))
171 (extra (if (null? rest) (+ extra 1) 0)))
172 (tail3 rest col1 (pr val1 (indent col3 col2) extra pp-2)))
173 (tail3 rest col1 col2)))
174
175 (define (tail3 rest col1 col2)
176 (pp-down rest col2 col1 extra pp-3))
177
178 (let* ((head (car expr))
179 (rest (cdr expr))
180 (col* (wr head (out "(" col))))
181 (if (and named? (pair? rest))
182 (let* ((name (car rest))
183 (rest (cdr rest))
184 (col** (wr name (out " " col*))))
185 (tail1 rest (+ col indent-general) col** (+ col** 1)))
186 (tail1 rest (+ col indent-general) col* (+ col* 1)))))
187
188 (define (pp-expr-list l col extra)
189 (pp-list l col extra pp-expr))
190
191 (define (pp-LAMBDA expr col extra)
192 (pp-general expr col extra #f pp-expr-list #f pp-expr))
193
194 (define (pp-IF expr col extra)
195 (pp-general expr col extra #f pp-expr #f pp-expr))
196
197 (define (pp-COND expr col extra)
198 (pp-call expr col extra pp-expr-list))
199
200 (define (pp-CASE expr col extra)
201 (pp-general expr col extra #f pp-expr #f pp-expr-list))
202
203 (define (pp-AND expr col extra)
204 (pp-call expr col extra pp-expr))
205
206 (define (pp-LET expr col extra)
207 (let* ((rest (cdr expr))
208 (named? (and (pair? rest) (symbol? (car rest)))))
209 (pp-general expr col extra named? pp-expr-list #f pp-expr)))
210
211 (define (pp-BEGIN expr col extra)
212 (pp-general expr col extra #f #f #f pp-expr))
213
214 (define (pp-DO expr col extra)
215 (pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))
216
253f2608
LC
217 (define (pp-SYNTAX-CASE expr col extra)
218 (pp-general expr col extra #t pp-expr-list #f pp-expr))
219
b337528f
MV
220 ; define formatting style (change these to suit your style)
221
222 (define indent-general 2)
223
224 (define max-call-head-width 5)
225
226 (define max-expr-width 50)
227
228 (define (style head)
229 (case head
253f2608
LC
230 ((lambda let* letrec define define-public
231 define-syntax let-syntax letrec-syntax)
232 pp-LAMBDA)
b337528f
MV
233 ((if set!) pp-IF)
234 ((cond) pp-COND)
235 ((case) pp-CASE)
236 ((and or) pp-AND)
237 ((let) pp-LET)
238 ((begin) pp-BEGIN)
239 ((do) pp-DO)
253f2608
LC
240 ((syntax-rules) pp-LAMBDA)
241 ((syntax-case) pp-SYNTAX-CASE)
b337528f
MV
242 (else #f)))
243
244 (pr obj col 0 pp-expr))
245
f5259dd3 246 (out per-line-prefix 0)
b337528f
MV
247 (if width
248 (out genwrite:newline-str (pp obj 0))
21a10205
MV
249 (wr obj 0))
250 ;; Return `unspecified'
251 (if #f #f))
b337528f
MV
252
253; (reverse-string-append l) = (apply string-append (reverse l))
254
255(define (reverse-string-append l)
256
257 (define (rev-string-append l i)
258 (if (pair? l)
259 (let* ((str (car l))
260 (len (string-length str))
261 (result (rev-string-append (cdr l) (+ i len))))
262 (let loop ((j 0) (k (- (- (string-length result) i) len)))
263 (if (< j len)
264 (begin
265 (string-set! result k (string-ref str j))
266 (loop (+ j 1) (+ k 1)))
267 result)))
268 (make-string i)))
269
270 (rev-string-append l 0))
271
8c6eea2f 272(define* (pretty-print obj #:optional port*
c1ff4aa7 273 #:key
8c6eea2f 274 (port (or port* (current-output-port)))
c1ff4aa7
AW
275 (width 79)
276 (display? #f)
277 (per-line-prefix ""))
f5259dd3
MV
278 "Pretty-print OBJ on PORT, which is a keyword argument defaulting to
279the current output port. Formatting can be controlled by a number of
280keyword arguments: Each line in the output is preceded by the string
281PER-LINE-PREFIX, which is empty by default. The output lines will be
282at most WIDTH characters wide; the default is 79. If DISPLAY? is
283true, display rather than write representation will be used.
284
285Instead of with a keyword argument, you can also specify the output
286port directly after OBJ, like (pretty-print OBJ PORT)."
f5259dd3
MV
287 (generic-write obj display?
288 (- width (string-length per-line-prefix))
289 per-line-prefix
8c6eea2f
AW
290 (lambda (s) (display s port) #t)))
291
c5e05a1c 292\f
8c6eea2f
AW
293;; `truncated-print' was written in 2009 by Andy Wingo, and is not from
294;; genwrite.scm.
295(define* (truncated-print x #:optional port*
296 #:key
297 (port (or port* (current-output-port)))
298 (width 79)
299 (display? #f)
300 (breadth-first? #f))
301 "Print @var{obj}, truncating the output, if necessary, to make it fit
302into @var{width} characters. By default, @var{x} will be printed using
303@code{write}, though that behavior can be overriden via the
304@var{display?} keyword argument.
305
306The default behaviour is to print depth-first, meaning that the entire
c5e05a1c 307remaining width will be available to each sub-expression of @var{x} --
8c6eea2f
AW
308e.g., if @var{x} is a vector, each member of @var{x}. One can attempt to
309\"ration\" the available width, trying to allocate it equally to each
310sub-expression, via the @var{breadth-first?} keyword argument."
311
c5e05a1c
LC
312 ;; Make sure string ports are created with the right encoding.
313 (with-fluids ((%default-port-encoding (port-encoding port)))
314
315 (define ellipsis
316 ;; Choose between `HORIZONTAL ELLIPSIS' (U+2026) and three dots, depending
317 ;; on the encoding of PORT.
318 (let ((e "…"))
319 (catch 'encoding-error
320 (lambda ()
321 (with-output-to-string
322 (lambda ()
323 (display e))))
324 (lambda (key . args)
325 "..."))))
326
327 (let ((ellipsis-width (string-length ellipsis)))
328
329 (define (print-sequence x width len ref next)
330 (let lp ((x x)
331 (width width)
332 (i 0))
333 (if (> i 0)
334 (display #\space))
335 (cond
336 ((= i len)) ; catches 0-length case
bf61ca33 337 ((and (= i (1- len)) (or (zero? i) (> width 1)))
c5e05a1c
LC
338 (print (ref x i) (if (zero? i) width (1- width))))
339 ((<= width (+ 1 ellipsis-width))
8a6b6938 340 (display ellipsis))
c5e05a1c
LC
341 (else
342 (let ((str
343 (with-fluids ((%default-port-encoding (port-encoding port)))
344 (with-output-to-string
345 (lambda ()
346 (print (ref x i)
347 (if breadth-first?
348 (max 1
349 (1- (floor (/ width (- len i)))))
350 (- width (+ 1 ellipsis-width)))))))))
351 (display str)
352 (lp (next x) (- width 1 (string-length str)) (1+ i)))))))
353
354 (define (print-tree x width)
355 ;; width is >= the width of # . #, which is 5
356 (let lp ((x x)
357 (width width))
358 (cond
359 ((or (not (pair? x)) (<= width 4))
360 (display ". ")
361 (print x (- width 2)))
362 (else
363 ;; width >= 5
364 (let ((str (with-output-to-string
365 (lambda ()
366 (print (car x)
367 (if breadth-first?
368 (floor (/ (- width 3) 2))
369 (- width 4)))))))
370 (display str)
371 (display " ")
372 (lp (cdr x) (- width 1 (string-length str))))))))
373
374 (define (truncate-string str width)
375 ;; width is < (string-length str)
376 (let lp ((fixes '(("#<" . ">")
377 ("#(" . ")")
378 ("(" . ")")
379 ("\"" . "\""))))
380 (cond
381 ((null? fixes)
382 "#")
383 ((and (string-prefix? (caar fixes) str)
384 (string-suffix? (cdar fixes) str)
385 (>= (string-length str)
386 width
387 (+ (string-length (caar fixes))
388 (string-length (cdar fixes))
389 ellipsis-width)))
390 (format #f "~a~a~a~a"
391 (caar fixes)
392 (substring str (string-length (caar fixes))
393 (- width (string-length (cdar fixes))
394 ellipsis-width))
395 ellipsis
396 (cdar fixes)))
397 (else
398 (lp (cdr fixes))))))
399
400 (define (print x width)
401 (cond
402 ((<= width 0)
403 (error "expected a positive width" width))
404 ((list? x)
405 (cond
406 ((>= width (+ 2 ellipsis-width))
407 (display "(")
408 (print-sequence x (- width 2) (length x)
409 (lambda (x i) (car x)) cdr)
410 (display ")"))
411 (else
412 (display "#"))))
413 ((vector? x)
414 (cond
415 ((>= width (+ 3 ellipsis-width))
416 (display "#(")
417 (print-sequence x (- width 3) (vector-length x)
418 vector-ref identity)
419 (display ")"))
420 (else
421 (display "#"))))
422 ((uniform-vector? x)
423 (cond
424 ((>= width 9)
425 (format #t "#~a(" (uniform-vector-element-type x))
426 (print-sequence x (- width 6) (uniform-vector-length x)
427 uniform-vector-ref identity)
428 (display ")"))
429 (else
430 (display "#"))))
431 ((pair? x)
432 (cond
433 ((>= width (+ 4 ellipsis-width))
434 (display "(")
435 (print-tree x (- width 2))
436 (display ")"))
437 (else
438 (display "#"))))
439 (else
440 (let* ((str (with-output-to-string
441 (lambda () (if display? (display x) (write x)))))
442 (len (string-length str)))
443 (display (if (<= (string-length str) width)
444 str
445 (truncate-string str width)))))))
446
447 (with-output-to-port port
448 (lambda ()
449 (print x width))))))