]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup.scm
81614a68839fcddf458fd105d841fc4301ccd2dc
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.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 builtin markup command, use the define-builtin-markup-command
24 utility. In a user file, the define-markup-command macro shall be used
25 (see ly/markup-init.ly).
26
27   (define-markup-command (mycommand layout prop arg1 ...) (arg1-type? ...)
28     \"my command usage and description\"
29     ...function body...)
30
31 The command is now available in markup mode, e.g.
32
33   \\markup { .... \\MYCOMMAND #1 argument ... }
34
35 " ; "
36
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;;; markup definer utilities
39
40 ;; For documentation purposes
41 ;; category -> markup functions
42 (define-public markup-functions-by-category (make-hash-table 150))
43 ;; markup function -> used properties
44 (define-public markup-functions-properties (make-hash-table 150))
45 ;; List of markup list functions
46 (define-public markup-list-function-list (list))
47
48 (define-macro (define-builtin-markup-command command-and-args signature
49                 category properties-or-copied-function . body)
50   "
51 * Define a COMMAND-markup function after command-and-args and body,
52 register COMMAND-markup and its signature,
53
54 * add COMMAND-markup to markup-functions-by-category,
55
56 * sets COMMAND-markup markup-signature and markup-keyword object properties,
57
58 * define a make-COMMAND-markup function.
59
60 Syntax:
61   (define-builtin-markup-command (COMMAND layout props . arguments)
62                                  argument-types
63                                  category
64                                  properties
65     \"documentation string\"
66     ...command body...)
67  or:
68   (define-builtin-markup-command COMMAND
69                                  argument-types
70                                  category
71                                  function)
72
73 where:
74   argument-types is a list of type predicates for arguments
75   category is either a symbol or a symbol list
76   properties a list of (property default-value) lists or COMMANDx-markup elements
77     (when a COMMANDx-markup is found, the properties of the said commandx are
78     added instead). No check is performed against cyclical references!
79 "
80   (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
81          (args (if (pair? command-and-args) (cdr command-and-args) '()))
82          (command-name (string->symbol (format #f "~a-markup" command)))
83          (make-markup-name (string->symbol (format #f "make-~a-markup" command))))
84     `(begin
85        ;; define the COMMAND-markup function
86        ,(if (pair? args)
87             (let ((documentation (car body))
88                   (real-body (cdr body))
89                   (properties properties-or-copied-function))
90               `(define-public (,command-name ,@args)
91                  ,documentation
92                  (let ,(filter identity
93                                (map (lambda (prop-spec)
94                                       (if (pair? prop-spec)
95                                           (let ((prop (car prop-spec))
96                                                 (default-value (if (null? (cdr prop-spec))
97                                                                    #f
98                                                                    (cadr prop-spec)))
99                                                 (props (cadr args)))
100                                             `(,prop (chain-assoc-get ',prop ,props ,default-value)))
101                                           #f))
102                                     properties))
103                    ,@real-body)))
104             (let ((args (gensym "args"))
105                   (markup-command properties-or-copied-function))
106               `(define-public (,command-name . ,args)
107                  ,(format #f "Copy of the ~a command." markup-command)
108                  (apply ,markup-command ,args))))
109        (set! (markup-command-signature ,command-name) (list ,@signature))
110        ;; Register the new function, for markup documentation
111        ,@(map (lambda (category)
112                 `(hashq-set! markup-functions-by-category ',category
113                              (cons ,command-name
114                                    (or (hashq-ref markup-functions-by-category ',category)
115                                        (list)))))
116               (if (list? category) category (list category)))
117        ;; Used properties, for markup documentation
118        (hashq-set! markup-functions-properties
119                    ,command-name
120                    (list ,@(map (lambda (prop-spec)
121                                   (cond ((symbol? prop-spec)
122                                          prop-spec)
123                                          ((not (null? (cdr prop-spec)))
124                                           `(list ',(car prop-spec) ,(cadr prop-spec)))
125                                          (else
126                                           `(list ',(car prop-spec)))))
127                                 (if (pair? args)
128                                     properties-or-copied-function
129                                     (list)))))
130        ;; define the make-COMMAND-markup function
131        (define-public (,make-markup-name . args)
132          (let ((sig (list ,@signature)))
133            (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
134
135 (define-macro (define-builtin-markup-list-command command-and-args signature
136                 properties . body)
137   "Same as `define-builtin-markup-command, but defines a command that, when
138 interpreted, returns a list of stencils instead os a single one"
139   (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
140          (args (if (pair? command-and-args) (cdr command-and-args) '()))
141          (command-name (string->symbol (format #f "~a-markup-list" command)))
142          (make-markup-name (string->symbol (format #f "make-~a-markup-list" command))))
143     `(begin
144        ;; define the COMMAND-markup-list function
145        ,(if (pair? args)
146             (let ((documentation (car body))
147                   (real-body (cdr body)))
148               `(define-public (,command-name ,@args)
149                  ,documentation
150                  (let ,(filter identity
151                                (map (lambda (prop-spec)
152                                       (if (pair? prop-spec)
153                                           (let ((prop (car prop-spec))
154                                                 (default-value (if (null? (cdr prop-spec))
155                                                                    #f
156                                                                    (cadr prop-spec)))
157                                                 (props (cadr args)))
158                                             `(,prop (chain-assoc-get ',prop ,props ,default-value)))
159                                           #f))
160                                     properties))
161                    ,@body)))
162             (let ((args (gensym "args"))
163                   (markup-command (car body)))
164             `(define-public (,command-name . ,args)
165                ,(format #f "Copy of the ~a command." markup-command)
166                (apply ,markup-command ,args))))
167        (set! (markup-command-signature ,command-name) (list ,@signature))
168        ;; add the command to markup-list-function-list, for markup documentation
169        (if (not (member ,command-name markup-list-function-list))
170            (set! markup-list-function-list (cons ,command-name
171                                                  markup-list-function-list)))
172        ;; Used properties, for markup documentation
173        (hashq-set! markup-functions-properties
174                    ,command-name
175                    (list ,@(map (lambda (prop-spec)
176                                   (cond ((symbol? prop-spec)
177                                          prop-spec)
178                                          ((not (null? (cdr prop-spec)))
179                                           `(list ',(car prop-spec) ,(cadr prop-spec)))
180                                          (else
181                                           `(list ',(car prop-spec)))))
182                                 (if (pair? args)
183                                     properties
184                                     (list)))))
185        ;; it's a markup-list command:
186        (set-object-property! ,command-name 'markup-list-command #t)
187        ;; define the make-COMMAND-markup-list function
188        (define-public (,make-markup-name . args)
189          (let ((sig (list ,@signature)))
190            (list (make-markup ,command-name
191                               ,(symbol->string make-markup-name) sig args)))))))
192
193 (define-public (make-markup markup-function make-name signature args)
194   " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
195 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
196 "
197   (let* ((arglen (length args))
198          (siglen (length signature))
199          (error-msg (if (and (> siglen 0) (> arglen 0))
200                         (markup-argument-list-error signature args 1)
201                         #f)))
202     (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
203         (ly:error (string-append make-name ": "
204                    (_ "Wrong number of arguments.  Expect: ~A, found ~A: ~S"))
205                   siglen arglen args))
206     (if error-msg
207         (ly:error
208          (string-append
209           make-name ": "
210           (_ "Invalid argument in position ~A.  Expect: ~A, found: ~S.")
211           error-msg))
212         (cons markup-function args))))
213
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 ;;; markup constructors
216 ;;; lilypond-like syntax for markup construction in scheme.
217
218 (use-modules (ice-9 optargs)
219              (ice-9 receive))
220
221 (defmacro*-public markup (#:rest body)
222   "The `markup' macro provides a lilypond-like syntax for building markups.
223
224  - #:COMMAND is used instead of \\COMMAND
225  - #:line ( ... ) is used instead of \\line { ... }
226  - etc.
227
228 Example:
229   \\markup { foo
230             \\raise #0.2 \\hbracket \\bold bar
231             \\override #'(baseline-skip . 4)
232             \\bracket \\column { baz bazr bla }
233   }
234          <==>
235   (markup \"foo\"
236           #:raise 0.2 #:hbracket #:bold \"bar\"
237           #:override '(baseline-skip . 4) 
238           #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
239 Use `markup*' in a \\notemode context."
240   
241   (car (compile-all-markup-expressions `(#:line ,body))))
242
243 (defmacro*-public markup* (#:rest body)
244   "Same as `markup', for use in a \\notes block."
245   `(ly:export (markup ,@body)))
246   
247   
248 (define (compile-all-markup-expressions expr)
249   "Return a list of canonical markups expressions, e.g.:
250   (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
251   ===>
252   ((make-COMMAND1-markup arg11 arg12)
253    (make-COMMAND2-markup arg21 arg22 arg23) ...)"
254   (do ((rest expr rest)
255        (markps '() markps))
256       ((null? rest) (reverse markps))
257     (receive (m r) (compile-markup-expression rest)
258              (set! markps (cons m markps))
259              (set! rest r))))
260
261 (define (keyword->make-markup key)
262   "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
263   (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
264
265 (define (compile-markup-expression expr)
266   "Return two values: the first complete canonical markup expression
267    found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
268    and the rest expression."
269   (cond ((and (pair? expr)
270               (keyword? (car expr)))
271          ;; expr === (#:COMMAND arg1 ...)
272          (let ((command (symbol->string (keyword->symbol (car expr)))))
273             (if (not (pair? (lookup-markup-command command)))
274                 (ly:error (_ "Not a markup command: ~A") command))
275             (let* ((sig (markup-command-signature
276                          (car (lookup-markup-command command))))
277                    (sig-len (length sig)))
278               (do ((i 0 (1+ i))
279                    (args '() args)
280                    (rest (cdr expr) rest))
281                   ((>= i sig-len)
282                    (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
283                 (cond ((eqv? (list-ref sig i) markup-list?)
284                        ;; (car rest) is a markup list
285                        (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
286                        (set! rest (cdr rest)))
287                       (else
288                        ;; pick up one arg in `rest'
289                        (receive (a r) (compile-markup-arg rest)
290                          (set! args (cons a args))
291                          (set! rest r))))))))
292         ((and (pair? expr)
293               (pair? (car expr))
294               (keyword? (caar expr)))
295          ;; expr === ((#:COMMAND arg1 ...) ...)
296          (receive (m r) (compile-markup-expression (car expr))
297                   (values m (cdr expr))))
298         ((and (pair? expr)
299               (string? (car expr))) ;; expr === ("string" ...)
300          (values `(make-simple-markup ,(car expr)) (cdr expr)))
301         (else
302          ;; expr === (symbol ...) or ((funcall ...) ...)
303          (values (car expr)
304                  (cdr expr)))))
305
306 (define (compile-all-markup-args expr)
307   "Transform `expr' into markup arguments"
308   (do ((rest expr rest)
309        (args '() args))
310       ((null? rest) (reverse args))
311     (receive (a r) (compile-markup-arg rest)
312              (set! args (cons a args))
313              (set! rest r))))
314
315 (define (compile-markup-arg expr)
316   "Return two values: the desired markup argument, and the rest arguments"
317   (cond ((null? expr)
318          ;; no more args
319          (values '() '()))
320         ((keyword? (car expr))
321          ;; expr === (#:COMMAND ...)
322          ;; ==> build and return the whole markup expression
323          (compile-markup-expression expr))
324         ((and (pair? (car expr))
325               (keyword? (caar expr)))
326          ;; expr === ((#:COMMAND ...) ...)
327          ;; ==> build and return the whole markup expression(s)
328          ;; found in (car expr)
329          (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
330                   (if (null? rest-expr)
331                       (values markup-expr (cdr expr))
332                       (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
333                               (cdr expr)))))
334         ((and (pair? (car expr))
335               (pair? (caar expr)))
336          ;; expr === (((foo ...) ...) ...)
337          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
338         (else (values (car expr) (cdr expr)))))
339
340 ;;;;;;;;;;;;;;;
341 ;;; Utilities for storing and accessing markup commands signature
342 ;;; and keyword.
343 ;;; Examples:
344 ;;;
345 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
346 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
347 ;;;
348 ;;; (markup-command-signature raise-markup)
349 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
350 ;;;
351 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
352 ;;; 
353
354 (define-public (markup-command-keyword markup-command)
355   "Return markup-command's argument keyword, ie a string describing the command
356   arguments, eg. \"scheme0markup1\""
357   (object-property markup-command 'markup-keyword))
358
359 (define-public (markup-command-signature-ref markup-command)
360   "Return markup-command's signature (the 'markup-signature object property)"
361   (object-property markup-command 'markup-signature))
362
363 (define-public (markup-command-signature-set! markup-command signature)
364   "Set markup-command's signature and keyword (as object properties)"
365   (set-object-property! markup-command 'markup-signature signature)
366   (set-object-property! markup-command 'markup-keyword 
367                         (markup-signature-to-keyword signature))
368   signature)
369
370 (define-public markup-command-signature
371   (make-procedure-with-setter markup-command-signature-ref
372                               markup-command-signature-set!))
373
374 (define-public (markup-signature-to-keyword sig)
375   " (A B C) -> a0-b1-c2 "
376   (if (null? sig)
377       'empty
378       (string->symbol (string-join (map
379                                     (let* ((count 0))
380                                       (lambda (func)
381                                         (set! count (+ count 1))
382                                         (string-append
383                                          ;; for reasons I don't get,
384                                          ;; (case func ((markup?) .. )
385                                          ;; doesn't work.
386                                          (cond 
387                                           ((eq? func markup?) "markup")
388                                           ((eq? func markup-list?) "markup-list")
389                                           (else "scheme"))
390                                          (number->string (- count 1)))))
391                                     sig)
392                          "-"))))
393
394 (define (lookup-markup-command-aux symbol)
395   (let ((proc (catch 'misc-error
396                 (lambda ()
397                   (module-ref (current-module) symbol))
398                 (lambda (key . args) #f))))
399     (and (procedure? proc) proc)))
400
401 (define-public (lookup-markup-command code)
402   (let ((proc (lookup-markup-command-aux
403                (string->symbol (format #f "~a-markup" code)))))
404     (and proc (markup-function? proc)
405          (cons proc (markup-command-keyword proc)))))
406
407 (define-public (lookup-markup-list-command code)
408   (let ((proc (lookup-markup-command-aux
409                (string->symbol (format #f "~a-markup-list" code)))))
410      (and proc (markup-list-function? proc)
411           (cons proc (markup-command-keyword proc)))))
412
413 ;;;;;;;;;;;;;;;;;;;;;;
414 ;;; used in parser.yy to map a list of markup commands on markup arguments
415 (define-public (map-markup-command-list commands markups)
416   "`markups' being a list of markups, eg (markup1 markup2 markup3),
417 and `commands' a list of commands with their scheme arguments, in reverse order,
418 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
419  ((bold (raise 4 (italic markup1)))
420   (bold (raise 4 (italic markup2)))
421   (bold (raise 4 (italic markup3))))
422 "
423   (map-in-order (lambda (arg)
424                   (let ((result arg))
425                     (for-each (lambda (cmd)
426                                 (set! result (append cmd (list result))))
427                               commands)
428                     result))
429                 markups))
430
431 ;;;;;;;;;;;;;;;;;;;;;;
432 ;;; markup type predicates
433
434 (define (markup-function? x)
435   (and (markup-command-signature x)
436        (not (object-property x 'markup-list-command))))
437
438 (define (markup-list-function? x)
439   (and (markup-command-signature x)
440        (object-property x 'markup-list-command)))
441
442 (define-public (markup-command-list? x)
443   "Determine if `x' is a markup command list, ie. a list composed of
444 a markup list function and its arguments."
445   (and (pair? x) (markup-list-function? (car x))))
446
447 (define-public (markup-list? arg)
448   "Return a true value if `x' is a list of markups or markup command lists."
449   (define (markup-list-inner? lst)
450     (or (null? lst)
451         (and (or (markup? (car lst)) (markup-command-list? (car lst)))
452              (markup-list-inner? (cdr lst)))))
453   (not (not (and (list? arg) (markup-list-inner? arg)))))
454
455 (define (markup-argument-list? signature arguments)
456   "Typecheck argument list."
457   (if (and (pair? signature) (pair? arguments))
458       (and ((car signature) (car arguments))
459            (markup-argument-list? (cdr signature) (cdr arguments)))
460       (and (null? signature) (null? arguments))))
461
462
463 (define (markup-argument-list-error signature arguments number)
464   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
465 #f is no error found.
466 "
467   (if (and (pair? signature) (pair? arguments))
468       (if (not ((car signature) (car arguments)))
469           (list number (type-name (car signature)) (car arguments))
470           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
471       #f))
472
473 ;;
474 ;; full recursive typecheck.
475 ;;
476 (define (markup-typecheck? arg)
477   (or (string? arg)
478       (and (pair? arg)
479            (markup-function? (car arg))
480            (markup-argument-list? (markup-command-signature (car arg))
481                                   (cdr arg)))))
482
483 ;; 
484 ;; 
485 ;;
486 ;; 
487 (define (markup-thrower-typecheck arg)
488   "typecheck, and throw an error when something amiss.
489
490 Uncovered - cheap-markup? is used."
491
492   (cond ((string? arg) #t)
493         ((not (pair? arg))
494          (throw 'markup-format "Not a pair" arg))
495         ((not (markup-function? (car arg)))
496          (throw 'markup-format "Not a markup function " (car arg)))
497         ((not (markup-argument-list? (markup-command-signature (car arg))
498                                      (cdr arg)))
499          (throw 'markup-format "Arguments failed  typecheck for " arg)))
500   #t)
501
502 ;;
503 ;; good enough if you only  use make-XXX-markup functions.
504 ;; 
505 (define (cheap-markup? x)
506   (or (string? x)
507       (and (pair? x)
508            (markup-function? (car x)))))
509
510 ;;
511 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
512 ;; 
513 (define-public markup? cheap-markup?)
514
515 ;; utility
516
517 (define (markup-join markups sep)
518   "Return line-markup of MARKUPS, joining them with markup SEP"
519   (if (pair? markups)
520       (make-line-markup (list-insert-separator markups sep))
521       empty-markup))
522
523
524 (define-public interpret-markup ly:text-interface::interpret-markup)
525
526 (define-public (interpret-markup-list layout props markup-list)
527   (let ((stencils (list)))
528     (for-each (lambda (m)
529                 (set! stencils
530                       (if (markup-command-list? m)
531                           (append! (reverse! (apply (car m) layout props (cdr m)))
532                                    stencils)
533                           (cons (interpret-markup layout props m) stencils))))
534               markup-list)
535     (reverse! stencils)))
536
537 (define-public (prepend-alist-chain key val chain)
538   (cons (acons key val (car chain)) (cdr chain)))
539
540 (define-public (stack-stencil-line space stencils)
541   "DOCME"
542   (if (and (pair? stencils)
543            (ly:stencil? (car stencils)))
544       
545       (if (and (pair? (cdr stencils))
546                (ly:stencil? (cadr stencils)))
547           (let* ((tail (stack-stencil-line space (cdr stencils)))
548                  (head (car stencils))
549                  (xoff (+ space (cdr (ly:stencil-extent head X)))))
550             (ly:stencil-add head
551                              (ly:stencil-translate-axis tail xoff X)))
552           (car stencils))
553       (ly:make-stencil '() '(0 . 0) '(0 . 0))))
554