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