1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2003--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 Internally markup is stored as lists, whose head is a function.
21 (FUNCTION ARG1 ARG2 ... )
23 When the markup is formatted, then FUNCTION is called as follows
25 (FUNCTION GROB PROPS ARG1 ARG2 ... )
27 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
28 the rest of the arguments.
30 The function should return a stencil (i.e. a formatted, ready to
34 To add a builtin markup command, use the define-builtin-markup-command
35 utility. In a user file, the define-markup-command macro shall be used
36 (see ly/markup-init.ly).
38 (define-markup-command (mycommand layout prop arg1 ...) (arg1-type? ...)
39 \"my command usage and description\"
42 The command is now available in markup mode, e.g.
44 \\markup { .... \\MYCOMMAND #1 argument ... }
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 ;;; markup definer utilities
51 ;; For documentation purposes
52 ;; category -> markup functions
53 (define-public markup-functions-by-category (make-hash-table 150))
54 ;; markup function -> used properties
55 (define-public markup-functions-properties (make-hash-table 150))
56 ;; List of markup list functions
57 (define-public markup-list-function-list (list))
59 (define-macro (define-builtin-markup-command command-and-args signature
60 category properties-or-copied-function . body)
62 * Define a COMMAND-markup function after command-and-args and body,
63 register COMMAND-markup and its signature,
65 * add COMMAND-markup to markup-functions-by-category,
67 * sets COMMAND-markup markup-signature and markup-keyword object properties,
69 * define a make-COMMAND-markup function.
72 (define-builtin-markup-command (COMMAND layout props . arguments)
76 \"documentation string\"
79 (define-builtin-markup-command COMMAND
85 argument-types is a list of type predicates for arguments
86 category is either a symbol or a symbol list
87 properties a list of (property default-value) lists or COMMANDx-markup elements
88 (when a COMMANDx-markup is found, the properties of the said commandx are
89 added instead). No check is performed against cyclical references!
91 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
92 (args (if (pair? command-and-args) (cdr command-and-args) '()))
93 (command-name (string->symbol (format #f "~a-markup" command)))
94 (make-markup-name (string->symbol (format #f "make-~a-markup" command))))
96 ;; define the COMMAND-markup function
98 (let ((documentation (car body))
99 (real-body (cdr body))
100 (properties properties-or-copied-function))
101 `(define-public (,command-name ,@args)
103 (let ,(filter identity
104 (map (lambda (prop-spec)
105 (if (pair? prop-spec)
106 (let ((prop (car prop-spec))
107 (default-value (if (null? (cdr prop-spec))
111 `(,prop (chain-assoc-get ',prop ,props ,default-value)))
115 (let ((args (gensym "args"))
116 (markup-command properties-or-copied-function))
117 `(define-public (,command-name . ,args)
118 ,(format #f "Copy of the ~a command." markup-command)
119 (apply ,markup-command ,args))))
120 (set! (markup-command-signature ,command-name) (list ,@signature))
121 ;; Register the new function, for markup documentation
122 ,@(map (lambda (category)
123 `(hashq-set! markup-functions-by-category ',category
125 (or (hashq-ref markup-functions-by-category ',category)
127 (if (list? category) category (list category)))
128 ;; Used properties, for markup documentation
129 (hashq-set! markup-functions-properties
131 (list ,@(map (lambda (prop-spec)
132 (cond ((symbol? prop-spec)
134 ((not (null? (cdr prop-spec)))
135 `(list ',(car prop-spec) ,(cadr prop-spec)))
137 `(list ',(car prop-spec)))))
139 properties-or-copied-function
141 ;; define the make-COMMAND-markup function
142 (define-public (,make-markup-name . args)
143 (let ((sig (list ,@signature)))
144 (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
146 (define-macro (define-builtin-markup-list-command command-and-args signature
148 "Same as `define-builtin-markup-command, but defines a command that, when
149 interpreted, returns a list of stencils instead os a single one"
150 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
151 (args (if (pair? command-and-args) (cdr command-and-args) '()))
152 (command-name (string->symbol (format #f "~a-markup-list" command)))
153 (make-markup-name (string->symbol (format #f "make-~a-markup-list" command))))
155 ;; define the COMMAND-markup-list function
157 (let ((documentation (car body))
158 (real-body (cdr body)))
159 `(define-public (,command-name ,@args)
161 (let ,(filter identity
162 (map (lambda (prop-spec)
163 (if (pair? prop-spec)
164 (let ((prop (car prop-spec))
165 (default-value (if (null? (cdr prop-spec))
169 `(,prop (chain-assoc-get ',prop ,props ,default-value)))
173 (let ((args (gensym "args"))
174 (markup-command (car body)))
175 `(define-public (,command-name . ,args)
176 ,(format #f "Copy of the ~a command." markup-command)
177 (apply ,markup-command ,args))))
178 (set! (markup-command-signature ,command-name) (list ,@signature))
179 ;; add the command to markup-list-function-list, for markup documentation
180 (if (not (member ,command-name markup-list-function-list))
181 (set! markup-list-function-list (cons ,command-name
182 markup-list-function-list)))
183 ;; Used properties, for markup documentation
184 (hashq-set! markup-functions-properties
186 (list ,@(map (lambda (prop-spec)
187 (cond ((symbol? prop-spec)
189 ((not (null? (cdr prop-spec)))
190 `(list ',(car prop-spec) ,(cadr prop-spec)))
192 `(list ',(car prop-spec)))))
196 ;; it's a markup-list command:
197 (set-object-property! ,command-name 'markup-list-command #t)
198 ;; define the make-COMMAND-markup-list function
199 (define-public (,make-markup-name . args)
200 (let ((sig (list ,@signature)))
201 (list (make-markup ,command-name
202 ,(symbol->string make-markup-name) sig args)))))))
204 (define-public (make-markup markup-function make-name signature args)
205 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
206 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
208 (let* ((arglen (length args))
209 (siglen (length signature))
210 (error-msg (if (and (> siglen 0) (> arglen 0))
211 (markup-argument-list-error signature args 1)
213 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
214 (ly:error (string-append make-name ": "
215 (_ "Wrong number of arguments. Expect: ~A, found ~A: ~S"))
221 (_ "Invalid argument in position ~A. Expect: ~A, found: ~S."))
222 (car error-msg) (cadr error-msg)(caddr error-msg))
223 (cons markup-function args))))
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 ;;; markup constructors
227 ;;; lilypond-like syntax for markup construction in scheme.
229 (use-modules (ice-9 optargs)
232 (defmacro*-public markup (#:rest body)
233 "The `markup' macro provides a lilypond-like syntax for building markups.
235 - #:COMMAND is used instead of \\COMMAND
236 - #:line ( ... ) is used instead of \\line { ... }
241 \\raise #0.2 \\hbracket \\bold bar
242 \\override #'(baseline-skip . 4)
243 \\bracket \\column { baz bazr bla }
247 #:raise 0.2 #:hbracket #:bold \"bar\"
248 #:override '(baseline-skip . 4)
249 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
250 Use `markup*' in a \\notemode context."
252 (car (compile-all-markup-expressions `(#:line ,body))))
254 (defmacro*-public markup* (#:rest body)
255 "Same as `markup', for use in a \\notes block."
256 `(ly:export (markup ,@body)))
259 (define (compile-all-markup-expressions expr)
260 "Return a list of canonical markups expressions, e.g.:
261 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
263 ((make-COMMAND1-markup arg11 arg12)
264 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
265 (do ((rest expr rest)
267 ((null? rest) (reverse markps))
268 (receive (m r) (compile-markup-expression rest)
269 (set! markps (cons m markps))
272 (define (keyword->make-markup key)
273 "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
274 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
276 (define (compile-markup-expression expr)
277 "Return two values: the first complete canonical markup expression
278 found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
279 and the rest expression."
280 (cond ((and (pair? expr)
281 (keyword? (car expr)))
282 ;; expr === (#:COMMAND arg1 ...)
283 (let ((command (symbol->string (keyword->symbol (car expr)))))
284 (if (not (pair? (lookup-markup-command command)))
285 (ly:error (_ "Not a markup command: ~A") command))
286 (let* ((sig (markup-command-signature
287 (car (lookup-markup-command command))))
288 (sig-len (length sig)))
291 (rest (cdr expr) rest))
293 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
294 (cond ((eqv? (list-ref sig i) markup-list?)
295 ;; (car rest) is a markup list
296 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
297 (set! rest (cdr rest)))
299 ;; pick up one arg in `rest'
300 (receive (a r) (compile-markup-arg rest)
301 (set! args (cons a args))
305 (keyword? (caar expr)))
306 ;; expr === ((#:COMMAND arg1 ...) ...)
307 (receive (m r) (compile-markup-expression (car expr))
308 (values m (cdr expr))))
310 (string? (car expr))) ;; expr === ("string" ...)
311 (values `(make-simple-markup ,(car expr)) (cdr expr)))
313 ;; expr === (symbol ...) or ((funcall ...) ...)
317 (define (compile-all-markup-args expr)
318 "Transform `expr' into markup arguments"
319 (do ((rest expr rest)
321 ((null? rest) (reverse args))
322 (receive (a r) (compile-markup-arg rest)
323 (set! args (cons a args))
326 (define (compile-markup-arg expr)
327 "Return two values: the desired markup argument, and the rest arguments"
331 ((keyword? (car expr))
332 ;; expr === (#:COMMAND ...)
333 ;; ==> build and return the whole markup expression
334 (compile-markup-expression expr))
335 ((and (pair? (car expr))
336 (keyword? (caar expr)))
337 ;; expr === ((#:COMMAND ...) ...)
338 ;; ==> build and return the whole markup expression(s)
339 ;; found in (car expr)
340 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
341 (if (null? rest-expr)
342 (values markup-expr (cdr expr))
343 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
345 ((and (pair? (car expr))
347 ;; expr === (((foo ...) ...) ...)
348 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
349 (else (values (car expr) (cdr expr)))))
352 ;;; Utilities for storing and accessing markup commands signature
356 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
357 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
359 ;;; (markup-command-signature raise-markup)
360 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
362 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
365 (define-public (markup-command-keyword markup-command)
366 "Return markup-command's argument keyword, ie a string describing the command
367 arguments, eg. \"scheme0markup1\""
368 (object-property markup-command 'markup-keyword))
370 (define-public (markup-command-signature-ref markup-command)
371 "Return markup-command's signature (the 'markup-signature object property)"
372 (object-property markup-command 'markup-signature))
374 (define-public (markup-command-signature-set! markup-command signature)
375 "Set markup-command's signature and keyword (as object properties)"
376 (set-object-property! markup-command 'markup-signature signature)
377 (set-object-property! markup-command 'markup-keyword
378 (markup-signature-to-keyword signature))
381 (define-public markup-command-signature
382 (make-procedure-with-setter markup-command-signature-ref
383 markup-command-signature-set!))
385 (define-public (markup-signature-to-keyword sig)
386 " (A B C) -> a0-b1-c2 "
389 (string->symbol (string-join (map
392 (set! count (+ count 1))
394 ;; for reasons I don't get,
395 ;; (case func ((markup?) .. )
398 ((eq? func markup?) "markup")
399 ((eq? func markup-list?) "markup-list")
401 (number->string (- count 1)))))
405 (define (lookup-markup-command-aux symbol)
406 (let ((proc (catch 'misc-error
408 (module-ref (current-module) symbol))
409 (lambda (key . args) #f))))
410 (and (procedure? proc) proc)))
412 (define-public (lookup-markup-command code)
413 (let ((proc (lookup-markup-command-aux
414 (string->symbol (format #f "~a-markup" code)))))
415 (and proc (markup-function? proc)
416 (cons proc (markup-command-keyword proc)))))
418 (define-public (lookup-markup-list-command code)
419 (let ((proc (lookup-markup-command-aux
420 (string->symbol (format #f "~a-markup-list" code)))))
421 (and proc (markup-list-function? proc)
422 (cons proc (markup-command-keyword proc)))))
424 ;;;;;;;;;;;;;;;;;;;;;;
425 ;;; used in parser.yy to map a list of markup commands on markup arguments
426 (define-public (map-markup-command-list commands markups)
427 "`markups' being a list of markups, eg (markup1 markup2 markup3),
428 and `commands' a list of commands with their scheme arguments, in reverse order,
429 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
430 ((bold (raise 4 (italic markup1)))
431 (bold (raise 4 (italic markup2)))
432 (bold (raise 4 (italic markup3))))
434 (map-in-order (lambda (arg)
436 (for-each (lambda (cmd)
437 (set! result (append cmd (list result))))
442 ;;;;;;;;;;;;;;;;;;;;;;
443 ;;; markup type predicates
445 (define (markup-function? x)
446 (and (markup-command-signature x)
447 (not (object-property x 'markup-list-command))))
449 (define (markup-list-function? x)
450 (and (markup-command-signature x)
451 (object-property x 'markup-list-command)))
453 (define-public (markup-command-list? x)
454 "Determine if `x' is a markup command list, ie. a list composed of
455 a markup list function and its arguments."
456 (and (pair? x) (markup-list-function? (car x))))
458 (define-public (markup-list? arg)
459 "Return a true value if `x' is a list of markups or markup command lists."
460 (define (markup-list-inner? lst)
462 (and (or (markup? (car lst)) (markup-command-list? (car lst)))
463 (markup-list-inner? (cdr lst)))))
464 (not (not (and (list? arg) (markup-list-inner? arg)))))
466 (define (markup-argument-list? signature arguments)
467 "Typecheck argument list."
468 (if (and (pair? signature) (pair? arguments))
469 (and ((car signature) (car arguments))
470 (markup-argument-list? (cdr signature) (cdr arguments)))
471 (and (null? signature) (null? arguments))))
474 (define (markup-argument-list-error signature arguments number)
475 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
476 #f is no error found.
478 (if (and (pair? signature) (pair? arguments))
479 (if (not ((car signature) (car arguments)))
480 (list number (type-name (car signature)) (car arguments))
481 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
485 ;; full recursive typecheck.
487 (define (markup-typecheck? arg)
490 (markup-function? (car arg))
491 (markup-argument-list? (markup-command-signature (car arg))
498 (define (markup-thrower-typecheck arg)
499 "typecheck, and throw an error when something amiss.
501 Uncovered - cheap-markup? is used."
503 (cond ((string? arg) #t)
505 (throw 'markup-format "Not a pair" arg))
506 ((not (markup-function? (car arg)))
507 (throw 'markup-format "Not a markup function " (car arg)))
508 ((not (markup-argument-list? (markup-command-signature (car arg))
510 (throw 'markup-format "Arguments failed typecheck for " arg)))
514 ;; good enough if you only use make-XXX-markup functions.
516 (define (cheap-markup? x)
519 (markup-function? (car x)))))
522 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
524 (define-public markup? cheap-markup?)
528 (define (markup-join markups sep)
529 "Return line-markup of MARKUPS, joining them with markup SEP"
531 (make-line-markup (list-insert-separator markups sep))
535 (define-public interpret-markup ly:text-interface::interpret-markup)
537 (define-public (interpret-markup-list layout props markup-list)
538 (let ((stencils (list)))
539 (for-each (lambda (m)
541 (if (markup-command-list? m)
542 (append! (reverse! (apply (car m) layout props (cdr m)))
544 (cons (interpret-markup layout props m) stencils))))
546 (reverse! stencils)))
548 (define-public (prepend-alist-chain key val chain)
549 (cons (acons key val (car chain)) (cdr chain)))
551 (define-public (stack-stencil-line space stencils)
553 (if (and (pair? stencils)
554 (ly:stencil? (car stencils)))
556 (if (and (pair? (cdr stencils))
557 (ly:stencil? (cadr stencils)))
558 (let* ((tail (stack-stencil-line space (cdr stencils)))
559 (head (car stencils))
560 (xoff (+ space (cdr (ly:stencil-extent head X)))))
562 (ly:stencil-translate-axis tail xoff X)))
564 (ly:make-stencil '() '(0 . 0) '(0 . 0))))