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 markup-command-signatures (make-hash-table 50))
244 (define (markup-command-signature-ref markup-command)
245 "Return markup-command's signature, e.g. (number? markup?).
246 markup-command may be a procedure."
247 (let ((sig-key (hashq-ref markup-command-signatures
249 (if sig-key (car sig-key) #f)))
251 (define-public (markup-command-keyword markup-command)
252 "Return markup-command's keyword, e.g. \"scheme0markup1\".
253 markup-command may be a procedure."
254 (let ((sig-key (hashq-ref markup-command-signatures
256 (if sig-key (cdr sig-key) #f)))
258 (define (markup-command-signatureset! markup-command signature)
259 "Set markup-command's signature. markup-command must be a named procedure.
260 Also set markup-signature and markup-keyword object properties."
261 (hashq-set! markup-command-signatures
263 (cons signature (markup-signature-to-keyword signature)))
264 ;; these object properties are still in use somewhere
265 (set-object-property! markup-command 'markup-signature signature)
266 (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
268 (define-public markup-command-signature
269 (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
271 (define (markup-symbol-to-proc markup-sym)
272 "Return the markup command procedure which name is `markup-sym', if any."
273 (hash-fold (lambda (key val prev)
275 (if (eqv? (procedure-name key) markup-sym) key #f)))
277 markup-command-signatures))
279 (define-public markup-function-list '())
281 (define-public (markup-signature-to-keyword sig)
282 " (A B C) -> a0-b1-c2 "
285 (string->symbol (string-join (map
288 (set! count (+ count 1))
290 ;; for reasons I don't get,
291 ;; (case func ((markup?) .. )
294 ((eq? func markup?) "markup")
295 ((eq? func markup-list?) "markup-list")
297 (number->string (- count 1)))))
301 (define-public (lookup-markup-command code)
302 (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
303 (and proc (cons proc (markup-command-keyword proc)))))
305 ;;;;;;;;;;;;;;;;;;;;;;
306 ;;; used in parser.yy to map a list of markup commands on markup arguments
307 (define-public (map-markup-command-list commands markups)
308 "`markups' being a list of markups, eg (markup1 markup2 markup3),
309 and `commands' a list of commands with their scheme arguments, in reverse order,
310 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
311 ((bold (raise 4 (italic markup1)))
312 (bold (raise 4 (italic markup2)))
313 (bold (raise 4 (italic markup3))))
315 (map-in-order (lambda (arg)
317 (for-each (lambda (cmd)
318 (set! result (append cmd (list result))))
323 ;;;;;;;;;;;;;;;;;;;;;;
324 ;;; markup type predicates
326 (define (markup-function? x)
327 (not (not (markup-command-signature x))))
329 (define-public (markup-list? arg)
330 (define (markup-list-inner? lst)
332 (and (markup? (car lst)) (markup-list-inner? (cdr lst)))))
333 (and (list? arg) (markup-list-inner? arg)))
335 (define (markup-argument-list? signature arguments)
336 "Typecheck argument list."
337 (if (and (pair? signature) (pair? arguments))
338 (and ((car signature) (car arguments))
339 (markup-argument-list? (cdr signature) (cdr arguments)))
340 (and (null? signature) (null? arguments))))
343 (define (markup-argument-list-error signature arguments number)
344 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
345 #f is no error found.
347 (if (and (pair? signature) (pair? arguments))
348 (if (not ((car signature) (car arguments)))
349 (list number (type-name (car signature)) (car arguments))
350 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
354 ;; full recursive typecheck.
356 (define (markup-typecheck? arg)
359 (markup-function? (car arg))
360 (markup-argument-list? (markup-command-signature (car arg))
364 ;; typecheck, and throw an error when something amiss.
366 (define (markup-thrower-typecheck arg)
367 (cond ((string? arg) #t)
369 (throw 'markup-format "Not a pair" arg))
370 ((not (markup-function? (car arg)))
371 (throw 'markup-format "Not a markup function " (car arg)))
372 ((not (markup-argument-list? (markup-command-signature (car arg))
374 (throw 'markup-format "Arguments failed typecheck for " arg)))
378 ;; good enough if you only use make-XXX-markup functions.
380 (define (cheap-markup? x)
383 (markup-function? (car x)))))
386 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
388 (define-public markup? cheap-markup?)
392 (define (markup-join markups sep)
393 "Return line-markup of MARKUPS, joining them with markup SEP"
395 (make-line-markup (list-insert-separator markups sep))
399 (define-public interpret-markup ly:text-interface::interpret-markup)
400 (define-public (prepend-alist-chain key val chain)
401 (cons (acons key val (car chain)) (cdr chain)))
403 (define-public (stack-stencil-line space stencils)
405 (if (and (pair? stencils)
406 (ly:stencil? (car stencils)))
408 (if (and (pair? (cdr stencils))
409 (ly:stencil? (cadr stencils)))
410 (let* ((tail (stack-stencil-line space (cdr stencils)))
411 (head (car stencils))
412 (xoff (+ space (cdr (ly:stencil-extent head X)))))
414 (ly:stencil-translate-axis tail xoff X)))
416 (ly:make-stencil '() '(0 . 0) '(0 . 0))))