]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup.scm
Merge branch 'master' of git://git.sv.gnu.org/lilypond
[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 (define-macro (define-builtin-markup-command command-and-args signature . body)
41   "
42 * Define a COMMAND-markup function after command-and-args and body,
43 register COMMAND-markup and its signature,
44
45 * add COMMAND-markup to markup-function-list,
46
47 * sets COMMAND-markup markup-signature and markup-keyword object properties,
48
49 * define a make-COMMAND-markup function.
50
51 Syntax:
52   (define-builtin-markup-command (COMMAND layout props arg1 arg2 ...)
53                                  (arg1-type? arg2-type? ...)
54     \"documentation string\"
55     ...command body...)
56  or:
57   (define-builtin-markup-command COMMAND (arg1-type? arg2-type? ...)
58     function)
59 "
60   (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
61          (args (if (pair? command-and-args) (cdr command-and-args) '()))
62          (command-name (string->symbol (format #f "~a-markup" command)))
63          (make-markup-name (string->symbol (format #f "make-~a-markup" command))))
64     `(begin
65        ;; define the COMMAND-markup function
66        ,(if (pair? args)
67             `(define-public (,command-name ,@args)
68                ,@body)
69             (let ((args (gensym "args"))
70                   (markup-command (car body)))
71             `(define-public (,command-name . ,args)
72                ,(format #f "Copy of the ~a command." markup-command)
73                (apply ,markup-command ,args))))
74        (set! (markup-command-signature ,command-name) (list ,@signature))
75        ;; add the command to markup-function-list, for markup documentation
76        (if (not (member ,command-name markup-function-list))
77            (set! markup-function-list (cons ,command-name markup-function-list)))
78        ;; define the make-COMMAND-markup function
79        (define-public (,make-markup-name . args)
80          (let ((sig (list ,@signature)))
81            (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
82
83 (define-macro (define-builtin-markup-list-command command-and-args signature . body)
84   "Same as `define-builtin-markup-command, but defines a command that, when
85 interpreted, returns a list of stencils instead os a single one"
86   (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
87          (args (if (pair? command-and-args) (cdr command-and-args) '()))
88          (command-name (string->symbol (format #f "~a-markup-list" command)))
89          (make-markup-name (string->symbol (format #f "make-~a-markup-list" command))))
90     `(begin
91        ;; define the COMMAND-markup-list function
92        ,(if (pair? args)
93             `(define-public (,command-name ,@args)
94                ,@body)
95             (let ((args (gensym "args"))
96                   (markup-command (car body)))
97             `(define-public (,command-name . ,args)
98                ,(format #f "Copy of the ~a command." markup-command)
99                (apply ,markup-command ,args))))
100        (set! (markup-command-signature ,command-name) (list ,@signature))
101        ;; add the command to markup-list-function-list, for markup documentation
102        (if (not (member ,command-name markup-list-function-list))
103            (set! markup-list-function-list (cons ,command-name
104                                                  markup-list-function-list)))
105        ;; it's a markup-list command:
106        (set-object-property! ,command-name 'markup-list-command #t)
107        ;; define the make-COMMAND-markup-list function
108        (define-public (,make-markup-name . args)
109          (let ((sig (list ,@signature)))
110            (list (make-markup ,command-name
111                               ,(symbol->string make-markup-name) sig args)))))))
112
113 (define-public (make-markup markup-function make-name signature args)
114   " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
115 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
116 "
117   (let* ((arglen (length args))
118          (siglen (length signature))
119          (error-msg (if (and (> siglen 0) (> arglen 0))
120                         (markup-argument-list-error signature args 1)
121                         #f)))
122     (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
123         (ly:error (string-append make-name ": "
124                    (_ "Wrong number of arguments.  Expect: ~A, found ~A: ~S"))
125                   siglen arglen args))
126     (if error-msg
127         (ly:error
128          (string-append
129           make-name ": "
130           (_ "Invalid argument in position ~A.  Expect: ~A, found: ~S.")
131           error-msg))
132         (cons markup-function args))))
133
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 ;;; markup constructors
136 ;;; lilypond-like syntax for markup construction in scheme.
137
138 (use-modules (ice-9 optargs)
139              (ice-9 receive))
140
141 (defmacro*-public markup (#:rest body)
142   "The `markup' macro provides a lilypond-like syntax for building markups.
143
144  - #:COMMAND is used instead of \\COMMAND
145  - #:line ( ... ) is used instead of \\line { ... }
146  - etc.
147
148 Example:
149   \\markup { foo
150             \\raise #0.2 \\hbracket \\bold bar
151             \\override #'(baseline-skip . 4)
152             \\bracket \\column { baz bazr bla }
153   }
154          <==>
155   (markup \"foo\"
156           #:raise 0.2 #:hbracket #:bold \"bar\"
157           #:override '(baseline-skip . 4) 
158           #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
159 Use `markup*' in a \\notemode context."
160   
161   (car (compile-all-markup-expressions `(#:line ,body))))
162
163 (defmacro*-public markup* (#:rest body)
164   "Same as `markup', for use in a \\notes block."
165   `(ly:export (markup ,@body)))
166   
167   
168 (define (compile-all-markup-expressions expr)
169   "Return a list of canonical markups expressions, e.g.:
170   (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
171   ===>
172   ((make-COMMAND1-markup arg11 arg12)
173    (make-COMMAND2-markup arg21 arg22 arg23) ...)"
174   (do ((rest expr rest)
175        (markps '() markps))
176       ((null? rest) (reverse markps))
177     (receive (m r) (compile-markup-expression rest)
178              (set! markps (cons m markps))
179              (set! rest r))))
180
181 (define (keyword->make-markup key)
182   "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
183   (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
184
185 (define (compile-markup-expression expr)
186   "Return two values: the first complete canonical markup expression
187    found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
188    and the rest expression."
189   (cond ((and (pair? expr)
190               (keyword? (car expr)))
191          ;; expr === (#:COMMAND arg1 ...)
192          (let ((command (symbol->string (keyword->symbol (car expr)))))
193             (if (not (pair? (lookup-markup-command command)))
194                 (ly:error (_ "Not a markup command: ~A") command))
195             (let* ((sig (markup-command-signature
196                          (car (lookup-markup-command command))))
197                    (sig-len (length sig)))
198               (do ((i 0 (1+ i))
199                    (args '() args)
200                    (rest (cdr expr) rest))
201                   ((>= i sig-len)
202                    (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
203                 (cond ((eqv? (list-ref sig i) markup-list?)
204                        ;; (car rest) is a markup list
205                        (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
206                        (set! rest (cdr rest)))
207                       (else
208                        ;; pick up one arg in `rest'
209                        (receive (a r) (compile-markup-arg rest)
210                          (set! args (cons a args))
211                          (set! rest r))))))))
212         ((and (pair? expr)
213               (pair? (car expr))
214               (keyword? (caar expr)))
215          ;; expr === ((#:COMMAND arg1 ...) ...)
216          (receive (m r) (compile-markup-expression (car expr))
217                   (values m (cdr expr))))
218         ((and (pair? expr)
219               (string? (car expr))) ;; expr === ("string" ...)
220          (values `(make-simple-markup ,(car expr)) (cdr expr)))
221         (else
222          ;; expr === (symbol ...) or ((funcall ...) ...)
223          (values (car expr)
224                  (cdr expr)))))
225
226 (define (compile-all-markup-args expr)
227   "Transform `expr' into markup arguments"
228   (do ((rest expr rest)
229        (args '() args))
230       ((null? rest) (reverse args))
231     (receive (a r) (compile-markup-arg rest)
232              (set! args (cons a args))
233              (set! rest r))))
234
235 (define (compile-markup-arg expr)
236   "Return two values: the desired markup argument, and the rest arguments"
237   (cond ((null? expr)
238          ;; no more args
239          (values '() '()))
240         ((keyword? (car expr))
241          ;; expr === (#:COMMAND ...)
242          ;; ==> build and return the whole markup expression
243          (compile-markup-expression expr))
244         ((and (pair? (car expr))
245               (keyword? (caar expr)))
246          ;; expr === ((#:COMMAND ...) ...)
247          ;; ==> build and return the whole markup expression(s)
248          ;; found in (car expr)
249          (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
250                   (if (null? rest-expr)
251                       (values markup-expr (cdr expr))
252                       (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
253                               (cdr expr)))))
254         ((and (pair? (car expr))
255               (pair? (caar expr)))
256          ;; expr === (((foo ...) ...) ...)
257          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
258         (else (values (car expr) (cdr 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-public (markup-command-keyword markup-command)
275   "Return markup-command's argument keyword, ie a string describing the command
276   arguments, eg. \"scheme0markup1\""
277   (object-property markup-command 'markup-keyword))
278
279 (define-public (markup-command-signature-ref markup-command)
280   "Return markup-command's signature (the 'markup-signature object property)"
281   (object-property markup-command 'markup-signature))
282
283 (define-public (markup-command-signature-set! markup-command signature)
284   "Set markup-command's signature and keyword (as object properties)"
285   (set-object-property! markup-command 'markup-signature signature)
286   (set-object-property! markup-command 'markup-keyword 
287                         (markup-signature-to-keyword signature))
288   signature)
289
290 (define-public markup-command-signature
291   (make-procedure-with-setter markup-command-signature-ref
292                               markup-command-signature-set!))
293
294 ;; For documentation purposes
295 (define-public markup-function-list (list))
296 (define-public markup-list-function-list (list))
297
298 (define-public (markup-signature-to-keyword sig)
299   " (A B C) -> a0-b1-c2 "
300   (if (null? sig)
301       'empty
302       (string->symbol (string-join (map
303                                     (let* ((count 0))
304                                       (lambda (func)
305                                         (set! count (+ count 1))
306                                         (string-append
307                                          ;; for reasons I don't get,
308                                          ;; (case func ((markup?) .. )
309                                          ;; doesn't work.
310                                          (cond 
311                                           ((eq? func markup?) "markup")
312                                           ((eq? func markup-list?) "markup-list")
313                                           (else "scheme"))
314                                          (number->string (- count 1)))))
315                                     sig)
316                          "-"))))
317
318 (define (lookup-markup-command-aux symbol)
319   (let ((proc (catch 'misc-error
320                 (lambda ()
321                   (module-ref (current-module) symbol))
322                 (lambda (key . args) #f))))
323     (and (procedure? proc) proc)))
324
325 (define-public (lookup-markup-command code)
326   (let ((proc (lookup-markup-command-aux
327                (string->symbol (format #f "~a-markup" code)))))
328     (and proc (markup-function? proc)
329          (cons proc (markup-command-keyword proc)))))
330
331 (define-public (lookup-markup-list-command code)
332   (let ((proc (lookup-markup-command-aux
333                (string->symbol (format #f "~a-markup-list" code)))))
334      (and proc (markup-list-function? proc)
335           (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   (and (markup-command-signature x)
360        (not (object-property x 'markup-list-command))))
361
362 (define (markup-list-function? x)
363   (and (markup-command-signature x)
364        (object-property x 'markup-list-command)))
365
366 (define-public (markup-command-list? x)
367   "Determine if `x' is a markup command list, ie. a list composed of
368 a markup list function and its arguments."
369   (and (pair? x) (markup-list-function? (car x))))
370
371 (define-public (markup-list? arg)
372   "Return a true value if `x' is a list of markups or markup command lists."
373   (define (markup-list-inner? lst)
374     (or (null? lst)
375         (and (or (markup? (car lst)) (markup-command-list? (car lst)))
376              (markup-list-inner? (cdr lst)))))
377   (not (not (and (list? arg) (markup-list-inner? arg)))))
378
379 (define (markup-argument-list? signature arguments)
380   "Typecheck argument list."
381   (if (and (pair? signature) (pair? arguments))
382       (and ((car signature) (car arguments))
383            (markup-argument-list? (cdr signature) (cdr arguments)))
384       (and (null? signature) (null? arguments))))
385
386
387 (define (markup-argument-list-error signature arguments number)
388   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
389 #f is no error found.
390 "
391   (if (and (pair? signature) (pair? arguments))
392       (if (not ((car signature) (car arguments)))
393           (list number (type-name (car signature)) (car arguments))
394           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
395       #f))
396
397 ;;
398 ;; full recursive typecheck.
399 ;;
400 (define (markup-typecheck? arg)
401   (or (string? arg)
402       (and (pair? arg)
403            (markup-function? (car arg))
404            (markup-argument-list? (markup-command-signature (car arg))
405                                   (cdr arg)))))
406
407 ;; 
408 ;; 
409 ;;
410 ;; 
411 (define (markup-thrower-typecheck arg)
412   "typecheck, and throw an error when something amiss.
413
414 Uncovered - cheap-markup? is used."
415
416   (cond ((string? arg) #t)
417         ((not (pair? arg))
418          (throw 'markup-format "Not a pair" arg))
419         ((not (markup-function? (car arg)))
420          (throw 'markup-format "Not a markup function " (car arg)))
421         ((not (markup-argument-list? (markup-command-signature (car arg))
422                                      (cdr arg)))
423          (throw 'markup-format "Arguments failed  typecheck for " arg)))
424   #t)
425
426 ;;
427 ;; good enough if you only  use make-XXX-markup functions.
428 ;; 
429 (define (cheap-markup? x)
430   (or (string? x)
431       (and (pair? x)
432            (markup-function? (car x)))))
433
434 ;;
435 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
436 ;; 
437 (define-public markup? cheap-markup?)
438
439 ;; utility
440
441 (define (markup-join markups sep)
442   "Return line-markup of MARKUPS, joining them with markup SEP"
443   (if (pair? markups)
444       (make-line-markup (list-insert-separator markups sep))
445       empty-markup))
446
447
448 (define-public interpret-markup ly:text-interface::interpret-markup)
449
450 (define-public (interpret-markup-list layout props markup-list)
451   (let ((stencils (list)))
452     (for-each (lambda (m)
453                 (set! stencils
454                       (if (markup-command-list? m)
455                           (append! (reverse! (apply (car m) layout props (cdr m)))
456                                    stencils)
457                           (cons (interpret-markup layout props m) stencils))))
458               markup-list)
459     (reverse! stencils)))
460
461 (define-public (prepend-alist-chain key val chain)
462   (cons (acons key val (car chain)) (cdr chain)))
463
464 (define-public (stack-stencil-line space stencils)
465   "DOCME"
466   (if (and (pair? stencils)
467            (ly:stencil? (car stencils)))
468       
469       (if (and (pair? (cdr stencils))
470                (ly:stencil? (cadr stencils)))
471           (let* ((tail (stack-stencil-line space (cdr stencils)))
472                  (head (car stencils))
473                  (xoff (+ space (cdr (ly:stencil-extent head X)))))
474             (ly:stencil-add head
475                              (ly:stencil-translate-axis tail xoff X)))
476           (car stencils))
477       (ly:make-stencil '() '(0 . 0) '(0 . 0))))
478