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 ,(filter identity
114 (map (lambda (prop-spec)
115 (if (pair? prop-spec)
116 (let ((prop (car prop-spec))
117 (default-value (if (null? (cdr prop-spec))
121 `(,prop (chain-assoc-get ',prop ,props ,default-value)))
125 (let ((args (gensym "args"))
126 (markup-command (car body)))
127 `(define-public (,command-name . ,args)
128 ,(format #f "Copy of the ~a command." markup-command)
129 (apply ,markup-command ,args))))
130 (set! (markup-command-signature ,command-name) (list ,@signature))
131 ;; Register the new function, for markup documentation
132 ,@(map (lambda (category)
134 (or (hashq-ref markup-functions-by-category ',category)
135 (let ((hash (make-weak-key-hash-table 151)))
136 (hashq-set! markup-functions-by-category ',category
140 (if (list? category) category (list category)))
141 ;; Used properties, for markup documentation
142 (hashq-set! markup-functions-properties
144 (list ,@(map (lambda (prop-spec)
145 (cond ((symbol? prop-spec)
147 ((not (null? (cdr prop-spec)))
148 `(list ',(car prop-spec) ,(cadr prop-spec)))
150 `(list ',(car prop-spec)))))
154 ;; define the make-COMMAND-markup function
155 (define-public (,make-markup-name . args)
156 (let ((sig (list ,@signature)))
157 (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
159 (defmacro*-public define-markup-list-command
160 (command-and-args signature #:key (properties '()) #:rest body)
161 "Same as `define-markup-command, but defines a command that, when
162 interpreted, returns a list of stencils instead os a single one"
163 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
164 (args (if (pair? command-and-args) (cdr command-and-args) '()))
165 (command-name (string->symbol (format #f "~a-markup-list" command)))
166 (make-markup-name (string->symbol (format #f "make-~a-markup-list" command))))
167 (while (and (pair? body) (keyword? (car body)))
168 (set! body (cddr body)))
170 ;; define the COMMAND-markup-list function
172 (let* ((documentation (if (string? (car body))
175 (real-body (if (or (null? documentation)
178 `(define-public (,command-name ,@args)
180 (let ,(filter identity
181 (map (lambda (prop-spec)
182 (if (pair? prop-spec)
183 (let ((prop (car prop-spec))
184 (default-value (if (null? (cdr prop-spec))
188 `(,prop (chain-assoc-get ',prop ,props ,default-value)))
192 (let ((args (gensym "args"))
193 (markup-command (car body)))
194 `(define-public (,command-name . ,args)
195 ,(format #f "Copy of the ~a command." markup-command)
196 (apply ,markup-command ,args))))
197 (set! (markup-command-signature ,command-name) (list ,@signature))
198 ;; add the command to markup-list-function-list, for markup documentation
199 (hashq-set! markup-list-functions ,command-name #t)
200 ;; Used properties, for markup documentation
201 (hashq-set! markup-functions-properties
203 (list ,@(map (lambda (prop-spec)
204 (cond ((symbol? prop-spec)
206 ((not (null? (cdr prop-spec)))
207 `(list ',(car prop-spec) ,(cadr prop-spec)))
209 `(list ',(car prop-spec)))))
213 ;; it's a markup-list command:
214 (set-object-property! ,command-name 'markup-list-command #t)
215 ;; define the make-COMMAND-markup-list function
216 (define-public (,make-markup-name . args)
217 (let ((sig (list ,@signature)))
218 (list (make-markup ,command-name
219 ,(symbol->string make-markup-name) sig args)))))))
221 (define-public (make-markup markup-function make-name signature args)
222 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
223 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
225 (let* ((arglen (length args))
226 (siglen (length signature))
227 (error-msg (if (and (> siglen 0) (> arglen 0))
228 (markup-argument-list-error signature args 1)
230 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
231 (ly:error (string-append make-name ": "
232 (_ "Wrong number of arguments. Expect: ~A, found ~A: ~S"))
238 (_ "Invalid argument in position ~A. Expect: ~A, found: ~S."))
239 (car error-msg) (cadr error-msg)(caddr error-msg))
240 (cons markup-function args))))
242 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 ;;; markup constructors
244 ;;; lilypond-like syntax for markup construction in scheme.
246 (use-modules (ice-9 receive))
248 (defmacro*-public markup (#:rest body)
249 "The `markup' macro provides a lilypond-like syntax for building markups.
251 - #:COMMAND is used instead of \\COMMAND
252 - #:line ( ... ) is used instead of \\line { ... }
257 \\raise #0.2 \\hbracket \\bold bar
258 \\override #'(baseline-skip . 4)
259 \\bracket \\column { baz bazr bla }
263 #:raise 0.2 #:hbracket #:bold \"bar\"
264 #:override '(baseline-skip . 4)
265 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
266 Use `markup*' in a \\notemode context."
268 (car (compile-all-markup-expressions `(#:line ,body))))
270 (defmacro*-public markup* (#:rest body)
271 "Same as `markup', for use in a \\notes block."
272 `(ly:export (markup ,@body)))
275 (define (compile-all-markup-expressions expr)
276 "Return a list of canonical markups expressions, e.g.:
277 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
279 ((make-COMMAND1-markup arg11 arg12)
280 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
281 (do ((rest expr rest)
283 ((null? rest) (reverse markps))
284 (receive (m r) (compile-markup-expression rest)
285 (set! markps (cons m markps))
288 (define (keyword->make-markup key)
289 "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
290 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
292 (define (compile-markup-expression expr)
293 "Return two values: the first complete canonical markup expression
294 found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
295 and the rest expression."
296 (cond ((and (pair? expr)
297 (keyword? (car expr)))
298 ;; expr === (#:COMMAND arg1 ...)
299 (let ((command (symbol->string (keyword->symbol (car expr)))))
300 (if (not (pair? (lookup-markup-command command)))
301 (ly:error (_ "Not a markup command: ~A") command))
302 (let* ((sig (markup-command-signature
303 (car (lookup-markup-command command))))
304 (sig-len (length sig)))
307 (rest (cdr expr) rest))
309 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
310 (cond ((eqv? (list-ref sig i) markup-list?)
311 ;; (car rest) is a markup list
312 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
313 (set! rest (cdr rest)))
315 ;; pick up one arg in `rest'
316 (receive (a r) (compile-markup-arg rest)
317 (set! args (cons a args))
321 (keyword? (caar expr)))
322 ;; expr === ((#:COMMAND arg1 ...) ...)
323 (receive (m r) (compile-markup-expression (car expr))
324 (values m (cdr expr))))
326 (string? (car expr))) ;; expr === ("string" ...)
327 (values `(make-simple-markup ,(car expr)) (cdr expr)))
329 ;; expr === (symbol ...) or ((funcall ...) ...)
333 (define (compile-all-markup-args expr)
334 "Transform `expr' into markup arguments"
335 (do ((rest expr rest)
337 ((null? rest) (reverse args))
338 (receive (a r) (compile-markup-arg rest)
339 (set! args (cons a args))
342 (define (compile-markup-arg expr)
343 "Return two values: the desired markup argument, and the rest arguments"
347 ((keyword? (car expr))
348 ;; expr === (#:COMMAND ...)
349 ;; ==> build and return the whole markup expression
350 (compile-markup-expression expr))
351 ((and (pair? (car expr))
352 (keyword? (caar expr)))
353 ;; expr === ((#:COMMAND ...) ...)
354 ;; ==> build and return the whole markup expression(s)
355 ;; found in (car expr)
356 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
357 (if (null? rest-expr)
358 (values markup-expr (cdr expr))
359 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
361 ((and (pair? (car expr))
363 ;; expr === (((foo ...) ...) ...)
364 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
365 (else (values (car expr) (cdr expr)))))
368 ;;; Utilities for storing and accessing markup commands signature
371 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
372 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
374 ;;; (markup-command-signature raise-markup)
375 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
378 (define-public (markup-command-signature-ref markup-command)
379 "Return markup-command's signature (the 'markup-signature object property)"
380 (object-property markup-command 'markup-signature))
382 (define-public (markup-command-signature-set! markup-command signature)
383 "Set markup-command's signature (as object property)"
384 (set-object-property! markup-command 'markup-signature signature)
387 (define-public markup-command-signature
388 (make-procedure-with-setter markup-command-signature-ref
389 markup-command-signature-set!))
391 (define (lookup-markup-command-aux symbol)
392 (let ((proc (catch 'misc-error
394 (module-ref (current-module) symbol))
395 (lambda (key . args) #f))))
396 (and (procedure? proc) proc)))
398 (define-public (lookup-markup-command code)
399 (let ((proc (lookup-markup-command-aux
400 (string->symbol (format #f "~a-markup" code)))))
401 (and proc (markup-function? proc)
402 (cons proc (markup-command-signature proc)))))
404 (define-public (lookup-markup-list-command code)
405 (let ((proc (lookup-markup-command-aux
406 (string->symbol (format #f "~a-markup-list" code)))))
407 (and proc (markup-list-function? proc)
408 (cons proc (markup-command-signature proc)))))
410 ;;;;;;;;;;;;;;;;;;;;;;
411 ;;; used in parser.yy to map a list of markup commands on markup arguments
412 (define-public (map-markup-command-list commands markups)
413 "`markups' being a list of markups, eg (markup1 markup2 markup3),
414 and `commands' a list of commands with their scheme arguments, in reverse order,
415 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
416 ((bold (raise 4 (italic markup1)))
417 (bold (raise 4 (italic markup2)))
418 (bold (raise 4 (italic markup3))))
420 (map-in-order (lambda (arg)
422 (for-each (lambda (cmd)
423 (set! result (append cmd (list result))))
428 ;;;;;;;;;;;;;;;;;;;;;;
429 ;;; markup type predicates
431 (define (markup-function? x)
432 (and (markup-command-signature x)
433 (not (object-property x 'markup-list-command))))
435 (define (markup-list-function? x)
436 (and (markup-command-signature x)
437 (object-property x 'markup-list-command)))
439 (define-public (markup-command-list? x)
440 "Determine if `x' is a markup command list, ie. a list composed of
441 a markup list function and its arguments."
442 (and (pair? x) (markup-list-function? (car x))))
444 (define-public (markup-list? arg)
445 "Return a true value if `x' is a list of markups or markup command lists."
446 (define (markup-list-inner? lst)
448 (and (or (markup? (car lst)) (markup-command-list? (car lst)))
449 (markup-list-inner? (cdr lst)))))
450 (not (not (and (list? arg) (markup-list-inner? arg)))))
452 (define (markup-argument-list? signature arguments)
453 "Typecheck argument list."
454 (if (and (pair? signature) (pair? arguments))
455 (and ((car signature) (car arguments))
456 (markup-argument-list? (cdr signature) (cdr arguments)))
457 (and (null? signature) (null? arguments))))
460 (define (markup-argument-list-error signature arguments number)
461 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
462 #f is no error found.
464 (if (and (pair? signature) (pair? arguments))
465 (if (not ((car signature) (car arguments)))
466 (list number (type-name (car signature)) (car arguments))
467 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
471 ;; full recursive typecheck.
473 (define (markup-typecheck? arg)
476 (markup-function? (car arg))
477 (markup-argument-list? (markup-command-signature (car arg))
484 (define (markup-thrower-typecheck arg)
485 "typecheck, and throw an error when something amiss.
487 Uncovered - cheap-markup? is used."
489 (cond ((string? arg) #t)
491 (throw 'markup-format "Not a pair" arg))
492 ((not (markup-function? (car arg)))
493 (throw 'markup-format "Not a markup function " (car arg)))
494 ((not (markup-argument-list? (markup-command-signature (car arg))
496 (throw 'markup-format "Arguments failed typecheck for " arg)))
500 ;; good enough if you only use make-XXX-markup functions.
502 (define (cheap-markup? x)
505 (markup-function? (car x)))))
508 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
510 (define-public markup? cheap-markup?)
514 (define (markup-join markups sep)
515 "Return line-markup of MARKUPS, joining them with markup SEP"
517 (make-line-markup (list-insert-separator markups sep))
521 (define-public interpret-markup ly:text-interface::interpret-markup)
523 (define-public (interpret-markup-list layout props markup-list)
524 (let ((stencils (list)))
525 (for-each (lambda (m)
527 (if (markup-command-list? m)
528 (append! (reverse! (apply (car m) layout props (cdr m)))
530 (cons (interpret-markup layout props m) stencils))))
532 (reverse! stencils)))
534 (define-public (prepend-alist-chain key val chain)
535 (cons (acons key val (car chain)) (cdr chain)))
537 (define-public (stack-stencil-line space stencils)
539 (if (and (pair? stencils)
540 (ly:stencil? (car stencils)))
542 (if (and (pair? (cdr stencils))
543 (ly:stencil? (cadr stencils)))
544 (let* ((tail (stack-stencil-line space (cdr stencils)))
545 (head (car stencils))
546 (xoff (+ space (cdr (ly:stencil-extent head X)))))
548 (ly:stencil-translate-axis tail xoff X)))
550 (ly:make-stencil '() '(0 . 0) '(0 . 0))))