read-print.scm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix read-print)
  19. #:use-module (ice-9 control)
  20. #:use-module (ice-9 match)
  21. #:use-module (ice-9 rdelim)
  22. #:use-module (ice-9 vlist)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (srfi srfi-34)
  26. #:use-module (srfi srfi-35)
  27. #:use-module (guix i18n)
  28. #:use-module ((guix diagnostics)
  29. #:select (formatted-message
  30. &fix-hint &error-location
  31. location))
  32. #:export (pretty-print-with-comments
  33. pretty-print-with-comments/splice
  34. read-with-comments
  35. read-with-comments/sequence
  36. object->string*
  37. blank?
  38. vertical-space
  39. vertical-space?
  40. vertical-space-height
  41. canonicalize-vertical-space
  42. page-break
  43. page-break?
  44. comment
  45. comment?
  46. comment->string
  47. comment-margin?
  48. canonicalize-comment))
  49. ;;; Commentary:
  50. ;;;
  51. ;;; This module provides a comment-preserving reader and a comment-preserving
  52. ;;; pretty-printer smarter than (ice-9 pretty-print).
  53. ;;;
  54. ;;; Code:
  55. ;;;
  56. ;;; Comment-preserving reader.
  57. ;;;
  58. (define <blank>
  59. ;; The parent class for "blanks".
  60. (make-record-type '<blank> '()
  61. (lambda (obj port)
  62. (format port "#<blank ~a>"
  63. (number->string (object-address obj) 16)))
  64. #:extensible? #t))
  65. (define blank? (record-predicate <blank>))
  66. (define <vertical-space>
  67. (make-record-type '<vertical-space> '(height)
  68. #:parent <blank>
  69. #:extensible? #f))
  70. (define vertical-space? (record-predicate <vertical-space>))
  71. (define vertical-space (record-type-constructor <vertical-space>))
  72. (define vertical-space-height (record-accessor <vertical-space> 'height))
  73. (define canonicalize-vertical-space
  74. (let ((unit (vertical-space 1)))
  75. (lambda (space)
  76. "Return a vertical space corresponding to a single blank line."
  77. unit)))
  78. (define <page-break>
  79. (make-record-type '<page-break> '()
  80. #:parent <blank>
  81. #:extensible? #f))
  82. (define page-break? (record-predicate <page-break>))
  83. (define page-break
  84. (let ((break ((record-type-constructor <page-break>))))
  85. (lambda ()
  86. break)))
  87. (define <comment>
  88. ;; Comments.
  89. (make-record-type '<comment> '(str margin?)
  90. #:parent <blank>
  91. #:extensible? #f))
  92. (define comment? (record-predicate <comment>))
  93. (define string->comment (record-type-constructor <comment>))
  94. (define comment->string (record-accessor <comment> 'str))
  95. (define comment-margin? (record-accessor <comment> 'margin?))
  96. (define* (comment str #:optional margin?)
  97. "Return a new comment made from STR. When MARGIN? is true, return a margin
  98. comment; otherwise return a line comment. STR must start with a semicolon and
  99. end with newline, otherwise an error is raised."
  100. (when (or (string-null? str)
  101. (not (eqv? #\; (string-ref str 0)))
  102. (not (string-suffix? "\n" str)))
  103. (raise (condition
  104. (&message (message "invalid comment string")))))
  105. (string->comment str margin?))
  106. (define char-set:whitespace-sans-page-break
  107. ;; White space, excluding #\page.
  108. (char-set-difference char-set:whitespace (char-set #\page)))
  109. (define (space? chr)
  110. "Return true if CHR is white space, except for page breaks."
  111. (char-set-contains? char-set:whitespace-sans-page-break chr))
  112. (define (read-vertical-space port)
  113. "Read from PORT until a non-vertical-space character is met, and return a
  114. single <vertical-space> record."
  115. (let loop ((height 1))
  116. (match (read-char port)
  117. (#\newline (loop (+ 1 height)))
  118. ((? eof-object?) (vertical-space height))
  119. ((? space?) (loop height))
  120. (chr (unread-char chr port) (vertical-space height)))))
  121. (define (read-until-end-of-line port)
  122. "Read white space from PORT until the end of line, included."
  123. (let loop ()
  124. (match (read-char port)
  125. (#\newline #t)
  126. ((? eof-object?) #t)
  127. ((? space?) (loop))
  128. (chr (unread-char chr port)))))
  129. (define* (read-with-comments port #:key (blank-line? #t))
  130. "Like 'read', but include <blank> objects when they're encountered. When
  131. BLANK-LINE? is true, assume PORT is at the beginning of a new line."
  132. ;; Note: Instead of implementing this functionality in 'read' proper, which
  133. ;; is the best approach long-term, this code is a layer on top of 'read',
  134. ;; such that we don't have to rely on a specific Guile version.
  135. (define dot (list 'dot))
  136. (define (dot? x) (eq? x dot))
  137. (define (missing-closing-paren-error)
  138. (raise (make-compound-condition
  139. (formatted-message (G_ "unexpected end of file"))
  140. (condition
  141. (&error-location
  142. (location (match (port-filename port)
  143. (#f #f)
  144. (file (location file
  145. (port-line port)
  146. (port-column port))))))
  147. (&fix-hint
  148. (hint (G_ "Did you forget a closing parenthesis?")))))))
  149. (define (reverse/dot lst)
  150. ;; Reverse LST and make it an improper list if it contains DOT.
  151. (let loop ((result '())
  152. (lst lst))
  153. (match lst
  154. (() result)
  155. (((? dot?) . rest)
  156. (let ((dotted (reverse rest)))
  157. (set-cdr! (last-pair dotted) (car result))
  158. dotted))
  159. ((x . rest) (loop (cons x result) rest)))))
  160. (let loop ((blank-line? blank-line?)
  161. (return (const 'unbalanced)))
  162. (match (read-char port)
  163. ((? eof-object? eof)
  164. eof) ;oops!
  165. (chr
  166. (cond ((eqv? chr #\newline)
  167. (if blank-line?
  168. (read-vertical-space port)
  169. (loop #t return)))
  170. ((eqv? chr #\page)
  171. ;; Assume that a page break is on a line of its own and read
  172. ;; subsequent white space and newline.
  173. (read-until-end-of-line port)
  174. (page-break))
  175. ((char-set-contains? char-set:whitespace chr)
  176. (loop blank-line? return))
  177. ((memv chr '(#\( #\[))
  178. (let/ec return
  179. (let liip ((lst '()))
  180. (define item
  181. (loop (match lst
  182. (((? blank?) . _) #t)
  183. (_ #f))
  184. (lambda ()
  185. (return (reverse/dot lst)))))
  186. (if (eof-object? item)
  187. (missing-closing-paren-error)
  188. (liip (cons item lst))))))
  189. ((memv chr '(#\) #\]))
  190. (return))
  191. ((eq? chr #\')
  192. (list 'quote (loop #f return)))
  193. ((eq? chr #\`)
  194. (list 'quasiquote (loop #f return)))
  195. ((eq? chr #\,)
  196. (list (match (peek-char port)
  197. (#\@
  198. (read-char port)
  199. 'unquote-splicing)
  200. (_
  201. 'unquote))
  202. (loop #f return)))
  203. ((eqv? chr #\;)
  204. (unread-char chr port)
  205. (string->comment (read-line port 'concat)
  206. (not blank-line?)))
  207. (else
  208. (unread-char chr port)
  209. (match (read port)
  210. ((and token '#{.}#)
  211. (if (eq? chr #\.) dot token))
  212. (token token))))))))
  213. (define (read-with-comments/sequence port)
  214. "Read from PORT until the end-of-file is reached and return the list of
  215. expressions and blanks that were read."
  216. (let loop ((lst '())
  217. (blank-line? #t))
  218. (match (read-with-comments port #:blank-line? blank-line?)
  219. ((? eof-object?)
  220. (reverse! lst))
  221. ((? blank? blank)
  222. (loop (cons blank lst) #t))
  223. (exp
  224. (loop (cons exp lst) #f)))))
  225. ;;;
  226. ;;; Comment-preserving pretty-printer.
  227. ;;;
  228. (define-syntax vhashq
  229. (syntax-rules (quote)
  230. ((_) vlist-null)
  231. ((_ (key (quote (lst ...))) rest ...)
  232. (vhash-consq key '(lst ...) (vhashq rest ...)))
  233. ((_ (key value) rest ...)
  234. (vhash-consq key '((() . value)) (vhashq rest ...)))))
  235. (define %special-forms
  236. ;; Forms that are indented specially. The number is meant to be understood
  237. ;; like Emacs' 'scheme-indent-function' symbol property. When given an
  238. ;; alist instead of a number, the alist gives "context" in which the symbol
  239. ;; is a special form; for instance, context (modify-phases) means that the
  240. ;; symbol must appear within a (modify-phases ...) expression.
  241. (vhashq
  242. ('begin 1)
  243. ('case 2)
  244. ('cond 1)
  245. ('lambda 2)
  246. ('lambda* 2)
  247. ('match-lambda 1)
  248. ('match-lambda* 2)
  249. ('define 2)
  250. ('define* 2)
  251. ('define-public 2)
  252. ('define*-public 2)
  253. ('define-syntax 2)
  254. ('define-syntax-rule 2)
  255. ('define-module 2)
  256. ('define-gexp-compiler 2)
  257. ('let 2)
  258. ('let* 2)
  259. ('letrec 2)
  260. ('letrec* 2)
  261. ('match 2)
  262. ('when 2)
  263. ('unless 2)
  264. ('package 1)
  265. ('origin 1)
  266. ('modify-inputs 2)
  267. ('modify-phases 2)
  268. ('add-after '(((modify-phases) . 3)))
  269. ('add-before '(((modify-phases) . 3)))
  270. ('replace '(((modify-phases) . 2))) ;different from 'modify-inputs'
  271. ('substitute* 2)
  272. ('substitute-keyword-arguments 2)
  273. ('call-with-input-file 2)
  274. ('call-with-output-file 2)
  275. ('with-output-to-file 2)
  276. ('with-input-from-file 2)
  277. ('with-directory-excursion 2)
  278. ;; (gnu system) and (gnu services).
  279. ('operating-system 1)
  280. ('bootloader-configuration 1)
  281. ('mapped-device 1)
  282. ('file-system 1)
  283. ('swap-space 1)
  284. ('user-account 1)
  285. ('user-group 1)
  286. ('setuid-program 1)
  287. ('modify-services 2)
  288. ;; (gnu home).
  289. ('home-environment 1)))
  290. (define %newline-forms
  291. ;; List heads that must be followed by a newline. The second argument is
  292. ;; the context in which they must appear. This is similar to a special form
  293. ;; of 1, except that indent is 1 instead of 2 columns.
  294. (vhashq
  295. ('arguments '(package))
  296. ('sha256 '(origin source package))
  297. ('base32 '(sha256 origin))
  298. ('git-reference '(uri origin source))
  299. ('search-paths '(package))
  300. ('native-search-paths '(package))
  301. ('search-path-specification '())
  302. ('services '(operating-system))
  303. ('set-xorg-configuration '())
  304. ('services '(home-environment))
  305. ('home-bash-configuration '(service))))
  306. (define (prefix? candidate lst)
  307. "Return true if CANDIDATE is a prefix of LST."
  308. (let loop ((candidate candidate)
  309. (lst lst))
  310. (match candidate
  311. (() #t)
  312. ((head1 . rest1)
  313. (match lst
  314. (() #f)
  315. ((head2 . rest2)
  316. (and (equal? head1 head2)
  317. (loop rest1 rest2))))))))
  318. (define (special-form-lead symbol context)
  319. "If SYMBOL is a special form in the given CONTEXT, return its number of
  320. arguments; otherwise return #f. CONTEXT is a stack of symbols lexically
  321. surrounding SYMBOL."
  322. (match (vhash-assq symbol %special-forms)
  323. (#f #f)
  324. ((_ . alist)
  325. (any (match-lambda
  326. ((prefix . level)
  327. (and (prefix? prefix context) (- level 1))))
  328. alist))))
  329. (define (newline-form? symbol context)
  330. "Return true if parenthesized expressions starting with SYMBOL must be
  331. followed by a newline."
  332. (let ((matches (vhash-foldq* cons '() symbol %newline-forms)))
  333. (find (cut prefix? <> context)
  334. matches)))
  335. (define (escaped-string str)
  336. "Return STR with backslashes and double quotes escaped. Everything else, in
  337. particular newlines, is left as is."
  338. (list->string
  339. `(#\"
  340. ,@(string-fold-right (lambda (chr lst)
  341. (match chr
  342. (#\" (cons* #\\ #\" lst))
  343. (#\\ (cons* #\\ #\\ lst))
  344. (_ (cons chr lst))))
  345. '()
  346. str)
  347. #\")))
  348. (define %natural-whitespace-string-forms
  349. ;; When a string has one of these forms as its parent, only double quotes
  350. ;; and backslashes are escaped; newlines, tabs, etc. are left as-is.
  351. '(synopsis description G_ N_))
  352. (define (printed-string str context)
  353. "Return the read syntax for STR depending on CONTEXT."
  354. (match context
  355. (()
  356. (object->string str))
  357. ((head . _)
  358. (if (memq head %natural-whitespace-string-forms)
  359. (escaped-string str)
  360. (object->string str)))))
  361. (define (string-width str)
  362. "Return the \"width\" of STR--i.e., the width of the longest line of STR."
  363. (apply max (map string-length (string-split str #\newline))))
  364. (define (canonicalize-comment comment indent)
  365. "Canonicalize COMMENT, which is to be printed at INDENT, ensuring it has the
  366. \"right\" number of leading semicolons."
  367. (if (zero? indent)
  368. comment ;leave top-level comments unchanged
  369. (let ((line (string-trim-both
  370. (string-trim (comment->string comment) (char-set #\;)))))
  371. (string->comment (string-append
  372. (if (comment-margin? comment)
  373. ";"
  374. (if (string-null? line)
  375. ";;" ;no trailing space
  376. ";; "))
  377. line "\n")
  378. (comment-margin? comment)))))
  379. (define %not-newline
  380. (char-set-complement (char-set #\newline)))
  381. (define (print-multi-line-comment str indent port)
  382. "Print to PORT STR as a multi-line comment, with INDENT spaces preceding
  383. each line except the first one (they're assumed to be already there)."
  384. ;; While 'read-with-comments' only returns one-line comments, user-provided
  385. ;; comments might span multiple lines, which is why this is necessary.
  386. (let loop ((lst (string-tokenize str %not-newline)))
  387. (match lst
  388. (() #t)
  389. ((last)
  390. (display last port)
  391. (newline port))
  392. ((head tail ...)
  393. (display head port)
  394. (newline port)
  395. (display (make-string indent #\space) port)
  396. (loop tail)))))
  397. (define %integer-forms
  398. ;; Forms that take an integer as their argument, where said integer should
  399. ;; be printed in base other than decimal base.
  400. (letrec-syntax ((vhashq (syntax-rules ()
  401. ((_) vlist-null)
  402. ((_ (key value) rest ...)
  403. (vhash-consq key value (vhashq rest ...))))))
  404. (vhashq
  405. ('chmod 8)
  406. ('umask 8)
  407. ('mkdir 8)
  408. ('mkstemp 8)
  409. ('logand 16)
  410. ('logior 16)
  411. ('logxor 16)
  412. ('lognot 16))))
  413. (define (integer->string integer context)
  414. "Render INTEGER as a string using a base suitable based on CONTEXT."
  415. (define (form-base form)
  416. (match (vhash-assq form %integer-forms)
  417. (#f 10)
  418. ((_ . base) base)))
  419. (define (octal? form)
  420. (= 8 (form-base form)))
  421. (define base
  422. (match context
  423. ((head . tail)
  424. (match (form-base head)
  425. (8 8)
  426. (16 (if (any octal? tail) 8 16))
  427. (10 10)))
  428. (_ 10)))
  429. (string-append (match base
  430. (10 "")
  431. (16 "#x")
  432. (8 "#o"))
  433. (number->string integer base)))
  434. (define* (pretty-print-with-comments port obj
  435. #:key
  436. (format-comment
  437. (lambda (comment indent) comment))
  438. (format-vertical-space identity)
  439. (indent 0)
  440. (max-width 78)
  441. (long-list 5))
  442. "Pretty-print OBJ to PORT, attempting to at most MAX-WIDTH character columns
  443. and assuming the current column is INDENT. Comments present in OBJ are
  444. included in the output.
  445. Lists longer than LONG-LIST are written as one element per line. Comments are
  446. passed through FORMAT-COMMENT before being emitted; a useful value for
  447. FORMAT-COMMENT is 'canonicalize-comment'. Vertical space is passed through
  448. FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'."
  449. (define (list-of-lists? head tail)
  450. ;; Return true if HEAD and TAIL denote a list of lists--e.g., a list of
  451. ;; 'let' bindings.
  452. (match head
  453. ((thing _ ...) ;proper list
  454. (and (not (memq thing
  455. '(quote quasiquote unquote unquote-splicing)))
  456. (pair? tail)))
  457. (_ #f)))
  458. (let loop ((indent indent)
  459. (column indent)
  460. (delimited? #t) ;true if comes after a delimiter
  461. (context '()) ;list of "parent" symbols
  462. (obj obj))
  463. (define (print-sequence context indent column lst delimited?)
  464. (define long?
  465. (> (length lst) long-list))
  466. (let print ((lst lst)
  467. (first? #t)
  468. (delimited? delimited?)
  469. (column column))
  470. (match lst
  471. (()
  472. column)
  473. ((item . tail)
  474. (define newline?
  475. ;; Insert a newline if ITEM is itself a list, or if TAIL is long,
  476. ;; but only if ITEM is not the first item. Also insert a newline
  477. ;; before a keyword.
  478. (and (or (pair? item) long?
  479. (and (keyword? item)
  480. (not (eq? item #:allow-other-keys))))
  481. (not first?) (not delimited?)
  482. (not (blank? item))))
  483. (when newline?
  484. (newline port)
  485. (display (make-string indent #\space) port))
  486. (let ((column (if newline? indent column)))
  487. (print tail
  488. (keyword? item) ;keep #:key value next to one another
  489. (blank? item)
  490. (loop indent column
  491. (or newline? delimited?)
  492. context
  493. item)))))))
  494. (define (sequence-would-protrude? indent lst)
  495. ;; Return true if elements of LST written at INDENT would protrude
  496. ;; beyond MAX-WIDTH. This is implemented as a cheap test with false
  497. ;; negatives to avoid actually rendering all of LST.
  498. (find (match-lambda
  499. ((? string? str)
  500. (>= (+ (string-width str) 2 indent) max-width))
  501. ((? symbol? symbol)
  502. (>= (+ (string-width (symbol->string symbol)) indent)
  503. max-width))
  504. ((? boolean?)
  505. (>= (+ 2 indent) max-width))
  506. (()
  507. (>= (+ 2 indent) max-width))
  508. (_ ;don't know
  509. #f))
  510. lst))
  511. (define (special-form? head)
  512. (special-form-lead head context))
  513. (match obj
  514. ((? comment? comment)
  515. (if (comment-margin? comment)
  516. (begin
  517. (display " " port)
  518. (display (comment->string (format-comment comment indent))
  519. port))
  520. (begin
  521. ;; When already at the beginning of a line, for example because
  522. ;; COMMENT follows a margin comment, no need to emit a newline.
  523. (unless (= column indent)
  524. (newline port)
  525. (display (make-string indent #\space) port))
  526. (print-multi-line-comment (comment->string
  527. (format-comment comment indent))
  528. indent port)))
  529. (display (make-string indent #\space) port)
  530. indent)
  531. ((? vertical-space? space)
  532. (unless delimited? (newline port))
  533. (let loop ((i (vertical-space-height (format-vertical-space space))))
  534. (unless (zero? i)
  535. (newline port)
  536. (loop (- i 1))))
  537. (display (make-string indent #\space) port)
  538. indent)
  539. ((? page-break?)
  540. (unless delimited? (newline port))
  541. (display #\page port)
  542. (newline port)
  543. (display (make-string indent #\space) port)
  544. indent)
  545. (('quote lst)
  546. (unless delimited? (display " " port))
  547. (display "'" port)
  548. (loop indent (+ column (if delimited? 1 2)) #t context lst))
  549. (('quasiquote lst)
  550. (unless delimited? (display " " port))
  551. (display "`" port)
  552. (loop indent (+ column (if delimited? 1 2)) #t context lst))
  553. (('unquote lst)
  554. (unless delimited? (display " " port))
  555. (display "," port)
  556. (loop indent (+ column (if delimited? 1 2)) #t context lst))
  557. (('unquote-splicing lst)
  558. (unless delimited? (display " " port))
  559. (display ",@" port)
  560. (loop indent (+ column (if delimited? 2 3)) #t context lst))
  561. (('gexp lst)
  562. (unless delimited? (display " " port))
  563. (display "#~" port)
  564. (loop indent (+ column (if delimited? 2 3)) #t context lst))
  565. (('ungexp obj)
  566. (unless delimited? (display " " port))
  567. (display "#$" port)
  568. (loop indent (+ column (if delimited? 2 3)) #t context obj))
  569. (('ungexp-native obj)
  570. (unless delimited? (display " " port))
  571. (display "#+" port)
  572. (loop indent (+ column (if delimited? 2 3)) #t context obj))
  573. (('ungexp-splicing lst)
  574. (unless delimited? (display " " port))
  575. (display "#$@" port)
  576. (loop indent (+ column (if delimited? 3 4)) #t context lst))
  577. (('ungexp-native-splicing lst)
  578. (unless delimited? (display " " port))
  579. (display "#+@" port)
  580. (loop indent (+ column (if delimited? 3 4)) #t context lst))
  581. (((? special-form? head) arguments ...)
  582. ;; Special-case 'let', 'lambda', 'modify-inputs', etc. so the second
  583. ;; and following arguments are less indented.
  584. (let* ((lead (special-form-lead head context))
  585. (context (cons head context))
  586. (head (symbol->string head))
  587. (total (length arguments)))
  588. (unless delimited? (display " " port))
  589. (display "(" port)
  590. (display head port)
  591. (unless (zero? lead)
  592. (display " " port))
  593. ;; Print the first LEAD arguments.
  594. (let* ((indent (+ column 2
  595. (if delimited? 0 1)))
  596. (column (+ column 1
  597. (if (zero? lead) 0 1)
  598. (if delimited? 0 1)
  599. (string-length head)))
  600. (initial-indent column))
  601. (define new-column
  602. (let inner ((n lead)
  603. (arguments (take arguments (min lead total)))
  604. (column column))
  605. (if (zero? n)
  606. (begin
  607. (newline port)
  608. (display (make-string indent #\space) port)
  609. indent)
  610. (match arguments
  611. (() column)
  612. ((head . tail)
  613. (inner (- n 1) tail
  614. (loop initial-indent column
  615. (= n lead)
  616. context
  617. head)))))))
  618. ;; Print the remaining arguments.
  619. (let ((column (print-sequence
  620. context indent new-column
  621. (drop arguments (min lead total))
  622. #t)))
  623. (display ")" port)
  624. (+ column 1)))))
  625. ((head tail ...)
  626. (let* ((overflow? (>= column max-width))
  627. (column (if overflow?
  628. (+ indent 1)
  629. (+ column (if delimited? 1 2))))
  630. (newline? (or (newline-form? head context)
  631. (list-of-lists? head tail))) ;'let' bindings
  632. (context (cons head context)))
  633. (if overflow?
  634. (begin
  635. (newline port)
  636. (display (make-string indent #\space) port))
  637. (unless delimited? (display " " port)))
  638. (display "(" port)
  639. (let* ((new-column (loop column column #t context head))
  640. (indent (if (or (>= new-column max-width)
  641. (not (symbol? head))
  642. (sequence-would-protrude?
  643. (+ new-column 1) tail)
  644. newline?)
  645. column
  646. (+ new-column 1))))
  647. (when newline?
  648. ;; Insert a newline right after HEAD.
  649. (newline port)
  650. (display (make-string indent #\space) port))
  651. (let ((column
  652. (print-sequence context indent
  653. (if newline? indent new-column)
  654. tail newline?)))
  655. (display ")" port)
  656. (+ column 1)))))
  657. (_
  658. (let* ((str (cond ((string? obj)
  659. (printed-string obj context))
  660. ((integer? obj)
  661. (integer->string obj context))
  662. (else
  663. (object->string obj))))
  664. (len (string-width str)))
  665. (if (and (> (+ column 1 len) max-width)
  666. (not delimited?))
  667. (begin
  668. (newline port)
  669. (display (make-string indent #\space) port)
  670. (display str port)
  671. (+ indent len))
  672. (begin
  673. (unless delimited? (display " " port))
  674. (display str port)
  675. (+ column (if delimited? 0 1) len))))))))
  676. (define (object->string* obj indent . args)
  677. "Pretty-print OBJ with INDENT columns as the initial indent. ARGS are
  678. passed as-is to 'pretty-print-with-comments'."
  679. (call-with-output-string
  680. (lambda (port)
  681. (apply pretty-print-with-comments port obj
  682. #:indent indent
  683. args))))
  684. (define* (pretty-print-with-comments/splice port lst
  685. #:rest rest)
  686. "Write to PORT the expressions and blanks listed in LST."
  687. (for-each (lambda (exp)
  688. (apply pretty-print-with-comments port exp rest)
  689. (unless (blank? exp)
  690. (newline port)))
  691. lst))