;; for define-safe-public when byte-compiling using Guile V2
(use-modules (scm safe-utility-defs))
+(use-modules (ice-9 pretty-print))
+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; constants.
;; x)
(define-public (stderr string . rest)
- (apply format (cons (current-error-port) (cons string rest)))
+ (apply format (current-error-port) string rest)
(force-output (current-error-port)))
(define-public (debugf string . rest)
(if #f
- (apply stderr (cons string rest))))
+ (apply stderr string rest)))
(define (index-cell cell dir)
(if (equal? dir 1)
(reverse matches))
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; numbering styles
+
+(define-public (number-format number-type num . custom-format)
+ "Print NUM accordingly to the requested NUMBER-TYPE.
+Choices include @code{roman-lower} (by default),
+@code{roman-upper}, @code{arabic} and @code{custom}.
+In the latter case, CUSTOM-FORMAT must be supplied
+and will be applied to NUM."
+ (cond
+ ((equal? number-type 'roman-lower)
+ (fancy-format #f "~(~@r~)" num))
+ ((equal? number-type 'roman-upper)
+ (fancy-format #f "~@r" num))
+ ((equal? number-type 'arabic)
+ (fancy-format #f "~d" num))
+ ((equal? number-type 'custom)
+ (fancy-format #f (car custom-format) num))
+ (else (fancy-format #f "~(~@r~)" num))))
+
;;;;;;;;;;;;;;;;
;; other
(object->string def))
def))))
-;;
-;; don't confuse users with #<procedure .. > syntax.
-;;
+(define (self-evaluating? x)
+ (or (number? x) (string? x) (procedure? x) (boolean? x)))
+
+(define (ly-type? x)
+ (any (lambda (p) ((car p) x)) lilypond-exported-predicates))
+
+(define-public (pretty-printable? val)
+ (and (not (self-evaluating? val))
+ (not (symbol? val))
+ (not (hash-table? val))
+ (not (ly-type? val))))
+
(define-public (scm->string val)
- (if (and (procedure? val)
- (symbol? (procedure-name val)))
- (symbol->string (procedure-name val))
- (string-append
- (if (self-evaluating? val)
- (if (string? val)
- "\""
- "")
- "'")
- (call-with-output-string (lambda (port) (display val port)))
- (if (string? val)
- "\""
- ""))))
+ (let* ((quote-style (if (string? val)
+ 'double
+ (if (or (null? val) ; (ly-type? '()) => #t
+ (and (not (self-evaluating? val))
+ (not (vector? val))
+ (not (hash-table? val))
+ (not (ly-type? val))))
+ 'single
+ 'none)))
+ ; don't confuse users with #<procedure ...> syntax
+ (str (if (and (procedure? val)
+ (symbol? (procedure-name val)))
+ (symbol->string (procedure-name val))
+ (call-with-output-string
+ (if (pretty-printable? val)
+ ; property values in PDF hit margin after 64 columns
+ (lambda (port)
+ (pretty-print val port #:width (case quote-style
+ ((single) 63)
+ (else 64))))
+ (lambda (port) (display val port)))))))
+ (case quote-style
+ ((single) (string-append
+ "'"
+ (string-regexp-substitute "\n " "\n " str)))
+ ((double) (string-append "\"" str "\""))
+ (else str))))
(define-public (!= lst r)
(not (= lst r)))