pretty-print.scm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. ;;;; -*-scheme-*-
  2. ;;;;
  3. ;;;; Copyright (C) 2001, 2004, 2006 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 2.1 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 optargs)
  21. :export (pretty-print))
  22. ;; From SLIB.
  23. ;;"genwrite.scm" generic write used by pretty-print and truncated-print.
  24. ;; Copyright (c) 1991, Marc Feeley
  25. ;; Author: Marc Feeley (feeley@iro.umontreal.ca)
  26. ;; Distribution restrictions: none
  27. (define genwrite:newline-str (make-string 1 #\newline))
  28. (define (generic-write obj display? width per-line-prefix output)
  29. (define (read-macro? l)
  30. (define (length1? l) (and (pair? l) (null? (cdr l))))
  31. (let ((head (car l)) (tail (cdr l)))
  32. (case head
  33. ((quote quasiquote unquote unquote-splicing) (length1? tail))
  34. (else #f))))
  35. (define (read-macro-body l)
  36. (cadr l))
  37. (define (read-macro-prefix l)
  38. (let ((head (car l)))
  39. (case head
  40. ((quote) "'")
  41. ((quasiquote) "`")
  42. ((unquote) ",")
  43. ((unquote-splicing) ",@"))))
  44. (define (out str col)
  45. (and col (output str) (+ col (string-length str))))
  46. (define (wr obj col)
  47. (cond ((and (pair? obj)
  48. (read-macro? obj))
  49. (wr (read-macro-body obj)
  50. (out (read-macro-prefix obj) col)))
  51. (else
  52. (out (object->string obj (if display? display write)) col))))
  53. (define (pp obj col)
  54. (define (spaces n col)
  55. (if (> n 0)
  56. (if (> n 7)
  57. (spaces (- n 8) (out " " col))
  58. (out (substring " " 0 n) col))
  59. col))
  60. (define (indent to col)
  61. (and col
  62. (if (< to col)
  63. (and (out genwrite:newline-str col)
  64. (out per-line-prefix 0)
  65. (spaces to 0))
  66. (spaces (- to col) col))))
  67. (define (pr obj col extra pp-pair)
  68. (if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
  69. (let ((result '())
  70. (left (min (+ (- (- width col) extra) 1) max-expr-width)))
  71. (generic-write obj display? #f ""
  72. (lambda (str)
  73. (set! result (cons str result))
  74. (set! left (- left (string-length str)))
  75. (> left 0)))
  76. (if (> left 0) ; all can be printed on one line
  77. (out (reverse-string-append result) col)
  78. (if (pair? obj)
  79. (pp-pair obj col extra)
  80. (pp-list (vector->list obj) (out "#" col) extra pp-expr))))
  81. (wr obj col)))
  82. (define (pp-expr expr col extra)
  83. (if (read-macro? expr)
  84. (pr (read-macro-body expr)
  85. (out (read-macro-prefix expr) col)
  86. extra
  87. pp-expr)
  88. (let ((head (car expr)))
  89. (if (symbol? head)
  90. (let ((proc (style head)))
  91. (if proc
  92. (proc expr col extra)
  93. (if (> (string-length (symbol->string head))
  94. max-call-head-width)
  95. (pp-general expr col extra #f #f #f pp-expr)
  96. (pp-call expr col extra pp-expr))))
  97. (pp-list expr col extra pp-expr)))))
  98. ; (head item1
  99. ; item2
  100. ; item3)
  101. (define (pp-call expr col extra pp-item)
  102. (let ((col* (wr (car expr) (out "(" col))))
  103. (and col
  104. (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))
  105. ; (item1
  106. ; item2
  107. ; item3)
  108. (define (pp-list l col extra pp-item)
  109. (let ((col (out "(" col)))
  110. (pp-down l col col extra pp-item)))
  111. (define (pp-down l col1 col2 extra pp-item)
  112. (let loop ((l l) (col col1))
  113. (and col
  114. (cond ((pair? l)
  115. (let ((rest (cdr l)))
  116. (let ((extra (if (null? rest) (+ extra 1) 0)))
  117. (loop rest
  118. (pr (car l) (indent col2 col) extra pp-item)))))
  119. ((null? l)
  120. (out ")" col))
  121. (else
  122. (out ")"
  123. (pr l
  124. (indent col2 (out "." (indent col2 col)))
  125. (+ extra 1)
  126. pp-item)))))))
  127. (define (pp-general expr col extra named? pp-1 pp-2 pp-3)
  128. (define (tail1 rest col1 col2 col3)
  129. (if (and pp-1 (pair? rest))
  130. (let* ((val1 (car rest))
  131. (rest (cdr rest))
  132. (extra (if (null? rest) (+ extra 1) 0)))
  133. (tail2 rest col1 (pr val1 (indent col3 col2) extra pp-1) col3))
  134. (tail2 rest col1 col2 col3)))
  135. (define (tail2 rest col1 col2 col3)
  136. (if (and pp-2 (pair? rest))
  137. (let* ((val1 (car rest))
  138. (rest (cdr rest))
  139. (extra (if (null? rest) (+ extra 1) 0)))
  140. (tail3 rest col1 (pr val1 (indent col3 col2) extra pp-2)))
  141. (tail3 rest col1 col2)))
  142. (define (tail3 rest col1 col2)
  143. (pp-down rest col2 col1 extra pp-3))
  144. (let* ((head (car expr))
  145. (rest (cdr expr))
  146. (col* (wr head (out "(" col))))
  147. (if (and named? (pair? rest))
  148. (let* ((name (car rest))
  149. (rest (cdr rest))
  150. (col** (wr name (out " " col*))))
  151. (tail1 rest (+ col indent-general) col** (+ col** 1)))
  152. (tail1 rest (+ col indent-general) col* (+ col* 1)))))
  153. (define (pp-expr-list l col extra)
  154. (pp-list l col extra pp-expr))
  155. (define (pp-LAMBDA expr col extra)
  156. (pp-general expr col extra #f pp-expr-list #f pp-expr))
  157. (define (pp-IF expr col extra)
  158. (pp-general expr col extra #f pp-expr #f pp-expr))
  159. (define (pp-COND expr col extra)
  160. (pp-call expr col extra pp-expr-list))
  161. (define (pp-CASE expr col extra)
  162. (pp-general expr col extra #f pp-expr #f pp-expr-list))
  163. (define (pp-AND expr col extra)
  164. (pp-call expr col extra pp-expr))
  165. (define (pp-LET expr col extra)
  166. (let* ((rest (cdr expr))
  167. (named? (and (pair? rest) (symbol? (car rest)))))
  168. (pp-general expr col extra named? pp-expr-list #f pp-expr)))
  169. (define (pp-BEGIN expr col extra)
  170. (pp-general expr col extra #f #f #f pp-expr))
  171. (define (pp-DO expr col extra)
  172. (pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))
  173. ; define formatting style (change these to suit your style)
  174. (define indent-general 2)
  175. (define max-call-head-width 5)
  176. (define max-expr-width 50)
  177. (define (style head)
  178. (case head
  179. ((lambda let* letrec define) pp-LAMBDA)
  180. ((if set!) pp-IF)
  181. ((cond) pp-COND)
  182. ((case) pp-CASE)
  183. ((and or) pp-AND)
  184. ((let) pp-LET)
  185. ((begin) pp-BEGIN)
  186. ((do) pp-DO)
  187. (else #f)))
  188. (pr obj col 0 pp-expr))
  189. (out per-line-prefix 0)
  190. (if width
  191. (out genwrite:newline-str (pp obj 0))
  192. (wr obj 0))
  193. ;; Return `unspecified'
  194. (if #f #f))
  195. ; (reverse-string-append l) = (apply string-append (reverse l))
  196. (define (reverse-string-append l)
  197. (define (rev-string-append l i)
  198. (if (pair? l)
  199. (let* ((str (car l))
  200. (len (string-length str))
  201. (result (rev-string-append (cdr l) (+ i len))))
  202. (let loop ((j 0) (k (- (- (string-length result) i) len)))
  203. (if (< j len)
  204. (begin
  205. (string-set! result k (string-ref str j))
  206. (loop (+ j 1) (+ k 1)))
  207. result)))
  208. (make-string i)))
  209. (rev-string-append l 0))
  210. (define (pretty-print obj . opts)
  211. "Pretty-print OBJ on PORT, which is a keyword argument defaulting to
  212. the current output port. Formatting can be controlled by a number of
  213. keyword arguments: Each line in the output is preceded by the string
  214. PER-LINE-PREFIX, which is empty by default. The output lines will be
  215. at most WIDTH characters wide; the default is 79. If DISPLAY? is
  216. true, display rather than write representation will be used.
  217. Instead of with a keyword argument, you can also specify the output
  218. port directly after OBJ, like (pretty-print OBJ PORT)."
  219. (if (pair? opts)
  220. (if (keyword? (car opts))
  221. (apply pretty-print-with-keys obj opts)
  222. (apply pretty-print-with-keys obj #:port (car opts) (cdr opts)))
  223. (pretty-print-with-keys obj)))
  224. (define* (pretty-print-with-keys obj
  225. #:key
  226. (port (current-output-port))
  227. (width 79)
  228. (display? #f)
  229. (per-line-prefix ""))
  230. (generic-write obj display?
  231. (- width (string-length per-line-prefix))
  232. per-line-prefix
  233. (lambda (s) (display s port) #t)))