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