]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup.scm
Merge branch 'master' of git+ssh://jneem@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--2006 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-public (make-markup markup-function make-name signature args)
84   " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
85 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
86 "
87   (let* ((arglen (length args))
88          (siglen (length signature))
89          (error-msg (if (and (> siglen 0) (> arglen 0))
90                         (markup-argument-list-error signature args 1)
91                         #f)))
92     (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
93         (ly:error (string-append make-name ": "
94                    (_ "Wrong number of arguments.  Expect: ~A, found ~A: ~S"))
95                   siglen arglen args))
96     (if error-msg
97         (ly:error
98          (string-append
99           make-name ": "
100           (_ "Invalid argument in position ~A.  Expect: ~A, found: ~S.")
101           error-msg))
102         (cons markup-function args))))
103
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
105 ;;; markup constructors
106 ;;; lilypond-like syntax for markup construction in scheme.
107
108 (use-modules (ice-9 optargs)
109              (ice-9 receive))
110
111 (defmacro*-public markup (#:rest body)
112   "The `markup' macro provides a lilypond-like syntax for building markups.
113
114  - #:COMMAND is used instead of \\COMMAND
115  - #:line ( ... ) is used instead of \\line { ... }
116  - etc.
117
118 Example:
119   \\markup { foo
120             \\raise #0.2 \\hbracket \\bold bar
121             \\override #'(baseline-skip . 4)
122             \\bracket \\column { baz bazr bla }
123   }
124          <==>
125   (markup \"foo\"
126           #:raise 0.2 #:hbracket #:bold \"bar\"
127           #:override '(baseline-skip . 4) 
128           #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
129 Use `markup*' in a \\notemode context."
130   
131   (car (compile-all-markup-expressions `(#:line ,body))))
132
133 (defmacro*-public markup* (#:rest body)
134   "Same as `markup', for use in a \\notes block."
135   `(ly:export (markup ,@body)))
136   
137   
138 (define (compile-all-markup-expressions expr)
139   "Return a list of canonical markups expressions, e.g.:
140   (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
141   ===>
142   ((make-COMMAND1-markup arg11 arg12)
143    (make-COMMAND2-markup arg21 arg22 arg23) ...)"
144   (do ((rest expr rest)
145        (markps '() markps))
146       ((null? rest) (reverse markps))
147     (receive (m r) (compile-markup-expression rest)
148              (set! markps (cons m markps))
149              (set! rest r))))
150
151 (define (keyword->make-markup key)
152   "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
153   (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
154
155 (define (compile-markup-expression expr)
156   "Return two values: the first complete canonical markup expression
157    found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
158    and the rest expression."
159   (cond ((and (pair? expr)
160               (keyword? (car expr)))
161          ;; expr === (#:COMMAND arg1 ...)
162          (let* ((command (symbol->string (keyword->symbol (car expr))))
163                 (sig (markup-command-signature
164                       (car (lookup-markup-command command))))
165                 (sig-len (length sig)))
166            (do ((i 0 (1+ i))
167                 (args '() args)
168                 (rest (cdr expr) rest))
169                ((>= i sig-len)
170                 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
171              (cond ((eqv? (list-ref sig i) markup-list?)
172                     ;; (car rest) is a markup list
173                     (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
174                     (set! rest (cdr rest)))
175                    (else
176                     ;; pick up one arg in `rest'
177                     (receive (a r) (compile-markup-arg rest)
178                              (set! args (cons a args))
179                              (set! rest r)))))))
180         ((and (pair? expr)
181               (pair? (car expr))
182               (keyword? (caar expr)))
183          ;; expr === ((#:COMMAND arg1 ...) ...)
184          (receive (m r) (compile-markup-expression (car expr))
185                   (values m (cdr expr))))
186         ((and (pair? expr)
187               (string? (car expr))) ;; expr === ("string" ...)
188          (values `(make-simple-markup ,(car expr)) (cdr expr)))
189         (else
190          ;; expr === (symbol ...) or ((funcall ...) ...)
191          (values (car expr)
192                  (cdr expr)))))
193
194 (define (compile-all-markup-args expr)
195   "Transform `expr' into markup arguments"
196   (do ((rest expr rest)
197        (args '() args))
198       ((null? rest) (reverse args))
199     (receive (a r) (compile-markup-arg rest)
200              (set! args (cons a args))
201              (set! rest r))))
202
203 (define (compile-markup-arg expr)
204   "Return two values: the desired markup argument, and the rest arguments"
205   (cond ((null? expr)
206          ;; no more args
207          (values '() '()))
208         ((keyword? (car expr))
209          ;; expr === (#:COMMAND ...)
210          ;; ==> build and return the whole markup expression
211          (compile-markup-expression expr))
212         ((and (pair? (car expr))
213               (keyword? (caar expr)))
214          ;; expr === ((#:COMMAND ...) ...)
215          ;; ==> build and return the whole markup expression(s)
216          ;; found in (car expr)
217          (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
218                   (if (null? rest-expr)
219                       (values markup-expr (cdr expr))
220                       (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
221                               (cdr expr)))))
222         ((and (pair? (car expr))
223               (pair? (caar expr)))
224          ;; expr === (((foo ...) ...) ...)
225          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
226         (else (values (car expr) (cdr expr)))))
227
228 ;;;;;;;;;;;;;;;
229 ;;; Utilities for storing and accessing markup commands signature
230 ;;; and keyword.
231 ;;; Examples:
232 ;;;
233 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
234 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
235 ;;;
236 ;;; (markup-command-signature raise-markup)
237 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
238 ;;;
239 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
240 ;;; 
241
242 (define-public (markup-command-keyword markup-command)
243   "Return markup-command's argument keyword, ie a string describing the command
244   arguments, eg. \"scheme0markup1\""
245   (object-property markup-command 'markup-keyword))
246
247 (define-public (markup-command-signature-ref markup-command)
248   "Return markup-command's signature (the 'markup-signature object property)"
249   (object-property markup-command 'markup-signature))
250
251 (define-public (markup-command-signature-set! markup-command signature)
252   "Set markup-command's signature and keyword (as object properties)"
253   (set-object-property! markup-command 'markup-signature signature)
254   (set-object-property! markup-command 'markup-keyword 
255                         (markup-signature-to-keyword signature))
256   signature)
257
258 (define-public markup-command-signature
259   (make-procedure-with-setter markup-command-signature-ref
260                               markup-command-signature-set!))
261
262 ;; For documentation purposes
263 (define-public markup-function-list (list))
264
265 (define-public (markup-signature-to-keyword sig)
266   " (A B C) -> a0-b1-c2 "
267   (if (null? sig)
268       'empty
269       (string->symbol (string-join (map
270                                     (let* ((count 0))
271                                       (lambda (func)
272                                         (set! count (+ count 1))
273                                         (string-append
274                                          ;; for reasons I don't get,
275                                          ;; (case func ((markup?) .. )
276                                          ;; doesn't work.
277                                          (cond 
278                                           ((eq? func markup?) "markup")
279                                           ((eq? func markup-list?) "markup-list")
280                                           (else "scheme"))
281                                          (number->string (- count 1)))))
282                                     sig)
283                          "-"))))
284
285 (define-public (lookup-markup-command code)
286   (let ((proc (catch 'misc-error
287                 (lambda ()
288                   (module-ref (current-module)
289                               (string->symbol (format #f "~a-markup" code))))
290                 (lambda (key . args) #f))))
291     (and (procedure? proc)
292          (cons proc (markup-command-keyword proc)))))
293
294 ;;;;;;;;;;;;;;;;;;;;;;
295 ;;; used in parser.yy to map a list of markup commands on markup arguments
296 (define-public (map-markup-command-list commands markups)
297   "`markups' being a list of markups, eg (markup1 markup2 markup3),
298 and `commands' a list of commands with their scheme arguments, in reverse order,
299 eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
300  ((bold (raise 4 (italic markup1)))
301   (bold (raise 4 (italic markup2)))
302   (bold (raise 4 (italic markup3))))
303 "
304   (map-in-order (lambda (arg)
305                   (let ((result arg))
306                     (for-each (lambda (cmd)
307                                 (set! result (append cmd (list result))))
308                               commands)
309                     result))
310                 markups))
311
312 ;;;;;;;;;;;;;;;;;;;;;;
313 ;;; markup type predicates
314
315 (define (markup-function? x)
316   (not (not (markup-command-signature x))))
317
318 (define-public (markup-list? arg)
319   (define (markup-list-inner? lst)
320     (or (null? lst)
321         (and (markup? (car lst)) (markup-list-inner? (cdr lst)))))
322   (and (list? arg) (markup-list-inner? arg)))
323
324 (define (markup-argument-list? signature arguments)
325   "Typecheck argument list."
326   (if (and (pair? signature) (pair? arguments))
327       (and ((car signature) (car arguments))
328            (markup-argument-list? (cdr signature) (cdr arguments)))
329       (and (null? signature) (null? arguments))))
330
331
332 (define (markup-argument-list-error signature arguments number)
333   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
334 #f is no error found.
335 "
336   (if (and (pair? signature) (pair? arguments))
337       (if (not ((car signature) (car arguments)))
338           (list number (type-name (car signature)) (car arguments))
339           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
340       #f))
341
342 ;;
343 ;; full recursive typecheck.
344 ;;
345 (define (markup-typecheck? arg)
346   (or (string? arg)
347       (and (pair? arg)
348            (markup-function? (car arg))
349            (markup-argument-list? (markup-command-signature (car arg))
350                                   (cdr arg)))))
351
352 ;; 
353 ;; 
354 ;;
355 ;; 
356 (define (markup-thrower-typecheck arg)
357   "typecheck, and throw an error when something amiss.
358
359 Uncovered - cheap-markup? is used."
360
361   (cond ((string? arg) #t)
362         ((not (pair? arg))
363          (throw 'markup-format "Not a pair" arg))
364         ((not (markup-function? (car arg)))
365          (throw 'markup-format "Not a markup function " (car arg)))
366         ((not (markup-argument-list? (markup-command-signature (car arg))
367                                      (cdr arg)))
368          (throw 'markup-format "Arguments failed  typecheck for " arg)))
369   #t)
370
371 ;;
372 ;; good enough if you only  use make-XXX-markup functions.
373 ;; 
374 (define (cheap-markup? x)
375   (or (string? x)
376       (and (pair? x)
377            (markup-function? (car x)))))
378
379 ;;
380 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
381 ;; 
382 (define-public markup? cheap-markup?)
383
384 ;; utility
385
386 (define (markup-join markups sep)
387   "Return line-markup of MARKUPS, joining them with markup SEP"
388   (if (pair? markups)
389       (make-line-markup (list-insert-separator markups sep))
390       empty-markup))
391
392
393 (define-public interpret-markup ly:text-interface::interpret-markup)
394 (define-public (prepend-alist-chain key val chain)
395   (cons (acons key val (car chain)) (cdr chain)))
396
397 (define-public (stack-stencil-line space stencils)
398   "DOCME"
399   (if (and (pair? stencils)
400            (ly:stencil? (car stencils)))
401       
402       (if (and (pair? (cdr stencils))
403                (ly:stencil? (cadr stencils)))
404           (let* ((tail (stack-stencil-line space (cdr stencils)))
405                  (head (car stencils))
406                  (xoff (+ space (cdr (ly:stencil-extent head X)))))
407             (ly:stencil-add head
408                              (ly:stencil-translate-axis tail xoff X)))
409           (car stencils))
410       (ly:make-stencil '() '(0 . 0) '(0 . 0))))
411