3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2003--2004 Han-Wen Nienhuys <hanwen@cs.uu.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 def-markup-command utility.
25 (def-markup-command (mycommand paper 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 ;;; `def-markup-command' can be used both for built-in markup
39 ;;; definitions and user defined markups.
41 (defmacro-public def-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 (def-markup-command (COMMAND paper props arg1 arg2 ...) (arg1-type? arg2-type? ...)
55 \"documentation string\"
58 (def-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 (scm-error 'markup-format make-name
88 "Expect ~A arguments for ~A. Found ~A: ~S"
89 (list siglen make-name arglen args)
92 (scm-error 'markup-format make-name
93 "Invalid argument in position ~A\nExpect: ~A\nFound: ~S."
95 (cons markup-function args))))
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
98 ;;; markup constructors
99 ;;; lilypond-like syntax for markup construction in scheme.
101 (use-modules (ice-9 optargs)
104 (defmacro*-public markup (#:rest body)
105 "The `markup' macro provides a lilypond-like syntax for building markups.
107 - #:COMMAND is used instead of \\COMMAND
108 - #:lines ( ... ) is used instead of { ... }
109 - #:center ( ... ) is used instead of \\center < ... >
114 \\raise #0.2 \\hbracket \\bold bar
115 \\override #'(baseline-skip . 4)
116 \\bracket \\column < baz bazr bla >
120 #:raise 0.2 #:hbracket #:bold \"bar\"
121 #:override '(baseline-skip . 4)
122 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
123 Use `markup*' in a \\notes block."
125 (car (compile-all-markup-expressions `(#:line ,body))))
127 (defmacro*-public markup* (#:rest body)
128 "Same as `markup', for use in a \\notes block."
129 `(ly:export (markup ,@body)))
132 (define (compile-all-markup-expressions expr)
133 "Return a list of canonical markups expressions, eg:
134 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
136 ((make-COMMAND1-markup arg11 arg12)
137 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
138 (do ((rest expr rest)
140 ((null? rest) (reverse markps))
141 (receive (m r) (compile-markup-expression rest)
142 (set! markps (cons m markps))
145 (define (keyword->make-markup key)
146 "Transform a keyword, eg. #:COMMAND, in a make-COMMAND-markup symbol."
147 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
149 (define (compile-markup-expression expr)
150 "Return two values: the first complete canonical markup expression found in `expr',
151 eg (make-COMMAND-markup arg1 arg2 ...), and the rest expression."
152 (cond ((and (pair? expr)
153 (keyword? (car expr)))
154 ;; expr === (#:COMMAND arg1 ...)
155 (let* ((command (symbol->string (keyword->symbol (car expr))))
156 (sig (markup-command-signature (car (lookup-markup-command command))))
157 (sig-len (length sig)))
160 (rest (cdr expr) rest))
162 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
163 (cond ((eqv? (list-ref sig i) markup-list?)
164 ;; (car rest) is a markup list
165 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
166 (set! rest (cdr rest)))
168 ;; pick up one arg in `rest'
169 (receive (a r) (compile-markup-arg rest)
170 (set! args (cons a args))
174 (keyword? (caar expr)))
175 ;; expr === ((#:COMMAND arg1 ...) ...)
176 (receive (m r) (compile-markup-expression (car expr))
177 (values m (cdr expr))))
179 ;; expr === (symbol ...) or ("string" ...) or ((funcall ...) ...)
183 (define (compile-all-markup-args expr)
184 "Transform `expr' into markup arguments"
185 (do ((rest expr rest)
187 ((null? rest) (reverse args))
188 (receive (a r) (compile-markup-arg rest)
189 (set! args (cons a args))
192 (define (compile-markup-arg expr)
193 "Return two values: the desired markup argument, and the rest arguments"
197 ((keyword? (car expr))
198 ;; expr === (#:COMMAND ...)
199 ;; ==> build and return the whole markup expression
200 (compile-markup-expression expr))
201 ((and (pair? (car expr))
202 (keyword? (caar expr)))
203 ;; expr === ((#:COMMAND ...) ...)
204 ;; ==> build and return the whole markup expression(s)
205 ;; found in (car expr)
206 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
207 (if (null? rest-expr)
208 (values markup-expr (cdr expr))
209 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
211 ((and (pair? (car expr))
213 ;; expr === (((foo ...) ...) ...)
214 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
215 (else (values (car expr) (cdr expr)))))
218 ;;; Utilities for storing and accessing markup commands signature
222 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
223 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
225 ;;; (markup-command-signature raise-markup)
226 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
228 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
231 (define markup-command-signatures (make-hash-table 50))
233 (define (markup-command-signature-ref markup-command)
234 "Return markup-command's signature, e.g. (number? markup?).
235 markup-command may be a procedure."
236 (let ((sig-key (hashq-ref markup-command-signatures
238 (if sig-key (car sig-key) #f)))
240 (define-public (markup-command-keyword markup-command)
241 "Return markup-command's keyword, e.g. \"scheme0markup1\".
242 markup-command may be a procedure."
243 (let ((sig-key (hashq-ref markup-command-signatures
245 (if sig-key (cdr sig-key) #f)))
247 (define (markup-command-signatureset! markup-command signature)
248 "Set markup-command's signature. markup-command must be a named procedure.
249 Also set markup-signature and markup-keyword object properties."
250 (hashq-set! markup-command-signatures
252 (cons signature (markup-signature-to-keyword signature)))
253 ;; these object properties are still in use somewhere
254 (set-object-property! markup-command 'markup-signature signature)
255 (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
257 (define-public markup-command-signature
258 (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
260 (define (markup-symbol-to-proc markup-sym)
261 "Return the markup command procedure which name is `markup-sym', if any."
262 (hash-fold (lambda (key val prev)
264 (if (eqv? (procedure-name key) markup-sym) key #f)))
266 markup-command-signatures))
268 (define-public markup-function-list '())
270 (define-public (markup-signature-to-keyword sig)
271 " (A B C) -> a0-b1-c2 "
274 (string->symbol (string-join (map
277 (set! count (+ count 1))
279 ;; for reasons I don't get,
280 ;; (case func ((markup?) .. )
283 ((eq? func markup?) "markup")
284 ((eq? func markup-list?) "markup-list")
286 (number->string (- count 1)))))
290 (define-public (lookup-markup-command code)
291 (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
292 (and proc (cons proc (markup-command-keyword proc)))))
294 ;;;;;;;;;;;;;;;;;;;;;;
295 ;;; markup type predicates
297 (define (markup-function? x)
298 (not (not (markup-command-signature x))))
300 (define (markup-list? arg)
301 (define (markup-list-inner? l)
303 (and (markup? (car l)) (markup-list-inner? (cdr l)))))
304 (and (list? arg) (markup-list-inner? arg)))
306 (define (markup-argument-list? signature arguments)
307 "Typecheck argument list."
308 (if (and (pair? signature) (pair? arguments))
309 (and ((car signature) (car arguments))
310 (markup-argument-list? (cdr signature) (cdr arguments)))
311 (and (null? signature) (null? arguments))))
314 (define (markup-argument-list-error signature arguments number)
315 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
316 #f is no error found.
318 (if (and (pair? signature) (pair? arguments))
319 (if (not ((car signature) (car arguments)))
320 (list number (type-name (car signature)) (car arguments))
321 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
325 ;; full recursive typecheck.
327 (define (markup-typecheck? arg)
330 (markup-function? (car arg))
331 (markup-argument-list? (markup-command-signature (car arg))
335 ;; typecheck, and throw an error when something amiss.
337 (define (markup-thrower-typecheck arg)
338 (cond ((string? arg) #t)
340 (throw 'markup-format "Not a pair" arg))
341 ((not (markup-function? (car arg)))
342 (throw 'markup-format "Not a markup function " (car arg)))
343 ((not (markup-argument-list? (markup-command-signature (car arg))
345 (throw 'markup-format "Arguments failed typecheck for " arg)))
349 ;; good enough if you only use make-XXX-markup functions.
351 (define (cheap-markup? x)
354 (markup-function? (car x)))))
357 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
359 (define-public markup? cheap-markup?)
363 (define (markup-join markups sep)
364 "Return line-markup of MARKUPS, joining them with markup SEP"
366 (make-line-markup (list-insert-separator markups sep))
369 (define-public brew-new-markup-stencil Text_item::print)
370 (define-public interpret-markup Text_item::interpret_markup)
371 (define-public (prepend-alist-chain key val chain)
372 (cons (acons key val (car chain)) (cdr chain)))
377 (define-public (stack-stencil-line space stencils)
379 (if (pair? (cdr stencils))
380 (let* ((tail (stack-stencil-line space (cdr stencils)))
381 (head (car stencils))
382 (xoff (+ space (cdr (ly:stencil-get-extent head X)))))
384 (ly:stencil-translate-axis tail xoff X)))