1 ;;;; markup.scm -- Implement a user extensible markup scheme.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2003--2006 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-public (make-markup markup-function make-name signature args)
84 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
85 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
87 (let* ((arglen (length args))
88 (siglen (length signature))
89 (error-msg (if (and (> siglen 0) (> arglen 0))
90 (markup-argument-list-error signature args 1)
92 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
93 (ly:error (string-append make-name ": "
94 (_ "Wrong number of arguments. Expect: ~A, found ~A: ~S"))
100 (_ "Invalid argument in position ~A. Expect: ~A, found: ~S.")
102 (cons markup-function args))))
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
105 ;;; markup constructors
106 ;;; lilypond-like syntax for markup construction in scheme.
108 (use-modules (ice-9 optargs)
111 (defmacro*-public markup (#:rest body)
112 "The `markup' macro provides a lilypond-like syntax for building markups.
114 - #:COMMAND is used instead of \\COMMAND
115 - #:line ( ... ) is used instead of \\line { ... }
120 \\raise #0.2 \\hbracket \\bold bar
121 \\override #'(baseline-skip . 4)
122 \\bracket \\column { baz bazr bla }
126 #:raise 0.2 #:hbracket #:bold \"bar\"
127 #:override '(baseline-skip . 4)
128 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
129 Use `markup*' in a \\notemode context."
131 (car (compile-all-markup-expressions `(#:line ,body))))
133 (defmacro*-public markup* (#:rest body)
134 "Same as `markup', for use in a \\notes block."
135 `(ly:export (markup ,@body)))
138 (define (compile-all-markup-expressions expr)
139 "Return a list of canonical markups expressions, e.g.:
140 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
142 ((make-COMMAND1-markup arg11 arg12)
143 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
144 (do ((rest expr rest)
146 ((null? rest) (reverse markps))
147 (receive (m r) (compile-markup-expression rest)
148 (set! markps (cons m markps))
151 (define (keyword->make-markup key)
152 "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
153 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
155 (define (compile-markup-expression expr)
156 "Return two values: the first complete canonical markup expression
157 found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
158 and the rest expression."
159 (cond ((and (pair? expr)
160 (keyword? (car expr)))
161 ;; expr === (#:COMMAND arg1 ...)
162 (let* ((command (symbol->string (keyword->symbol (car expr))))
163 (sig (markup-command-signature
164 (car (lookup-markup-command command))))
165 (sig-len (length sig)))
168 (rest (cdr expr) rest))
170 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
171 (cond ((eqv? (list-ref sig i) markup-list?)
172 ;; (car rest) is a markup list
173 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
174 (set! rest (cdr rest)))
176 ;; pick up one arg in `rest'
177 (receive (a r) (compile-markup-arg rest)
178 (set! args (cons a args))
182 (keyword? (caar expr)))
183 ;; expr === ((#:COMMAND arg1 ...) ...)
184 (receive (m r) (compile-markup-expression (car expr))
185 (values m (cdr expr))))
187 (string? (car expr))) ;; expr === ("string" ...)
188 (values `(make-simple-markup ,(car expr)) (cdr expr)))
190 ;; expr === (symbol ...) or ((funcall ...) ...)
194 (define (compile-all-markup-args expr)
195 "Transform `expr' into markup arguments"
196 (do ((rest expr rest)
198 ((null? rest) (reverse args))
199 (receive (a r) (compile-markup-arg rest)
200 (set! args (cons a args))
203 (define (compile-markup-arg expr)
204 "Return two values: the desired markup argument, and the rest arguments"
208 ((keyword? (car expr))
209 ;; expr === (#:COMMAND ...)
210 ;; ==> build and return the whole markup expression
211 (compile-markup-expression expr))
212 ((and (pair? (car expr))
213 (keyword? (caar expr)))
214 ;; expr === ((#:COMMAND ...) ...)
215 ;; ==> build and return the whole markup expression(s)
216 ;; found in (car expr)
217 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
218 (if (null? rest-expr)
219 (values markup-expr (cdr expr))
220 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
222 ((and (pair? (car expr))
224 ;; expr === (((foo ...) ...) ...)
225 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
226 (else (values (car expr) (cdr expr)))))
229 ;;; Utilities for storing and accessing markup commands signature
233 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
234 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
236 ;;; (markup-command-signature raise-markup)
237 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
239 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
242 (define-public (markup-command-keyword markup-command)
243 "Return markup-command's argument keyword, ie a string describing the command
244 arguments, eg. \"scheme0markup1\""
245 (object-property markup-command 'markup-keyword))
247 (define-public (markup-command-signature-ref markup-command)
248 "Return markup-command's signature (the 'markup-signature object property)"
249 (object-property markup-command 'markup-signature))
251 (define-public (markup-command-signature-set! markup-command signature)
252 "Set markup-command's signature and keyword (as object properties)"
253 (set-object-property! markup-command 'markup-signature signature)
254 (set-object-property! markup-command 'markup-keyword
255 (markup-signature-to-keyword signature))
258 (define-public markup-command-signature
259 (make-procedure-with-setter markup-command-signature-ref
260 markup-command-signature-set!))
262 ;; For documentation purposes
263 (define-public markup-function-list (list))
265 (define-public (markup-signature-to-keyword sig)
266 " (A B C) -> a0-b1-c2 "
269 (string->symbol (string-join (map
272 (set! count (+ count 1))
274 ;; for reasons I don't get,
275 ;; (case func ((markup?) .. )
278 ((eq? func markup?) "markup")
279 ((eq? func markup-list?) "markup-list")
281 (number->string (- count 1)))))
285 (define-public (lookup-markup-command code)
286 (let ((proc (catch 'misc-error
288 (module-ref (current-module)
289 (string->symbol (format #f "~a-markup" code))))
290 (lambda (key . args) #f))))
291 (and (procedure? proc)
292 (cons proc (markup-command-keyword proc)))))
294 ;;;;;;;;;;;;;;;;;;;;;;
295 ;;; used in parser.yy to map a list of markup commands on markup arguments
296 (define-public (map-markup-command-list commands markups)
297 "`markups' being a list of markups, eg (markup1 markup2 markup3),
298 and `commands' a list of commands with their scheme arguments, in reverse order,
299 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
300 ((bold (raise 4 (italic markup1)))
301 (bold (raise 4 (italic markup2)))
302 (bold (raise 4 (italic markup3))))
304 (map-in-order (lambda (arg)
306 (for-each (lambda (cmd)
307 (set! result (append cmd (list result))))
312 ;;;;;;;;;;;;;;;;;;;;;;
313 ;;; markup type predicates
315 (define (markup-function? x)
316 (not (not (markup-command-signature x))))
318 (define-public (markup-list? arg)
319 (define (markup-list-inner? lst)
321 (and (markup? (car lst)) (markup-list-inner? (cdr lst)))))
322 (and (list? arg) (markup-list-inner? arg)))
324 (define (markup-argument-list? signature arguments)
325 "Typecheck argument list."
326 (if (and (pair? signature) (pair? arguments))
327 (and ((car signature) (car arguments))
328 (markup-argument-list? (cdr signature) (cdr arguments)))
329 (and (null? signature) (null? arguments))))
332 (define (markup-argument-list-error signature arguments number)
333 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
334 #f is no error found.
336 (if (and (pair? signature) (pair? arguments))
337 (if (not ((car signature) (car arguments)))
338 (list number (type-name (car signature)) (car arguments))
339 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
343 ;; full recursive typecheck.
345 (define (markup-typecheck? arg)
348 (markup-function? (car arg))
349 (markup-argument-list? (markup-command-signature (car arg))
356 (define (markup-thrower-typecheck arg)
357 "typecheck, and throw an error when something amiss.
359 Uncovered - cheap-markup? is used."
361 (cond ((string? arg) #t)
363 (throw 'markup-format "Not a pair" arg))
364 ((not (markup-function? (car arg)))
365 (throw 'markup-format "Not a markup function " (car arg)))
366 ((not (markup-argument-list? (markup-command-signature (car arg))
368 (throw 'markup-format "Arguments failed typecheck for " arg)))
372 ;; good enough if you only use make-XXX-markup functions.
374 (define (cheap-markup? x)
377 (markup-function? (car x)))))
380 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
382 (define-public markup? cheap-markup?)
386 (define (markup-join markups sep)
387 "Return line-markup of MARKUPS, joining them with markup SEP"
389 (make-line-markup (list-insert-separator markups sep))
393 (define-public interpret-markup ly:text-interface::interpret-markup)
394 (define-public (prepend-alist-chain key val chain)
395 (cons (acons key val (car chain)) (cdr chain)))
397 (define-public (stack-stencil-line space stencils)
399 (if (and (pair? stencils)
400 (ly:stencil? (car stencils)))
402 (if (and (pair? (cdr stencils))
403 (ly:stencil? (cadr stencils)))
404 (let* ((tail (stack-stencil-line space (cdr stencils)))
405 (head (car stencils))
406 (xoff (+ space (cdr (ly:stencil-extent head X)))))
408 (ly:stencil-translate-axis tail xoff X)))
410 (ly:make-stencil '() '(0 . 0) '(0 . 0))))