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