1 ;;;; markup.scm -- Implement a user extensible markup scheme.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2003--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 Internally markup is stored as lists, whose head is a function.
10 (FUNCTION ARG1 ARG2 ... )
12 When the markup is formatted, then FUNCTION is called as follows
14 (FUNCTION GROB PROPS ARG1 ARG2 ... )
16 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
17 the rest of the arguments.
19 The function should return a stencil (i.e. a formatted, ready to
23 To add a builtin markup command, use the define-builtin-markup-command
24 utility. In a user file, the define-markup-command macro shall be used
25 (see ly/markup-init.ly).
27 (define-markup-command (mycommand layout prop arg1 ...) (arg1-type? ...)
28 \"my command usage and description\"
31 The command is now available in markup mode, e.g.
33 \\markup { .... \\MYCOMMAND #1 argument ... }
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;;; markup definer utilities
40 ;; For documentation purposes
41 ;; category -> markup functions
42 (define-public markup-functions-by-category (make-hash-table 150))
43 ;; markup function -> used properties
44 (define-public markup-functions-properties (make-hash-table 150))
45 ;; List of markup list functions
46 (define-public markup-list-function-list (list))
48 (define-macro (define-builtin-markup-command command-and-args signature
49 category properties-or-copied-function . body)
51 * Define a COMMAND-markup function after command-and-args and body,
52 register COMMAND-markup and its signature,
54 * add COMMAND-markup to markup-functions-by-category,
56 * sets COMMAND-markup markup-signature and markup-keyword object properties,
58 * define a make-COMMAND-markup function.
61 (define-builtin-markup-command (COMMAND layout props . arguments)
65 \"documentation string\"
68 (define-builtin-markup-command COMMAND
74 argument-types is a list of type predicates for arguments
75 category is either a symbol or a symbol list
76 properties a list of (property default-value) lists or COMMANDx-markup elements
77 (when a COMMANDx-markup is found, the properties of the said commandx are
78 added instead). No check is performed against cyclical references!
80 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
81 (args (if (pair? command-and-args) (cdr command-and-args) '()))
82 (command-name (string->symbol (format #f "~a-markup" command)))
83 (make-markup-name (string->symbol (format #f "make-~a-markup" command))))
85 ;; define the COMMAND-markup function
87 (let ((documentation (car body))
88 (real-body (cdr body))
89 (properties properties-or-copied-function))
90 `(define-public (,command-name ,@args)
92 (let ,(filter identity
93 (map (lambda (prop-spec)
95 (let ((prop (car prop-spec))
96 (default-value (if (null? (cdr prop-spec))
100 `(,prop (chain-assoc-get ',prop ,props ,default-value)))
104 (let ((args (gensym "args"))
105 (markup-command properties-or-copied-function))
106 `(define-public (,command-name . ,args)
107 ,(format #f "Copy of the ~a command." markup-command)
108 (apply ,markup-command ,args))))
109 (set! (markup-command-signature ,command-name) (list ,@signature))
110 ;; Register the new function, for markup documentation
111 ,@(map (lambda (category)
112 `(hashq-set! markup-functions-by-category ',category
114 (or (hashq-ref markup-functions-by-category ',category)
116 (if (list? category) category (list category)))
117 ;; Used properties, for markup documentation
118 (hashq-set! markup-functions-properties
120 (list ,@(map (lambda (prop-spec)
121 (cond ((symbol? prop-spec)
123 ((not (null? (cdr prop-spec)))
124 `(list ',(car prop-spec) ,(cadr prop-spec)))
126 `(list ',(car prop-spec)))))
128 properties-or-copied-function
130 ;; define the make-COMMAND-markup function
131 (define-public (,make-markup-name . args)
132 (let ((sig (list ,@signature)))
133 (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
135 (define-macro (define-builtin-markup-list-command command-and-args signature
137 "Same as `define-builtin-markup-command, but defines a command that, when
138 interpreted, returns a list of stencils instead os a single one"
139 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
140 (args (if (pair? command-and-args) (cdr command-and-args) '()))
141 (command-name (string->symbol (format #f "~a-markup-list" command)))
142 (make-markup-name (string->symbol (format #f "make-~a-markup-list" command))))
144 ;; define the COMMAND-markup-list function
146 (let ((documentation (car body))
147 (real-body (cdr body)))
148 `(define-public (,command-name ,@args)
150 (let ,(filter identity
151 (map (lambda (prop-spec)
152 (if (pair? prop-spec)
153 (let ((prop (car prop-spec))
154 (default-value (if (null? (cdr prop-spec))
158 `(,prop (chain-assoc-get ',prop ,props ,default-value)))
162 (let ((args (gensym "args"))
163 (markup-command (car body)))
164 `(define-public (,command-name . ,args)
165 ,(format #f "Copy of the ~a command." markup-command)
166 (apply ,markup-command ,args))))
167 (set! (markup-command-signature ,command-name) (list ,@signature))
168 ;; add the command to markup-list-function-list, for markup documentation
169 (if (not (member ,command-name markup-list-function-list))
170 (set! markup-list-function-list (cons ,command-name
171 markup-list-function-list)))
172 ;; Used properties, for markup documentation
173 (hashq-set! markup-functions-properties
175 (list ,@(map (lambda (prop-spec)
176 (cond ((symbol? prop-spec)
178 ((not (null? (cdr prop-spec)))
179 `(list ',(car prop-spec) ,(cadr prop-spec)))
181 `(list ',(car prop-spec)))))
185 ;; it's a markup-list command:
186 (set-object-property! ,command-name 'markup-list-command #t)
187 ;; define the make-COMMAND-markup-list function
188 (define-public (,make-markup-name . args)
189 (let ((sig (list ,@signature)))
190 (list (make-markup ,command-name
191 ,(symbol->string make-markup-name) sig args)))))))
193 (define-public (make-markup markup-function make-name signature args)
194 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
195 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
197 (let* ((arglen (length args))
198 (siglen (length signature))
199 (error-msg (if (and (> siglen 0) (> arglen 0))
200 (markup-argument-list-error signature args 1)
202 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
203 (ly:error (string-append make-name ": "
204 (_ "Wrong number of arguments. Expect: ~A, found ~A: ~S"))
210 (_ "Invalid argument in position ~A. Expect: ~A, found: ~S.")
212 (cons markup-function args))))
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 ;;; markup constructors
216 ;;; lilypond-like syntax for markup construction in scheme.
218 (use-modules (ice-9 optargs)
221 (defmacro*-public markup (#:rest body)
222 "The `markup' macro provides a lilypond-like syntax for building markups.
224 - #:COMMAND is used instead of \\COMMAND
225 - #:line ( ... ) is used instead of \\line { ... }
230 \\raise #0.2 \\hbracket \\bold bar
231 \\override #'(baseline-skip . 4)
232 \\bracket \\column { baz bazr bla }
236 #:raise 0.2 #:hbracket #:bold \"bar\"
237 #:override '(baseline-skip . 4)
238 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
239 Use `markup*' in a \\notemode context."
241 (car (compile-all-markup-expressions `(#:line ,body))))
243 (defmacro*-public markup* (#:rest body)
244 "Same as `markup', for use in a \\notes block."
245 `(ly:export (markup ,@body)))
248 (define (compile-all-markup-expressions expr)
249 "Return a list of canonical markups expressions, e.g.:
250 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
252 ((make-COMMAND1-markup arg11 arg12)
253 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
254 (do ((rest expr rest)
256 ((null? rest) (reverse markps))
257 (receive (m r) (compile-markup-expression rest)
258 (set! markps (cons m markps))
261 (define (keyword->make-markup key)
262 "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
263 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
265 (define (compile-markup-expression expr)
266 "Return two values: the first complete canonical markup expression
267 found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
268 and the rest expression."
269 (cond ((and (pair? expr)
270 (keyword? (car expr)))
271 ;; expr === (#:COMMAND arg1 ...)
272 (let ((command (symbol->string (keyword->symbol (car expr)))))
273 (if (not (pair? (lookup-markup-command command)))
274 (ly:error (_ "Not a markup command: ~A") command))
275 (let* ((sig (markup-command-signature
276 (car (lookup-markup-command command))))
277 (sig-len (length sig)))
280 (rest (cdr expr) rest))
282 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
283 (cond ((eqv? (list-ref sig i) markup-list?)
284 ;; (car rest) is a markup list
285 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
286 (set! rest (cdr rest)))
288 ;; pick up one arg in `rest'
289 (receive (a r) (compile-markup-arg rest)
290 (set! args (cons a args))
294 (keyword? (caar expr)))
295 ;; expr === ((#:COMMAND arg1 ...) ...)
296 (receive (m r) (compile-markup-expression (car expr))
297 (values m (cdr expr))))
299 (string? (car expr))) ;; expr === ("string" ...)
300 (values `(make-simple-markup ,(car expr)) (cdr expr)))
302 ;; expr === (symbol ...) or ((funcall ...) ...)
306 (define (compile-all-markup-args expr)
307 "Transform `expr' into markup arguments"
308 (do ((rest expr rest)
310 ((null? rest) (reverse args))
311 (receive (a r) (compile-markup-arg rest)
312 (set! args (cons a args))
315 (define (compile-markup-arg expr)
316 "Return two values: the desired markup argument, and the rest arguments"
320 ((keyword? (car expr))
321 ;; expr === (#:COMMAND ...)
322 ;; ==> build and return the whole markup expression
323 (compile-markup-expression expr))
324 ((and (pair? (car expr))
325 (keyword? (caar expr)))
326 ;; expr === ((#:COMMAND ...) ...)
327 ;; ==> build and return the whole markup expression(s)
328 ;; found in (car expr)
329 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
330 (if (null? rest-expr)
331 (values markup-expr (cdr expr))
332 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
334 ((and (pair? (car expr))
336 ;; expr === (((foo ...) ...) ...)
337 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
338 (else (values (car expr) (cdr expr)))))
341 ;;; Utilities for storing and accessing markup commands signature
345 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
346 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
348 ;;; (markup-command-signature raise-markup)
349 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
351 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
354 (define-public (markup-command-keyword markup-command)
355 "Return markup-command's argument keyword, ie a string describing the command
356 arguments, eg. \"scheme0markup1\""
357 (object-property markup-command 'markup-keyword))
359 (define-public (markup-command-signature-ref markup-command)
360 "Return markup-command's signature (the 'markup-signature object property)"
361 (object-property markup-command 'markup-signature))
363 (define-public (markup-command-signature-set! markup-command signature)
364 "Set markup-command's signature and keyword (as object properties)"
365 (set-object-property! markup-command 'markup-signature signature)
366 (set-object-property! markup-command 'markup-keyword
367 (markup-signature-to-keyword signature))
370 (define-public markup-command-signature
371 (make-procedure-with-setter markup-command-signature-ref
372 markup-command-signature-set!))
374 (define-public (markup-signature-to-keyword sig)
375 " (A B C) -> a0-b1-c2 "
378 (string->symbol (string-join (map
381 (set! count (+ count 1))
383 ;; for reasons I don't get,
384 ;; (case func ((markup?) .. )
387 ((eq? func markup?) "markup")
388 ((eq? func markup-list?) "markup-list")
390 (number->string (- count 1)))))
394 (define (lookup-markup-command-aux symbol)
395 (let ((proc (catch 'misc-error
397 (module-ref (current-module) symbol))
398 (lambda (key . args) #f))))
399 (and (procedure? proc) proc)))
401 (define-public (lookup-markup-command code)
402 (let ((proc (lookup-markup-command-aux
403 (string->symbol (format #f "~a-markup" code)))))
404 (and proc (markup-function? proc)
405 (cons proc (markup-command-keyword proc)))))
407 (define-public (lookup-markup-list-command code)
408 (let ((proc (lookup-markup-command-aux
409 (string->symbol (format #f "~a-markup-list" code)))))
410 (and proc (markup-list-function? proc)
411 (cons proc (markup-command-keyword proc)))))
413 ;;;;;;;;;;;;;;;;;;;;;;
414 ;;; used in parser.yy to map a list of markup commands on markup arguments
415 (define-public (map-markup-command-list commands markups)
416 "`markups' being a list of markups, eg (markup1 markup2 markup3),
417 and `commands' a list of commands with their scheme arguments, in reverse order,
418 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
419 ((bold (raise 4 (italic markup1)))
420 (bold (raise 4 (italic markup2)))
421 (bold (raise 4 (italic markup3))))
423 (map-in-order (lambda (arg)
425 (for-each (lambda (cmd)
426 (set! result (append cmd (list result))))
431 ;;;;;;;;;;;;;;;;;;;;;;
432 ;;; markup type predicates
434 (define (markup-function? x)
435 (and (markup-command-signature x)
436 (not (object-property x 'markup-list-command))))
438 (define (markup-list-function? x)
439 (and (markup-command-signature x)
440 (object-property x 'markup-list-command)))
442 (define-public (markup-command-list? x)
443 "Determine if `x' is a markup command list, ie. a list composed of
444 a markup list function and its arguments."
445 (and (pair? x) (markup-list-function? (car x))))
447 (define-public (markup-list? arg)
448 "Return a true value if `x' is a list of markups or markup command lists."
449 (define (markup-list-inner? lst)
451 (and (or (markup? (car lst)) (markup-command-list? (car lst)))
452 (markup-list-inner? (cdr lst)))))
453 (not (not (and (list? arg) (markup-list-inner? arg)))))
455 (define (markup-argument-list? signature arguments)
456 "Typecheck argument list."
457 (if (and (pair? signature) (pair? arguments))
458 (and ((car signature) (car arguments))
459 (markup-argument-list? (cdr signature) (cdr arguments)))
460 (and (null? signature) (null? arguments))))
463 (define (markup-argument-list-error signature arguments number)
464 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
465 #f is no error found.
467 (if (and (pair? signature) (pair? arguments))
468 (if (not ((car signature) (car arguments)))
469 (list number (type-name (car signature)) (car arguments))
470 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
474 ;; full recursive typecheck.
476 (define (markup-typecheck? arg)
479 (markup-function? (car arg))
480 (markup-argument-list? (markup-command-signature (car arg))
487 (define (markup-thrower-typecheck arg)
488 "typecheck, and throw an error when something amiss.
490 Uncovered - cheap-markup? is used."
492 (cond ((string? arg) #t)
494 (throw 'markup-format "Not a pair" arg))
495 ((not (markup-function? (car arg)))
496 (throw 'markup-format "Not a markup function " (car arg)))
497 ((not (markup-argument-list? (markup-command-signature (car arg))
499 (throw 'markup-format "Arguments failed typecheck for " arg)))
503 ;; good enough if you only use make-XXX-markup functions.
505 (define (cheap-markup? x)
508 (markup-function? (car x)))))
511 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
513 (define-public markup? cheap-markup?)
517 (define (markup-join markups sep)
518 "Return line-markup of MARKUPS, joining them with markup SEP"
520 (make-line-markup (list-insert-separator markups sep))
524 (define-public interpret-markup ly:text-interface::interpret-markup)
526 (define-public (interpret-markup-list layout props markup-list)
527 (let ((stencils (list)))
528 (for-each (lambda (m)
530 (if (markup-command-list? m)
531 (append! (reverse! (apply (car m) layout props (cdr m)))
533 (cons (interpret-markup layout props m) stencils))))
535 (reverse! stencils)))
537 (define-public (prepend-alist-chain key val chain)
538 (cons (acons key val (car chain)) (cdr chain)))
540 (define-public (stack-stencil-line space stencils)
542 (if (and (pair? stencils)
543 (ly:stencil? (car stencils)))
545 (if (and (pair? (cdr stencils))
546 (ly:stencil? (cadr stencils)))
547 (let* ((tail (stack-stencil-line space (cdr stencils)))
548 (head (car stencils))
549 (xoff (+ space (cdr (ly:stencil-extent head X)))))
551 (ly:stencil-translate-axis tail xoff X)))
553 (ly:make-stencil '() '(0 . 0) '(0 . 0))))