]> git.donarmstrong.com Git - lilypond.git/blob - scm/new-markup.scm
This commit was manufactured by cvs2svn to create tag 'lilypond_2_1_17'.
[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 molecule (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   "Define a COMMAND-markup function after command-and-args and body,
37 register COMMAND-markup and its signature,
38 add COMMAND-markup to markup-function-list,
39 sets COMMAND-markup markup-signature and markup-keyword object properties,
40 define a make-COMMAND-markup function.
41 Syntax:
42   (def-markup-command (COMMAND paper props arg1 arg2 ...) (arg1-type? arg2-type? ...)
43     \"documentation string\"
44     ...command body...)
45  or:
46   (def-markup-command COMMAND (arg1-type? arg2-type? ...)
47     function)
48 "
49   (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
50          (args (if (pair? command-and-args) (cdr command-and-args) '()))
51          (command-name (string->symbol (string-append (symbol->string command) "-markup")))
52          (make-markup-name (string->symbol (string-append "make-" (symbol->string command-name)))))
53     `(begin
54        (define-public ,(if (pair? args)
55                            (cons command-name args)
56                            command-name)
57          ,@body)
58        (set! (markup-command-signature ,command-name) (list ,@signature))
59        (if (not (member ,command-name markup-function-list))
60            (set! markup-function-list (cons ,command-name markup-function-list)))
61        (define-public (,make-markup-name . args)
62          (let ((sig (list ,@signature)))
63            (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
64
65 (define-public (make-markup markup-function make-name signature args)
66   " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
67 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
68 "
69   (let* ((arglen (length args))
70          (siglen (length signature))
71          (error-msg (if (and (> siglen 0) (> arglen 0))
72                         (markup-argument-list-error signature args 1)
73                         #f)))
74     (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
75         (scm-error 'markup-format make-name
76                    "Expect ~A arguments for ~A. Found ~A: ~S"
77                    (list siglen make-name arglen args)
78                    #f))
79     (if error-msg
80         (scm-error 'markup-format make-name
81                    "Invalid argument in position ~A\nExpect: ~A\nFound: ~S."
82                    error-msg #f)
83         (cons markup-function args))))
84
85 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 ;;; markup constructors
87 ;;; lilypond-like syntax for markup construction in scheme.
88
89 (use-modules (ice-9 optargs)
90              (ice-9 receive))
91
92 (defmacro*-public markup (#:rest body)
93   "The `markup' macro provides a lilypond-like syntax for building markups.
94
95  - #:COMMAND is used instead of \\COMMAND
96  - #:lines ( ... ) is used instead of { ... }
97  - #:center ( ... ) is used instead of \\center < ... >
98  - etc.
99
100 Example:
101   \\markup { foo
102             \\raise #0.2 \\hbracket \\bold bar
103             \\override #'(baseline-skip . 4)
104             \\bracket \\column < baz bazr bla >
105   }
106          <==>
107   (markup \"foo\"
108           #:raise 0.2 #:hbracket #:bold \"bar\"
109           #:override '(baseline-skip . 4) 
110           #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
111 Use `markup*' in a \\notes block."
112   
113   (car (compile-all-markup-expressions `(#:line ,body))))
114
115 (defmacro*-public markup* (#:rest body)
116   "Same as `markup', for use in a \\notes block."
117   `(ly:export (markup ,@body)))
118   
119   
120 (define (compile-all-markup-expressions expr)
121   "Return a list of canonical markups expressions, eg:
122   (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
123   ===>
124   ((make-COMMAND1-markup arg11 arg12)
125    (make-COMMAND2-markup arg21 arg22 arg23) ...)"
126   (do ((rest expr rest)
127        (markps '() markps))
128       ((null? rest) (reverse markps))
129     (receive (m r) (compile-markup-expression rest)
130              (set! markps (cons m markps))
131              (set! rest r))))
132
133 (define (keyword->make-markup key)
134   "Transform a keyword, eg. #:COMMAND, in a make-COMMAND-markup symbol."
135   (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
136
137 (define (compile-markup-expression expr)
138   "Return two values: the first complete canonical markup expression found in `expr',
139 eg (make-COMMAND-markup arg1 arg2 ...), and the rest expression."
140   (cond ((and (pair? expr)
141               (keyword? (car expr)))
142          ;; expr === (#:COMMAND arg1 ...)
143          (let* ((command (symbol->string (keyword->symbol (car expr))))
144                 (sig (markup-command-signature (car (lookup-markup-command command))))
145                 (sig-len (length sig)))
146            (do ((i 0 (1+ i))
147                 (args '() args)
148                 (rest (cdr expr) rest))
149                ((>= i sig-len)
150                 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
151              (cond ((eqv? (list-ref sig i) markup-list?)
152                     ;; (car rest) is a markup list
153                     (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
154                     (set! rest (cdr rest)))
155                    (else
156                     ;; pick up one arg in `rest'
157                     (receive (a r) (compile-markup-arg rest)
158                              (set! args (cons a args))
159                              (set! rest r)))))))
160         ((and (pair? expr)
161               (pair? (car expr))
162               (keyword? (caar expr)))
163          ;; expr === ((#:COMMAND arg1 ...) ...)
164          (receive (m r) (compile-markup-expression (car expr))
165                   (values m (cdr expr))))
166         (else
167          ;; expr === (symbol ...) or ("string" ...) or ((funcall ...) ...)
168          (values (car expr)
169                  (cdr expr)))))
170
171 (define (compile-all-markup-args expr)
172   "Transform `expr' into markup arguments"
173   (do ((rest expr rest)
174        (args '() args))
175       ((null? rest) (reverse args))
176     (receive (a r) (compile-markup-arg rest)
177              (set! args (cons a args))
178              (set! rest r))))
179
180 (define (compile-markup-arg expr)
181   "Return two values: the desired markup argument, and the rest arguments"
182   (cond ((null? expr)
183          ;; no more args
184          (values '() '()))
185         ((keyword? (car expr))
186          ;; expr === (#:COMMAND ...)
187          ;; ==> build and return the whole markup expression
188          (compile-markup-expression expr))
189         ((and (pair? (car expr))
190               (keyword? (caar expr)))
191          ;; expr === ((#:COMMAND ...) ...)
192          ;; ==> build and return the whole markup expression(s)
193          ;; found in (car expr)
194          (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
195                   (if (null? rest-expr)
196                       (values markup-expr (cdr expr))
197                       (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
198                               (cdr expr)))))
199         ((and (pair? (car expr))
200               (pair? (caar expr)))
201          ;; expr === (((foo ...) ...) ...)
202          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
203         (else (values (car expr) (cdr expr)))))
204
205 ;;;;;;;;;;;;;;;
206 ;;; Utilities for storing and accessing markup commands signature
207 ;;; and keyword.
208 ;;; Examples:
209 ;;;
210 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
211 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
212 ;;;
213 ;;; (markup-command-signature raise-markup)
214 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
215 ;;;
216 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
217 ;;; 
218
219 (define markup-command-signatures (make-hash-table 50))
220
221 (define (markup-command-signature-ref markup-command)
222   "Return markup-command's signature, e.g. (number? markup?).
223 markup-command may be a procedure."
224   (let ((sig-key (hashq-ref markup-command-signatures
225                             markup-command)))
226     (if sig-key (car sig-key) #f)))
227
228 (define-public (markup-command-keyword markup-command)
229   "Return markup-command's keyword, e.g. \"scheme0markup1\".
230 markup-command may be a procedure."
231   (let ((sig-key (hashq-ref markup-command-signatures
232                             markup-command)))
233     (if sig-key (cdr sig-key) #f)))
234
235 (define (markup-command-signatureset! markup-command signature)
236   "Set markup-command's signature. markup-command must be a named procedure.
237 Also set markup-signature and markup-keyword object properties."
238   (hashq-set! markup-command-signatures
239               markup-command
240               (cons signature (markup-signature-to-keyword signature)))
241   ;; these object properties are still in use somewhere
242   (set-object-property! markup-command 'markup-signature signature)
243   (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
244   
245 (define-public markup-command-signature
246   (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
247
248 (define (markup-symbol-to-proc markup-sym)
249   "Return the markup command procedure which name is `markup-sym', if any."
250   (hash-fold (lambda (key val prev)
251                             (or prev
252                                 (if (eqv? (procedure-name key) markup-sym) key #f)))
253              #f
254              markup-command-signatures))
255
256 (define-public markup-function-list '())
257
258 (define-public (markup-signature-to-keyword sig)
259   " (A B C) -> a0-b1-c2 "
260   (if (null? sig)
261       'empty
262       (string->symbol (string-join (map
263                                     (let* ((count 0))
264                                       (lambda (func)
265                                         (set! count (+ count 1))
266                                         (string-append
267                                          ;; for reasons I don't get,
268                                          ;; (case func ((markup?) .. )
269                                          ;; doesn't work.
270                                          (cond 
271                                           ((eq? func markup?) "markup")
272                                           ((eq? func markup-list?) "markup-list")
273                                           (else "scheme"))
274                                          (number->string (- count 1)))))
275                                     sig)
276                          "-"))))
277
278 (define-public (lookup-markup-command code)
279   (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
280     (and proc (cons proc (markup-command-keyword proc)))))
281
282 ;;;;;;;;;;;;;;;;;;;;;;
283 ;;; markup type predicates
284
285 (define (markup-function? x)
286   (not (not (markup-command-signature x))))
287
288 (define (markup-list? arg)
289   (define (markup-list-inner? l)
290     (or (null? l)
291         (and (markup? (car l)) (markup-list-inner? (cdr l)))))
292   (and (list? arg) (markup-list-inner? arg)))
293
294 (define (markup-argument-list? signature arguments)
295   "Typecheck argument list."
296   (if (and (pair? signature) (pair? arguments))
297       (and ((car signature) (car arguments))
298            (markup-argument-list? (cdr signature) (cdr arguments)))
299       (and (null? signature) (null? arguments))))
300
301
302 (define (markup-argument-list-error signature arguments number)
303   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
304 #f is no error found.
305 "
306   (if (and (pair? signature) (pair? arguments))
307       (if (not ((car signature) (car arguments)))
308           (list number (type-name (car signature)) (car arguments))
309           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
310       #f))
311
312 ;;
313 ;; full recursive typecheck.
314 ;;
315 (define (markup-typecheck? arg)
316   (or (string? arg)
317       (and (pair? arg)
318            (markup-function? (car arg))
319            (markup-argument-list? (markup-command-signature (car arg))
320                                   (cdr arg)))))
321
322 ;; 
323 ;; typecheck, and throw an error when something amiss.
324 ;; 
325 (define (markup-thrower-typecheck arg)
326   (cond ((string? arg) #t)
327         ((not (pair? arg))
328          (throw 'markup-format "Not a pair" arg))
329         ((not (markup-function? (car arg)))
330          (throw 'markup-format "Not a markup function " (car arg)))
331         ((not (markup-argument-list? (markup-command-signature (car arg))
332                                      (cdr arg)))
333          (throw 'markup-format "Arguments failed  typecheck for " arg)))
334   #t)
335
336 ;;
337 ;; good enough if you only  use make-XXX-markup functions.
338 ;; 
339 (define (cheap-markup? x)
340   (or (string? x)
341       (and (pair? x)
342            (markup-function? (car x)))))
343
344 ;;
345 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
346 ;; 
347 (define-public markup? cheap-markup?)
348
349 ;; utility
350
351 (define (markup-join markups sep)
352   "Return line-markup of MARKUPS, joining them with markup SEP"
353   (if (pair? markups)
354       (make-line-markup (list-insert-separator markups sep))
355       empty-markup))
356
357
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
359 ;;; markup commands
360 ;; TODO:
361 ;; each markup function should have a doc string with
362 ;; syntax, description and example. 
363 ;;
364
365 (define-public brew-new-markup-molecule Text_item::brew_molecule)
366
367 (define-public interpret-markup Text_item::interpret_markup)
368
369 (def-markup-command (simple paper props str) (string?)
370   "A simple text-string; @code{\\markup @{ foo @}} is equivalent with
371 @code{\\markup @{ \\simple #\"foo\" @}}.
372 "
373   (interpret-markup paper props str))
374
375 (define-public empty-markup (make-simple-markup ""))
376
377 (define-public (stack-molecule-line space molecules)
378   (if (pair? molecules)
379       (if (pair? (cdr molecules))
380           (let* ((tail (stack-molecule-line  space (cdr molecules)))
381                  (head (car molecules))
382                  (xoff (+ space (cdr (ly:molecule-get-extent head X)))))
383             (ly:molecule-add head
384                              (ly:molecule-translate-axis tail xoff X)))
385           (car molecules))
386       '()))
387
388 (def-markup-command (line paper props markps) (markup-list?)
389   "A horizontal line of markups. Syntax:
390 \\line << MARKUPS >>
391 "
392   (stack-molecule-line
393    (cdr (chain-assoc 'word-space props))
394    (map (lambda (m) (interpret-markup paper props m)) markps)))
395
396 (def-markup-command (combine paper props m1 m2) (markup? markup?)
397   "Overstrike two markups."
398   (ly:molecule-add
399    (interpret-markup paper props m1)
400    (interpret-markup paper props m2)))
401
402 (def-markup-command (finger paper props arg) (markup?)
403   (interpret-markup paper
404                     (cons '((font-size . -4) (font-family . number)) props)
405                     arg))
406
407 (define-public (set-property-markup qualifier)
408   (lambda (paper props qualifier-val markp)
409     (interpret-markup paper
410                       (cons (cons `(,qualifier . ,qualifier-val) (car props)) (cdr props))
411                       markp)))
412
413 (def-markup-command fontsize (number? markup?)
414   (set-property-markup 'font-size))
415
416 (def-markup-command magnify (number? markup?)
417   (set-property-markup 'font-magnification))
418
419 (define (font-markup qualifier value)
420   (lambda (paper props markp)
421     (interpret-markup paper
422                       (cons (cons `(,qualifier . ,value) (car props)) (cdr props))
423                       markp)))
424
425 (def-markup-command bold (markup?)
426   (font-markup 'font-series 'bold))
427
428 (def-markup-command sans (markup?)
429   (font-markup 'font-family 'sans))
430
431 (def-markup-command number (markup?)
432   (font-markup 'font-family 'number))
433
434 (def-markup-command roman (markup?)
435   (font-markup 'font-family 'roman))
436
437 (def-markup-command huge (markup?)
438   (font-markup 'font-size 2))
439
440 (def-markup-command large (markup?)
441   (font-markup 'font-size 1))
442
443 (def-markup-command normalsize (markup?)
444   (font-markup 'font-size 0))
445
446 (def-markup-command small (markup?)
447   (font-markup 'font-size -1))
448
449 (def-markup-command tiny (markup?)
450   (font-markup 'font-size -2))
451
452 (def-markup-command teeny (markup?)
453   (font-markup 'font-size -3))
454
455 (def-markup-command dynamic (markup?)
456   (font-markup 'font-family 'dynamic))
457
458 (def-markup-command italic (markup?)
459   (font-markup 'font-shape 'italic))
460
461 (def-markup-command typewriter (markup?)
462   (font-markup 'font-family 'typewriter))
463
464 (def-markup-command (column paper props mrkups) (markup-list?)
465   (stack-lines
466    -1 0.0 (cdr (chain-assoc 'baseline-skip props))
467    (map (lambda (m) (interpret-markup paper props m)) mrkups)))
468
469 (def-markup-command (dir-column paper props mrkups) (markup-list?)
470   "Make a column of args, going up or down, depending on the setting
471 of the #'direction layout property."
472   (let* ((dir (cdr (chain-assoc 'direction props))))
473     (stack-lines
474      (if (number? dir) dir -1)
475      0.0
476      (cdr (chain-assoc 'baseline-skip props))
477      (map (lambda (x) (interpret-markup paper props x)) mrkups))))
478
479 (def-markup-command (center paper props mrkups) (markup-list?)
480   (let* ((mols (map (lambda (x) (interpret-markup paper props x)) mrkups))
481          (cmols (map (lambda (x) (ly:molecule-align-to! x X CENTER)) mols)))
482     (stack-lines -1 0.0 (cdr (chain-assoc 'baseline-skip props)) mols)))
483
484 (def-markup-command (right-align paper props mrkup) (markup?)
485   (let* ((m (interpret-markup paper props mrkup)))
486     (ly:molecule-align-to! m X RIGHT)
487     m))
488
489 (def-markup-command (left-align paper props mrkup) (markup?)
490   (let* ((m (interpret-markup paper props mrkup)))
491     (ly:molecule-align-to! m X LEFT)
492     m))
493
494 (def-markup-command (halign paper props dir mrkup) (number? markup?)
495   "Set horizontal alignment. Syntax: halign A MARKUP. A=-1 is LEFT,
496 A=1 is right, values in between vary alignment accordingly."
497   (let* ((m (interpret-markup paper props mrkup)))
498     (ly:molecule-align-to! m X dir)
499     m))
500
501 (def-markup-command (musicglyph paper props glyph-name) (string?)
502   (ly:find-glyph-by-name
503    (ly:paper-get-font paper (cons '((font-name . ())
504                                     (font-shape . *)
505                                     (font-series . *)
506                                     (font-family . music))
507                                   props))
508    glyph-name))
509
510
511 (def-markup-command (lookup paper props glyph-name) (string?)
512   "Lookup a glyph by name."
513   (ly:find-glyph-by-name (ly:paper-get-font paper props)
514                          glyph-name))
515
516 (def-markup-command (char paper props num) (integer?)
517   "Syntax: \\char NUMBER. "
518   (ly:get-glyph (ly:paper-get-font paper props) num))
519
520 (def-markup-command (raise paper props amount mrkup) (number? markup?)
521   "Syntax: \\raise AMOUNT MARKUP. "
522   (ly:molecule-translate-axis (interpret-markup paper props mrkup)
523                               amount Y))
524
525 (def-markup-command (fraction paper props mrkup1 mrkup2) (markup? markup?)
526   "Make a fraction of two markups.
527
528 Syntax: \\fraction MARKUP1 MARKUP2."
529   (let* ((m1 (interpret-markup paper props mrkup1))
530          (m2 (interpret-markup paper props mrkup2)))
531     (ly:molecule-align-to! m1 X CENTER)
532     (ly:molecule-align-to! m2 X CENTER)    
533     (let* ((x1 (ly:molecule-get-extent m1 X))
534            (x2 (ly:molecule-get-extent m2 X))
535            (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
536            ;; should stack mols separately, to maintain LINE on baseline
537            (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
538       (ly:molecule-align-to! stack Y CENTER)
539       (ly:molecule-align-to! stack X LEFT)
540       ;; should have EX dimension
541       ;; empirical anyway
542       (ly:molecule-translate-axis stack 0.75 Y))))
543
544
545 ;; TODO: better syntax.
546
547 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
548   "Syntax: \\note-by-number #LOG #DOTS #DIR.  By using fractional values
549 for DIR, you can obtain longer or shorter stems."
550   (let* ((font (ly:paper-get-font paper (cons '((font-family .  music)) props)))
551          (stemlen (max 3 (- log 1)))
552          (headgl (ly:find-glyph-by-name
553                   font
554                   (string-append "noteheads-" (number->string (min log 2)))))
555          (stemth 0.13)
556          (stemy (* dir stemlen))
557          (attachx (if (> dir 0)
558                       (- (cdr (ly:molecule-get-extent headgl X)) stemth)
559                       0))
560          (attachy (* dir 0.28))
561          (stemgl (and (> log 0)
562                       (ly:round-filled-box
563                        (cons attachx (+ attachx  stemth))
564                        (cons (min stemy attachy)
565                              (max stemy attachy))
566                        (/ stemth 3))))
567          (dot (ly:find-glyph-by-name font "dots-dot"))
568          (dotwid (interval-length (ly:molecule-get-extent dot X)))
569          (dots (and (> dot-count 0)
570                     (apply ly:molecule-add
571                            (map (lambda (x)
572                                   (ly:molecule-translate-axis
573                                    dot  (* (+ 1 (* 2 x)) dotwid) X) )
574                                 (iota dot-count 1)))))
575          (flaggl (and (> log 2)
576                       (ly:molecule-translate
577                        (ly:find-glyph-by-name font
578                                               (string-append "flags-"
579                                                              (if (> dir 0) "u" "d")
580                                                              (number->string log)))
581                        (cons (+ attachx (/ stemth 2)) stemy)))))
582     (if flaggl
583         (set! stemgl (ly:molecule-add flaggl stemgl)))
584     (if (ly:molecule? stemgl)
585         (set! stemgl (ly:molecule-add stemgl headgl))
586         (set! stemgl headgl))
587     (if (ly:molecule? dots)
588         (set! stemgl
589               (ly:molecule-add
590                (ly:molecule-translate-axis dots
591                                            (+ (if (and (> dir 0) (> log 2))
592                                                   (* 1.5 dotwid)
593                                                   0)
594                                               ;; huh ? why not necessary?
595                                               ;;(cdr (ly:molecule-get-extent headgl X))
596                                               dotwid)
597                                            X)
598                stemgl)))
599     stemgl))
600
601 (use-modules (ice-9 regex))
602
603 (define-public log2 
604   (let ((divisor (log 2)))
605     (lambda (z) (inexact->exact (/ (log z) divisor)))))
606
607 (define (parse-simple-duration duration-string)
608   "Parse the `duration-string', eg ''4..'' or ''breve.'', and return a (log dots) list."
609   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
610     (if (and match (string=? duration-string (match:substring match 0)))
611         (let ((len  (match:substring match 1))
612               (dots (match:substring match 2)))
613           (list (cond ((string=? len "breve")  -1)
614                       ((string=? len "longa")  -2)
615                       ((string=? len "maxima") -3)
616                       (else (log2 (string->number len))))
617                 (if dots (string-length dots) 0)))
618         (error "This is not a valid duration string:" duration-string))))
619
620 (def-markup-command (note paper props duration-string dir) (string? number?)
621   "This produces a note with a stem pointing in @var{dir} direction, with
622 the @var{duration} for the note head type and augmentation dots. For
623 example, @code{\note #\"4.\" #-0.75} creates a dotted quarter note, with
624 a shortened down stem."
625   (let ((parsed (parse-simple-duration duration-string)))
626     (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
627
628 (def-markup-command (normal-size-super paper props mrkup) (markup?)
629   (ly:molecule-translate-axis (interpret-markup
630                                paper
631                                props mrkup)
632                               (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
633                               Y))
634
635 (def-markup-command (super paper props mrkup) (markup?)
636   "Syntax: \\super MARKUP. "
637   (ly:molecule-translate-axis
638    (interpret-markup
639     paper
640     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
641     mrkup)
642    (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
643    Y))
644
645 (def-markup-command (translate paper props offset mrkup) (number-pair? markup?)
646   "Syntax: \\translate OFFSET MARKUP. "
647   (ly:molecule-translate (interpret-markup  paper props mrkup)
648                          offset))
649
650 (def-markup-command (sub paper props mrkup) (markup?)
651   "Syntax: \\sub MARKUP."
652   (ly:molecule-translate-axis
653    (interpret-markup
654     paper
655     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
656     mrkup)
657    (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
658    Y))
659
660 (def-markup-command (normal-size-sub paper props mrkup) (markup?)
661   (ly:molecule-translate-axis
662    (interpret-markup paper props mrkup)
663    (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
664    Y))
665
666 (def-markup-command (hbracket paper props mrkup) (markup?)
667   "Horizontal brackets around its single argument. Syntax \\hbracket MARKUP."  
668   (let ((th 0.1) ;; todo: take from GROB.
669         (m (interpret-markup paper props mrkup)))
670     (bracketify-molecule m X th (* 2.5 th) th)))
671
672 (def-markup-command (bracket paper props mrkup) (markup?)
673   "Vertical brackets around its single argument. Syntax \\bracket MARKUP."  
674   (let ((th 0.1) ;; todo: take from GROB.
675         (m (interpret-markup paper props mrkup)))
676     (bracketify-molecule m Y th (* 2.5 th) th)))
677
678 ;; todo: fix negative space
679 (def-markup-command (hspace paper props amount) (number?)
680   "Syntax: \\hspace NUMBER."
681   (if (> amount 0)
682       (ly:make-molecule "" (cons 0 amount) '(-1 . 1) )
683       (ly:make-molecule "" (cons amount amount) '(-1 . 1))))
684
685 (def-markup-command (override paper props new-prop mrkup) (pair? markup?)
686   "Add the first argument in to the property list.  Properties may be
687 any sort of property supported by @ref{font-interface} and
688 @ref{text-interface}, for example
689
690 \\override #'(font-family . married) \"bla\"
691 "
692   (interpret-markup paper (cons (list new-prop) props) mrkup))
693
694 (def-markup-command (smaller paper props mrkup) (markup?)
695   "Syntax: \\smaller MARKUP"
696   (let* ((fs (chain-assoc-get 'font-size props 0))
697          (entry (cons 'font-size (- fs 1))))
698     (interpret-markup paper (cons (list entry) props) mrkup)))
699
700
701 (def-markup-command (bigger paper props mrkup) (markup?)
702   "Syntax: \\bigger MARKUP"
703   (let* ((fs (chain-assoc-get 'font-size props 0))
704          (entry (cons 'font-size (+ fs 1))))
705     (interpret-markup paper (cons (list entry) props) mrkup)))
706
707 (def-markup-command larger (markup?)
708   bigger-markup)
709
710 (def-markup-command (box paper props mrkup) (markup?)
711   "Syntax: \\box MARKUP"
712   (let ((th 0.1)
713         (pad 0.2)
714         (m (interpret-markup paper props mrkup)))
715     (box-molecule m th pad)))
716
717 (def-markup-command (strut paper props) ()
718   "Syntax: \\strut
719
720  A box of the same height as the space.
721 "
722   (let ((m (Text_item::interpret_markup paper props " ")))
723     (ly:molecule-set-extent! m X '(1000 . -1000))
724     m))
725
726 (define number->mark-letter-vector (make-vector 25 #\A))
727
728 (do ((i 0 (1+ i))
729      (j 0 (1+ j)))
730     ((>= i 26))
731   (if (= i (- (char->integer #\I) (char->integer #\A)))
732       (set! i (1+ i)))
733   (vector-set! number->mark-letter-vector j
734                (integer->char (+ i (char->integer #\A)))))
735
736 (define (number->markletter-string n)
737   "Double letters for big marks."
738   (let*
739       ((l (vector-length number->mark-letter-vector)))
740     
741   (if (>= n l)
742       (string-append (number->markletter-string (1- (quotient n l)))
743                      (number->markletter-string (remainder n l)))
744       (make-string 1 (vector-ref number->mark-letter-vector n)))))
745
746
747 (def-markup-command (markletter paper props num) (number?)
748   "Markup letters: skip I and do double letters for big marks.
749 Syntax: \\markletter #25"
750   (Text_item::interpret_markup paper props (number->markletter-string num)))
751
752 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
753
754 (if #f
755     (define (typecheck-with-error x)
756       (catch
757        'markup-format
758        (lambda () (markup? x))
759        (lambda (key message arg)
760          (display "\nERROR: markup format error: \n")
761          (display message)
762          (newline)
763          (write arg (current-output-port))))))
764
765 ;; test make-foo-markup functions
766 (if #f
767     (begin
768       (newline)
769       (newline)
770       (display (make-line-markup (list (make-simple-markup "FOO"))))
771       
772       (make-line-markup (make-simple-markup "FOO"))
773       (make-line-markup (make-simple-markup "FOO") (make-simple-markup "foo"))
774       (make-raise-markup "foo" (make-simple-markup "foo"))))
775
776 ;;
777 ;; test typecheckers. Not wholly useful, because errors are detected
778 ;; in other places than they're made.
779 ;;
780 (if #f
781     (begin
782       ;; To get error messages, see above to install the alternate
783       ;; typecheck routine for markup?.
784       (display (typecheck-with-error `(,simple-markup "foobar")))
785       (display (typecheck-with-error `(,simple-markup "foobar")))
786       (display (typecheck-with-error `(,simple-markup 1)))
787       (display
788        (typecheck-with-error `(,line-markup ((,simple-markup "foobar"))
789                                             (,simple-markup 1))))
790       (display
791        (typecheck-with-error `(,line-markup (,simple-markup "foobar")
792                                             (,simple-markup "bla"))))))