]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-event-classes.scm
define-event-classes.scm: Stop define-event-class from returning random value.
[lilypond.git] / scm / define-event-classes.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2011 Erik Sandberg <mandolaerik@gmail.com>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 (use-modules (srfi srfi-1))
20
21 ;; Event class hierarchy. Each line is on the form (Parent . (List of children))
22 (define event-classes
23   '((() . (StreamEvent))
24     (StreamEvent .
25                  (RemoveContext ChangeParent Override Revert UnsetProperty
26                                 SetProperty music-event OldMusicEvent CreateContext Prepare
27                                 OneTimeStep Finish))
28     (music-event . (annotate-output-event footnote-event
29                     arpeggio-event breathing-event extender-event span-event
30       rhythmic-event dynamic-event break-event label-event percent-event
31       key-change-event string-number-event stroke-finger-event tie-event
32       part-combine-event part-combine-force-event
33       beam-forbid-event script-event tempo-change-event
34       tremolo-event bend-after-event fingering-event glissando-event
35       harmonic-event hyphen-event laissez-vibrer-event mark-event
36       multi-measure-text-event note-grouping-event
37       pes-or-flexa-event repeat-tie-event spacing-section-event
38       layout-instruction-event completize-extender-event break-span-event))
39
40     (layout-instruction-event . (apply-output-event))
41     (script-event . (articulation-event text-script-event))
42     (part-combine-event . (solo-one-event solo-two-event unisono-event))
43     (break-event . (line-break-event page-break-event page-turn-event))
44     (dynamic-event . (absolute-dynamic-event))
45     (span-event . (span-dynamic-event beam-event episema-event ligature-event
46                          pedal-event phrasing-slur-event slur-event staff-span-event
47                          text-span-event trill-span-event tremolo-span-event
48                          tuplet-span-event))
49     (span-dynamic-event . (decrescendo-event crescendo-event))
50     (break-span-event . (break-dynamic-span-event))
51     (pedal-event . (sostenuto-event sustain-event una-corda-event))
52     (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
53                                    double-percent-event percent-event
54                                    repeat-slash-event rest-event
55                                    skip-event bass-figure-event))
56     (melodic-event . (cluster-note-event note-event))
57     (() . (Announcement))
58     (Announcement . (AnnounceNewContext))
59     ))
60
61 ;; Maps event-class to a list of ancestors (inclusive)
62 (define ancestor-lookup (make-hash-table 11))
63
64 ;; Each class will be defined as
65 ;; (class parent grandparent .. )
66 ;; so that (eq? (cdr class) parent) holds.
67 (for-each
68  (lambda (rel)
69    (for-each
70     (lambda (type)
71       (hashq-set! ancestor-lookup type
72                   (cons type (hashq-ref ancestor-lookup (car rel) '()))))
73     (cdr rel)))
74  event-classes)
75
76 (define-public (define-event-class leaf heritage)
77   (cond
78    ((not (eq? leaf (car heritage)))
79     (ly:warning (_ "All classes must be the last in their matrilineal line.")))
80    ((not (equal? (cdr heritage)
81                  (list-head (hashq-ref ancestor-lookup (cadr heritage) '())
82                             (length (cdr heritage)))))
83     (ly:warning (_ "All classes must have a well-defined pedigree in the existing class hierarchy.")))
84    (else (hashq-set! ancestor-lookup
85                      leaf
86                      (cons leaf
87                            (hashq-ref ancestor-lookup
88                                       (cadr heritage)
89                                       '())))))
90   (begin))
91
92 ;; TODO: Allow entering more complex classes, by taking unions.
93 (define-public (ly:make-event-class leaf)
94  (hashq-ref ancestor-lookup leaf))
95
96 (define-public (ly:in-event-class? ev cl)
97   "Does event @var{ev} belong to event class @var{cl}?"
98   (memq cl (ly:make-event-class (ly:event-property ev 'class))))
99
100 ;; does this exist in guile already?
101 (define (map-tree f t)
102   (cond
103    ((list? t)
104     (map (lambda (x) (map-tree f x)) t))
105    ((pair? t)
106     (cons (map-tree f (car t)) (map-tree f (cdr t))))
107    (else (f t))))
108
109 ;; expand each non-leaf subtree to (root . children), recursively
110 (define (expand-event-tree root)
111   (let ((children (assq root event-classes)))
112     (if children
113         (cons root (map expand-event-tree (cdr children)))
114         root)))
115
116 ;; All leaf event classes that no translator listens to
117 ;; directly.  Avoids printing a warning.
118 (define unlistened-music-event-classes
119   '(harmonic-event line-break-event page-break-event page-turn-event label-event
120                    solo-one-event solo-two-event skip-event unisono-event
121                    part-combine-force-event break-dynamic-span-event))
122
123 ;; produce neater representation of music event tree.
124 ;; TODO: switch to this representation for the event-classes list?
125 (define music-event-tree (expand-event-tree 'music-event))
126 (define (sort-tree t)
127   (define (stringify el)
128               (if (symbol? el)
129                   (symbol->string el)
130                   (symbol->string (first el))))
131   (if (list? t)
132       (sort (map (lambda (el)
133                    (if (list? el)
134                        (cons (car el) (sort-tree (cdr el)))
135                        el))
136                  t)
137             (lambda (a b) (string<? (stringify a) (stringify b))))
138       t))
139
140 ;;(use-modules (ice-9 pretty-print))
141 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
142
143 ;; check that the music event tree corresponds well with the set of
144 ;; available translators; print warnings otherwise.
145 (map-tree (lambda (sym)
146             (if (and (symbol? sym)
147                      (not (ly:is-listened-event-class sym))
148                      (not (assq sym event-classes))
149                      (not (memq sym unlistened-music-event-classes)))
150                 (ly:programming-error (_ "event class ~A seems to be unused") sym)))
151           music-event-tree)
152
153 (map (lambda (sym)
154        (if (not (pair? (ly:make-event-class sym)))
155            ;; should be programming-error
156            (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
157      (ly:get-listened-event-classes))
158
159 (defmacro-public make-stream-event (expr)
160   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
161
162 (define* (simplify e)
163   (cond
164    ;; Special case for lists reduces stack consumption.
165    ((list? e) (map simplify e))
166    ((pair? e) (cons (simplify (car e))
167                     (simplify (cdr e))))
168    ((ly:stream-event? e)
169     (list 'unquote (list 'make-stream-event (simplify (Stream_event::dump e)))))
170    ((ly:music? e)
171     (list 'unquote (music->make-music e)))
172    ((ly:moment? e)
173     (list 'unquote `(ly:make-moment
174                      ,(ly:moment-main-numerator e)
175                      ,(ly:moment-main-denominator e)
176                      . ,(if (eq? 0 (ly:moment-grace-numerator e))
177                             '()
178                             (list (ly:moment-grace-numerator e)
179                                   (ly:moment-grace-denominator e))))))
180    ((ly:duration? e)
181     (list 'unquote `(ly:make-duration
182                      ,(ly:duration-log e)
183                      ,(ly:duration-dot-count e)
184                      ,(car (ly:duration-factor e))
185                      ,(cdr (ly:duration-factor e)))))
186    ((ly:pitch? e)
187     (list 'unquote `(ly:make-pitch
188                      ,(ly:pitch-octave e)
189                      ,(ly:pitch-notename e)
190                      ,(ly:pitch-alteration e))))
191    ((ly:input-location? e)
192     (list 'unquote '(ly:dummy-input-location)))
193    (#t e)))
194
195 (define-public (ly:simplify-scheme e)
196   (list 'quasiquote (simplify e)))