Skip to content

Commit

Permalink
read-print: Define forms for which \n, \t, etc. are not escaped.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
civodul committed Sep 2, 2022
1 parent ac9a7f6 commit 8296836
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 16 additions & 1 deletion guix/read-print.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/read-print.scm
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ expressions."
(lambda _
xyz))))")

(test-pretty-print "\
(string-append \"a\\tb\" \"\\n\")")

(test-pretty-print "\
(description \"abcdefghijkl
mnopqrstuvwxyz.\")"
Expand Down

0 comments on commit 8296836

Please sign in to comment.