]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup.scm
kill some dead code.
[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 function, use the define-markup-command utility.
24
25   (define-markup-command (mycommand layout prop arg1 ...) (arg1-type? ...)
26     \"my command usage and description\"
27     ...function body...)
28
29 The command is now available in markup mode, e.g.
30
31
32   \\markup { .... \\MYCOMMAND #1 argument ... }
33
34 " ; "
35
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;;; markup definer utilities
38
39 (defmacro-public in-module-define-variable (module-name symbol value)
40   "Define a variable in a module and export its name.
41   (in-module-define-variable (some module) symbol value)"
42   (let ((gmodule (gensym "module")))
43     `(let ((,gmodule (resolve-module ',module-name)))
44        (module-define! ,gmodule ',symbol ,value)
45        (module-export! ,gmodule '(,symbol)))))
46
47 (defmacro-public in-module-define-function
48                  (module-name function-name+arg-list . body)
49   "Define a public function in a module:
50   (in-module-define-function (some module) (function-name . args)
51     ..body..)"
52   `(in-module-define-variable
53     ,module-name
54     ,(car function-name+arg-list)
55     (let ((proc (lambda ,(cdr function-name+arg-list)
56                   ,@body)))
57       (set-procedure-property! proc
58                                'name
59                                ',(car function-name+arg-list))
60       proc)))
61
62 ;;; `define-markup-command' can be used both for built-in markup
63 ;;; definitions and user defined markups.
64 (defmacro-public define-markup-command (command-and-args signature . body)
65   "
66
67 * Define a COMMAND-markup function after command-and-args and body,
68 register COMMAND-markup and its signature,
69
70 * add COMMAND-markup to markup-function-list,
71
72 * sets COMMAND-markup markup-signature and markup-keyword object properties,
73
74 * define a make-COMMAND-markup function.
75
76 Syntax:
77   (define-markup-command (COMMAND layout props arg1 arg2 ...) (arg1-type? arg2-type? ...)
78     \"documentation string\"
79     ...command body...)
80 or:
81   (define-markup-command COMMAND (arg1-type? arg2-type? ...) function)
82 "
83   (let* ((command (if (pair? command-and-args)
84                       (car command-and-args)
85                       command-and-args))
86          (command-name (string->symbol (format #f "~a-markup" command)))
87          (make-markup-name (string->symbol (format #f "make-~a-markup" command))))
88     `(let ((lily-module (resolve-module '(lily))))
89        ;; define the COMMAND-markup procedure in (lily) module
90        ,(if (pair? command-and-args)
91             ;; two cases:
92             ;; 1/ (define (COMMAND-markup layout props arg1 arg2 ...)
93             ;;      ..command body))
94             `(in-module-define-function (lily) (,command-name ,@(cdr command-and-args))
95                ,@body)
96             ;; 2/ (define COMMAND-markup function)
97             `(in-module-define-variable (lily) ,command-name ,(car body)))
98        (let ((command-proc (module-ref lily-module ',command-name)))
99          ;; register its command signature
100          (set! (markup-command-signature command-proc)
101                (list ,@signature))
102          ;; add the COMMAND-markup procedure to the list of markup functions
103          (if (not (member command-proc markup-function-list))
104              (set! markup-function-list (cons command-proc markup-function-list)))
105          ;; define the make-COMMAND-markup procedure in (lily) module
106          (in-module-define-function (lily) (,make-markup-name . args)
107            (make-markup command-proc
108                         ,(symbol->string make-markup-name)
109                         (list ,@signature)
110                         args))))))
111
112 (define-public (make-markup markup-function make-name signature args)
113   " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
114 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
115 "
116   (let* ((arglen (length args))
117          (siglen (length signature))
118          (error-msg (if (and (> siglen 0) (> arglen 0))
119                         (markup-argument-list-error signature args 1)
120                         #f)))
121     (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
122         (ly:error (string-append make-name ": "
123                    (_ "Wrong number of arguments.  Expect: ~A, found ~A: ~S"))
124                   siglen arglen args))
125     (if error-msg
126         (ly:error
127          (string-append
128           make-name ": "
129           (_ "Invalid argument in position ~A.  Expect: ~A, found: ~S.")
130           error-msg))
131         (cons markup-function args))))
132
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;;; markup constructors
135 ;;; lilypond-like syntax for markup construction in scheme.
136
137 (use-modules (ice-9 optargs)
138              (ice-9 receive))
139
140 (defmacro*-public markup (#:rest body)
141   "The `markup' macro provides a lilypond-like syntax for building markups.
142
143  - #:COMMAND is used instead of \\COMMAND
144  - #:lines ( ... ) is used instead of { ... }
145  - #:center-align ( ... ) is used instead of \\center-align < ... >
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 \\notes block."
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 markup-command-signatures (make-hash-table 50))
273
274 (define (markup-command-signature-ref markup-command)
275   "Return markup-command's signature, e.g. (number? markup?).
276 markup-command may be a procedure."
277   (let ((sig-key (hashq-ref markup-command-signatures
278                             markup-command)))
279     (if sig-key (car sig-key) #f)))
280
281 (define-public (markup-command-keyword markup-command)
282   "Return markup-command's keyword, e.g. \"scheme0markup1\".
283 markup-command may be a procedure."
284   (let ((sig-key (hashq-ref markup-command-signatures
285                             markup-command)))
286     (if sig-key (cdr sig-key) #f)))
287
288 (define (markup-command-signatureset! markup-command signature)
289   "Set markup-command's signature. markup-command must be a named procedure.
290 Also set markup-signature and markup-keyword object properties."
291   (hashq-set! markup-command-signatures
292               markup-command
293               (cons signature (markup-signature-to-keyword signature)))
294   ;; these object properties are still in use somewhere
295   (set-object-property! markup-command 'markup-signature signature)
296   (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
297   
298 (define-public markup-command-signature
299   (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
300
301 (define (markup-symbol-to-proc markup-sym)
302   "Return the markup command procedure which name is `markup-sym', if any."
303   (hash-fold (lambda (key val prev)
304                (or prev
305                    (if (eqv? (procedure-name key) markup-sym) key #f)))
306              #f
307              markup-command-signatures))
308
309 (define-public markup-function-list '())
310
311 (define-public (markup-signature-to-keyword sig)
312   " (A B C) -> a0-b1-c2 "
313   (if (null? sig)
314       'empty
315       (string->symbol (string-join (map
316                                     (let* ((count 0))
317                                       (lambda (func)
318                                         (set! count (+ count 1))
319                                         (string-append
320                                          ;; for reasons I don't get,
321                                          ;; (case func ((markup?) .. )
322                                          ;; doesn't work.
323                                          (cond 
324                                           ((eq? func markup?) "markup")
325                                           ((eq? func markup-list?) "markup-list")
326                                           (else "scheme"))
327                                          (number->string (- count 1)))))
328                                     sig)
329                          "-"))))
330
331 (define-public (lookup-markup-command code)
332   (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
333     (and proc (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   (not (not (markup-command-signature x))))
358
359 (define-public (markup-list? arg)
360   (define (markup-list-inner? lst)
361     (or (null? lst)
362         (and (markup? (car lst)) (markup-list-inner? (cdr lst)))))
363   (and (list? arg) (markup-list-inner? arg)))
364
365 (define (markup-argument-list? signature arguments)
366   "Typecheck argument list."
367   (if (and (pair? signature) (pair? arguments))
368       (and ((car signature) (car arguments))
369            (markup-argument-list? (cdr signature) (cdr arguments)))
370       (and (null? signature) (null? arguments))))
371
372
373 (define (markup-argument-list-error signature arguments number)
374   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
375 #f is no error found.
376 "
377   (if (and (pair? signature) (pair? arguments))
378       (if (not ((car signature) (car arguments)))
379           (list number (type-name (car signature)) (car arguments))
380           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
381       #f))
382
383 ;;
384 ;; full recursive typecheck.
385 ;;
386 (define (markup-typecheck? arg)
387   (or (string? arg)
388       (and (pair? arg)
389            (markup-function? (car arg))
390            (markup-argument-list? (markup-command-signature (car arg))
391                                   (cdr arg)))))
392
393 ;; 
394 ;; typecheck, and throw an error when something amiss.
395 ;; 
396 (define (markup-thrower-typecheck arg)
397   (cond ((string? arg) #t)
398         ((not (pair? arg))
399          (throw 'markup-format "Not a pair" arg))
400         ((not (markup-function? (car arg)))
401          (throw 'markup-format "Not a markup function " (car arg)))
402         ((not (markup-argument-list? (markup-command-signature (car arg))
403                                      (cdr arg)))
404          (throw 'markup-format "Arguments failed  typecheck for " arg)))
405   #t)
406
407 ;;
408 ;; good enough if you only  use make-XXX-markup functions.
409 ;; 
410 (define (cheap-markup? x)
411   (or (string? x)
412       (and (pair? x)
413            (markup-function? (car x)))))
414
415 ;;
416 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
417 ;; 
418 (define-public markup? cheap-markup?)
419
420 ;; utility
421
422 (define (markup-join markups sep)
423   "Return line-markup of MARKUPS, joining them with markup SEP"
424   (if (pair? markups)
425       (make-line-markup (list-insert-separator markups sep))
426       empty-markup))
427
428
429 (define-public interpret-markup ly:text-interface::interpret-markup)
430 (define-public (prepend-alist-chain key val chain)
431   (cons (acons key val (car chain)) (cdr chain)))
432
433 (define-public (stack-stencil-line space stencils)
434   "DOCME"
435   (if (and (pair? stencils)
436            (ly:stencil? (car stencils)))
437       
438       (if (and (pair? (cdr stencils))
439                (ly:stencil? (cadr stencils)))
440           (let* ((tail (stack-stencil-line space (cdr stencils)))
441                  (head (car stencils))
442                  (xoff (+ space (cdr (ly:stencil-extent head X)))))
443             (ly:stencil-add head
444                              (ly:stencil-translate-axis tail xoff X)))
445           (car stencils))
446       (ly:make-stencil '() '(0 . 0) '(0 . 0))))
447