]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-event-classes.scm
rewrite balloon-text support. This is now a separate grob, with its
[lilypond.git] / scm / define-event-classes.scm
1 ;;;; stream-event-classes.scm -- define the tree of stream-event classes.
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2005-2006 Erik Sandberg <mandolaerik@gmail.com>
6
7
8 (use-modules (srfi srfi-1))
9
10 ;; Event class hierarchy. Each line is on the form (Parent . (List of children))
11 (define event-classes
12   '((() . (StreamEvent))
13     (StreamEvent .
14                  (RemoveContext ChangeParent Override Revert UnsetProperty
15                                 SetProperty music-event OldMusicEvent CreateContext Prepare
16                                 OneTimeStep Finish)) 
17     (music-event . (annotate-output-event
18                     arpeggio-event breathing-event extender-event span-event
19       rhythmic-event dynamic-event break-event percent-event
20       key-change-event string-number-event stroke-finger-event tie-event part-combine-event
21       beam-forbid-event script-event
22       tremolo-event bend-after-event fingering-event glissando-event
23       harmonic-event hyphen-event laissez-vibrer-event mark-event
24       multi-measure-text-event note-grouping-event 
25       pes-or-flexa-event repeat-tie-event spacing-section-event
26       layout-instruction-event))
27     
28     (layout-instruction-event . (apply-output-event ))
29     (script-event . (articulation-event text-script-event))
30     (part-combine-event . (solo-one-event solo-two-event unisono-event))
31     (break-event . (line-break-event page-break-event page-turn-event))
32     (dynamic-event . (absolute-dynamic-event))
33     (span-event . (span-dynamic-event beam-event ligature-event
34                          pedal-event phrasing-slur-event slur-event staff-span-event
35                          text-span-event trill-span-event tremolo-span-event 
36                          tuplet-span-event))
37     (span-dynamic-event . (decrescendo-event crescendo-event))
38     (pedal-event . (sostenuto-event sustain-event una-corda-event))
39     (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
40                                    rest-event skip-event bass-figure-event))
41     (melodic-event . (cluster-note-event note-event))
42     (() . (Announcement))
43     (Announcement . (AnnounceNewContext))
44     ))
45
46 ;; Maps event-class to a list of ancestors (inclusive)
47 (define ancestor-lookup (make-hash-table 11))
48
49 ;; Each class will be defined as
50 ;; (class parent grandparent .. )
51 ;; so that (eq? (cdr class) parent) holds.
52 (for-each
53  (lambda (rel)
54    (for-each
55     (lambda (type)
56       (hashq-set! ancestor-lookup type 
57                   (cons type (hashq-ref ancestor-lookup (car rel) '()))))
58     (cdr rel)))
59  event-classes)
60
61 ;; TODO: Allow entering more complex classes, by taking unions.
62 (define-public (ly:make-event-class leaf)
63  (hashq-ref ancestor-lookup leaf))
64
65 (define-public (ly:in-event-class? ev cl)
66   "Does event @var{ev} belong to event class @var{cl}?"
67   (memq cl (ly:make-event-class (ly:event-property ev 'class))))
68
69 ;; does this exist in guile already?
70 (define (map-tree f t)
71   (cond
72    ((list? t)
73     (map (lambda (x) (map-tree f x)) t))
74    ((pair? t)
75     (cons (map-tree f (car t)) (map-tree f (cdr t))))
76    (else (f t))))
77
78 ;; expand each non-leaf subtree to (root . children), recursively
79 (define (expand-event-tree root)
80   (let ((children (assq root event-classes)))
81     (if children
82         (cons root (map expand-event-tree (cdr children)))
83         root)))
84
85 ;; All leaf event classes that no translator listens to
86 ;; directly. Avoids printing a warning.
87 (define unlistened-music-event-classes
88   '(harmonic-event line-break-event page-break-event page-turn-event
89                    solo-one-event solo-two-event skip-event unisono-event))
90
91 ;; produce neater representation of music event tree.
92 ;; TODO: switch to this representation for the event-classes list?
93 (define music-event-tree (expand-event-tree 'music-event))
94 (define (sort-tree t)
95   (define (stringify el)
96               (if (symbol? el)
97                   (symbol->string el)
98                   (symbol->string (first el))))
99   (if (list? t)
100       (sort (map (lambda (el)
101                    (if (list? el)
102                        (cons (car el) (sort-tree (cdr el)))
103                        el))
104                  t)
105             (lambda (a b) (string<? (stringify a) (stringify b))))
106       t))
107
108 ;;(use-modules (ice-9 pretty-print))
109 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
110
111 ;; check that the music event tree corresponds well with the set of
112 ;; available translators; print warnings otherwise.
113 (map-tree (lambda (sym) 
114             (if (and (symbol? sym)
115                      (not (ly:is-listened-event-class sym))
116                      (not (assq sym event-classes))
117                      (not (memq sym unlistened-music-event-classes)))
118                 (ly:programming-error (_ "event class ~A seems to be unused") sym)))      
119           music-event-tree)
120
121 (map (lambda (sym)
122        (if (not (pair? (ly:make-event-class sym)))
123            ;; should be programming-error
124            (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
125      (ly:get-listened-event-classes))
126
127 (defmacro-public make-stream-event (expr)
128   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
129
130 (define* (simplify e)
131   (cond
132    ;; Special case for lists reduces stack consumption.
133    ((list? e) (map simplify e))
134    ((pair? e) (cons (simplify (car e))
135                     (simplify (cdr e))))
136    ((ly:stream-event? e)
137     (list 'unquote (list 'make-stream-event (simplify (Stream_event::dump e)))))
138    ((ly:music? e)
139     (list 'unquote (music->make-music e)))
140    ((ly:moment? e)
141     (list 'unquote `(ly:make-moment
142                      ,(ly:moment-main-numerator e)
143                      ,(ly:moment-main-denominator e)
144                      . ,(if (eq? 0 (ly:moment-grace-numerator e))
145                             '()
146                             (list (ly:moment-grace-numerator e)
147                                   (ly:moment-grace-denominator e))))))
148    ((ly:duration? e)
149     (list 'unquote `(ly:make-duration
150                      ,(ly:duration-log e)
151                      ,(ly:duration-dot-count e)
152                      ,(car (ly:duration-factor e))
153                      ,(cdr (ly:duration-factor e)))))
154    ((ly:pitch? e)
155     (list 'unquote `(ly:make-pitch
156                      ,(ly:pitch-octave e)
157                      ,(ly:pitch-notename e)
158                      ,(ly:pitch-alteration e))))
159    ((ly:input-location? e)
160     (list 'unquote '(ly:dummy-input-location)))
161    (#t e)))
162
163 (define-public (ly:simplify-scheme e)
164   (list 'quasiquote (simplify e)))
165