1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2003--2010 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 markup command, use the define-markup-command utility.
36 (define-markup-command (mycommand layout prop arg1 ...) (arg1-type? ...)
37 \"my command usage and description\"
40 The command is now available in markup mode, e.g.
42 \\markup { .... \\MYCOMMAND #1 argument ... }
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 ;;; markup definer utilities
49 ;; For documentation purposes
50 ;; category -> markup functions
51 (define-public markup-functions-by-category (make-hash-table 150))
52 ;; markup function -> used properties
53 (define-public markup-functions-properties (make-weak-key-hash-table 151))
54 ;; List of markup list functions
55 (define-public markup-list-functions (make-weak-key-hash-table 151))
57 (use-modules (ice-9 optargs))
59 (defmacro*-public define-markup-command
60 (command-and-args signature
61 #:key (category '()) (properties '())
64 * Define a COMMAND-markup function after command-and-args and body,
65 register COMMAND-markup and its signature,
67 * add COMMAND-markup to markup-functions-by-category,
69 * sets COMMAND-markup markup-signature object property,
71 * define a make-COMMAND-markup function.
74 (define-markup-command (COMMAND layout props . arguments)
76 [ #:category category ]
77 [ #:properties properties ]
78 \"documentation string\"
81 (define-markup-command COMMAND
83 [ #:category category ]
87 argument-types is a list of type predicates for arguments
88 category is either a symbol or a symbol list
89 properties a list of (property default-value) lists or COMMANDx-markup elements
90 (when a COMMANDx-markup is found, the properties of the said commandx are
91 added instead). No check is performed against cyclical references!
93 The specified properties are available as let-bound variables in the
96 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
97 (args (if (pair? command-and-args) (cdr command-and-args) '()))
98 (command-name (string->symbol (format #f "~a-markup" command)))
99 (make-markup-name (string->symbol (format #f "make-~a-markup" command))))
100 (while (and (pair? body) (keyword? (car body)))
101 (set! body (cddr body)))
103 ;; define the COMMAND-markup function
105 (let* ((documentation (if (string? (car body))
108 (real-body (if (or (null? documentation)
111 `(define-public (,command-name ,@args)
113 (let ,(map (lambda (prop-spec)
114 (let ((prop (car prop-spec))
115 (default-value (if (null? (cdr prop-spec))
119 `(,prop (chain-assoc-get ',prop ,props ,default-value))))
120 (filter pair? properties))
122 (let ((args (gensym "args"))
123 (markup-command (car body)))
124 `(define-public (,command-name . ,args)
125 ,(format #f "Copy of the ~a command." markup-command)
126 (apply ,markup-command ,args))))
127 (set! (markup-command-signature ,command-name) (list ,@signature))
128 ;; Register the new function, for markup documentation
129 ,@(map (lambda (category)
131 (or (hashq-ref markup-functions-by-category ',category)
132 (let ((hash (make-weak-key-hash-table 151)))
133 (hashq-set! markup-functions-by-category ',category
137 (if (list? category) category (list category)))
138 ;; Used properties, for markup documentation
139 (hashq-set! markup-functions-properties
141 (list ,@(map (lambda (prop-spec)
142 (cond ((symbol? prop-spec)
144 ((not (null? (cdr prop-spec)))
145 `(list ',(car prop-spec) ,(cadr prop-spec)))
147 `(list ',(car prop-spec)))))
151 ;; define the make-COMMAND-markup function
152 (define-public (,make-markup-name . args)
153 (let ((sig (list ,@signature)))
154 (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
156 (defmacro*-public define-markup-list-command
157 (command-and-args signature #:key (properties '()) #:rest body)
158 "Same as `define-markup-command, but defines a command that, when
159 interpreted, returns a list of stencils instead os a single one"
160 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
161 (args (if (pair? command-and-args) (cdr command-and-args) '()))
162 (command-name (string->symbol (format #f "~a-markup-list" command)))
163 (make-markup-name (string->symbol (format #f "make-~a-markup-list" command))))
164 (while (and (pair? body) (keyword? (car body)))
165 (set! body (cddr body)))
167 ;; define the COMMAND-markup-list function
169 (let* ((documentation (if (string? (car body))
172 (real-body (if (or (null? documentation)
175 `(define-public (,command-name ,@args)
177 (let ,(map (lambda (prop-spec)
178 (let ((prop (car prop-spec))
179 (default-value (if (null? (cdr prop-spec))
183 `(,prop (chain-assoc-get ',prop ,props ,default-value))))
184 (filter pair? properties))
186 (let ((args (gensym "args"))
187 (markup-command (car body)))
188 `(define-public (,command-name . ,args)
189 ,(format #f "Copy of the ~a command." markup-command)
190 (apply ,markup-command ,args))))
191 (set! (markup-command-signature ,command-name) (list ,@signature))
192 ;; add the command to markup-list-function-list, for markup documentation
193 (hashq-set! markup-list-functions ,command-name #t)
194 ;; Used properties, for markup documentation
195 (hashq-set! markup-functions-properties
197 (list ,@(map (lambda (prop-spec)
198 (cond ((symbol? prop-spec)
200 ((not (null? (cdr prop-spec)))
201 `(list ',(car prop-spec) ,(cadr prop-spec)))
203 `(list ',(car prop-spec)))))
207 ;; it's a markup-list command:
208 (set-object-property! ,command-name 'markup-list-command #t)
209 ;; define the make-COMMAND-markup-list function
210 (define-public (,make-markup-name . args)
211 (let ((sig (list ,@signature)))
212 (list (make-markup ,command-name
213 ,(symbol->string make-markup-name) sig args)))))))
215 (define-public (make-markup markup-function make-name signature args)
216 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
217 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
219 (let* ((arglen (length args))
220 (siglen (length signature))
221 (error-msg (if (and (> siglen 0) (> arglen 0))
222 (markup-argument-list-error signature args 1)
224 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
225 (ly:error (string-append make-name ": "
226 (_ "Wrong number of arguments. Expect: ~A, found ~A: ~S"))
232 (_ "Invalid argument in position ~A. Expect: ~A, found: ~S."))
233 (car error-msg) (cadr error-msg)(caddr error-msg))
234 (cons markup-function args))))
236 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
237 ;;; markup constructors
238 ;;; lilypond-like syntax for markup construction in scheme.
240 (use-modules (ice-9 receive))
242 (defmacro*-public markup (#:rest body)
243 "The `markup' macro provides a lilypond-like syntax for building markups.
245 - #:COMMAND is used instead of \\COMMAND
246 - #:line ( ... ) is used instead of \\line { ... }
251 \\raise #0.2 \\hbracket \\bold bar
252 \\override #'(baseline-skip . 4)
253 \\bracket \\column { baz bazr bla }
257 #:raise 0.2 #:hbracket #:bold \"bar\"
258 #:override '(baseline-skip . 4)
259 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
260 Use `markup*' in a \\notemode context."
262 (car (compile-all-markup-expressions `(#:line ,body))))
264 (defmacro*-public markup* (#:rest body)
265 "Same as `markup', for use in a \\notes block."
266 `(ly:export (markup ,@body)))
269 (define (compile-all-markup-expressions expr)
270 "Return a list of canonical markups expressions, e.g.:
271 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
273 ((make-COMMAND1-markup arg11 arg12)
274 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
275 (do ((rest expr rest)
277 ((null? rest) (reverse markps))
278 (receive (m r) (compile-markup-expression rest)
279 (set! markps (cons m markps))
282 (define (keyword->make-markup key)
283 "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
284 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
286 (define (compile-markup-expression expr)
287 "Return two values: the first complete canonical markup expression
288 found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
289 and the rest expression."
290 (cond ((and (pair? expr)
291 (keyword? (car expr)))
292 ;; expr === (#:COMMAND arg1 ...)
293 (let ((command (symbol->string (keyword->symbol (car expr)))))
294 (if (not (pair? (lookup-markup-command command)))
295 (ly:error (_ "Not a markup command: ~A") command))
296 (let* ((sig (markup-command-signature
297 (car (lookup-markup-command command))))
298 (sig-len (length sig)))
301 (rest (cdr expr) rest))
303 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
304 (cond ((eqv? (list-ref sig i) markup-list?)
305 ;; (car rest) is a markup list
306 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
307 (set! rest (cdr rest)))
309 ;; pick up one arg in `rest'
310 (receive (a r) (compile-markup-arg rest)
311 (set! args (cons a args))
315 (keyword? (caar expr)))
316 ;; expr === ((#:COMMAND arg1 ...) ...)
317 (receive (m r) (compile-markup-expression (car expr))
318 (values m (cdr expr))))
320 (string? (car expr))) ;; expr === ("string" ...)
321 (values `(make-simple-markup ,(car expr)) (cdr expr)))
323 ;; expr === (symbol ...) or ((funcall ...) ...)
327 (define (compile-all-markup-args expr)
328 "Transform `expr' into markup arguments"
329 (do ((rest expr rest)
331 ((null? rest) (reverse args))
332 (receive (a r) (compile-markup-arg rest)
333 (set! args (cons a args))
336 (define (compile-markup-arg expr)
337 "Return two values: the desired markup argument, and the rest arguments"
341 ((keyword? (car expr))
342 ;; expr === (#:COMMAND ...)
343 ;; ==> build and return the whole markup expression
344 (compile-markup-expression expr))
345 ((and (pair? (car expr))
346 (keyword? (caar expr)))
347 ;; expr === ((#:COMMAND ...) ...)
348 ;; ==> build and return the whole markup expression(s)
349 ;; found in (car expr)
350 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
351 (if (null? rest-expr)
352 (values markup-expr (cdr expr))
353 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
355 ((and (pair? (car expr))
357 ;; expr === (((foo ...) ...) ...)
358 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
359 (else (values (car expr) (cdr expr)))))
362 ;;; Utilities for storing and accessing markup commands signature
365 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
366 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
368 ;;; (markup-command-signature raise-markup)
369 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
372 (define-public (markup-command-signature-ref markup-command)
373 "Return markup-command's signature (the 'markup-signature object property)"
374 (object-property markup-command 'markup-signature))
376 (define-public (markup-command-signature-set! markup-command signature)
377 "Set markup-command's signature (as object property)"
378 (set-object-property! markup-command 'markup-signature signature)
381 (define-public markup-command-signature
382 (make-procedure-with-setter markup-command-signature-ref
383 markup-command-signature-set!))
385 (define (lookup-markup-command-aux symbol)
386 (let ((proc (catch 'misc-error
388 (module-ref (current-module) symbol))
389 (lambda (key . args) #f))))
390 (and (procedure? proc) proc)))
392 (define-public (lookup-markup-command code)
393 (let ((proc (lookup-markup-command-aux
394 (string->symbol (format #f "~a-markup" code)))))
395 (and proc (markup-function? proc)
396 (cons proc (markup-command-signature proc)))))
398 (define-public (lookup-markup-list-command code)
399 (let ((proc (lookup-markup-command-aux
400 (string->symbol (format #f "~a-markup-list" code)))))
401 (and proc (markup-list-function? proc)
402 (cons proc (markup-command-signature proc)))))
404 ;;;;;;;;;;;;;;;;;;;;;;
405 ;;; used in parser.yy to map a list of markup commands on markup arguments
406 (define-public (map-markup-command-list commands markups)
407 "`markups' being a list of markups, eg (markup1 markup2 markup3),
408 and `commands' a list of commands with their scheme arguments, in reverse order,
409 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
410 ((bold (raise 4 (italic markup1)))
411 (bold (raise 4 (italic markup2)))
412 (bold (raise 4 (italic markup3))))
414 (map-in-order (lambda (arg)
416 (for-each (lambda (cmd)
417 (set! result (append cmd (list result))))
422 ;;;;;;;;;;;;;;;;;;;;;;
423 ;;; markup type predicates
425 (define (markup-function? x)
426 (and (markup-command-signature x)
427 (not (object-property x 'markup-list-command))))
429 (define (markup-list-function? x)
430 (and (markup-command-signature x)
431 (object-property x 'markup-list-command)))
433 (define-public (markup-command-list? x)
434 "Determine if `x' is a markup command list, ie. a list composed of
435 a markup list function and its arguments."
436 (and (pair? x) (markup-list-function? (car x))))
438 (define-public (markup-list? arg)
439 "Return a true value if `x' is a list of markups or markup command lists."
440 (define (markup-list-inner? lst)
442 (and (or (markup? (car lst)) (markup-command-list? (car lst)))
443 (markup-list-inner? (cdr lst)))))
444 (not (not (and (list? arg) (markup-list-inner? arg)))))
446 (define (markup-argument-list? signature arguments)
447 "Typecheck argument list."
448 (if (and (pair? signature) (pair? arguments))
449 (and ((car signature) (car arguments))
450 (markup-argument-list? (cdr signature) (cdr arguments)))
451 (and (null? signature) (null? arguments))))
454 (define (markup-argument-list-error signature arguments number)
455 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
456 #f is no error found.
458 (if (and (pair? signature) (pair? arguments))
459 (if (not ((car signature) (car arguments)))
460 (list number (type-name (car signature)) (car arguments))
461 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
465 ;; full recursive typecheck.
467 (define (markup-typecheck? arg)
470 (markup-function? (car arg))
471 (markup-argument-list? (markup-command-signature (car arg))
478 (define (markup-thrower-typecheck arg)
479 "typecheck, and throw an error when something amiss.
481 Uncovered - cheap-markup? is used."
483 (cond ((string? arg) #t)
485 (throw 'markup-format "Not a pair" arg))
486 ((not (markup-function? (car arg)))
487 (throw 'markup-format "Not a markup function " (car arg)))
488 ((not (markup-argument-list? (markup-command-signature (car arg))
490 (throw 'markup-format "Arguments failed typecheck for " arg)))
494 ;; good enough if you only use make-XXX-markup functions.
496 (define (cheap-markup? x)
499 (markup-function? (car x)))))
502 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
504 (define-public markup? cheap-markup?)
508 (define (markup-join markups sep)
509 "Return line-markup of MARKUPS, joining them with markup SEP"
511 (make-line-markup (list-insert-separator markups sep))
515 (define-public interpret-markup ly:text-interface::interpret-markup)
517 (define-public (interpret-markup-list layout props markup-list)
518 (let ((stencils (list)))
519 (for-each (lambda (m)
521 (if (markup-command-list? m)
522 (append! (reverse! (apply (car m) layout props (cdr m)))
524 (cons (interpret-markup layout props m) stencils))))
526 (reverse! stencils)))
528 (define-public (prepend-alist-chain key val chain)
529 (cons (acons key val (car chain)) (cdr chain)))
531 (define-public (stack-stencil-line space stencils)
533 (if (and (pair? stencils)
534 (ly:stencil? (car stencils)))
536 (if (and (pair? (cdr stencils))
537 (ly:stencil? (cadr stencils)))
538 (let* ((tail (stack-stencil-line space (cdr stencils)))
539 (head (car stencils))
540 (xoff (+ space (cdr (ly:stencil-extent head X)))))
542 (ly:stencil-translate-axis tail xoff X)))
544 (ly:make-stencil '() '(0 . 0) '(0 . 0))))