]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup.scm
Merge branch 'master' of ssh+git://gpercival@git.sv.gnu.org/srv/git/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                 (sig (markup-command-signature
194                       (car (lookup-markup-command command))))
195                 (sig-len (length sig)))
196            (do ((i 0 (1+ i))
197                 (args '() args)
198                 (rest (cdr expr) rest))
199                ((>= i sig-len)
200                 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
201              (cond ((eqv? (list-ref sig i) markup-list?)
202                     ;; (car rest) is a markup list
203                     (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
204                     (set! rest (cdr rest)))
205                    (else
206                     ;; pick up one arg in `rest'
207                     (receive (a r) (compile-markup-arg rest)
208                              (set! args (cons a args))
209                              (set! rest r)))))))
210         ((and (pair? expr)
211               (pair? (car expr))
212               (keyword? (caar expr)))
213          ;; expr === ((#:COMMAND arg1 ...) ...)
214          (receive (m r) (compile-markup-expression (car expr))
215                   (values m (cdr expr))))
216         ((and (pair? expr)
217               (string? (car expr))) ;; expr === ("string" ...)
218          (values `(make-simple-markup ,(car expr)) (cdr expr)))
219         (else
220          ;; expr === (symbol ...) or ((funcall ...) ...)
221          (values (car expr)
222                  (cdr expr)))))
223
224 (define (compile-all-markup-args expr)
225   "Transform `expr' into markup arguments"
226   (do ((rest expr rest)
227        (args '() args))
228       ((null? rest) (reverse args))
229     (receive (a r) (compile-markup-arg rest)
230              (set! args (cons a args))
231              (set! rest r))))
232
233 (define (compile-markup-arg expr)
234   "Return two values: the desired markup argument, and the rest arguments"
235   (cond ((null? expr)
236          ;; no more args
237          (values '() '()))
238         ((keyword? (car expr))
239          ;; expr === (#:COMMAND ...)
240          ;; ==> build and return the whole markup expression
241          (compile-markup-expression expr))
242         ((and (pair? (car expr))
243               (keyword? (caar expr)))
244          ;; expr === ((#:COMMAND ...) ...)
245          ;; ==> build and return the whole markup expression(s)
246          ;; found in (car expr)
247          (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
248                   (if (null? rest-expr)
249                       (values markup-expr (cdr expr))
250                       (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
251                               (cdr expr)))))
252         ((and (pair? (car expr))
253               (pair? (caar expr)))
254          ;; expr === (((foo ...) ...) ...)
255          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
256         (else (values (car expr) (cdr expr)))))
257
258 ;;;;;;;;;;;;;;;
259 ;;; Utilities for storing and accessing markup commands signature
260 ;;; and keyword.
261 ;;; Examples:
262 ;;;
263 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
264 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
265 ;;;
266 ;;; (markup-command-signature raise-markup)
267 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
268 ;;;
269 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
270 ;;; 
271
272 (define-public (markup-command-keyword markup-command)
273   "Return markup-command's argument keyword, ie a string describing the command
274   arguments, eg. \"scheme0markup1\""
275   (object-property markup-command 'markup-keyword))
276
277 (define-public (markup-command-signature-ref markup-command)
278   "Return markup-command's signature (the 'markup-signature object property)"
279   (object-property markup-command 'markup-signature))
280
281 (define-public (markup-command-signature-set! markup-command signature)
282   "Set markup-command's signature and keyword (as object properties)"
283   (set-object-property! markup-command 'markup-signature signature)
284   (set-object-property! markup-command 'markup-keyword 
285                         (markup-signature-to-keyword signature))
286   signature)
287
288 (define-public markup-command-signature
289   (make-procedure-with-setter markup-command-signature-ref
290                               markup-command-signature-set!))
291
292 ;; For documentation purposes
293 (define-public markup-function-list (list))
294 (define-public markup-list-function-list (list))
295
296 (define-public (markup-signature-to-keyword sig)
297   " (A B C) -> a0-b1-c2 "
298   (if (null? sig)
299       'empty
300       (string->symbol (string-join (map
301                                     (let* ((count 0))
302                                       (lambda (func)
303                                         (set! count (+ count 1))
304                                         (string-append
305                                          ;; for reasons I don't get,
306                                          ;; (case func ((markup?) .. )
307                                          ;; doesn't work.
308                                          (cond 
309                                           ((eq? func markup?) "markup")
310                                           ((eq? func markup-list?) "markup-list")
311                                           (else "scheme"))
312                                          (number->string (- count 1)))))
313                                     sig)
314                          "-"))))
315
316 (define (lookup-markup-command-aux symbol)
317   (let ((proc (catch 'misc-error
318                 (lambda ()
319                   (module-ref (current-module) symbol))
320                 (lambda (key . args) #f))))
321     (and (procedure? proc) proc)))
322
323 (define-public (lookup-markup-command code)
324   (let ((proc (lookup-markup-command-aux
325                (string->symbol (format #f "~a-markup" code)))))
326     (and proc (markup-function? proc)
327          (cons proc (markup-command-keyword proc)))))
328
329 (define-public (lookup-markup-list-command code)
330   (let ((proc (lookup-markup-command-aux
331                (string->symbol (format #f "~a-markup-list" code)))))
332      (and proc (markup-list-function? proc)
333           (cons proc (markup-command-keyword proc)))))
334
335 ;;;;;;;;;;;;;;;;;;;;;;
336 ;;; used in parser.yy to map a list of markup commands on markup arguments
337 (define-public (map-markup-command-list commands markups)
338   "`markups' being a list of markups, eg (markup1 markup2 markup3),
339 and `commands' a list of commands with their scheme arguments, in reverse order,
340 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
341  ((bold (raise 4 (italic markup1)))
342   (bold (raise 4 (italic markup2)))
343   (bold (raise 4 (italic markup3))))
344 "
345   (map-in-order (lambda (arg)
346                   (let ((result arg))
347                     (for-each (lambda (cmd)
348                                 (set! result (append cmd (list result))))
349                               commands)
350                     result))
351                 markups))
352
353 ;;;;;;;;;;;;;;;;;;;;;;
354 ;;; markup type predicates
355
356 (define (markup-function? x)
357   (and (markup-command-signature x)
358        (not (object-property x 'markup-list-command))))
359
360 (define (markup-list-function? x)
361   (and (markup-command-signature x)
362        (object-property x 'markup-list-command)))
363
364 (define-public (markup-command-list? x)
365   "Determine if `x' is a markup command list, ie. a list composed of
366 a markup list function and its arguments."
367   (and (pair? x) (markup-list-function? (car x))))
368
369 (define-public (markup-list? arg)
370   "Return a true value if `x' is a list of markups or markup command lists."
371   (define (markup-list-inner? lst)
372     (or (null? lst)
373         (and (or (markup? (car lst)) (markup-command-list? (car lst)))
374              (markup-list-inner? (cdr lst)))))
375   (not (not (and (list? arg) (markup-list-inner? arg)))))
376
377 (define (markup-argument-list? signature arguments)
378   "Typecheck argument list."
379   (if (and (pair? signature) (pair? arguments))
380       (and ((car signature) (car arguments))
381            (markup-argument-list? (cdr signature) (cdr arguments)))
382       (and (null? signature) (null? arguments))))
383
384
385 (define (markup-argument-list-error signature arguments number)
386   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
387 #f is no error found.
388 "
389   (if (and (pair? signature) (pair? arguments))
390       (if (not ((car signature) (car arguments)))
391           (list number (type-name (car signature)) (car arguments))
392           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
393       #f))
394
395 ;;
396 ;; full recursive typecheck.
397 ;;
398 (define (markup-typecheck? arg)
399   (or (string? arg)
400       (and (pair? arg)
401            (markup-function? (car arg))
402            (markup-argument-list? (markup-command-signature (car arg))
403                                   (cdr arg)))))
404
405 ;; 
406 ;; 
407 ;;
408 ;; 
409 (define (markup-thrower-typecheck arg)
410   "typecheck, and throw an error when something amiss.
411
412 Uncovered - cheap-markup? is used."
413
414   (cond ((string? arg) #t)
415         ((not (pair? arg))
416          (throw 'markup-format "Not a pair" arg))
417         ((not (markup-function? (car arg)))
418          (throw 'markup-format "Not a markup function " (car arg)))
419         ((not (markup-argument-list? (markup-command-signature (car arg))
420                                      (cdr arg)))
421          (throw 'markup-format "Arguments failed  typecheck for " arg)))
422   #t)
423
424 ;;
425 ;; good enough if you only  use make-XXX-markup functions.
426 ;; 
427 (define (cheap-markup? x)
428   (or (string? x)
429       (and (pair? x)
430            (markup-function? (car x)))))
431
432 ;;
433 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
434 ;; 
435 (define-public markup? cheap-markup?)
436
437 ;; utility
438
439 (define (markup-join markups sep)
440   "Return line-markup of MARKUPS, joining them with markup SEP"
441   (if (pair? markups)
442       (make-line-markup (list-insert-separator markups sep))
443       empty-markup))
444
445
446 (define-public interpret-markup ly:text-interface::interpret-markup)
447
448 (define-public (interpret-markup-list layout props markup-list)
449   (let ((stencils (list)))
450     (for-each (lambda (m)
451                 (set! stencils
452                       (if (markup-command-list? m)
453                           (append! (reverse! (apply (car m) layout props (cdr m)))
454                                    stencils)
455                           (cons (interpret-markup layout props m) stencils))))
456               markup-list)
457     (reverse! stencils)))
458
459 (define-public (prepend-alist-chain key val chain)
460   (cons (acons key val (car chain)) (cdr chain)))
461
462 (define-public (stack-stencil-line space stencils)
463   "DOCME"
464   (if (and (pair? stencils)
465            (ly:stencil? (car stencils)))
466       
467       (if (and (pair? (cdr stencils))
468                (ly:stencil? (cadr stencils)))
469           (let* ((tail (stack-stencil-line space (cdr stencils)))
470                  (head (car stencils))
471                  (xoff (+ space (cdr (ly:stencil-extent head X)))))
472             (ly:stencil-add head
473                              (ly:stencil-translate-axis tail xoff X)))
474           (car stencils))
475       (ly:make-stencil '() '(0 . 0) '(0 . 0))))
476