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