Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / scm / define-event-classes.scm
blobf5d7b790de852048db22b202fefa30c7ff4e1560
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>
8 (use-modules (srfi srfi-1))
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 label-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                                    percent-event
41                                    rest-event skip-event bass-figure-event))
42     (melodic-event . (cluster-note-event note-event))
43     (() . (Announcement))
44     (Announcement . (AnnounceNewContext))
45     ))
47 ;; Maps event-class to a list of ancestors (inclusive)
48 (define ancestor-lookup (make-hash-table 11))
50 ;; Each class will be defined as
51 ;; (class parent grandparent .. )
52 ;; so that (eq? (cdr class) parent) holds.
53 (for-each
54  (lambda (rel)
55    (for-each
56     (lambda (type)
57       (hashq-set! ancestor-lookup type 
58                   (cons type (hashq-ref ancestor-lookup (car rel) '()))))
59     (cdr rel)))
60  event-classes)
62 ;; TODO: Allow entering more complex classes, by taking unions.
63 (define-public (ly:make-event-class leaf)
64  (hashq-ref ancestor-lookup leaf))
66 (define-public (ly:in-event-class? ev cl)
67   "Does event @var{ev} belong to event class @var{cl}?"
68   (memq cl (ly:make-event-class (ly:event-property ev 'class))))
70 ;; does this exist in guile already?
71 (define (map-tree f t)
72   (cond
73    ((list? t)
74     (map (lambda (x) (map-tree f x)) t))
75    ((pair? t)
76     (cons (map-tree f (car t)) (map-tree f (cdr t))))
77    (else (f t))))
79 ;; expand each non-leaf subtree to (root . children), recursively
80 (define (expand-event-tree root)
81   (let ((children (assq root event-classes)))
82     (if children
83         (cons root (map expand-event-tree (cdr children)))
84         root)))
86 ;; All leaf event classes that no translator listens to
87 ;; directly. Avoids printing a warning.
88 (define unlistened-music-event-classes
89   '(harmonic-event line-break-event page-break-event page-turn-event label-event
90                    solo-one-event solo-two-event skip-event unisono-event))
92 ;; produce neater representation of music event tree.
93 ;; TODO: switch to this representation for the event-classes list?
94 (define music-event-tree (expand-event-tree 'music-event))
95 (define (sort-tree t)
96   (define (stringify el)
97               (if (symbol? el)
98                   (symbol->string el)
99                   (symbol->string (first el))))
100   (if (list? t)
101       (sort (map (lambda (el)
102                    (if (list? el)
103                        (cons (car el) (sort-tree (cdr el)))
104                        el))
105                  t)
106             (lambda (a b) (string<? (stringify a) (stringify b))))
107       t))
109 ;;(use-modules (ice-9 pretty-print))
110 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
112 ;; check that the music event tree corresponds well with the set of
113 ;; available translators; print warnings otherwise.
114 (map-tree (lambda (sym) 
115             (if (and (symbol? sym)
116                      (not (ly:is-listened-event-class sym))
117                      (not (assq sym event-classes))
118                      (not (memq sym unlistened-music-event-classes)))
119                 (ly:programming-error (_ "event class ~A seems to be unused") sym)))      
120           music-event-tree)
122 (map (lambda (sym)
123        (if (not (pair? (ly:make-event-class sym)))
124            ;; should be programming-error
125            (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
126      (ly:get-listened-event-classes))
128 (defmacro-public make-stream-event (expr)
129   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
131 (define* (simplify e)
132   (cond
133    ;; Special case for lists reduces stack consumption.
134    ((list? e) (map simplify e))
135    ((pair? e) (cons (simplify (car e))
136                     (simplify (cdr e))))
137    ((ly:stream-event? e)
138     (list 'unquote (list 'make-stream-event (simplify (Stream_event::dump e)))))
139    ((ly:music? e)
140     (list 'unquote (music->make-music e)))
141    ((ly:moment? e)
142     (list 'unquote `(ly:make-moment
143                      ,(ly:moment-main-numerator e)
144                      ,(ly:moment-main-denominator e)
145                      . ,(if (eq? 0 (ly:moment-grace-numerator e))
146                             '()
147                             (list (ly:moment-grace-numerator e)
148                                   (ly:moment-grace-denominator e))))))
149    ((ly:duration? e)
150     (list 'unquote `(ly:make-duration
151                      ,(ly:duration-log e)
152                      ,(ly:duration-dot-count e)
153                      ,(car (ly:duration-factor e))
154                      ,(cdr (ly:duration-factor e)))))
155    ((ly:pitch? e)
156     (list 'unquote `(ly:make-pitch
157                      ,(ly:pitch-octave e)
158                      ,(ly:pitch-notename e)
159                      ,(ly:pitch-alteration e))))
160    ((ly:input-location? e)
161     (list 'unquote '(ly:dummy-input-location)))
162    (#t e)))
164 (define-public (ly:simplify-scheme e)
165   (list 'quasiquote (simplify e)))