1 ;;;; markup.scm -- Implement a user extensible markup scheme.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2003--2007 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 (define-macro (define-builtin-markup-command command-and-args signature . body)
42 * Define a COMMAND-markup function after command-and-args and body,
43 register COMMAND-markup and its signature,
45 * add COMMAND-markup to markup-function-list,
47 * sets COMMAND-markup markup-signature and markup-keyword object properties,
49 * define a make-COMMAND-markup function.
52 (define-builtin-markup-command (COMMAND layout props arg1 arg2 ...)
53 (arg1-type? arg2-type? ...)
54 \"documentation string\"
57 (define-builtin-markup-command COMMAND (arg1-type? arg2-type? ...)
60 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
61 (args (if (pair? command-and-args) (cdr command-and-args) '()))
62 (command-name (string->symbol (format #f "~a-markup" command)))
63 (make-markup-name (string->symbol (format #f "make-~a-markup" command))))
65 ;; define the COMMAND-markup function
67 `(define-public (,command-name ,@args)
69 (let ((args (gensym "args"))
70 (markup-command (car body)))
71 `(define-public (,command-name . ,args)
72 ,(format #f "Copy of the ~a command." markup-command)
73 (apply ,markup-command ,args))))
74 (set! (markup-command-signature ,command-name) (list ,@signature))
75 ;; add the command to markup-function-list, for markup documentation
76 (if (not (member ,command-name markup-function-list))
77 (set! markup-function-list (cons ,command-name markup-function-list)))
78 ;; define the make-COMMAND-markup function
79 (define-public (,make-markup-name . args)
80 (let ((sig (list ,@signature)))
81 (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
83 (define-macro (define-builtin-markup-list-command command-and-args signature . body)
84 "Same as `define-builtin-markup-command, but defines a command that, when
85 interpreted, returns a list of stencils instead os a single one"
86 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
87 (args (if (pair? command-and-args) (cdr command-and-args) '()))
88 (command-name (string->symbol (format #f "~a-markup-list" command)))
89 (make-markup-name (string->symbol (format #f "make-~a-markup-list" command))))
91 ;; define the COMMAND-markup-list function
93 `(define-public (,command-name ,@args)
95 (let ((args (gensym "args"))
96 (markup-command (car body)))
97 `(define-public (,command-name . ,args)
98 ,(format #f "Copy of the ~a command." markup-command)
99 (apply ,markup-command ,args))))
100 (set! (markup-command-signature ,command-name) (list ,@signature))
101 ;; add the command to markup-list-function-list, for markup documentation
102 (if (not (member ,command-name markup-list-function-list))
103 (set! markup-list-function-list (cons ,command-name
104 markup-list-function-list)))
105 ;; it's a markup-list command:
106 (set-object-property! ,command-name 'markup-list-command #t)
107 ;; define the make-COMMAND-markup-list function
108 (define-public (,make-markup-name . args)
109 (let ((sig (list ,@signature)))
110 (list (make-markup ,command-name
111 ,(symbol->string make-markup-name) sig args)))))))
113 (define-public (make-markup markup-function make-name signature args)
114 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
115 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
117 (let* ((arglen (length args))
118 (siglen (length signature))
119 (error-msg (if (and (> siglen 0) (> arglen 0))
120 (markup-argument-list-error signature args 1)
122 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
123 (ly:error (string-append make-name ": "
124 (_ "Wrong number of arguments. Expect: ~A, found ~A: ~S"))
130 (_ "Invalid argument in position ~A. Expect: ~A, found: ~S.")
132 (cons markup-function args))))
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 ;;; markup constructors
136 ;;; lilypond-like syntax for markup construction in scheme.
138 (use-modules (ice-9 optargs)
141 (defmacro*-public markup (#:rest body)
142 "The `markup' macro provides a lilypond-like syntax for building markups.
144 - #:COMMAND is used instead of \\COMMAND
145 - #:line ( ... ) is used instead of \\line { ... }
150 \\raise #0.2 \\hbracket \\bold bar
151 \\override #'(baseline-skip . 4)
152 \\bracket \\column { baz bazr bla }
156 #:raise 0.2 #:hbracket #:bold \"bar\"
157 #:override '(baseline-skip . 4)
158 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
159 Use `markup*' in a \\notemode context."
161 (car (compile-all-markup-expressions `(#:line ,body))))
163 (defmacro*-public markup* (#:rest body)
164 "Same as `markup', for use in a \\notes block."
165 `(ly:export (markup ,@body)))
168 (define (compile-all-markup-expressions expr)
169 "Return a list of canonical markups expressions, e.g.:
170 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
172 ((make-COMMAND1-markup arg11 arg12)
173 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
174 (do ((rest expr rest)
176 ((null? rest) (reverse markps))
177 (receive (m r) (compile-markup-expression rest)
178 (set! markps (cons m markps))
181 (define (keyword->make-markup key)
182 "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
183 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
185 (define (compile-markup-expression expr)
186 "Return two values: the first complete canonical markup expression
187 found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
188 and the rest expression."
189 (cond ((and (pair? expr)
190 (keyword? (car expr)))
191 ;; expr === (#:COMMAND arg1 ...)
192 (let ((command (symbol->string (keyword->symbol (car expr)))))
193 (if (not (pair? (lookup-markup-command command)))
194 (ly:error (_ "Not a markup command: ~A") command))
195 (let* ((sig (markup-command-signature
196 (car (lookup-markup-command command))))
197 (sig-len (length sig)))
200 (rest (cdr expr) rest))
202 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
203 (cond ((eqv? (list-ref sig i) markup-list?)
204 ;; (car rest) is a markup list
205 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
206 (set! rest (cdr rest)))
208 ;; pick up one arg in `rest'
209 (receive (a r) (compile-markup-arg rest)
210 (set! args (cons a args))
214 (keyword? (caar expr)))
215 ;; expr === ((#:COMMAND arg1 ...) ...)
216 (receive (m r) (compile-markup-expression (car expr))
217 (values m (cdr expr))))
219 (string? (car expr))) ;; expr === ("string" ...)
220 (values `(make-simple-markup ,(car expr)) (cdr expr)))
222 ;; expr === (symbol ...) or ((funcall ...) ...)
226 (define (compile-all-markup-args expr)
227 "Transform `expr' into markup arguments"
228 (do ((rest expr rest)
230 ((null? rest) (reverse args))
231 (receive (a r) (compile-markup-arg rest)
232 (set! args (cons a args))
235 (define (compile-markup-arg expr)
236 "Return two values: the desired markup argument, and the rest arguments"
240 ((keyword? (car expr))
241 ;; expr === (#:COMMAND ...)
242 ;; ==> build and return the whole markup expression
243 (compile-markup-expression expr))
244 ((and (pair? (car expr))
245 (keyword? (caar expr)))
246 ;; expr === ((#:COMMAND ...) ...)
247 ;; ==> build and return the whole markup expression(s)
248 ;; found in (car expr)
249 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
250 (if (null? rest-expr)
251 (values markup-expr (cdr expr))
252 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
254 ((and (pair? (car expr))
256 ;; expr === (((foo ...) ...) ...)
257 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
258 (else (values (car expr) (cdr expr)))))
261 ;;; Utilities for storing and accessing markup commands signature
265 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
266 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
268 ;;; (markup-command-signature raise-markup)
269 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
271 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
274 (define-public (markup-command-keyword markup-command)
275 "Return markup-command's argument keyword, ie a string describing the command
276 arguments, eg. \"scheme0markup1\""
277 (object-property markup-command 'markup-keyword))
279 (define-public (markup-command-signature-ref markup-command)
280 "Return markup-command's signature (the 'markup-signature object property)"
281 (object-property markup-command 'markup-signature))
283 (define-public (markup-command-signature-set! markup-command signature)
284 "Set markup-command's signature and keyword (as object properties)"
285 (set-object-property! markup-command 'markup-signature signature)
286 (set-object-property! markup-command 'markup-keyword
287 (markup-signature-to-keyword signature))
290 (define-public markup-command-signature
291 (make-procedure-with-setter markup-command-signature-ref
292 markup-command-signature-set!))
294 ;; For documentation purposes
295 (define-public markup-function-list (list))
296 (define-public markup-list-function-list (list))
298 (define-public (markup-signature-to-keyword sig)
299 " (A B C) -> a0-b1-c2 "
302 (string->symbol (string-join (map
305 (set! count (+ count 1))
307 ;; for reasons I don't get,
308 ;; (case func ((markup?) .. )
311 ((eq? func markup?) "markup")
312 ((eq? func markup-list?) "markup-list")
314 (number->string (- count 1)))))
318 (define (lookup-markup-command-aux symbol)
319 (let ((proc (catch 'misc-error
321 (module-ref (current-module) symbol))
322 (lambda (key . args) #f))))
323 (and (procedure? proc) proc)))
325 (define-public (lookup-markup-command code)
326 (let ((proc (lookup-markup-command-aux
327 (string->symbol (format #f "~a-markup" code)))))
328 (and proc (markup-function? proc)
329 (cons proc (markup-command-keyword proc)))))
331 (define-public (lookup-markup-list-command code)
332 (let ((proc (lookup-markup-command-aux
333 (string->symbol (format #f "~a-markup-list" code)))))
334 (and proc (markup-list-function? proc)
335 (cons proc (markup-command-keyword proc)))))
337 ;;;;;;;;;;;;;;;;;;;;;;
338 ;;; used in parser.yy to map a list of markup commands on markup arguments
339 (define-public (map-markup-command-list commands markups)
340 "`markups' being a list of markups, eg (markup1 markup2 markup3),
341 and `commands' a list of commands with their scheme arguments, in reverse order,
342 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
343 ((bold (raise 4 (italic markup1)))
344 (bold (raise 4 (italic markup2)))
345 (bold (raise 4 (italic markup3))))
347 (map-in-order (lambda (arg)
349 (for-each (lambda (cmd)
350 (set! result (append cmd (list result))))
355 ;;;;;;;;;;;;;;;;;;;;;;
356 ;;; markup type predicates
358 (define (markup-function? x)
359 (and (markup-command-signature x)
360 (not (object-property x 'markup-list-command))))
362 (define (markup-list-function? x)
363 (and (markup-command-signature x)
364 (object-property x 'markup-list-command)))
366 (define-public (markup-command-list? x)
367 "Determine if `x' is a markup command list, ie. a list composed of
368 a markup list function and its arguments."
369 (and (pair? x) (markup-list-function? (car x))))
371 (define-public (markup-list? arg)
372 "Return a true value if `x' is a list of markups or markup command lists."
373 (define (markup-list-inner? lst)
375 (and (or (markup? (car lst)) (markup-command-list? (car lst)))
376 (markup-list-inner? (cdr lst)))))
377 (not (not (and (list? arg) (markup-list-inner? arg)))))
379 (define (markup-argument-list? signature arguments)
380 "Typecheck argument list."
381 (if (and (pair? signature) (pair? arguments))
382 (and ((car signature) (car arguments))
383 (markup-argument-list? (cdr signature) (cdr arguments)))
384 (and (null? signature) (null? arguments))))
387 (define (markup-argument-list-error signature arguments number)
388 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
389 #f is no error found.
391 (if (and (pair? signature) (pair? arguments))
392 (if (not ((car signature) (car arguments)))
393 (list number (type-name (car signature)) (car arguments))
394 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
398 ;; full recursive typecheck.
400 (define (markup-typecheck? arg)
403 (markup-function? (car arg))
404 (markup-argument-list? (markup-command-signature (car arg))
411 (define (markup-thrower-typecheck arg)
412 "typecheck, and throw an error when something amiss.
414 Uncovered - cheap-markup? is used."
416 (cond ((string? arg) #t)
418 (throw 'markup-format "Not a pair" arg))
419 ((not (markup-function? (car arg)))
420 (throw 'markup-format "Not a markup function " (car arg)))
421 ((not (markup-argument-list? (markup-command-signature (car arg))
423 (throw 'markup-format "Arguments failed typecheck for " arg)))
427 ;; good enough if you only use make-XXX-markup functions.
429 (define (cheap-markup? x)
432 (markup-function? (car x)))))
435 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
437 (define-public markup? cheap-markup?)
441 (define (markup-join markups sep)
442 "Return line-markup of MARKUPS, joining them with markup SEP"
444 (make-line-markup (list-insert-separator markups sep))
448 (define-public interpret-markup ly:text-interface::interpret-markup)
450 (define-public (interpret-markup-list layout props markup-list)
451 (let ((stencils (list)))
452 (for-each (lambda (m)
454 (if (markup-command-list? m)
455 (append! (reverse! (apply (car m) layout props (cdr m)))
457 (cons (interpret-markup layout props m) stencils))))
459 (reverse! stencils)))
461 (define-public (prepend-alist-chain key val chain)
462 (cons (acons key val (car chain)) (cdr chain)))
464 (define-public (stack-stencil-line space stencils)
466 (if (and (pair? stencils)
467 (ly:stencil? (car stencils)))
469 (if (and (pair? (cdr stencils))
470 (ly:stencil? (cadr stencils)))
471 (let* ((tail (stack-stencil-line space (cdr stencils)))
472 (head (car stencils))
473 (xoff (+ space (cdr (ly:stencil-extent head X)))))
475 (ly:stencil-translate-axis tail xoff X)))
477 (ly:make-stencil '() '(0 . 0) '(0 . 0))))