pretty-print.scm 17 KB

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