1 ;;;; -*- coding: utf-8; mode: scheme -*-
3 ;;;; Copyright (C) 2001, 2004, 2006, 2009, 2010 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 3 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 #:export (pretty-print
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
31 (define genwrite:newline-str (make-string 1 #\newline))
33 (define (generic-write obj display? width per-line-prefix output)
35 (define (read-macro? l)
36 (define (length1? l) (and (pair? l) (null? (cdr l))))
37 (let ((head (car l)) (tail (cdr l)))
39 ((quote quasiquote unquote unquote-splicing) (length1? tail))
42 (define (read-macro-body l)
45 (define (read-macro-prefix l)
51 ((unquote-splicing) ",@"))))
54 (and col (output str) (+ col (string-length str))))
57 (cond ((and (pair? obj)
59 (wr (read-macro-body obj)
60 (out (read-macro-prefix obj) col)))
62 (out (object->string obj (if display? display write)) col))))
66 (define (spaces n col)
69 (spaces (- n 8) (out " " col))
70 (out (substring " " 0 n) col))
73 (define (indent to col)
76 (and (out genwrite:newline-str col)
77 (out per-line-prefix 0)
79 (spaces (- to col) col))))
81 (define (pr obj col extra pp-pair)
82 (if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
84 (left (min (+ (- (- width col) extra) 1) max-expr-width)))
85 (generic-write obj display? #f ""
87 (set! result (cons str result))
88 (set! left (- left (string-length str)))
90 (if (> left 0) ; all can be printed on one line
91 (out (reverse-string-append result) col)
93 (pp-pair obj col extra)
94 (pp-list (vector->list obj) (out "#" col) extra pp-expr))))
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)
103 (let ((head (car expr)))
105 (let ((proc (style head)))
107 (proc expr col extra)
108 (if (> (string-length (symbol->string head))
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)))))
117 (define (pp-call expr col extra pp-item)
118 (let ((col* (wr (car expr) (out "(" col))))
120 (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))
125 (define (pp-list l col extra pp-item)
126 (let ((col (out "(" col)))
127 (pp-down l col col extra pp-item)))
129 (define (pp-down l col1 col2 extra pp-item)
130 (let loop ((l l) (col col1))
133 (let ((rest (cdr l)))
134 (let ((extra (if (null? rest) (+ extra 1) 0)))
136 (pr (car l) (indent col2 col) extra pp-item)))))
142 (indent col2 (out "." (indent col2 col)))
146 (define (pp-general expr col extra named? pp-1 pp-2 pp-3)
148 (define (tail1 rest col1 col2 col3)
149 (if (and pp-1 (pair? rest))
150 (let* ((val1 (car 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)))
156 (define (tail2 rest col1 col2 col3)
157 (if (and pp-2 (pair? rest))
158 (let* ((val1 (car 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)))
164 (define (tail3 rest col1 col2)
165 (pp-down rest col2 col1 extra pp-3))
167 (let* ((head (car expr))
169 (col* (wr head (out "(" col))))
170 (if (and named? (pair? rest))
171 (let* ((name (car 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)))))
177 (define (pp-expr-list l col extra)
178 (pp-list l col extra pp-expr))
180 (define (pp-LAMBDA expr col extra)
181 (pp-general expr col extra #f pp-expr-list #f pp-expr))
183 (define (pp-IF expr col extra)
184 (pp-general expr col extra #f pp-expr #f pp-expr))
186 (define (pp-COND expr col extra)
187 (pp-call expr col extra pp-expr-list))
189 (define (pp-CASE expr col extra)
190 (pp-general expr col extra #f pp-expr #f pp-expr-list))
192 (define (pp-AND expr col extra)
193 (pp-call expr col extra pp-expr))
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)))
200 (define (pp-BEGIN expr col extra)
201 (pp-general expr col extra #f #f #f pp-expr))
203 (define (pp-DO expr col extra)
204 (pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))
206 ; define formatting style (change these to suit your style)
208 (define indent-general 2)
210 (define max-call-head-width 5)
212 (define max-expr-width 50)
216 ((lambda let* letrec define) pp-LAMBDA)
226 (pr obj col 0 pp-expr))
228 (out per-line-prefix 0)
230 (out genwrite:newline-str (pp obj 0))
232 ;; Return `unspecified'
235 ; (reverse-string-append l) = (apply string-append (reverse l))
237 (define (reverse-string-append l)
239 (define (rev-string-append l i)
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)))
247 (string-set! result k (string-ref str j))
248 (loop (+ j 1) (+ k 1)))
252 (rev-string-append l 0))
254 (define* (pretty-print obj #:optional port*
256 (port (or port* (current-output-port)))
259 (per-line-prefix ""))
260 "Pretty-print OBJ on PORT, which is a keyword argument defaulting to
261 the current output port. Formatting can be controlled by a number of
262 keyword arguments: Each line in the output is preceded by the string
263 PER-LINE-PREFIX, which is empty by default. The output lines will be
264 at most WIDTH characters wide; the default is 79. If DISPLAY? is
265 true, display rather than write representation will be used.
267 Instead of with a keyword argument, you can also specify the output
268 port directly after OBJ, like (pretty-print OBJ PORT)."
269 (generic-write obj display?
270 (- width (string-length per-line-prefix))
272 (lambda (s) (display s port) #t)))
275 ;; `truncated-print' was written in 2009 by Andy Wingo, and is not from
277 (define* (truncated-print x #:optional port*
279 (port (or port* (current-output-port)))
283 "Print @var{obj}, truncating the output, if necessary, to make it fit
284 into @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.
288 The default behaviour is to print depth-first, meaning that the entire
289 remaining width will be available to each sub-expression of @var{x} --
290 e.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
292 sub-expression, via the @var{breadth-first?} keyword argument."
294 ;; Make sure string ports are created with the right encoding.
295 (with-fluids ((%default-port-encoding (port-encoding port)))
298 ;; Choose between `HORIZONTAL ELLIPSIS' (U+2026) and three dots, depending
299 ;; on the encoding of PORT.
301 (catch 'encoding-error
303 (with-output-to-string
309 (let ((ellipsis-width (string-length ellipsis)))
311 (define (print-sequence x width len ref next)
318 ((= i len)) ; catches 0-length case
320 (print (ref x i) (if (zero? i) width (1- width))))
321 ((<= width (+ 1 ellipsis-width))
325 (with-fluids ((%default-port-encoding (port-encoding port)))
326 (with-output-to-string
331 (1- (floor (/ width (- len i)))))
332 (- width (+ 1 ellipsis-width)))))))))
334 (lp (next x) (- width 1 (string-length str)) (1+ i)))))))
336 (define (print-tree x width)
337 ;; width is >= the width of # . #, which is 5
341 ((or (not (pair? x)) (<= width 4))
343 (print x (- width 2)))
346 (let ((str (with-output-to-string
350 (floor (/ (- width 3) 2))
354 (lp (cdr x) (- width 1 (string-length str))))))))
356 (define (truncate-string str width)
357 ;; width is < (string-length str)
358 (let lp ((fixes '(("#<" . ">")
365 ((and (string-prefix? (caar fixes) str)
366 (string-suffix? (cdar fixes) str)
367 (>= (string-length str)
369 (+ (string-length (caar fixes))
370 (string-length (cdar fixes))
372 (format #f "~a~a~a~a"
374 (substring str (string-length (caar fixes))
375 (- width (string-length (cdar fixes))
382 (define (print x width)
385 (error "expected a positive width" width))
388 ((>= width (+ 2 ellipsis-width))
390 (print-sequence x (- width 2) (length x)
391 (lambda (x i) (car x)) cdr)
397 ((>= width (+ 3 ellipsis-width))
399 (print-sequence x (- width 3) (vector-length x)
407 (format #t "#~a(" (uniform-vector-element-type x))
408 (print-sequence x (- width 6) (uniform-vector-length x)
409 uniform-vector-ref identity)
415 ((>= width (+ 4 ellipsis-width))
417 (print-tree x (- width 2))
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)
427 (truncate-string str width)))))))
429 (with-output-to-port port