2 Internally markup is stored as lists, whose head is a function.
4 (FUNCTION ARG1 ARG2 ... )
6 When the markup is formatted, then FUNCTION is called as follows
8 (FUNCTION GROB PROPS ARG1 ARG2 ... )
10 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
11 the rest of the arguments.
13 The function should return a stencil (i.e. a formatted, ready to
17 To add a function, use the def-markup-command utility.
19 (def-markup-command (mycommand paper prop arg1 ...) (arg1-type? ...)
20 \"my command usage and description\"
23 The command is now available in markup mode, e.g.
26 \\markup { .... \\MYCOMMAND #1 argument ... }
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;;; markup definer utilities
32 ;;; `def-markup-command' can be used both for built-in markup
33 ;;; definitions and user defined markups.
35 (defmacro-public def-markup-command (command-and-args signature . body)
38 * Define a COMMAND-markup function after command-and-args and body,
39 register COMMAND-markup and its signature,
41 * add COMMAND-markup to markup-function-list,
43 * sets COMMAND-markup markup-signature and markup-keyword object properties,
45 * define a make-COMMAND-markup function.
48 (def-markup-command (COMMAND paper props arg1 arg2 ...) (arg1-type? arg2-type? ...)
49 \"documentation string\"
52 (def-markup-command COMMAND (arg1-type? arg2-type? ...)
55 (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
56 (args (if (pair? command-and-args) (cdr command-and-args) '()))
57 (command-name (string->symbol (string-append (symbol->string command) "-markup")))
58 (make-markup-name (string->symbol (string-append "make-" (symbol->string command-name)))))
60 (define-public ,(if (pair? args)
61 (cons command-name args)
64 (set! (markup-command-signature ,command-name) (list ,@signature))
65 (if (not (member ,command-name markup-function-list))
66 (set! markup-function-list (cons ,command-name markup-function-list)))
67 (define-public (,make-markup-name . args)
68 (let ((sig (list ,@signature)))
69 (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
71 (define-public (make-markup markup-function make-name signature args)
72 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
73 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
75 (let* ((arglen (length args))
76 (siglen (length signature))
77 (error-msg (if (and (> siglen 0) (> arglen 0))
78 (markup-argument-list-error signature args 1)
80 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
81 (scm-error 'markup-format make-name
82 "Expect ~A arguments for ~A. Found ~A: ~S"
83 (list siglen make-name arglen args)
86 (scm-error 'markup-format make-name
87 "Invalid argument in position ~A\nExpect: ~A\nFound: ~S."
89 (cons markup-function args))))
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 ;;; markup constructors
93 ;;; lilypond-like syntax for markup construction in scheme.
95 (use-modules (ice-9 optargs)
98 (defmacro*-public markup (#:rest body)
99 "The `markup' macro provides a lilypond-like syntax for building markups.
101 - #:COMMAND is used instead of \\COMMAND
102 - #:lines ( ... ) is used instead of { ... }
103 - #:center ( ... ) is used instead of \\center < ... >
108 \\raise #0.2 \\hbracket \\bold bar
109 \\override #'(baseline-skip . 4)
110 \\bracket \\column < baz bazr bla >
114 #:raise 0.2 #:hbracket #:bold \"bar\"
115 #:override '(baseline-skip . 4)
116 #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
117 Use `markup*' in a \\notes block."
119 (car (compile-all-markup-expressions `(#:line ,body))))
121 (defmacro*-public markup* (#:rest body)
122 "Same as `markup', for use in a \\notes block."
123 `(ly:export (markup ,@body)))
126 (define (compile-all-markup-expressions expr)
127 "Return a list of canonical markups expressions, eg:
128 (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
130 ((make-COMMAND1-markup arg11 arg12)
131 (make-COMMAND2-markup arg21 arg22 arg23) ...)"
132 (do ((rest expr rest)
134 ((null? rest) (reverse markps))
135 (receive (m r) (compile-markup-expression rest)
136 (set! markps (cons m markps))
139 (define (keyword->make-markup key)
140 "Transform a keyword, eg. #:COMMAND, in a make-COMMAND-markup symbol."
141 (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
143 (define (compile-markup-expression expr)
144 "Return two values: the first complete canonical markup expression found in `expr',
145 eg (make-COMMAND-markup arg1 arg2 ...), and the rest expression."
146 (cond ((and (pair? expr)
147 (keyword? (car expr)))
148 ;; expr === (#:COMMAND arg1 ...)
149 (let* ((command (symbol->string (keyword->symbol (car expr))))
150 (sig (markup-command-signature (car (lookup-markup-command command))))
151 (sig-len (length sig)))
154 (rest (cdr expr) rest))
156 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
157 (cond ((eqv? (list-ref sig i) markup-list?)
158 ;; (car rest) is a markup list
159 (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
160 (set! rest (cdr rest)))
162 ;; pick up one arg in `rest'
163 (receive (a r) (compile-markup-arg rest)
164 (set! args (cons a args))
168 (keyword? (caar expr)))
169 ;; expr === ((#:COMMAND arg1 ...) ...)
170 (receive (m r) (compile-markup-expression (car expr))
171 (values m (cdr expr))))
173 ;; expr === (symbol ...) or ("string" ...) or ((funcall ...) ...)
177 (define (compile-all-markup-args expr)
178 "Transform `expr' into markup arguments"
179 (do ((rest expr rest)
181 ((null? rest) (reverse args))
182 (receive (a r) (compile-markup-arg rest)
183 (set! args (cons a args))
186 (define (compile-markup-arg expr)
187 "Return two values: the desired markup argument, and the rest arguments"
191 ((keyword? (car expr))
192 ;; expr === (#:COMMAND ...)
193 ;; ==> build and return the whole markup expression
194 (compile-markup-expression expr))
195 ((and (pair? (car expr))
196 (keyword? (caar expr)))
197 ;; expr === ((#:COMMAND ...) ...)
198 ;; ==> build and return the whole markup expression(s)
199 ;; found in (car expr)
200 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
201 (if (null? rest-expr)
202 (values markup-expr (cdr expr))
203 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
205 ((and (pair? (car expr))
207 ;; expr === (((foo ...) ...) ...)
208 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
209 (else (values (car expr) (cdr expr)))))
212 ;;; Utilities for storing and accessing markup commands signature
216 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
217 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
219 ;;; (markup-command-signature raise-markup)
220 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
222 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
225 (define markup-command-signatures (make-hash-table 50))
227 (define (markup-command-signature-ref markup-command)
228 "Return markup-command's signature, e.g. (number? markup?).
229 markup-command may be a procedure."
230 (let ((sig-key (hashq-ref markup-command-signatures
232 (if sig-key (car sig-key) #f)))
234 (define-public (markup-command-keyword markup-command)
235 "Return markup-command's keyword, e.g. \"scheme0markup1\".
236 markup-command may be a procedure."
237 (let ((sig-key (hashq-ref markup-command-signatures
239 (if sig-key (cdr sig-key) #f)))
241 (define (markup-command-signatureset! markup-command signature)
242 "Set markup-command's signature. markup-command must be a named procedure.
243 Also set markup-signature and markup-keyword object properties."
244 (hashq-set! markup-command-signatures
246 (cons signature (markup-signature-to-keyword signature)))
247 ;; these object properties are still in use somewhere
248 (set-object-property! markup-command 'markup-signature signature)
249 (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
251 (define-public markup-command-signature
252 (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
254 (define (markup-symbol-to-proc markup-sym)
255 "Return the markup command procedure which name is `markup-sym', if any."
256 (hash-fold (lambda (key val prev)
258 (if (eqv? (procedure-name key) markup-sym) key #f)))
260 markup-command-signatures))
262 (define-public markup-function-list '())
264 (define-public (markup-signature-to-keyword sig)
265 " (A B C) -> a0-b1-c2 "
268 (string->symbol (string-join (map
271 (set! count (+ count 1))
273 ;; for reasons I don't get,
274 ;; (case func ((markup?) .. )
277 ((eq? func markup?) "markup")
278 ((eq? func markup-list?) "markup-list")
280 (number->string (- count 1)))))
284 (define-public (lookup-markup-command code)
285 (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
286 (and proc (cons proc (markup-command-keyword proc)))))
288 ;;;;;;;;;;;;;;;;;;;;;;
289 ;;; markup type predicates
291 (define (markup-function? x)
292 (not (not (markup-command-signature x))))
294 (define (markup-list? arg)
295 (define (markup-list-inner? l)
297 (and (markup? (car l)) (markup-list-inner? (cdr l)))))
298 (and (list? arg) (markup-list-inner? arg)))
300 (define (markup-argument-list? signature arguments)
301 "Typecheck argument list."
302 (if (and (pair? signature) (pair? arguments))
303 (and ((car signature) (car arguments))
304 (markup-argument-list? (cdr signature) (cdr arguments)))
305 (and (null? signature) (null? arguments))))
308 (define (markup-argument-list-error signature arguments number)
309 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
310 #f is no error found.
312 (if (and (pair? signature) (pair? arguments))
313 (if (not ((car signature) (car arguments)))
314 (list number (type-name (car signature)) (car arguments))
315 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
319 ;; full recursive typecheck.
321 (define (markup-typecheck? arg)
324 (markup-function? (car arg))
325 (markup-argument-list? (markup-command-signature (car arg))
329 ;; typecheck, and throw an error when something amiss.
331 (define (markup-thrower-typecheck arg)
332 (cond ((string? arg) #t)
334 (throw 'markup-format "Not a pair" arg))
335 ((not (markup-function? (car arg)))
336 (throw 'markup-format "Not a markup function " (car arg)))
337 ((not (markup-argument-list? (markup-command-signature (car arg))
339 (throw 'markup-format "Arguments failed typecheck for " arg)))
343 ;; good enough if you only use make-XXX-markup functions.
345 (define (cheap-markup? x)
348 (markup-function? (car x)))))
351 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
353 (define-public markup? cheap-markup?)
357 (define (markup-join markups sep)
358 "Return line-markup of MARKUPS, joining them with markup SEP"
360 (make-line-markup (list-insert-separator markups sep))
363 (define-public brew-new-markup-stencil Text_item::print)
364 (define-public interpret-markup Text_item::interpret_markup)
365 (define-public (prepend-alist-chain key val chain)
366 (cons (acons key val (car chain)) (cdr chain)))
371 (define-public (stack-stencil-line space stencils)
373 (if (pair? (cdr stencils))
374 (let* ((tail (stack-stencil-line space (cdr stencils)))
375 (head (car stencils))
376 (xoff (+ space (cdr (ly:stencil-get-extent head X)))))
378 (ly:stencil-translate-axis tail xoff X)))