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 function, use the define-markup-command utility.
25 (define-markup-command (mycommand layout prop arg1 ...) (arg1-type? ...)
26 \"my command usage and description\"
29 The command is now available in markup mode, e.g.
32 \\markup { .... \\MYCOMMAND #1 argument ... }
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;;; markup definer utilities
38 ;;; `define-markup-command' can be used both for built-in markup
39 ;;; definitions and user defined markups.
41 (defmacro-public define-markup-command (command-and-args signature . body)
44 * Define a COMMAND-markup function after command-and-args and body,
45 register COMMAND-markup and its signature,
47 * add COMMAND-markup to markup-function-list,
49 * sets COMMAND-markup markup-signature and markup-keyword object properties,
51 * define a make-COMMAND-markup function.
54 (define-markup-command (COMMAND layout props arg1 arg2 ...) (arg1-type? arg2-type? ...)
55 \"documentation string\"
58 (define-markup-command COMMAND (arg1-type? arg2-type? ...)
61 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
62 (args (if (pair? command-and-args) (cdr command-and-args) '()))
63 (command-name (string->symbol (string-append (symbol->string command) "-markup")))
64 (make-markup-name (string->symbol (string-append "make-" (symbol->string command-name)))))
66 (define-public ,(if (pair? args)
67 (cons command-name args)
70 (set! (markup-command-signature ,command-name) (list ,@signature))
71 (if (not (member ,command-name markup-function-list))
72 (set! markup-function-list (cons ,command-name markup-function-list)))
73 (define-public (,make-markup-name . args)
74 (let ((sig (list ,@signature)))
75 (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
77 (define-public (make-markup markup-function make-name signature args)
78 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
79 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
81 (let* ((arglen (length args))
82 (siglen (length signature))
83 (error-msg (if (and (> siglen 0) (> arglen 0))
84 (markup-argument-list-error signature args 1)
86 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
87 (ly:error (string-append make-name ": "
88 (_ "Wrong number of arguments. Expect: ~A, found ~A: ~S"))
94 (_ "Invalid argument in position ~A. Expect: ~A, found: ~S.")
96 (cons markup-function args))))
98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 ;;; markup constructors
100 ;;; lilypond-like syntax for markup construction in scheme.
102 (use-modules (ice-9 optargs)
105 (defmacro*-public markup (#:rest body)
106 "The `markup' macro provides a lilypond-like syntax for building markups.
108 - #:COMMAND is used instead of \\COMMAND
109 - #:lines ( ... ) is used instead of { ... }
110 - #:center-align ( ... ) is used instead of \\center-align < ... >
115 \\raise #0.2 \\hbracket \\bold bar
116 \\override #'(baseline-skip . 4)
117 \\bracket \\column < baz bazr bla >
121 #:raise 0.2 #:hbracket #:bold \"bar\"
122 #:override '(baseline-skip . 4)
123 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
124 Use `markup*' in a \\notes block."
126 (car (compile-all-markup-expressions `(#:line ,body))))
128 (defmacro*-public markup* (#:rest body)
129 "Same as `markup', for use in a \\notes block."
130 `(ly:export (markup ,@body)))
133 (define (compile-all-markup-expressions expr)
134 "Return a list of canonical markups expressions, e.g.:
135 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
137 ((make-COMMAND1-markup arg11 arg12)
138 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
139 (do ((rest expr rest)
141 ((null? rest) (reverse markps))
142 (receive (m r) (compile-markup-expression rest)
143 (set! markps (cons m markps))
146 (define (keyword->make-markup key)
147 "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
148 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
150 (define (compile-markup-expression expr)
151 "Return two values: the first complete canonical markup expression
152 found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
153 and the rest expression."
154 (cond ((and (pair? expr)
155 (keyword? (car expr)))
156 ;; expr === (#:COMMAND arg1 ...)
157 (let* ((command (symbol->string (keyword->symbol (car expr))))
158 (sig (markup-command-signature
159 (car (lookup-markup-command command))))
160 (sig-len (length sig)))
163 (rest (cdr expr) rest))
165 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
166 (cond ((eqv? (list-ref sig i) markup-list?)
167 ;; (car rest) is a markup list
168 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
169 (set! rest (cdr rest)))
171 ;; pick up one arg in `rest'
172 (receive (a r) (compile-markup-arg rest)
173 (set! args (cons a args))
177 (keyword? (caar expr)))
178 ;; expr === ((#:COMMAND arg1 ...) ...)
179 (receive (m r) (compile-markup-expression (car expr))
180 (values m (cdr expr))))
182 (string? (car expr))) ;; expr === ("string" ...)
183 (values `(make-simple-markup ,(car expr)) (cdr expr)))
185 ;; expr === (symbol ...) or ((funcall ...) ...)
189 (define (compile-all-markup-args expr)
190 "Transform `expr' into markup arguments"
191 (do ((rest expr rest)
193 ((null? rest) (reverse args))
194 (receive (a r) (compile-markup-arg rest)
195 (set! args (cons a args))
198 (define (compile-markup-arg expr)
199 "Return two values: the desired markup argument, and the rest arguments"
203 ((keyword? (car expr))
204 ;; expr === (#:COMMAND ...)
205 ;; ==> build and return the whole markup expression
206 (compile-markup-expression expr))
207 ((and (pair? (car expr))
208 (keyword? (caar expr)))
209 ;; expr === ((#:COMMAND ...) ...)
210 ;; ==> build and return the whole markup expression(s)
211 ;; found in (car expr)
212 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
213 (if (null? rest-expr)
214 (values markup-expr (cdr expr))
215 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
217 ((and (pair? (car expr))
219 ;; expr === (((foo ...) ...) ...)
220 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
221 (else (values (car expr) (cdr expr)))))
224 ;;; Utilities for storing and accessing markup commands signature
228 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
229 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
231 ;;; (markup-command-signature raise-markup)
232 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
234 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
237 (define markup-command-signatures (make-hash-table 50))
239 (define (markup-command-signature-ref markup-command)
240 "Return markup-command's signature, e.g. (number? markup?).
241 markup-command may be a procedure."
242 (let ((sig-key (hashq-ref markup-command-signatures
244 (if sig-key (car sig-key) #f)))
246 (define-public (markup-command-keyword markup-command)
247 "Return markup-command's keyword, e.g. \"scheme0markup1\".
248 markup-command may be a procedure."
249 (let ((sig-key (hashq-ref markup-command-signatures
251 (if sig-key (cdr sig-key) #f)))
253 (define (markup-command-signatureset! markup-command signature)
254 "Set markup-command's signature. markup-command must be a named procedure.
255 Also set markup-signature and markup-keyword object properties."
256 (hashq-set! markup-command-signatures
258 (cons signature (markup-signature-to-keyword signature)))
259 ;; these object properties are still in use somewhere
260 (set-object-property! markup-command 'markup-signature signature)
261 (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
263 (define-public markup-command-signature
264 (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
266 (define (markup-symbol-to-proc markup-sym)
267 "Return the markup command procedure which name is `markup-sym', if any."
268 (hash-fold (lambda (key val prev)
270 (if (eqv? (procedure-name key) markup-sym) key #f)))
272 markup-command-signatures))
274 (define-public markup-function-list '())
276 (define-public (markup-signature-to-keyword sig)
277 " (A B C) -> a0-b1-c2 "
280 (string->symbol (string-join (map
283 (set! count (+ count 1))
285 ;; for reasons I don't get,
286 ;; (case func ((markup?) .. )
289 ((eq? func markup?) "markup")
290 ((eq? func markup-list?) "markup-list")
292 (number->string (- count 1)))))
296 (define-public (lookup-markup-command code)
297 (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
298 (and proc (cons proc (markup-command-keyword proc)))))
300 ;;;;;;;;;;;;;;;;;;;;;;
301 ;;; used in parser.yy to map a list of markup commands on markup arguments
302 (define-public (map-markup-command-list commands markups)
303 "`markups' being a list of markups, eg (markup1 markup2 markup3),
304 and `commands' a list of commands with their scheme arguments, in reverse order,
305 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
306 ((bold (raise 4 (italic markup1)))
307 (bold (raise 4 (italic markup2)))
308 (bold (raise 4 (italic markup3))))
310 (map-in-order (lambda (arg)
312 (for-each (lambda (cmd)
313 (set! result (append cmd (list result))))
318 ;;;;;;;;;;;;;;;;;;;;;;
319 ;;; markup type predicates
321 (define (markup-function? x)
322 (not (not (markup-command-signature x))))
324 (define-public (markup-list? arg)
325 (define (markup-list-inner? lst)
327 (and (markup? (car lst)) (markup-list-inner? (cdr lst)))))
328 (and (list? arg) (markup-list-inner? arg)))
330 (define (markup-argument-list? signature arguments)
331 "Typecheck argument list."
332 (if (and (pair? signature) (pair? arguments))
333 (and ((car signature) (car arguments))
334 (markup-argument-list? (cdr signature) (cdr arguments)))
335 (and (null? signature) (null? arguments))))
338 (define (markup-argument-list-error signature arguments number)
339 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
340 #f is no error found.
342 (if (and (pair? signature) (pair? arguments))
343 (if (not ((car signature) (car arguments)))
344 (list number (type-name (car signature)) (car arguments))
345 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
349 ;; full recursive typecheck.
351 (define (markup-typecheck? arg)
354 (markup-function? (car arg))
355 (markup-argument-list? (markup-command-signature (car arg))
359 ;; typecheck, and throw an error when something amiss.
361 (define (markup-thrower-typecheck arg)
362 (cond ((string? arg) #t)
364 (throw 'markup-format "Not a pair" arg))
365 ((not (markup-function? (car arg)))
366 (throw 'markup-format "Not a markup function " (car arg)))
367 ((not (markup-argument-list? (markup-command-signature (car arg))
369 (throw 'markup-format "Arguments failed typecheck for " arg)))
373 ;; good enough if you only use make-XXX-markup functions.
375 (define (cheap-markup? x)
378 (markup-function? (car x)))))
381 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
383 (define-public markup? cheap-markup?)
387 (define (markup-join markups sep)
388 "Return line-markup of MARKUPS, joining them with markup SEP"
390 (make-line-markup (list-insert-separator markups sep))
394 (define-public interpret-markup ly:text-interface::interpret-markup)
395 (define-public (prepend-alist-chain key val chain)
396 (cons (acons key val (car chain)) (cdr chain)))
398 (define-public (stack-stencil-line space stencils)
400 (if (and (pair? stencils)
401 (ly:stencil? (car stencils)))
403 (if (and (pair? (cdr stencils))
404 (ly:stencil? (cadr stencils)))
405 (let* ((tail (stack-stencil-line space (cdr stencils)))
406 (head (car stencils))
407 (xoff (+ space (cdr (ly:stencil-extent head X)))))
409 (ly:stencil-translate-axis tail xoff X)))
411 (ly:make-stencil '() '(0 . 0) '(0 . 0))))