summaryrefslogtreecommitdiff
path: root/guix/read-print.scm
diff options
context:
space:
mode:
authorMarius Bakke <marius@gnu.org>2023-01-10 00:24:11 +0100
committerMarius Bakke <marius@gnu.org>2023-01-10 00:24:11 +0100
commitd9bcd1a8d6300b79f5884e48b2cefff05de8dce4 (patch)
treee484be53fb1d3cd7c5bb76a34451c7364502d9ec /guix/read-print.scm
parent0d65f7daae7428b26a8141b83c2567e6d0d9d009 (diff)
parent79a9bb25bcb3d8b29968363db9b288cf3844108a (diff)
Merge branch 'master' into staging
Diffstat (limited to 'guix/read-print.scm')
-rw-r--r--guix/read-print.scm22
1 files changed, 19 insertions, 3 deletions
diff --git a/guix/read-print.scm b/guix/read-print.scm
index 8a720ef2ef..ccddca732d 100644
--- a/guix/read-print.scm
+++ b/guix/read-print.scm
@@ -1,5 +1,5 @@
;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2021-2022 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2021-2023 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -488,6 +488,19 @@ each line except the first one (they're assumed to be already there)."
(8 "#o"))
(number->string integer base)))
+(define %special-non-extended-symbols
+ ;; Special symbols that can be written without the #{...}# notation for
+ ;; extended symbols: 1+, 1-, 123/, etc.
+ (make-regexp "^[0-9]+[[:graph:]]+$" regexp/icase))
+
+(define (symbol->display-string symbol context)
+ "Return the most appropriate representation of SYMBOL, resorting to extended
+symbol notation only when strictly necessary."
+ (let ((str (symbol->string symbol)))
+ (if (regexp-exec %special-non-extended-symbols str)
+ str ;no need for the #{...}# notation
+ (object->string symbol))))
+
(define* (pretty-print-with-comments port obj
#:key
(format-comment
@@ -561,7 +574,8 @@ FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'."
((? string? str)
(>= (+ (string-width str) 2 indent) max-width))
((? symbol? symbol)
- (>= (+ (string-width (symbol->string symbol)) indent)
+ (>= (+ (string-width (symbol->display-string symbol context))
+ indent)
max-width))
((? boolean?)
(>= (+ 2 indent) max-width))
@@ -647,7 +661,7 @@ FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'."
;; and following arguments are less indented.
(let* ((lead (special-form-lead head context))
(context (cons head context))
- (head (symbol->string head))
+ (head (symbol->display-string head (cdr context)))
(total (length arguments)))
(unless delimited? (display " " port))
(display "(" port)
@@ -727,6 +741,8 @@ FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'."
(printed-string obj context))
((integer? obj)
(integer->string obj context))
+ ((symbol? obj)
+ (symbol->display-string obj context))
(else
(object->string obj))))
(len (string-width str)))