1 ;;;; -*- coding: utf-8; mode: scheme -*-
3 ;;;; Copyright (C) 2001, 2004, 2006, 2009, 2010,
4 ;;;; 2012, 2013 Free Software Foundation, Inc.
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
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
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
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (define-module (ice-9 pretty-print)
21 #:use-module (ice-9 match)
22 #:use-module (srfi srfi-1)
23 #:export (pretty-print
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
34 (define genwrite:newline-str (make-string 1 #\newline))
36 (define (generic-write
37 obj display? width max-expr-width per-line-prefix output)
39 (define (read-macro? l)
40 (define (length1? l) (and (pair? l) (null? (cdr l))))
41 (let ((head (car l)) (tail (cdr l)))
43 ((quote quasiquote unquote unquote-splicing) (length1? tail))
46 (define (read-macro-body l)
49 (define (read-macro-prefix l)
55 ((unquote-splicing) ",@"))))
58 (and col (output str) (+ col (string-length str))))
64 (((or 'quote 'quasiquote 'unquote 'unquote-splicing) body)
65 (wr body (out (read-macro-prefix obj) col)))
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))))
72 (loop i (out " " col)))
75 (out (object->string obj (if display? display write)) col)))))
79 (define (spaces n col)
82 (spaces (- n 8) (out " " col))
83 (out (substring " " 0 n) col))
86 (define (indent to col)
89 (and (out genwrite:newline-str col)
90 (out per-line-prefix 0)
92 (spaces (- to col) col))))
94 (define (pr obj col extra pp-pair)
95 (if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
97 (left (min (+ (- (- width col) extra) 1) max-expr-width)))
98 (generic-write obj display? #f max-expr-width ""
100 (set! result (cons str result))
101 (set! left (- left (string-length str)))
103 (if (> left 0) ; all can be printed on one line
104 (out (reverse-string-append result) col)
106 (pp-pair obj col extra)
107 (pp-list (vector->list obj) (out "#" col) extra pp-expr))))
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)
116 (let ((head (car expr)))
118 (let ((proc (style head)))
120 (proc expr col extra)
121 (if (> (string-length (symbol->string head))
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)))))
130 (define (pp-call expr col extra pp-item)
131 (let ((col* (wr (car expr) (out "(" col))))
133 (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))
138 (define (pp-list l col extra pp-item)
139 (let ((col (out "(" col)))
140 (pp-down l col col extra pp-item)))
142 (define (pp-down l col1 col2 extra pp-item)
143 (let loop ((l l) (col col1))
146 (let ((rest (cdr l)))
147 (let ((extra (if (null? rest) (+ extra 1) 0)))
149 (pr (car l) (indent col2 col) extra pp-item)))))
155 (indent col2 (out "." (indent col2 col)))
159 (define (pp-general expr col extra named? pp-1 pp-2 pp-3)
161 (define (tail1 rest col1 col2 col3)
162 (if (and pp-1 (pair? rest))
163 (let* ((val1 (car 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)))
169 (define (tail2 rest col1 col2 col3)
170 (if (and pp-2 (pair? rest))
171 (let* ((val1 (car 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)))
177 (define (tail3 rest col1 col2)
178 (pp-down rest col2 col1 extra pp-3))
180 (let* ((head (car expr))
182 (col* (wr head (out "(" col))))
183 (if (and named? (pair? rest))
184 (let* ((name (car 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)))))
190 (define (pp-expr-list l col extra)
191 (pp-list l col extra pp-expr))
193 (define (pp-LAMBDA expr col extra)
194 (pp-general expr col extra #f pp-expr-list #f pp-expr))
196 (define (pp-IF expr col extra)
197 (pp-general expr col extra #f pp-expr #f pp-expr))
199 (define (pp-COND expr col extra)
200 (pp-call expr col extra pp-expr-list))
202 (define (pp-CASE expr col extra)
203 (pp-general expr col extra #f pp-expr #f pp-expr-list))
205 (define (pp-AND expr col extra)
206 (pp-call expr col extra pp-expr))
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)))
213 (define (pp-BEGIN expr col extra)
214 (pp-general expr col extra #f #f #f pp-expr))
216 (define (pp-DO expr col extra)
217 (pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))
219 (define (pp-SYNTAX-CASE expr col extra)
220 (pp-general expr col extra #t pp-expr-list #f pp-expr))
222 ; define formatting style (change these to suit your style)
224 (define indent-general 2)
226 (define max-call-head-width 5)
230 ((lambda lambda* let* letrec define define* define-public
231 define-syntax let-syntax letrec-syntax with-syntax)
240 ((syntax-rules) pp-LAMBDA)
241 ((syntax-case) pp-SYNTAX-CASE)
244 (pr obj col 0 pp-expr))
246 (out per-line-prefix 0)
248 (out genwrite:newline-str (pp obj 0))
250 ;; Return `unspecified'
253 ; (reverse-string-append l) = (apply string-append (reverse l))
255 (define (reverse-string-append l)
257 (define (rev-string-append l i)
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)))
265 (string-set! result k (string-ref str j))
266 (loop (+ j 1) (+ k 1)))
270 (rev-string-append l 0))
272 (define* (pretty-print obj #:optional port*
274 (port (or port* (current-output-port)))
278 (per-line-prefix ""))
279 "Pretty-print OBJ on PORT, which is a keyword argument defaulting to
280 the current output port. Formatting can be controlled by a number of
281 keyword arguments: Each line in the output is preceded by the string
282 PER-LINE-PREFIX, which is empty by default. The output lines will be
283 at most WIDTH characters wide; the default is 79. If DISPLAY? is
284 true, display rather than write representation will be used.
286 Instead of with a keyword argument, you can also specify the output
287 port directly after OBJ, like (pretty-print OBJ PORT)."
288 (generic-write obj display?
289 (- width (string-length per-line-prefix))
292 (lambda (s) (display s port) #t)))
295 ;; `truncated-print' was written in 2009 by Andy Wingo, and is not from
297 (define* (truncated-print x #:optional port*
299 (port (or port* (current-output-port)))
303 "Print @var{x}, truncating the output, if necessary, to make it fit
304 into @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.
308 The default behaviour is to print depth-first, meaning that the entire
309 remaining width will be available to each sub-expression of @var{x} --
310 e.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
312 sub-expression, via the @var{breadth-first?} keyword argument."
315 ;; Choose between `HORIZONTAL ELLIPSIS' (U+2026) and three dots, depending
316 ;; on the encoding of PORT.
318 (catch 'encoding-error
320 (with-fluids ((%default-port-conversion-strategy 'error))
321 (call-with-output-string
323 (set-port-encoding! p (port-encoding port))
328 (let ((ellipsis-width (string-length ellipsis)))
330 (define (print-sequence x width len ref next)
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))
343 (let ((str (with-output-to-string
348 (1- (floor (/ width (- len i)))))
349 (- width (+ 1 ellipsis-width))))))))
351 (lp (next x) (- width 1 (string-length str)) (1+ i)))))))
353 (define (print-tree x width)
354 ;; width is >= the width of # . #, which is 5
358 ((or (not (pair? x)) (<= width 4))
360 (print x (- width 2)))
363 (let ((str (with-output-to-string
367 (floor (/ (- width 3) 2))
371 (lp (cdr x) (- width 1 (string-length str))))))))
373 (define (truncate-string str width)
374 ;; width is < (string-length str)
375 (let lp ((fixes '(("#<" . ">")
382 ((and (string-prefix? (caar fixes) str)
383 (string-suffix? (cdar fixes) str)
384 (>= (string-length str)
386 (+ (string-length (caar fixes))
387 (string-length (cdar fixes))
389 (format #f "~a~a~a~a"
391 (substring str (string-length (caar fixes))
392 (- width (string-length (cdar fixes))
399 (define (print x width)
402 (error "expected a positive width" width))
405 ((>= width (+ 2 ellipsis-width))
407 (print-sequence x (- width 2) (length x)
408 (lambda (x i) (car x)) cdr)
414 ((>= width (+ 3 ellipsis-width))
416 (print-sequence x (- width 3) (vector-length x)
424 (format #t "#~a(" (uniform-vector-element-type x))
425 (print-sequence x (- width 6) (uniform-vector-length x)
426 uniform-vector-ref identity)
432 ((>= width (+ 4 ellipsis-width))
434 (print-tree x (- width 2))
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)
444 (truncate-string str width)))))))
446 (with-output-to-port port