pretty-print.scm 16 KB

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