pretty-print.scm 15 KB

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