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-align ( ... ) is used instead of \\center-align < ... >
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, e.g.:
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, e.g. #: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 e.g. (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 (string? (car expr))) ;; expr === ("string" ...)
180 (values `(make-simple-markup ,(car expr)) (cdr expr)))
182 ;; expr === (symbol ...) or ((funcall ...) ...)
186 (define (compile-all-markup-args expr)
187 "Transform `expr' into markup arguments"
188 (do ((rest expr rest)
190 ((null? rest) (reverse args))
191 (receive (a r) (compile-markup-arg rest)
192 (set! args (cons a args))
195 (define (compile-markup-arg expr)
196 "Return two values: the desired markup argument, and the rest arguments"
200 ((keyword? (car expr))
201 ;; expr === (#:COMMAND ...)
202 ;; ==> build and return the whole markup expression
203 (compile-markup-expression expr))
204 ((and (pair? (car expr))
205 (keyword? (caar expr)))
206 ;; expr === ((#:COMMAND ...) ...)
207 ;; ==> build and return the whole markup expression(s)
208 ;; found in (car expr)
209 (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
210 (if (null? rest-expr)
211 (values markup-expr (cdr expr))
212 (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
214 ((and (pair? (car expr))
216 ;; expr === (((foo ...) ...) ...)
217 (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
218 (else (values (car expr) (cdr expr)))))
221 ;;; Debugging utilities: print markup expressions in a friendly fashion
223 (use-modules (ice-9 format))
224 (define (markup->string markup-expr)
225 "Return a string describing, in LilyPond syntax, the given markup expression."
226 (define (proc->command proc)
227 (let ((cmd-markup (symbol->string (procedure-name proc))))
228 (substring cmd-markup 0 (- (string-length cmd-markup)
229 (string-length "-markup")))))
230 (define (arg->string arg)
231 (cond ((and (pair? arg) (pair? (car arg))) ;; markup list
232 (format #f "~{ ~a~}" (map markup->string arg)))
233 ((pair? arg) ;; markup
234 (markup->string arg))
235 ((string? arg) ;; scheme string argument
236 (format #f "#\"~a\"" arg))
237 (else ;; other scheme arg
238 (format #f "#~a" arg))))
239 (let ((cmd (car markup-expr))
240 (args (cdr markup-expr)))
241 (cond ((eqv? cmd simple-markup) ;; a simple string
242 (format #f "\"~a\"" (car args)))
243 ((eqv? cmd line-markup) ;; { ... }
244 (format #f "{~a}" (arg->string (car args))))
245 ((eqv? cmd center-align-markup) ;; \center < ... >
246 (format #f "\\center-align <~a>" (arg->string (car args))))
247 ((eqv? cmd column-markup) ;; \column < ... >
248 (format #f "\\column <~a>" (arg->string (car args))))
249 (else ;; \command ...
250 (format #f "\\~a~{ ~a~} " (proc->command cmd) (map arg->string args))))))
252 (define-public (display-markup markup-expr)
253 "Print a LilyPond-syntax equivalent for the given markup expression."
254 (display "\\markup ")
255 (display (markup->string markup-expr)))
258 ;;; Utilities for storing and accessing markup commands signature
262 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
263 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
265 ;;; (markup-command-signature raise-markup)
266 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
268 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
271 (define markup-command-signatures (make-hash-table 50))
273 (define (markup-command-signature-ref markup-command)
274 "Return markup-command's signature, e.g. (number? markup?).
275 markup-command may be a procedure."
276 (let ((sig-key (hashq-ref markup-command-signatures
278 (if sig-key (car sig-key) #f)))
280 (define-public (markup-command-keyword markup-command)
281 "Return markup-command's keyword, e.g. \"scheme0markup1\".
282 markup-command may be a procedure."
283 (let ((sig-key (hashq-ref markup-command-signatures
285 (if sig-key (cdr sig-key) #f)))
287 (define (markup-command-signatureset! markup-command signature)
288 "Set markup-command's signature. markup-command must be a named procedure.
289 Also set markup-signature and markup-keyword object properties."
290 (hashq-set! markup-command-signatures
292 (cons signature (markup-signature-to-keyword signature)))
293 ;; these object properties are still in use somewhere
294 (set-object-property! markup-command 'markup-signature signature)
295 (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
297 (define-public markup-command-signature
298 (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
300 (define (markup-symbol-to-proc markup-sym)
301 "Return the markup command procedure which name is `markup-sym', if any."
302 (hash-fold (lambda (key val prev)
304 (if (eqv? (procedure-name key) markup-sym) key #f)))
306 markup-command-signatures))
308 (define-public markup-function-list '())
310 (define-public (markup-signature-to-keyword sig)
311 " (A B C) -> a0-b1-c2 "
314 (string->symbol (string-join (map
317 (set! count (+ count 1))
319 ;; for reasons I don't get,
320 ;; (case func ((markup?) .. )
323 ((eq? func markup?) "markup")
324 ((eq? func markup-list?) "markup-list")
326 (number->string (- count 1)))))
330 (define-public (lookup-markup-command code)
331 (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
332 (and proc (cons proc (markup-command-keyword proc)))))
334 ;;;;;;;;;;;;;;;;;;;;;;
335 ;;; markup type predicates
337 (define (markup-function? x)
338 (not (not (markup-command-signature x))))
340 (define (markup-list? arg)
341 (define (markup-list-inner? l)
343 (and (markup? (car l)) (markup-list-inner? (cdr l)))))
344 (and (list? arg) (markup-list-inner? arg)))
346 (define (markup-argument-list? signature arguments)
347 "Typecheck argument list."
348 (if (and (pair? signature) (pair? arguments))
349 (and ((car signature) (car arguments))
350 (markup-argument-list? (cdr signature) (cdr arguments)))
351 (and (null? signature) (null? arguments))))
354 (define (markup-argument-list-error signature arguments number)
355 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
356 #f is no error found.
358 (if (and (pair? signature) (pair? arguments))
359 (if (not ((car signature) (car arguments)))
360 (list number (type-name (car signature)) (car arguments))
361 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
365 ;; full recursive typecheck.
367 (define (markup-typecheck? arg)
370 (markup-function? (car arg))
371 (markup-argument-list? (markup-command-signature (car arg))
375 ;; typecheck, and throw an error when something amiss.
377 (define (markup-thrower-typecheck arg)
378 (cond ((string? arg) #t)
380 (throw 'markup-format "Not a pair" arg))
381 ((not (markup-function? (car arg)))
382 (throw 'markup-format "Not a markup function " (car arg)))
383 ((not (markup-argument-list? (markup-command-signature (car arg))
385 (throw 'markup-format "Arguments failed typecheck for " arg)))
389 ;; good enough if you only use make-XXX-markup functions.
391 (define (cheap-markup? x)
394 (markup-function? (car x)))))
397 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
399 (define-public markup? cheap-markup?)
403 (define (markup-join markups sep)
404 "Return line-markup of MARKUPS, joining them with markup SEP"
406 (make-line-markup (list-insert-separator markups sep))
409 (define-public brew-new-markup-stencil Text_item::print)
410 (define-public interpret-markup Text_item::interpret_markup)
411 (define-public (prepend-alist-chain key val chain)
412 (cons (acons key val (car chain)) (cdr chain)))
417 (define-public (stack-stencil-line space stencils)
419 (if (and (pair? stencils)
420 (ly:stencil? (car stencils)))
422 (if (and (pair? (cdr stencils))
423 (ly:stencil? (cadr stencils)))
424 (let* ((tail (stack-stencil-line space (cdr stencils)))
425 (head (car stencils))
426 (xoff (+ space (cdr (ly:stencil-extent head X)))))
428 (ly:stencil-translate-axis tail xoff X)))
430 (ly:make-stencil '() '(0 . 0) '(0 . 0))))