mirror of
https://git.in.rschanz.org/ryan77627/guix.git
synced 2025-01-25 20:19:18 -05:00
read-print: Do not use extended notation when printing '1+', '1-', etc.
* guix/read-print.scm (%special-non-extended-symbols): New variable. (symbol->display-string): New procedure. (pretty-print-with-comments): Use it in lieu of 'string->symbol'. * tests/read-print.scm: Add test.
This commit is contained in:
parent
407175a1d0
commit
6c343d0d0f
2 changed files with 25 additions and 4 deletions
|
@ -1,5 +1,5 @@
|
||||||
;;; GNU Guix --- Functional package management for GNU
|
;;; 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.
|
;;; This file is part of GNU Guix.
|
||||||
;;;
|
;;;
|
||||||
|
@ -488,6 +488,19 @@ (define base
|
||||||
(8 "#o"))
|
(8 "#o"))
|
||||||
(number->string integer base)))
|
(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
|
(define* (pretty-print-with-comments port obj
|
||||||
#:key
|
#:key
|
||||||
(format-comment
|
(format-comment
|
||||||
|
@ -561,7 +574,8 @@ (define (sequence-would-protrude? indent lst)
|
||||||
((? string? str)
|
((? string? str)
|
||||||
(>= (+ (string-width str) 2 indent) max-width))
|
(>= (+ (string-width str) 2 indent) max-width))
|
||||||
((? symbol? symbol)
|
((? symbol? symbol)
|
||||||
(>= (+ (string-width (symbol->string symbol)) indent)
|
(>= (+ (string-width (symbol->display-string symbol context))
|
||||||
|
indent)
|
||||||
max-width))
|
max-width))
|
||||||
((? boolean?)
|
((? boolean?)
|
||||||
(>= (+ 2 indent) max-width))
|
(>= (+ 2 indent) max-width))
|
||||||
|
@ -647,7 +661,7 @@ (define (special-form? head)
|
||||||
;; and following arguments are less indented.
|
;; and following arguments are less indented.
|
||||||
(let* ((lead (special-form-lead head context))
|
(let* ((lead (special-form-lead head context))
|
||||||
(context (cons head context))
|
(context (cons head context))
|
||||||
(head (symbol->string head))
|
(head (symbol->display-string head (cdr context)))
|
||||||
(total (length arguments)))
|
(total (length arguments)))
|
||||||
(unless delimited? (display " " port))
|
(unless delimited? (display " " port))
|
||||||
(display "(" port)
|
(display "(" port)
|
||||||
|
@ -727,6 +741,8 @@ (define new-column
|
||||||
(printed-string obj context))
|
(printed-string obj context))
|
||||||
((integer? obj)
|
((integer? obj)
|
||||||
(integer->string obj context))
|
(integer->string obj context))
|
||||||
|
((symbol? obj)
|
||||||
|
(symbol->display-string obj context))
|
||||||
(else
|
(else
|
||||||
(object->string obj))))
|
(object->string obj))))
|
||||||
(len (string-width str)))
|
(len (string-width str)))
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
;;; GNU Guix --- Functional package management for GNU
|
;;; 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.
|
;;; This file is part of GNU Guix.
|
||||||
;;;
|
;;;
|
||||||
|
@ -142,6 +142,11 @@ (define-syntax-rule (test-pretty-print/sequence str args ...)
|
||||||
(+ x y))"
|
(+ x y))"
|
||||||
#:max-width 11)
|
#:max-width 11)
|
||||||
|
|
||||||
|
(test-pretty-print "\
|
||||||
|
(begin
|
||||||
|
1+ 1- 123/ 456*
|
||||||
|
(1+ 41))")
|
||||||
|
|
||||||
(test-pretty-print "\
|
(test-pretty-print "\
|
||||||
(lambda (x y)
|
(lambda (x y)
|
||||||
;; This is a procedure.
|
;; This is a procedure.
|
||||||
|
|
Loading…
Reference in a new issue