]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-event-classes.scm
Apply scripts/auxiliar/fixscm.sh
[lilypond.git] / scm / define-event-classes.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2012 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
26                   ChangeParent Override Revert UnsetProperty SetProperty
27                   music-event OldMusicEvent CreateContext Prepare
28                   OneTimeStep Finish))
29     (music-event . (annotate-output-event
30                     footnote-event arpeggio-event breathing-event
31                     extender-event span-event rhythmic-event dynamic-event
32                     break-event label-event percent-event key-change-event
33                     string-number-event stroke-finger-event tie-event
34                     part-combine-event part-combine-force-event
35                     beam-forbid-event script-event tempo-change-event
36                     tremolo-event bend-after-event fingering-event
37                     glissando-event harmonic-event hyphen-event
38                     laissez-vibrer-event mark-event multi-measure-text-event
39                     note-grouping-event pes-or-flexa-event repeat-tie-event
40                     spacing-section-event layout-instruction-event
41                     completize-extender-event break-span-event alternative-event))
42
43     (layout-instruction-event . (apply-output-event))
44     (script-event . (articulation-event text-script-event))
45     (part-combine-event . (solo-one-event solo-two-event unisono-event))
46     (break-event . (line-break-event page-break-event page-turn-event))
47     (dynamic-event . (absolute-dynamic-event))
48     (span-event . (span-dynamic-event
49                    beam-event episema-event ligature-event
50                    measure-counter-event pedal-event
51                    phrasing-slur-event slur-event
52                    staff-span-event text-span-event
53                    trill-span-event tremolo-span-event
54                    tuplet-span-event))
55     (span-dynamic-event . (decrescendo-event crescendo-event))
56     (break-span-event . (break-dynamic-span-event))
57     (pedal-event . (sostenuto-event sustain-event una-corda-event))
58     (rhythmic-event . (lyric-event
59                        melodic-event multi-measure-rest-event
60                        double-percent-event percent-event
61                        repeat-slash-event rest-event
62                        skip-event bass-figure-event))
63     (melodic-event . (cluster-note-event note-event))
64     (() . (Announcement))
65     (Announcement . (AnnounceNewContext))
66     ))
67
68 (define-public (event-class-cons class parent classlist)
69   (let ((lineage (assq parent classlist)))
70     (if (not lineage)
71         (begin
72           (if (not (null? parent))
73               (ly:warning (_ "unknown parent class `~a'") parent))
74           (set! lineage '())))
75     (if (symbol? class)
76         (acons class lineage classlist)
77         (fold (lambda (elt alist)
78                 (acons elt lineage alist))
79               classlist class))))
80
81 ;; Each class will be defined as
82 ;; (class parent grandparent .. )
83 ;; so that (eq? (cdr class) parent) holds.
84
85 (define-public (ly:in-event-class? ev cl)
86   "Does event @var{ev} belong to event class @var{cl}?"
87   (memq cl (ly:event-property ev 'class)))
88
89 (define-public all-event-classes
90   (fold (lambda (elt classlist)
91           (event-class-cons (cdr elt) (car elt) classlist))
92         '() event-classes))
93
94 ;; does this exist in guile already?
95 (define (map-tree f t)
96   (cond
97    ((list? t)
98     (map (lambda (x) (map-tree f x)) t))
99    ((pair? t)
100     (cons (map-tree f (car t)) (map-tree f (cdr t))))
101    (else (f t))))
102
103 ;; expand each non-leaf subtree to (root . children), recursively
104 (define (expand-event-tree root)
105   (let ((children (assq root event-classes)))
106     (if children
107         (cons root (map expand-event-tree (cdr children)))
108         root)))
109
110 ;; produce neater representation of music event tree.
111 ;; TODO: switch to this representation for the event-classes list?
112 (define music-event-tree (expand-event-tree 'music-event))
113 (define (sort-tree t)
114   (define (stringify el)
115     (if (symbol? el)
116         (symbol->string el)
117         (symbol->string (first el))))
118   (if (list? t)
119       (sort (map (lambda (el)
120                    (if (list? el)
121                        (cons (car el) (sort-tree (cdr el)))
122                        el))
123                  t)
124             (lambda (a b) (string<? (stringify a) (stringify b))))
125       t))
126
127 ;;(use-modules (ice-9 pretty-print))
128 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
129
130 (defmacro-public make-stream-event (expr)
131   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
132
133 (define* (simplify e)
134   (cond
135    ;; Special case for lists reduces stack consumption.
136    ((list? e) (map simplify e))
137    ((pair? e) (cons (simplify (car e))
138                     (simplify (cdr e))))
139    ((ly:stream-event? e)
140     (list 'unquote (list 'make-stream-event (simplify (Stream_event::dump e)))))
141    ((ly:music? e)
142     (list 'unquote (music->make-music e)))
143    ((ly:moment? e)
144     (list 'unquote `(ly:make-moment
145                      ,(ly:moment-main-numerator e)
146                      ,(ly:moment-main-denominator e)
147                      . ,(if (zero? (ly:moment-grace-numerator e))
148                             '()
149                             (list (ly:moment-grace-numerator e)
150                                   (ly:moment-grace-denominator e))))))
151    ((ly:duration? e)
152     (list 'unquote `(ly:make-duration
153                      ,(ly:duration-log e)
154                      ,(ly:duration-dot-count e)
155                      ,(ly:duration-scale))))
156    ((ly:pitch? e)
157     (list 'unquote `(ly:make-pitch
158                      ,(ly:pitch-octave e)
159                      ,(ly:pitch-notename e)
160                      ,(ly:pitch-alteration e))))
161    ((ly:input-location? e)
162     (list 'unquote '(ly:dummy-input-location)))
163    (#t e)))
164
165 (define-public (ly:simplify-scheme e)
166   (list 'quasiquote (simplify e)))