~ruther/guix-local

7931ac810b8feaadcbbfa3a31786087da2d5ee73 — Ludovic Courtès 3 years ago 74e96c4
read-print: 'pretty-print-with-comments' keeps newlines on long strings.

* guix/read-print.scm (printed-string)[preserve-newlines?]: New
procedure.
Use it to preserve newlines on long strings.
* tests/read-print.scm: Add test.
2 files changed, 14 insertions(+), 2 deletions(-)

M guix/read-print.scm
M tests/read-print.scm
M guix/read-print.scm => guix/read-print.scm +9 -2
@@ 420,11 420,18 @@ particular newlines, is left as is."

(define (printed-string str context)
  "Return the read syntax for STR depending on CONTEXT."
  (define (preserve-newlines? str)
    (and (> (string-length str) 40)
         (string-index str #\newline)))

  (match context
    (()
     (object->string str))
     (if (preserve-newlines? str)
         (escaped-string str)
         (object->string str)))
    ((head . _)
     (if (memq head %natural-whitespace-string-forms)
     (if (or (memq head %natural-whitespace-string-forms)
             (preserve-newlines? str))
         (escaped-string str)
         (object->string str)))))


M tests/read-print.scm => tests/read-print.scm +5 -0
@@ 195,6 195,11 @@ expressions."
(string-append \"a\\tb\" \"\\n\")")

(test-pretty-print "\
(display \"This is a very long string.
It contains line breaks, which are preserved,
because it's a long string.\")")

(test-pretty-print "\
(description \"abcdefghijkl
mnopqrstuvwxyz.\")"
                   #:max-width 30)