aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2022-09-01 22:08:12 +0200
committerLudovic Courtès <ludo@gnu.org>2022-09-02 10:49:35 +0200
commit82968362ead0ed59c64ee8a21fec346c9265a149 (patch)
treece9143abf4469b619b37b36792faa7b3820ef4c8
parentac9a7f6be9c2e0f2ab218f7a423527490bb6aa9c (diff)
downloadguix-82968362ead0ed59c64ee8a21fec346c9265a149.tar.gz
guix-82968362ead0ed59c64ee8a21fec346c9265a149.zip
read-print: Define forms for which \n, \t, etc. are not escaped.
Previously, the pretty-printer would unconditionally leave everything but double-quotes and backslashes unescaped when rendering a string. With this change, the previous behavior only applies to forms listed in %NATURAL-WHITESPACE-STRING-FORMS. * guix/read-print.scm (%natural-whitespace-string-forms): New variable. (printed-string): New procedure. (pretty-print-with-comments): Use it instead of 'escaped-string'. * tests/read-print.scm: Add test.
-rw-r--r--guix/read-print.scm17
-rw-r--r--tests/read-print.scm3
2 files changed, 19 insertions, 1 deletions
diff --git a/guix/read-print.scm b/guix/read-print.scm
index 00dde870f4..6e1188e87e 100644
--- a/guix/read-print.scm
+++ b/guix/read-print.scm
@@ -386,6 +386,21 @@ particular newlines, is left as is."
str)
#\")))
+(define %natural-whitespace-string-forms
+ ;; When a string has one of these forms as its parent, only double quotes
+ ;; and backslashes are escaped; newlines, tabs, etc. are left as-is.
+ '(synopsis description G_ N_))
+
+(define (printed-string str context)
+ "Return the read syntax for STR depending on CONTEXT."
+ (match context
+ (()
+ (object->string str))
+ ((head . _)
+ (if (memq head %natural-whitespace-string-forms)
+ (escaped-string str)
+ (object->string str)))))
+
(define (string-width str)
"Return the \"width\" of STR--i.e., the width of the longest line of STR."
(apply max (map string-length (string-split str #\newline))))
@@ -691,7 +706,7 @@ FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'."
(+ column 1)))))
(_
(let* ((str (cond ((string? obj)
- (escaped-string obj))
+ (printed-string obj context))
((integer? obj)
(integer->string obj context))
(else
diff --git a/tests/read-print.scm b/tests/read-print.scm
index 1b0d865972..ca3f3193f7 100644
--- a/tests/read-print.scm
+++ b/tests/read-print.scm
@@ -187,6 +187,9 @@ expressions."
xyz))))")
(test-pretty-print "\
+(string-append \"a\\tb\" \"\\n\")")
+
+(test-pretty-print "\
(description \"abcdefghijkl
mnopqrstuvwxyz.\")"
#:max-width 30)