summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludovic.courtes@inria.fr>2023-04-24 10:10:00 +0200
committerLudovic Courtès <ludo@gnu.org>2023-04-24 10:27:13 +0200
commit7931ac810b8feaadcbbfa3a31786087da2d5ee73 (patch)
treeb9a45254cf47354eb8e48a43e26ba042ff645cb3
parent74e96c4cb171b17949f638d8b452d047a8f2dc6f (diff)
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.
-rw-r--r--guix/read-print.scm11
-rw-r--r--tests/read-print.scm5
2 files changed, 14 insertions, 2 deletions
diff --git a/guix/read-print.scm b/guix/read-print.scm
index 515eb7669c7..d834105dce7 100644
--- a/guix/read-print.scm
+++ b/guix/read-print.scm
@@ -420,11 +420,18 @@ particular newlines, is left as is."
420 420
421(define (printed-string str context) 421(define (printed-string str context)
422 "Return the read syntax for STR depending on CONTEXT." 422 "Return the read syntax for STR depending on CONTEXT."
423 (define (preserve-newlines? str)
424 (and (> (string-length str) 40)
425 (string-index str #\newline)))
426
423 (match context 427 (match context
424 (() 428 (()
425 (object->string str)) 429 (if (preserve-newlines? str)
430 (escaped-string str)
431 (object->string str)))
426 ((head . _) 432 ((head . _)
427 (if (memq head %natural-whitespace-string-forms) 433 (if (or (memq head %natural-whitespace-string-forms)
434 (preserve-newlines? str))
428 (escaped-string str) 435 (escaped-string str)
429 (object->string str))))) 436 (object->string str)))))
430 437
diff --git a/tests/read-print.scm b/tests/read-print.scm
index f4627e076a4..c2b236b1721 100644
--- a/tests/read-print.scm
+++ b/tests/read-print.scm
@@ -195,6 +195,11 @@ expressions."
195(string-append \"a\\tb\" \"\\n\")") 195(string-append \"a\\tb\" \"\\n\")")
196 196
197(test-pretty-print "\ 197(test-pretty-print "\
198(display \"This is a very long string.
199It contains line breaks, which are preserved,
200because it's a long string.\")")
201
202(test-pretty-print "\
198(description \"abcdefghijkl 203(description \"abcdefghijkl
199mnopqrstuvwxyz.\")" 204mnopqrstuvwxyz.\")"
200 #:max-width 30) 205 #:max-width 30)