]> git.donarmstrong.com Git - lilypond.git/blob - scm/new-markup.scm
* scm/music-functions.scm (determine-split-list): further analysis.
[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 (doublesharp paper props) ()
465   (interpret-markup paper props (markup #:musicglyph "accidentals-4")))
466 (def-markup-command (threeqsharp paper props) ()
467   (interpret-markup paper props (markup #:musicglyph "accidentals-3")))
468 (def-markup-command (sharp paper props) ()
469   (interpret-markup paper props (markup #:musicglyph "accidentals-2")))
470 (def-markup-command (semisharp paper props) ()
471   (interpret-markup paper props (markup #:musicglyph "accidentals-1")))
472 (def-markup-command (natural paper props) ()
473   (interpret-markup paper props (markup #:musicglyph "accidentals-0")))
474 (def-markup-command (semiflat paper props) ()
475   (interpret-markup paper props (markup #:musicglyph "accidentals--1")))
476 (def-markup-command (flat paper props) ()
477   (interpret-markup paper props (markup #:musicglyph "accidentals--2")))
478 (def-markup-command (threeqflat paper props) ()
479   (interpret-markup paper props (markup #:musicglyph "accidentals--3")))
480 (def-markup-command (doubleflat paper props) ()
481   (interpret-markup paper props (markup #:musicglyph "accidentals--4")))
482
483
484 (def-markup-command (column paper props mrkups) (markup-list?)
485   (stack-lines
486    -1 0.0 (cdr (chain-assoc 'baseline-skip props))
487    (map (lambda (m) (interpret-markup paper props m)) mrkups)))
488
489 (def-markup-command (dir-column paper props mrkups) (markup-list?)
490   "Make a column of args, going up or down, depending on the setting
491 of the #'direction layout property."
492   (let* ((dir (cdr (chain-assoc 'direction props))))
493     (stack-lines
494      (if (number? dir) dir -1)
495      0.0
496      (cdr (chain-assoc 'baseline-skip props))
497      (map (lambda (x) (interpret-markup paper props x)) mrkups))))
498
499 (def-markup-command (center paper props mrkups) (markup-list?)
500   (let* ((mols (map (lambda (x) (interpret-markup paper props x)) mrkups))
501          (cmols (map (lambda (x) (ly:molecule-align-to! x X CENTER)) mols)))
502     (stack-lines -1 0.0 (cdr (chain-assoc 'baseline-skip props)) mols)))
503
504 (def-markup-command (right-align paper props mrkup) (markup?)
505   (let* ((m (interpret-markup paper props mrkup)))
506     (ly:molecule-align-to! m X RIGHT)
507     m))
508
509 (def-markup-command (left-align paper props mrkup) (markup?)
510   (let* ((m (interpret-markup paper props mrkup)))
511     (ly:molecule-align-to! m X LEFT)
512     m))
513
514 (def-markup-command (halign paper props dir mrkup) (number? markup?)
515   "Set horizontal alignment. Syntax: halign A MARKUP. A=-1 is LEFT,
516 A=1 is right, values in between vary alignment accordingly."
517   (let* ((m (interpret-markup paper props mrkup)))
518     (ly:molecule-align-to! m X dir)
519     m))
520
521 (def-markup-command (musicglyph paper props glyph-name) (string?)
522   (ly:find-glyph-by-name
523    (ly:paper-get-font paper (cons '((font-name . ())
524                                     (font-shape . *)
525                                     (font-series . *)
526                                     (font-family . music))
527                                   props))
528    glyph-name))
529
530
531 (def-markup-command (lookup paper props glyph-name) (string?)
532   "Lookup a glyph by name."
533   (ly:find-glyph-by-name (ly:paper-get-font paper props)
534                          glyph-name))
535
536 (def-markup-command (char paper props num) (integer?)
537   "Syntax: \\char NUMBER. "
538   (ly:get-glyph (ly:paper-get-font paper props) num))
539
540 (def-markup-command (raise paper props amount mrkup) (number? markup?)
541   "Syntax: \\raise AMOUNT MARKUP. "
542   (ly:molecule-translate-axis (interpret-markup paper props mrkup)
543                               amount Y))
544
545 (def-markup-command (fraction paper props mrkup1 mrkup2) (markup? markup?)
546   "Make a fraction of two markups.
547
548 Syntax: \\fraction MARKUP1 MARKUP2."
549   (let* ((m1 (interpret-markup paper props mrkup1))
550          (m2 (interpret-markup paper props mrkup2)))
551     (ly:molecule-align-to! m1 X CENTER)
552     (ly:molecule-align-to! m2 X CENTER)    
553     (let* ((x1 (ly:molecule-get-extent m1 X))
554            (x2 (ly:molecule-get-extent m2 X))
555            (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
556            ;; should stack mols separately, to maintain LINE on baseline
557            (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
558       (ly:molecule-align-to! stack Y CENTER)
559       (ly:molecule-align-to! stack X LEFT)
560       ;; should have EX dimension
561       ;; empirical anyway
562       (ly:molecule-translate-axis stack 0.75 Y))))
563
564
565 ;; TODO: better syntax.
566
567 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
568   "Syntax: \\note-by-number #LOG #DOTS #DIR.  By using fractional values
569 for DIR, you can obtain longer or shorter stems."
570   (let* ((font (ly:paper-get-font paper (cons '((font-family .  music)) props)))
571          (stemlen (max 3 (- log 1)))
572          (headgl (ly:find-glyph-by-name
573                   font
574                   (string-append "noteheads-" (number->string (min log 2)))))
575          (stemth 0.13)
576          (stemy (* dir stemlen))
577          (attachx (if (> dir 0)
578                       (- (cdr (ly:molecule-get-extent headgl X)) stemth)
579                       0))
580          (attachy (* dir 0.28))
581          (stemgl (and (> log 0)
582                       (ly:round-filled-box
583                        (cons attachx (+ attachx  stemth))
584                        (cons (min stemy attachy)
585                              (max stemy attachy))
586                        (/ stemth 3))))
587          (dot (ly:find-glyph-by-name font "dots-dot"))
588          (dotwid (interval-length (ly:molecule-get-extent dot X)))
589          (dots (and (> dot-count 0)
590                     (apply ly:molecule-add
591                            (map (lambda (x)
592                                   (ly:molecule-translate-axis
593                                    dot  (* (+ 1 (* 2 x)) dotwid) X) )
594                                 (iota dot-count 1)))))
595          (flaggl (and (> log 2)
596                       (ly:molecule-translate
597                        (ly:find-glyph-by-name font
598                                               (string-append "flags-"
599                                                              (if (> dir 0) "u" "d")
600                                                              (number->string log)))
601                        (cons (+ attachx (/ stemth 2)) stemy)))))
602     (if flaggl
603         (set! stemgl (ly:molecule-add flaggl stemgl)))
604     (if (ly:molecule? stemgl)
605         (set! stemgl (ly:molecule-add stemgl headgl))
606         (set! stemgl headgl))
607     (if (ly:molecule? dots)
608         (set! stemgl
609               (ly:molecule-add
610                (ly:molecule-translate-axis dots
611                                            (+ (if (and (> dir 0) (> log 2))
612                                                   (* 1.5 dotwid)
613                                                   0)
614                                               ;; huh ? why not necessary?
615                                               ;;(cdr (ly:molecule-get-extent headgl X))
616                                               dotwid)
617                                            X)
618                stemgl)))
619     stemgl))
620
621 (use-modules (ice-9 regex))
622
623 (define-public log2 
624   (let ((divisor (log 2)))
625     (lambda (z) (inexact->exact (/ (log z) divisor)))))
626
627 (define (parse-simple-duration duration-string)
628   "Parse the `duration-string', eg ''4..'' or ''breve.'', and return a (log dots) list."
629   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
630     (if (and match (string=? duration-string (match:substring match 0)))
631         (let ((len  (match:substring match 1))
632               (dots (match:substring match 2)))
633           (list (cond ((string=? len "breve")  -1)
634                       ((string=? len "longa")  -2)
635                       ((string=? len "maxima") -3)
636                       (else (log2 (string->number len))))
637                 (if dots (string-length dots) 0)))
638         (error "This is not a valid duration string:" duration-string))))
639
640 (def-markup-command (note paper props duration-string dir) (string? number?)
641   "This produces a note with a stem pointing in @var{dir} direction, with
642 the @var{duration} for the note head type and augmentation dots. For
643 example, @code{\note #\"4.\" #-0.75} creates a dotted quarter note, with
644 a shortened down stem."
645   (let ((parsed (parse-simple-duration duration-string)))
646     (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
647
648 (def-markup-command (normal-size-super paper props mrkup) (markup?)
649   (ly:molecule-translate-axis (interpret-markup
650                                paper
651                                props mrkup)
652                               (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
653                               Y))
654
655 (def-markup-command (super paper props mrkup) (markup?)
656   "Syntax: \\super MARKUP. "
657   (ly:molecule-translate-axis
658    (interpret-markup
659     paper
660     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
661     mrkup)
662    (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
663    Y))
664
665 (def-markup-command (translate paper props offset mrkup) (number-pair? markup?)
666   "Syntax: \\translate OFFSET MARKUP. "
667   (ly:molecule-translate (interpret-markup  paper props mrkup)
668                          offset))
669
670 (def-markup-command (sub paper props mrkup) (markup?)
671   "Syntax: \\sub MARKUP."
672   (ly:molecule-translate-axis
673    (interpret-markup
674     paper
675     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
676     mrkup)
677    (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
678    Y))
679
680 (def-markup-command (normal-size-sub paper props mrkup) (markup?)
681   (ly:molecule-translate-axis
682    (interpret-markup paper props mrkup)
683    (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
684    Y))
685
686 (def-markup-command (hbracket paper props mrkup) (markup?)
687   "Horizontal brackets around its single argument. Syntax \\hbracket MARKUP."  
688   (let ((th 0.1) ;; todo: take from GROB.
689         (m (interpret-markup paper props mrkup)))
690     (bracketify-molecule m X th (* 2.5 th) th)))
691
692 (def-markup-command (bracket paper props mrkup) (markup?)
693   "Vertical brackets around its single argument. Syntax \\bracket MARKUP."  
694   (let ((th 0.1) ;; todo: take from GROB.
695         (m (interpret-markup paper props mrkup)))
696     (bracketify-molecule m Y th (* 2.5 th) th)))
697
698 ;; todo: fix negative space
699 (def-markup-command (hspace paper props amount) (number?)
700   "Syntax: \\hspace NUMBER."
701   (if (> amount 0)
702       (ly:make-molecule "" (cons 0 amount) '(-1 . 1) )
703       (ly:make-molecule "" (cons amount amount) '(-1 . 1))))
704
705 (def-markup-command (override paper props new-prop mrkup) (pair? markup?)
706   "Add the first argument in to the property list.  Properties may be
707 any sort of property supported by @ref{font-interface} and
708 @ref{text-interface}, for example
709
710 \\override #'(font-family . married) \"bla\"
711 "
712   (interpret-markup paper (cons (list new-prop) props) mrkup))
713
714 (def-markup-command (smaller paper props mrkup) (markup?)
715   "Syntax: \\smaller MARKUP"
716   (let* ((fs (chain-assoc-get 'font-size props 0))
717          (entry (cons 'font-size (- fs 1))))
718     (interpret-markup paper (cons (list entry) props) mrkup)))
719
720
721 (def-markup-command (bigger paper props mrkup) (markup?)
722   "Syntax: \\bigger MARKUP"
723   (let* ((fs (chain-assoc-get 'font-size props 0))
724          (entry (cons 'font-size (+ fs 1))))
725     (interpret-markup paper (cons (list entry) props) mrkup)))
726
727 (def-markup-command larger (markup?)
728   bigger-markup)
729
730 (def-markup-command (box paper props mrkup) (markup?)
731   "Syntax: \\box MARKUP"
732   (let ((th 0.1)
733         (pad 0.2)
734         (m (interpret-markup paper props mrkup)))
735     (box-molecule m th pad)))
736
737 (def-markup-command (strut paper props) ()
738   "Syntax: \\strut
739
740  A box of the same height as the space.
741 "
742   (let ((m (Text_item::interpret_markup paper props " ")))
743     (ly:molecule-set-extent! m X '(1000 . -1000))
744     m))
745
746 (define number->mark-letter-vector (make-vector 25 #\A))
747
748 (do ((i 0 (1+ i))
749      (j 0 (1+ j)))
750     ((>= i 26))
751   (if (= i (- (char->integer #\I) (char->integer #\A)))
752       (set! i (1+ i)))
753   (vector-set! number->mark-letter-vector j
754                (integer->char (+ i (char->integer #\A)))))
755
756 (define (number->markletter-string n)
757   "Double letters for big marks."
758   (let*
759       ((l (vector-length number->mark-letter-vector)))
760     
761   (if (>= n l)
762       (string-append (number->markletter-string (1- (quotient n l)))
763                      (number->markletter-string (remainder n l)))
764       (make-string 1 (vector-ref number->mark-letter-vector n)))))
765
766
767 (def-markup-command (markletter paper props num) (number?)
768   "Markup letters: skip I and do double letters for big marks.
769 Syntax: \\markletter #25"
770   (Text_item::interpret_markup paper props (number->markletter-string num)))
771
772 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
773
774 (if #f
775     (define (typecheck-with-error x)
776       (catch
777        'markup-format
778        (lambda () (markup? x))
779        (lambda (key message arg)
780          (display "\nERROR: markup format error: \n")
781          (display message)
782          (newline)
783          (write arg (current-output-port))))))
784
785 ;; test make-foo-markup functions
786 (if #f
787     (begin
788       (newline)
789       (newline)
790       (display (make-line-markup (list (make-simple-markup "FOO"))))
791       
792       (make-line-markup (make-simple-markup "FOO"))
793       (make-line-markup (make-simple-markup "FOO") (make-simple-markup "foo"))
794       (make-raise-markup "foo" (make-simple-markup "foo"))))
795
796 ;;
797 ;; test typecheckers. Not wholly useful, because errors are detected
798 ;; in other places than they're made.
799 ;;
800 (if #f
801     (begin
802       ;; To get error messages, see above to install the alternate
803       ;; typecheck routine for markup?.
804       (display (typecheck-with-error `(,simple-markup "foobar")))
805       (display (typecheck-with-error `(,simple-markup "foobar")))
806       (display (typecheck-with-error `(,simple-markup 1)))
807       (display
808        (typecheck-with-error `(,line-markup ((,simple-markup "foobar"))
809                                             (,simple-markup 1))))
810       (display
811        (typecheck-with-error `(,line-markup (,simple-markup "foobar")
812                                             (,simple-markup "bla"))))))