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