]> git.donarmstrong.com Git - lilypond.git/blob - scm/new-markup.scm
($(outdir)/%.pdf): add DVIPS_FLAGS. This will
[lilypond.git] / scm / new-markup.scm
1 "
2 Internally markup is stored as lists, whose head is a function.
3
4   (FUNCTION ARG1 ARG2 ... )
5
6 When the markup is formatted, then FUNCTION is called as follows
7
8   (FUNCTION GROB PROPS ARG1 ARG2 ... ) 
9
10 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
11 the rest of the arguments.
12
13 The function should return a stencil (i.e. a formatted, ready to
14 print object).
15
16
17 To add a function, use the def-markup-command utility.
18
19   (def-markup-command (mycommand paper prop arg1 ...) (arg1-type? ...)
20     \"my command usage and description\"
21     ...function body...)
22
23 The command is now available in markup mode, e.g.
24
25
26   \\markup { .... \\MYCOMMAND #1 argument ... }
27
28 " ; "
29
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;;; markup definer utilities
32 ;;; `def-markup-command' can be used both for built-in markup
33 ;;; definitions and user defined markups.
34
35 (defmacro-public def-markup-command (command-and-args signature . body)
36   "
37
38 * Define a COMMAND-markup function after command-and-args and body,
39 register COMMAND-markup and its signature,
40
41 * add COMMAND-markup to markup-function-list,
42
43 * sets COMMAND-markup markup-signature and markup-keyword object properties,
44
45 * define a make-COMMAND-markup function.
46
47 Syntax:
48   (def-markup-command (COMMAND paper props arg1 arg2 ...) (arg1-type? arg2-type? ...)
49     \"documentation string\"
50     ...command body...)
51  or:
52   (def-markup-command COMMAND (arg1-type? arg2-type? ...)
53     function)
54 "
55   (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
56          (args (if (pair? command-and-args) (cdr command-and-args) '()))
57          (command-name (string->symbol (string-append (symbol->string command) "-markup")))
58          (make-markup-name (string->symbol (string-append "make-" (symbol->string command-name)))))
59     `(begin
60        (define-public ,(if (pair? args)
61                            (cons command-name args)
62                            command-name)
63          ,@body)
64        (set! (markup-command-signature ,command-name) (list ,@signature))
65        (if (not (member ,command-name markup-function-list))
66            (set! markup-function-list (cons ,command-name markup-function-list)))
67        (define-public (,make-markup-name . args)
68          (let ((sig (list ,@signature)))
69            (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
70
71 (define-public (make-markup markup-function make-name signature args)
72   " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
73 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
74 "
75   (let* ((arglen (length args))
76          (siglen (length signature))
77          (error-msg (if (and (> siglen 0) (> arglen 0))
78                         (markup-argument-list-error signature args 1)
79                         #f)))
80     (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
81         (scm-error 'markup-format make-name
82                    "Expect ~A arguments for ~A. Found ~A: ~S"
83                    (list siglen make-name arglen args)
84                    #f))
85     (if error-msg
86         (scm-error 'markup-format make-name
87                    "Invalid argument in position ~A\nExpect: ~A\nFound: ~S."
88                    error-msg #f)
89         (cons markup-function args))))
90
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 ;;; markup constructors
93 ;;; lilypond-like syntax for markup construction in scheme.
94
95 (use-modules (ice-9 optargs)
96              (ice-9 receive))
97
98 (defmacro*-public markup (#:rest body)
99   "The `markup' macro provides a lilypond-like syntax for building markups.
100
101  - #:COMMAND is used instead of \\COMMAND
102  - #:lines ( ... ) is used instead of { ... }
103  - #:center ( ... ) is used instead of \\center < ... >
104  - etc.
105
106 Example:
107   \\markup { foo
108             \\raise #0.2 \\hbracket \\bold bar
109             \\override #'(baseline-skip . 4)
110             \\bracket \\column < baz bazr bla >
111   }
112          <==>
113   (markup \"foo\"
114           #:raise 0.2 #:hbracket #:bold \"bar\"
115           #:override '(baseline-skip . 4) 
116           #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
117 Use `markup*' in a \\notes block."
118   
119   (car (compile-all-markup-expressions `(#:line ,body))))
120
121 (defmacro*-public markup* (#:rest body)
122   "Same as `markup', for use in a \\notes block."
123   `(ly:export (markup ,@body)))
124   
125   
126 (define (compile-all-markup-expressions expr)
127   "Return a list of canonical markups expressions, eg:
128   (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
129   ===>
130   ((make-COMMAND1-markup arg11 arg12)
131    (make-COMMAND2-markup arg21 arg22 arg23) ...)"
132   (do ((rest expr rest)
133        (markps '() markps))
134       ((null? rest) (reverse markps))
135     (receive (m r) (compile-markup-expression rest)
136              (set! markps (cons m markps))
137              (set! rest r))))
138
139 (define (keyword->make-markup key)
140   "Transform a keyword, eg. #:COMMAND, in a make-COMMAND-markup symbol."
141   (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
142
143 (define (compile-markup-expression expr)
144   "Return two values: the first complete canonical markup expression found in `expr',
145 eg (make-COMMAND-markup arg1 arg2 ...), and the rest expression."
146   (cond ((and (pair? expr)
147               (keyword? (car expr)))
148          ;; expr === (#:COMMAND arg1 ...)
149          (let* ((command (symbol->string (keyword->symbol (car expr))))
150                 (sig (markup-command-signature (car (lookup-markup-command command))))
151                 (sig-len (length sig)))
152            (do ((i 0 (1+ i))
153                 (args '() args)
154                 (rest (cdr expr) rest))
155                ((>= i sig-len)
156                 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
157              (cond ((eqv? (list-ref sig i) markup-list?)
158                     ;; (car rest) is a markup list
159                     (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
160                     (set! rest (cdr rest)))
161                    (else
162                     ;; pick up one arg in `rest'
163                     (receive (a r) (compile-markup-arg rest)
164                              (set! args (cons a args))
165                              (set! rest r)))))))
166         ((and (pair? expr)
167               (pair? (car expr))
168               (keyword? (caar expr)))
169          ;; expr === ((#:COMMAND arg1 ...) ...)
170          (receive (m r) (compile-markup-expression (car expr))
171                   (values m (cdr expr))))
172         (else
173          ;; expr === (symbol ...) or ("string" ...) or ((funcall ...) ...)
174          (values (car expr)
175                  (cdr expr)))))
176
177 (define (compile-all-markup-args expr)
178   "Transform `expr' into markup arguments"
179   (do ((rest expr rest)
180        (args '() args))
181       ((null? rest) (reverse args))
182     (receive (a r) (compile-markup-arg rest)
183              (set! args (cons a args))
184              (set! rest r))))
185
186 (define (compile-markup-arg expr)
187   "Return two values: the desired markup argument, and the rest arguments"
188   (cond ((null? expr)
189          ;; no more args
190          (values '() '()))
191         ((keyword? (car expr))
192          ;; expr === (#:COMMAND ...)
193          ;; ==> build and return the whole markup expression
194          (compile-markup-expression expr))
195         ((and (pair? (car expr))
196               (keyword? (caar expr)))
197          ;; expr === ((#:COMMAND ...) ...)
198          ;; ==> build and return the whole markup expression(s)
199          ;; found in (car expr)
200          (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
201                   (if (null? rest-expr)
202                       (values markup-expr (cdr expr))
203                       (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
204                               (cdr expr)))))
205         ((and (pair? (car expr))
206               (pair? (caar expr)))
207          ;; expr === (((foo ...) ...) ...)
208          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
209         (else (values (car expr) (cdr expr)))))
210
211 ;;;;;;;;;;;;;;;
212 ;;; Utilities for storing and accessing markup commands signature
213 ;;; and keyword.
214 ;;; Examples:
215 ;;;
216 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
217 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
218 ;;;
219 ;;; (markup-command-signature raise-markup)
220 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
221 ;;;
222 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
223 ;;; 
224
225 (define markup-command-signatures (make-hash-table 50))
226
227 (define (markup-command-signature-ref markup-command)
228   "Return markup-command's signature, e.g. (number? markup?).
229 markup-command may be a procedure."
230   (let ((sig-key (hashq-ref markup-command-signatures
231                             markup-command)))
232     (if sig-key (car sig-key) #f)))
233
234 (define-public (markup-command-keyword markup-command)
235   "Return markup-command's keyword, e.g. \"scheme0markup1\".
236 markup-command may be a procedure."
237   (let ((sig-key (hashq-ref markup-command-signatures
238                             markup-command)))
239     (if sig-key (cdr sig-key) #f)))
240
241 (define (markup-command-signatureset! markup-command signature)
242   "Set markup-command's signature. markup-command must be a named procedure.
243 Also set markup-signature and markup-keyword object properties."
244   (hashq-set! markup-command-signatures
245               markup-command
246               (cons signature (markup-signature-to-keyword signature)))
247   ;; these object properties are still in use somewhere
248   (set-object-property! markup-command 'markup-signature signature)
249   (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
250   
251 (define-public markup-command-signature
252   (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
253
254 (define (markup-symbol-to-proc markup-sym)
255   "Return the markup command procedure which name is `markup-sym', if any."
256   (hash-fold (lambda (key val prev)
257                             (or prev
258                                 (if (eqv? (procedure-name key) markup-sym) key #f)))
259              #f
260              markup-command-signatures))
261
262 (define-public markup-function-list '())
263
264 (define-public (markup-signature-to-keyword sig)
265   " (A B C) -> a0-b1-c2 "
266   (if (null? sig)
267       'empty
268       (string->symbol (string-join (map
269                                     (let* ((count 0))
270                                       (lambda (func)
271                                         (set! count (+ count 1))
272                                         (string-append
273                                          ;; for reasons I don't get,
274                                          ;; (case func ((markup?) .. )
275                                          ;; doesn't work.
276                                          (cond 
277                                           ((eq? func markup?) "markup")
278                                           ((eq? func markup-list?) "markup-list")
279                                           (else "scheme"))
280                                          (number->string (- count 1)))))
281                                     sig)
282                          "-"))))
283
284 (define-public (lookup-markup-command code)
285   (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
286     (and proc (cons proc (markup-command-keyword proc)))))
287
288 ;;;;;;;;;;;;;;;;;;;;;;
289 ;;; markup type predicates
290
291 (define (markup-function? x)
292   (not (not (markup-command-signature x))))
293
294 (define (markup-list? arg)
295   (define (markup-list-inner? l)
296     (or (null? l)
297         (and (markup? (car l)) (markup-list-inner? (cdr l)))))
298   (and (list? arg) (markup-list-inner? arg)))
299
300 (define (markup-argument-list? signature arguments)
301   "Typecheck argument list."
302   (if (and (pair? signature) (pair? arguments))
303       (and ((car signature) (car arguments))
304            (markup-argument-list? (cdr signature) (cdr arguments)))
305       (and (null? signature) (null? arguments))))
306
307
308 (define (markup-argument-list-error signature arguments number)
309   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
310 #f is no error found.
311 "
312   (if (and (pair? signature) (pair? arguments))
313       (if (not ((car signature) (car arguments)))
314           (list number (type-name (car signature)) (car arguments))
315           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
316       #f))
317
318 ;;
319 ;; full recursive typecheck.
320 ;;
321 (define (markup-typecheck? arg)
322   (or (string? arg)
323       (and (pair? arg)
324            (markup-function? (car arg))
325            (markup-argument-list? (markup-command-signature (car arg))
326                                   (cdr arg)))))
327
328 ;; 
329 ;; typecheck, and throw an error when something amiss.
330 ;; 
331 (define (markup-thrower-typecheck arg)
332   (cond ((string? arg) #t)
333         ((not (pair? arg))
334          (throw 'markup-format "Not a pair" arg))
335         ((not (markup-function? (car arg)))
336          (throw 'markup-format "Not a markup function " (car arg)))
337         ((not (markup-argument-list? (markup-command-signature (car arg))
338                                      (cdr arg)))
339          (throw 'markup-format "Arguments failed  typecheck for " arg)))
340   #t)
341
342 ;;
343 ;; good enough if you only  use make-XXX-markup functions.
344 ;; 
345 (define (cheap-markup? x)
346   (or (string? x)
347       (and (pair? x)
348            (markup-function? (car x)))))
349
350 ;;
351 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
352 ;; 
353 (define-public markup? cheap-markup?)
354
355 ;; utility
356
357 (define (markup-join markups sep)
358   "Return line-markup of MARKUPS, joining them with markup SEP"
359   (if (pair? markups)
360       (make-line-markup (list-insert-separator markups sep))
361       empty-markup))
362
363 (define-public brew-new-markup-stencil Text_item::print)
364 (define-public interpret-markup Text_item::interpret_markup)
365 (define-public (prepend-alist-chain key val chain)
366   (cons (acons key val (car chain)) (cdr chain)))
367
368
369
370
371 (define-public (stack-stencil-line space stencils)
372   (if (pair? stencils)
373       (if (pair? (cdr stencils))
374           (let* ((tail (stack-stencil-line  space (cdr stencils)))
375                  (head (car stencils))
376                  (xoff (+ space (cdr (ly:stencil-get-extent head X)))))
377             (ly:stencil-add head
378                              (ly:stencil-translate-axis tail xoff X)))
379           (car stencils))
380       '()))
381
382
383
384
385
386
387