- ))
-
-
-(define-public (set-property-markup qualifier)
- (lambda (paper props . rest )
- (interpret-markup paper
- (cons (cons `(,qualifier . ,(car rest))
- (car props)) (cdr props))
- (cadr rest))
- ))
-
-(define-public (finger-markup paper props . rest)
- (interpret-markup paper
- (cons (list '(font-size . -4)
- '(font-family . number))
- props)
- (car rest)))
-
-(define-public fontsize-markup (set-property-markup 'font-size))
-(define-public magnify-markup (set-property-markup 'font-magnification))
-
-(define-public bold-markup
- (font-markup 'font-series 'bold))
-(define-public sans-markup
- (font-markup 'font-family 'sans))
-(define-public number-markup
- (font-markup 'font-family 'number))
-(define-public roman-markup
- (font-markup 'font-family 'roman))
-
-
-(define-public huge-markup
- (font-markup 'font-size 2))
-(define-public large-markup
- (font-markup 'font-size 1))
-(define-public normalsize-markup
- (font-markup 'font-size 0))
-(define-public small-markup
- (font-markup 'font-size -1))
-(define-public tiny-markup
- (font-markup 'font-size -2))
-(define-public teeny-markup
- (font-markup 'font-size -3))
-(define-public dynamic-markup
- (font-markup 'font-family 'dynamic))
-(define-public italic-markup
- (font-markup 'font-shape 'italic))
-(define-public typewriter-markup
- (font-markup 'font-family 'typewriter))
-
-
-;; TODO: baseline-skip should come from the font.
-(define-public (column-markup paper props . rest)
- (stack-lines
- -1 0.0 (cdr (chain-assoc 'baseline-skip props))
- (map (lambda (x) (interpret-markup paper props x)) (car rest)))
- )
-
-(define-public (dir-column-markup paper props . rest)
- "Make a column of args, going up or down, depending on the setting
-of the #'direction layout property."
+(define (compile-all-markup-expressions expr)
+ "Return a list of canonical markups expressions, e.g.:
+ (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
+ ===>
+ ((make-COMMAND1-markup arg11 arg12)
+ (make-COMMAND2-markup arg21 arg22 arg23) ...)"
+ (do ((rest expr rest)
+ (markps '() markps))
+ ((null? rest) (reverse markps))
+ (receive (m r) (compile-markup-expression rest)
+ (set! markps (cons m markps))
+ (set! rest r))))
+
+(define (keyword->make-markup key)
+ "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
+ (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
+
+(define (compile-markup-expression expr)
+ "Return two values: the first complete canonical markup expression found in `expr',
+e.g. (make-COMMAND-markup arg1 arg2 ...), and the rest expression."
+ (cond ((and (pair? expr)
+ (keyword? (car expr)))
+ ;; expr === (#:COMMAND arg1 ...)
+ (let* ((command (symbol->string (keyword->symbol (car expr))))
+ (sig (markup-command-signature (car (lookup-markup-command command))))
+ (sig-len (length sig)))
+ (do ((i 0 (1+ i))
+ (args '() args)
+ (rest (cdr expr) rest))
+ ((>= i sig-len)
+ (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
+ (cond ((eqv? (list-ref sig i) markup-list?)
+ ;; (car rest) is a markup list
+ (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
+ (set! rest (cdr rest)))
+ (else
+ ;; pick up one arg in `rest'
+ (receive (a r) (compile-markup-arg rest)
+ (set! args (cons a args))
+ (set! rest r)))))))
+ ((and (pair? expr)
+ (pair? (car expr))
+ (keyword? (caar expr)))
+ ;; expr === ((#:COMMAND arg1 ...) ...)
+ (receive (m r) (compile-markup-expression (car expr))
+ (values m (cdr expr))))
+ ((and (pair? expr)
+ (string? (car expr))) ;; expr === ("string" ...)
+ (values `(make-simple-markup ,(car expr)) (cdr expr)))
+ (else
+ ;; expr === (symbol ...) or ((funcall ...) ...)
+ (values (car expr)
+ (cdr expr)))))
+
+(define (compile-all-markup-args expr)
+ "Transform `expr' into markup arguments"
+ (do ((rest expr rest)
+ (args '() args))
+ ((null? rest) (reverse args))
+ (receive (a r) (compile-markup-arg rest)
+ (set! args (cons a args))
+ (set! rest r))))
+
+(define (compile-markup-arg expr)
+ "Return two values: the desired markup argument, and the rest arguments"
+ (cond ((null? expr)
+ ;; no more args
+ (values '() '()))
+ ((keyword? (car expr))
+ ;; expr === (#:COMMAND ...)
+ ;; ==> build and return the whole markup expression
+ (compile-markup-expression expr))
+ ((and (pair? (car expr))
+ (keyword? (caar expr)))
+ ;; expr === ((#:COMMAND ...) ...)
+ ;; ==> build and return the whole markup expression(s)
+ ;; found in (car expr)
+ (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
+ (if (null? rest-expr)
+ (values markup-expr (cdr expr))
+ (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
+ (cdr expr)))))
+ ((and (pair? (car expr))
+ (pair? (caar expr)))
+ ;; expr === (((foo ...) ...) ...)
+ (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
+ (else (values (car expr) (cdr expr)))))
+
+;;;;;;;;;;;;;;;
+;;; Debugging utilities: print markup expressions in a friendly fashion
+
+(use-modules (ice-9 format))
+(define (markup->string markup-expr)
+ "Return a string describing, in LilyPond syntax, the given markup expression."
+ (define (proc->command proc)
+ (let ((cmd-markup (symbol->string (procedure-name proc))))
+ (substring cmd-markup 0 (- (string-length cmd-markup)
+ (string-length "-markup")))))
+ (define (arg->string arg)
+ (cond ((and (pair? arg) (pair? (car arg))) ;; markup list
+ (format #f "~{ ~a~}" (map markup->string arg)))
+ ((pair? arg) ;; markup
+ (markup->string arg))
+ ((string? arg) ;; scheme string argument
+ (format #f "#\"~a\"" arg))
+ (else ;; other scheme arg
+ (format #f "#~a" arg))))
+ (let ((cmd (car markup-expr))
+ (args (cdr markup-expr)))
+ (cond ((eqv? cmd simple-markup) ;; a simple string
+ (format #f "\"~a\"" (car args)))
+ ((eqv? cmd line-markup) ;; { ... }
+ (format #f "{~a}" (arg->string (car args))))
+ ((eqv? cmd center-align-markup) ;; \center < ... >
+ (format #f "\\center-align <~a>" (arg->string (car args))))
+ ((eqv? cmd column-markup) ;; \column < ... >
+ (format #f "\\column <~a>" (arg->string (car args))))
+ (else ;; \command ...
+ (format #f "\\~a~{ ~a~} " (proc->command cmd) (map arg->string args))))))
+
+(define-public (display-markup markup-expr)
+ "Print a LilyPond-syntax equivalent for the given markup expression."
+ (display "\\markup ")
+ (display (markup->string markup-expr)))
+
+;;;;;;;;;;;;;;;
+;;; Utilities for storing and accessing markup commands signature
+;;; and keyword.
+;;; Examples:
+;;;
+;;; (set! (markup-command-signature raise-markup) (list number? markup?))
+;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
+;;;
+;;; (markup-command-signature raise-markup)
+;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
+;;;
+;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
+;;;
+
+(define markup-command-signatures (make-hash-table 50))
+
+(define (markup-command-signature-ref markup-command)
+ "Return markup-command's signature, e.g. (number? markup?).
+markup-command may be a procedure."
+ (let ((sig-key (hashq-ref markup-command-signatures
+ markup-command)))
+ (if sig-key (car sig-key) #f)))
+
+(define-public (markup-command-keyword markup-command)
+ "Return markup-command's keyword, e.g. \"scheme0markup1\".
+markup-command may be a procedure."
+ (let ((sig-key (hashq-ref markup-command-signatures
+ markup-command)))
+ (if sig-key (cdr sig-key) #f)))
+
+(define (markup-command-signatureset! markup-command signature)
+ "Set markup-command's signature. markup-command must be a named procedure.
+Also set markup-signature and markup-keyword object properties."
+ (hashq-set! markup-command-signatures
+ markup-command
+ (cons signature (markup-signature-to-keyword signature)))
+ ;; these object properties are still in use somewhere
+ (set-object-property! markup-command 'markup-signature signature)
+ (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))