1 ;;;; -*- coding: utf-8; mode: scheme -*-
3 ;;;; Copyright (C) 2001, 2004, 2006, 2009, 2010,
4 ;;;; 2012, 2013, 2014 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 #:use-module (rnrs bytevectors)
24 #:export (pretty-print
30 ;;"genwrite.scm" generic write used by pretty-print and truncated-print.
31 ;; Copyright (c) 1991, Marc Feeley
32 ;; Author: Marc Feeley (feeley@iro.umontreal.ca)
33 ;; Distribution restrictions: none
35 (define genwrite:newline-str (make-string 1 #\newline))
37 (define (generic-write
38 obj display? width max-expr-width per-line-prefix output)
40 (define (read-macro? l)
41 (define (length1? l) (and (pair? l) (null? (cdr l))))
42 (let ((head (car l)) (tail (cdr l)))
44 ((quote quasiquote unquote unquote-splicing) (length1? tail))
47 (define (read-macro-body l)
50 (define (read-macro-prefix l)
56 ((unquote-splicing) ",@"))))
59 (and col (output str) (+ col (string-length str))))
65 (((or 'quote 'quasiquote 'unquote 'unquote-splicing) body)
66 (wr body (out (read-macro-prefix obj) col)))
68 ;; A proper list: do our own list printing so as to catch read
69 ;; macros that appear in the middle of the list.
70 (let ((col (loop head (out "(" col))))
73 (loop i (out " " col)))
76 (out (object->string obj (if display? display write)) col)))))
80 (define (spaces n col)
83 (spaces (- n 8) (out " " col))
84 (out (substring " " 0 n) col))
87 (define (indent to col)
90 (and (out genwrite:newline-str col)
91 (out per-line-prefix 0)
93 (spaces (- to col) col))))
95 (define (pr obj col extra pp-pair)
96 (if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
98 (left (min (+ (- (- width col) extra) 1) max-expr-width)))
99 (generic-write obj display? #f max-expr-width ""
101 (set! result (cons str result))
102 (set! left (- left (string-length str)))
104 (if (> left 0) ; all can be printed on one line
105 (out (reverse-string-append result) col)
107 (pp-pair obj col extra)
108 (pp-list (vector->list obj) (out "#" col) extra pp-expr))))
111 (define (pp-expr expr col extra)
112 (if (read-macro? expr)
113 (pr (read-macro-body expr)
114 (out (read-macro-prefix expr) col)
117 (let ((head (car expr)))
119 (let ((proc (style head)))
121 (proc expr col extra)
122 (if (> (string-length (symbol->string head))
124 (pp-general expr col extra #f #f #f pp-expr)
125 (pp-call expr col extra pp-expr))))
126 (pp-list expr col extra pp-expr)))))
131 (define (pp-call expr col extra pp-item)
132 (let ((col* (wr (car expr) (out "(" col))))
134 (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))
139 (define (pp-list l col extra pp-item)
140 (let ((col (out "(" col)))
141 (pp-down l col col extra pp-item)))
143 (define (pp-down l col1 col2 extra pp-item)
144 (let loop ((l l) (col col1))
147 (let ((rest (cdr l)))
148 (let ((extra (if (null? rest) (+ extra 1) 0)))
150 (pr (car l) (indent col2 col) extra pp-item)))))
156 (indent col2 (out "." (indent col2 col)))
160 (define (pp-general expr col extra named? pp-1 pp-2 pp-3)
162 (define (tail1 rest col1 col2 col3)
163 (if (and pp-1 (pair? rest))
164 (let* ((val1 (car rest))
166 (extra (if (null? rest) (+ extra 1) 0)))
167 (tail2 rest col1 (pr val1 (indent col3 col2) extra pp-1) col3))
168 (tail2 rest col1 col2 col3)))
170 (define (tail2 rest col1 col2 col3)
171 (if (and pp-2 (pair? rest))
172 (let* ((val1 (car rest))
174 (extra (if (null? rest) (+ extra 1) 0)))
175 (tail3 rest col1 (pr val1 (indent col3 col2) extra pp-2)))
176 (tail3 rest col1 col2)))
178 (define (tail3 rest col1 col2)
179 (pp-down rest col2 col1 extra pp-3))
181 (let* ((head (car expr))
183 (col* (wr head (out "(" col))))
184 (if (and named? (pair? rest))
185 (let* ((name (car rest))
187 (col** (wr name (out " " col*))))
188 (tail1 rest (+ col indent-general) col** (+ col** 1)))
189 (tail1 rest (+ col indent-general) col* (+ col* 1)))))
191 (define (pp-expr-list l col extra)
192 (pp-list l col extra pp-expr))
194 (define (pp-LAMBDA expr col extra)
195 (pp-general expr col extra #f pp-expr-list #f pp-expr))
197 (define (pp-IF expr col extra)
198 (pp-general expr col extra #f pp-expr #f pp-expr))
200 (define (pp-COND expr col extra)
201 (pp-call expr col extra pp-expr-list))
203 (define (pp-CASE expr col extra)
204 (pp-general expr col extra #f pp-expr #f pp-expr-list))
206 (define (pp-AND expr col extra)
207 (pp-call expr col extra pp-expr))
209 (define (pp-LET expr col extra)
210 (let* ((rest (cdr expr))
211 (named? (and (pair? rest) (symbol? (car rest)))))
212 (pp-general expr col extra named? pp-expr-list #f pp-expr)))
214 (define (pp-BEGIN expr col extra)
215 (pp-general expr col extra #f #f #f pp-expr))
217 (define (pp-DO expr col extra)
218 (pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))
220 (define (pp-SYNTAX-CASE expr col extra)
221 (pp-general expr col extra #t pp-expr-list #f pp-expr))
223 ; define formatting style (change these to suit your style)
225 (define indent-general 2)
227 (define max-call-head-width 5)
231 ((lambda lambda* let* letrec define define* define-public
232 define-syntax let-syntax letrec-syntax with-syntax)
241 ((syntax-rules) pp-LAMBDA)
242 ((syntax-case) pp-SYNTAX-CASE)
245 (pr obj col 0 pp-expr))
247 (out per-line-prefix 0)
249 (out genwrite:newline-str (pp obj 0))
251 ;; Return `unspecified'
254 ; (reverse-string-append l) = (apply string-append (reverse l))
256 (define (reverse-string-append l)
258 (define (rev-string-append l i)
261 (len (string-length str))
262 (result (rev-string-append (cdr l) (+ i len))))
263 (let loop ((j 0) (k (- (- (string-length result) i) len)))
266 (string-set! result k (string-ref str j))
267 (loop (+ j 1) (+ k 1)))
271 (rev-string-append l 0))
273 (define* (pretty-print obj #:optional port*
275 (port (or port* (current-output-port)))
279 (per-line-prefix ""))
280 "Pretty-print OBJ on PORT, which is a keyword argument defaulting to
281 the current output port. Formatting can be controlled by a number of
282 keyword arguments: Each line in the output is preceded by the string
283 PER-LINE-PREFIX, which is empty by default. The output lines will be
284 at most WIDTH characters wide; the default is 79. If DISPLAY? is
285 true, display rather than write representation will be used.
287 Instead of with a keyword argument, you can also specify the output
288 port directly after OBJ, like (pretty-print OBJ PORT)."
289 (generic-write obj display?
290 (- width (string-length per-line-prefix))
293 (lambda (s) (display s port) #t)))
296 ;; `truncated-print' was written in 2009 by Andy Wingo, and is not from
298 (define* (truncated-print x #:optional port*
300 (port (or port* (current-output-port)))
304 "Print @var{x}, truncating the output, if necessary, to make it fit
305 into @var{width} characters. By default, @var{x} will be printed using
306 @code{write}, though that behavior can be overriden via the
307 @var{display?} keyword argument.
309 The default behaviour is to print depth-first, meaning that the entire
310 remaining width will be available to each sub-expression of @var{x} --
311 e.g., if @var{x} is a vector, each member of @var{x}. One can attempt to
312 \"ration\" the available width, trying to allocate it equally to each
313 sub-expression, via the @var{breadth-first?} keyword argument."
316 ;; Choose between `HORIZONTAL ELLIPSIS' (U+2026) and three dots, depending
317 ;; on the encoding of PORT.
319 (catch 'encoding-error
321 (with-fluids ((%default-port-conversion-strategy 'error))
322 (call-with-output-string
324 (set-port-encoding! p (port-encoding port))
329 (let ((ellipsis-width (string-length ellipsis)))
331 (define (print-sequence x width len ref next)
338 ((= i len)) ; catches 0-length case
339 ((and (= i (1- len)) (or (zero? i) (> width 1)))
340 (print (ref x i) (if (zero? i) width (1- width))))
341 ((<= width (+ 1 ellipsis-width))
344 (let ((str (with-output-to-string
349 (1- (floor (/ width (- len i)))))
350 (- width (+ 1 ellipsis-width))))))))
352 (lp (next x) (- width 1 (string-length str)) (1+ i)))))))
354 (define (print-tree x width)
355 ;; width is >= the width of # . #, which is 5
359 ((or (not (pair? x)) (<= width 4))
361 (print x (- width 2)))
364 (let ((str (with-output-to-string
368 (floor (/ (- width 3) 2))
372 (lp (cdr x) (- width 1 (string-length str))))))))
374 (define (truncate-string str width)
375 ;; width is < (string-length str)
376 (let lp ((fixes '(("#<" . ">")
383 ((and (string-prefix? (caar fixes) str)
384 (string-suffix? (cdar fixes) str)
385 (>= (string-length str)
387 (+ (string-length (caar fixes))
388 (string-length (cdar fixes))
390 (format #f "~a~a~a~a"
392 (substring str (string-length (caar fixes))
393 (- width (string-length (cdar fixes))
400 (define (print x width)
403 (error "expected a positive width" width))
406 ((>= width (+ 2 ellipsis-width))
408 (print-sequence x (- width 2) (length x)
409 (lambda (x i) (car x)) cdr)
415 ((>= width (+ 3 ellipsis-width))
417 (print-sequence x (- width 3) (vector-length x)
425 (format #t "#~a(" (array-type x))
426 (print-sequence x (- width 6) (array-length x)
433 ((>= width (+ 4 ellipsis-width))
435 (print-tree x (- width 2))
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)
445 (truncate-string str width)))))))
447 (with-output-to-port port