]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup.scm
* input/test/compound-time.ly: New file.
[lilypond.git] / scm / markup.scm
1 ;;;; markup.scm -- Implement a user extensible markup scheme.
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2003--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
6
7 "
8 Internally markup is stored as lists, whose head is a function.
9
10   (FUNCTION ARG1 ARG2 ... )
11
12 When the markup is formatted, then FUNCTION is called as follows
13
14   (FUNCTION GROB PROPS ARG1 ARG2 ... ) 
15
16 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
17 the rest of the arguments.
18
19 The function should return a stencil (i.e. a formatted, ready to
20 print object).
21
22
23 To add a function, use the def-markup-command utility.
24
25   (def-markup-command (mycommand layout prop arg1 ...) (arg1-type? ...)
26     \"my command usage and description\"
27     ...function body...)
28
29 The command is now available in markup mode, e.g.
30
31
32   \\markup { .... \\MYCOMMAND #1 argument ... }
33
34 " ; "
35
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;;; markup definer utilities
38 ;;; `def-markup-command' can be used both for built-in markup
39 ;;; definitions and user defined markups.
40
41 (defmacro-public def-markup-command (command-and-args signature . body)
42   "
43
44 * Define a COMMAND-markup function after command-and-args and body,
45 register COMMAND-markup and its signature,
46
47 * add COMMAND-markup to markup-function-list,
48
49 * sets COMMAND-markup markup-signature and markup-keyword object properties,
50
51 * define a make-COMMAND-markup function.
52
53 Syntax:
54   (def-markup-command (COMMAND layout props arg1 arg2 ...) (arg1-type? arg2-type? ...)
55     \"documentation string\"
56     ...command body...)
57  or:
58   (def-markup-command COMMAND (arg1-type? arg2-type? ...)
59     function)
60 "
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)))))
65     `(begin
66        (define-public ,(if (pair? args)
67                            (cons command-name args)
68                            command-name)
69          ,@body)
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))))))
76
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.
80 "
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)
85                         #f)))
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"))
89                   siglen arglen args))
90     (if error-msg
91         (ly:error
92          (string-append
93           make-name ": "
94           (_ "Invalid argument in position ~A.  Expect: ~A, found: ~S.")
95           error-msg))
96         (cons markup-function args))))
97
98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 ;;; markup constructors
100 ;;; lilypond-like syntax for markup construction in scheme.
101
102 (use-modules (ice-9 optargs)
103              (ice-9 receive))
104
105 (defmacro*-public markup (#:rest body)
106   "The `markup' macro provides a lilypond-like syntax for building markups.
107
108  - #:COMMAND is used instead of \\COMMAND
109  - #:lines ( ... ) is used instead of { ... }
110  - #:center-align ( ... ) is used instead of \\center-align < ... >
111  - etc.
112
113 Example:
114   \\markup { foo
115             \\raise #0.2 \\hbracket \\bold bar
116             \\override #'(baseline-skip . 4)
117             \\bracket \\column < baz bazr bla >
118   }
119          <==>
120   (markup \"foo\"
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."
125   
126   (car (compile-all-markup-expressions `(#:line ,body))))
127
128 (defmacro*-public markup* (#:rest body)
129   "Same as `markup', for use in a \\notes block."
130   `(ly:export (markup ,@body)))
131   
132   
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)
136   ===>
137   ((make-COMMAND1-markup arg11 arg12)
138    (make-COMMAND2-markup arg21 arg22 arg23) ...)"
139   (do ((rest expr rest)
140        (markps '() markps))
141       ((null? rest) (reverse markps))
142     (receive (m r) (compile-markup-expression rest)
143              (set! markps (cons m markps))
144              (set! rest r))))
145
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")))
149
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)))
161            (do ((i 0 (1+ i))
162                 (args '() args)
163                 (rest (cdr expr) rest))
164                ((>= i sig-len)
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)))
170                    (else
171                     ;; pick up one arg in `rest'
172                     (receive (a r) (compile-markup-arg rest)
173                              (set! args (cons a args))
174                              (set! rest r)))))))
175         ((and (pair? expr)
176               (pair? (car expr))
177               (keyword? (caar expr)))
178          ;; expr === ((#:COMMAND arg1 ...) ...)
179          (receive (m r) (compile-markup-expression (car expr))
180                   (values m (cdr expr))))
181         ((and (pair? expr)
182               (string? (car expr))) ;; expr === ("string" ...)
183          (values `(make-simple-markup ,(car expr)) (cdr expr)))
184         (else
185          ;; expr === (symbol ...) or ((funcall ...) ...)
186          (values (car expr)
187                  (cdr expr)))))
188
189 (define (compile-all-markup-args expr)
190   "Transform `expr' into markup arguments"
191   (do ((rest expr rest)
192        (args '() args))
193       ((null? rest) (reverse args))
194     (receive (a r) (compile-markup-arg rest)
195              (set! args (cons a args))
196              (set! rest r))))
197
198 (define (compile-markup-arg expr)
199   "Return two values: the desired markup argument, and the rest arguments"
200   (cond ((null? expr)
201          ;; no more args
202          (values '() '()))
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))
216                               (cdr expr)))))
217         ((and (pair? (car expr))
218               (pair? (caar expr)))
219          ;; expr === (((foo ...) ...) ...)
220          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
221         (else (values (car expr) (cdr expr)))))
222
223 ;;;;;;;;;;;;;;;
224 ;;; Debugging utilities: print markup expressions in a friendly fashion
225
226 (use-modules (ice-9 format))
227 (define (markup->string markup-expr)
228   "Return a string describing, in LilyPond syntax, the given markup expression."
229   (define (proc->command proc)
230     (let ((cmd-markup (symbol->string (procedure-name proc))))
231       (substring cmd-markup 0 (- (string-length cmd-markup)
232                                  (string-length "-markup")))))
233   (define (arg->string arg)
234     (cond ((and (pair? arg) (pair? (car arg))) ;; markup list
235            (format #f "~{ ~a~}" (map markup->string arg)))
236           ((pair? arg)                         ;; markup
237            (markup->string arg))
238           ((string? arg)                       ;; scheme string argument
239            (format #f "#\"~a\"" arg))
240           (else                                ;; other scheme arg
241            (format #f "#~a" arg))))
242   (let ((cmd (car markup-expr))
243         (args (cdr markup-expr)))
244     (cond ((eqv? cmd simple-markup) ;; a simple string
245            (format #f "\"~a\"" (car args)))
246           ((eqv? cmd line-markup)   ;; { ... }
247            (format #f "{~a}" (arg->string (car args))))
248           ((eqv? cmd center-align-markup) ;; \center < ... >
249            (format #f "\\center-align <~a>" (arg->string (car args))))
250           ((eqv? cmd column-markup) ;; \column < ... >
251            (format #f "\\column <~a>" (arg->string (car args))))
252           (else                ;; \command ...
253            (format #f "\\~a~{ ~a~} " (proc->command cmd) (map arg->string args))))))
254
255 (define-public (display-markup markup-expr)
256   "Print a LilyPond-syntax equivalent for the given markup expression."
257   (display "\\markup ")
258   (display (markup->string markup-expr)))
259
260 ;;;;;;;;;;;;;;;
261 ;;; Utilities for storing and accessing markup commands signature
262 ;;; and keyword.
263 ;;; Examples:
264 ;;;
265 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
266 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
267 ;;;
268 ;;; (markup-command-signature raise-markup)
269 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
270 ;;;
271 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
272 ;;; 
273
274 (define markup-command-signatures (make-hash-table 50))
275
276 (define (markup-command-signature-ref markup-command)
277   "Return markup-command's signature, e.g. (number? markup?).
278 markup-command may be a procedure."
279   (let ((sig-key (hashq-ref markup-command-signatures
280                             markup-command)))
281     (if sig-key (car sig-key) #f)))
282
283 (define-public (markup-command-keyword markup-command)
284   "Return markup-command's keyword, e.g. \"scheme0markup1\".
285 markup-command may be a procedure."
286   (let ((sig-key (hashq-ref markup-command-signatures
287                             markup-command)))
288     (if sig-key (cdr sig-key) #f)))
289
290 (define (markup-command-signatureset! markup-command signature)
291   "Set markup-command's signature. markup-command must be a named procedure.
292 Also set markup-signature and markup-keyword object properties."
293   (hashq-set! markup-command-signatures
294               markup-command
295               (cons signature (markup-signature-to-keyword signature)))
296   ;; these object properties are still in use somewhere
297   (set-object-property! markup-command 'markup-signature signature)
298   (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
299   
300 (define-public markup-command-signature
301   (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
302
303 (define (markup-symbol-to-proc markup-sym)
304   "Return the markup command procedure which name is `markup-sym', if any."
305   (hash-fold (lambda (key val prev)
306                             (or prev
307                                 (if (eqv? (procedure-name key) markup-sym) key #f)))
308              #f
309              markup-command-signatures))
310
311 (define-public markup-function-list '())
312
313 (define-public (markup-signature-to-keyword sig)
314   " (A B C) -> a0-b1-c2 "
315   (if (null? sig)
316       'empty
317       (string->symbol (string-join (map
318                                     (let* ((count 0))
319                                       (lambda (func)
320                                         (set! count (+ count 1))
321                                         (string-append
322                                          ;; for reasons I don't get,
323                                          ;; (case func ((markup?) .. )
324                                          ;; doesn't work.
325                                          (cond 
326                                           ((eq? func markup?) "markup")
327                                           ((eq? func markup-list?) "markup-list")
328                                           (else "scheme"))
329                                          (number->string (- count 1)))))
330                                     sig)
331                          "-"))))
332
333 (define-public (lookup-markup-command code)
334   (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
335     (and proc (cons proc (markup-command-keyword proc)))))
336
337 ;;;;;;;;;;;;;;;;;;;;;;
338 ;;; used in parser.yy to map a list of markup commands on markup arguments
339 (define-public (map-markup-command-list commands markups)
340   "`markups' being a list of markups, eg (markup1 markup2 markup3),
341 and `commands' a list of commands with their scheme arguments, in reverse order,
342 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
343  ((bold (raise 4 (italic markup1)))
344   (bold (raise 4 (italic markup2)))
345   (bold (raise 4 (italic markup3))))
346 "
347   (map-in-order (lambda (arg)
348                   (let ((result arg))
349                     (for-each (lambda (cmd)
350                                 (set! result (append cmd (list result))))
351                               commands)
352                     result))
353                 markups))
354
355 ;;;;;;;;;;;;;;;;;;;;;;
356 ;;; markup type predicates
357
358 (define (markup-function? x)
359   (not (not (markup-command-signature x))))
360
361 (define-public (markup-list? arg)
362   (define (markup-list-inner? lst)
363     (or (null? lst)
364         (and (markup? (car lst)) (markup-list-inner? (cdr lst)))))
365   (and (list? arg) (markup-list-inner? arg)))
366
367 (define (markup-argument-list? signature arguments)
368   "Typecheck argument list."
369   (if (and (pair? signature) (pair? arguments))
370       (and ((car signature) (car arguments))
371            (markup-argument-list? (cdr signature) (cdr arguments)))
372       (and (null? signature) (null? arguments))))
373
374
375 (define (markup-argument-list-error signature arguments number)
376   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
377 #f is no error found.
378 "
379   (if (and (pair? signature) (pair? arguments))
380       (if (not ((car signature) (car arguments)))
381           (list number (type-name (car signature)) (car arguments))
382           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
383       #f))
384
385 ;;
386 ;; full recursive typecheck.
387 ;;
388 (define (markup-typecheck? arg)
389   (or (string? arg)
390       (and (pair? arg)
391            (markup-function? (car arg))
392            (markup-argument-list? (markup-command-signature (car arg))
393                                   (cdr arg)))))
394
395 ;; 
396 ;; typecheck, and throw an error when something amiss.
397 ;; 
398 (define (markup-thrower-typecheck arg)
399   (cond ((string? arg) #t)
400         ((not (pair? arg))
401          (throw 'markup-format "Not a pair" arg))
402         ((not (markup-function? (car arg)))
403          (throw 'markup-format "Not a markup function " (car arg)))
404         ((not (markup-argument-list? (markup-command-signature (car arg))
405                                      (cdr arg)))
406          (throw 'markup-format "Arguments failed  typecheck for " arg)))
407   #t)
408
409 ;;
410 ;; good enough if you only  use make-XXX-markup functions.
411 ;; 
412 (define (cheap-markup? x)
413   (or (string? x)
414       (and (pair? x)
415            (markup-function? (car x)))))
416
417 ;;
418 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
419 ;; 
420 (define-public markup? cheap-markup?)
421
422 ;; utility
423
424 (define (markup-join markups sep)
425   "Return line-markup of MARKUPS, joining them with markup SEP"
426   (if (pair? markups)
427       (make-line-markup (list-insert-separator markups sep))
428       empty-markup))
429
430
431 (define-public interpret-markup Text_interface::interpret_markup)
432 (define-public (prepend-alist-chain key val chain)
433   (cons (acons key val (car chain)) (cdr chain)))
434
435 (define-public (stack-stencil-line space stencils)
436   "DOCME"
437   (if (and (pair? stencils)
438            (ly:stencil? (car stencils)))
439       
440       (if (and (pair? (cdr stencils))
441                (ly:stencil? (cadr stencils)))
442           (let* ((tail (stack-stencil-line  space (cdr stencils)))
443                  (head (car stencils))
444                  (xoff (+ space (cdr (ly:stencil-extent head X)))))
445             (ly:stencil-add head
446                              (ly:stencil-translate-axis tail xoff X)))
447           (car stencils))
448       (ly:make-stencil '() '(0 . 0) '(0 . 0))))
449
450
451
452
453
454
455