]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
Grammar nitpick.
[lilypond.git] / scm / ly-syntax-constructors.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2006--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 ;; TODO: use separate module for syntax
19 ;; constructors. Also create wrapper around the constructor?
20 (defmacro define-ly-syntax (args . body)
21   `(define-public ,args ,(cons 'begin body)))
22
23 ;; A ly-syntax constructor takes two extra parameters, parser and
24 ;; location. These are mainly used for reporting errors and
25 ;; warnings. This function is a syntactic sugar which uses the
26 ;; location arg to set the origin of the returned music object; this
27 ;; behaviour is usually desired
28 (defmacro define-ly-syntax-loc (args . body)
29   `(define-public ,args
30      (let ((m ,(cons 'begin body)))
31        (set! (ly:music-property m 'origin) ,(third args))
32        m)))
33 ;; Like define-ly-syntax-loc, but adds parser and location
34 ;; parameters. Useful for simple constructors that don't need to
35 ;; report errors.
36 (defmacro define-ly-syntax-simple (args . body)
37   `(define-public ,(cons* (car args)
38                           'parser
39                           'location
40                           (cdr args))
41      (let ((m ,(cons 'begin body)))
42        (set! (ly:music-property m 'origin) location)
43        m)))
44
45 ;; Music function: Apply function and check return value.
46 (define-ly-syntax (music-function parser loc pred default fun args)
47   (let ((m (apply fun parser loc args)))
48     (if (pred m)
49         m
50         (begin
51           (ly:parser-error parser
52                            (format #f (_ "~a function cannot return ~a")
53                                    (type-name pred) m)
54                            loc)
55           default))))
56
57 (define-ly-syntax-simple (void-music)
58   (make-music 'Music))
59
60 (define-ly-syntax-simple (sequential-music mlist)
61   (make-sequential-music mlist))
62
63 (define-ly-syntax-simple (simultaneous-music mlist)
64   (make-simultaneous-music mlist))
65
66 (define-ly-syntax-simple (event-chord mlist)
67   (make-music 'EventChord
68               'elements mlist))
69
70 (define-ly-syntax-simple (unrelativable-music mus)
71   (make-music 'UnrelativableMusic
72               'element mus))
73
74 (define-ly-syntax-simple (context-change type id)
75   (make-music 'ContextChange
76               'change-to-type type
77               'change-to-id id))
78
79 (define-ly-syntax-simple (voice-separator)
80   (make-music 'VoiceSeparator))
81
82 (define-ly-syntax-simple (bar-check)
83   (make-music 'BarCheck))
84
85 (define-ly-syntax-simple (time-scaled-music fraction music)
86   (make-music 'TimeScaledMusic
87               'element (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction)))
88               'numerator (car fraction)
89               'denominator (cdr fraction)))
90
91 (define-ly-syntax (tempo parser location text . rest)
92   (let* ((unit (and (pair? rest)
93                     (car rest)))
94          (count (and unit
95                      (cadr rest)))
96          (range-tempo? (pair? count))
97          (tempo-change (make-music 'TempoChangeEvent
98                                    'origin location
99                                    'text text
100                                    'tempo-unit unit
101                                    'metronome-count count))
102          (tempo-set
103           (and unit
104                (context-spec-music
105                 (make-property-set 'tempoWholesPerMinute
106                                    (ly:moment-mul
107                                     (ly:make-moment
108                                      (if range-tempo?
109                                          (round (/ (+ (car count) (cdr count))
110                                                    2))
111                                          count)
112                                      1)
113                                     (ly:duration-length unit)))
114                 'Score))))
115
116     (if tempo-set
117         (make-sequential-music (list tempo-change tempo-set))
118         tempo-change)))
119
120 (define-ly-syntax-simple (repeat type num body alts)
121   (make-repeat type num body alts))
122
123 (define (script-to-mmrest-text music)
124   "Extract @code{'direction} and @code{'text} from @var{music}, and transform
125 into a @code{MultiMeasureTextEvent}."
126
127   (if (memq 'script-event (ly:music-property music 'types))
128       (let* ((location (ly:music-property music 'origin))
129              (dir (ly:music-property music 'direction))
130              (tags (ly:music-property music 'tags))
131              (p (make-music 'MultiMeasureTextEvent
132                             'origin location
133                             'tags tags
134                             'text (ly:music-property music 'text))))
135         (if (ly:dir? dir)
136             (set! (ly:music-property p 'direction) dir))
137         p)
138       music))
139
140 (define-ly-syntax (multi-measure-rest parser location duration articulations)
141   (make-music 'MultiMeasureRestMusic
142               'articulations (map script-to-mmrest-text articulations)
143               'duration duration
144               'origin location))
145
146 (define-ly-syntax (repetition-chord parser location previous-chord repetition-function duration articulations)
147   (make-music 'RepeatedChord
148               'original-chord previous-chord
149               'element (repetition-function previous-chord location duration articulations)
150               'origin location))
151
152 (define-ly-syntax-simple (context-specification type id mus ops create-new)
153   (let* ((type-sym (if (symbol? type) type (string->symbol type)))
154          (csm (context-spec-music mus type-sym id)))
155     (set! (ly:music-property csm 'property-operations) ops)
156     (if create-new (set! (ly:music-property csm 'create-new) #t))
157     csm))
158
159 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
160   (let* ((props (case music-type
161                   ((PropertySet) (list 'value (car args)))
162                   ((PropertyUnset) '())
163                   ((OverrideProperty) (list 'grob-value (car args)
164                                             'grob-property-path (if (list? (cadr args))
165                                                                     (cadr args)
166                                                                     (cdr args))
167                                             'pop-first #t))
168                   ((RevertProperty)
169                    (if (list? (car args))
170                        (list 'grob-property-path (car args))
171                        (list 'grob-property-path args)))
172                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
173          (oprops (if once (cons* 'once once props) props))
174          (m (apply make-music music-type
175                    'symbol symbol
176                    'origin location
177                    oprops)))
178     (make-music 'ContextSpeccedMusic
179                 'element m
180                 'context-type ctx
181                 'origin location)))
182
183 ;; TODO: It seems that this function rarely returns anything useful.
184 (define (get-first-context-id type mus)
185   "Find the name of a ContextSpeccedMusic with given type"
186   (let ((id (ly:music-property mus 'context-id)))
187     (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
188              (eq? (ly:music-property mus 'context-type) type)
189              (string? id)
190              (not (string-null? id)))
191         id
192         '())))
193
194 (define unique-counter -1)
195 (define (get-next-unique-voice-name)
196   (set! unique-counter (1+ unique-counter))
197   (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
198
199 (define (lyric-combine-music sync music loc)
200   ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
201   ;; to signal to the Extender_engraver that any pending extender should
202   ;; be completed if the lyrics end before the associated voice.
203   (append! (ly:music-property music 'elements)
204            (list (make-music 'CompletizeExtenderEvent)))
205   (make-music 'LyricCombineMusic
206               'element music
207               'associated-context sync
208               'origin loc))
209
210 (define-ly-syntax (lyric-combine parser location voice music)
211   (lyric-combine-music voice music location))
212
213 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
214   (let* ((existing-voice-name (get-first-context-id 'Voice music))
215          (voice-name (if (string? existing-voice-name)
216                          existing-voice-name
217                          (get-next-unique-voice-name)))
218          (voice (if (string? existing-voice-name)
219                     (music)
220                     (make-music 'ContextSpeccedMusic
221                                 'element music
222                                 'context-type 'Voice
223                                 'context-id voice-name
224                                 'origin (ly:music-property music 'origin))))
225          (lyricstos (map (lambda (mus)
226                            (let* ((loc (ly:music-property mus 'origin))
227                                   (lyr (lyric-combine-music voice-name mus loc)))
228                              (make-music 'ContextSpeccedMusic
229                                          'create-new #t
230                                          'context-type 'Lyrics
231                                          'element lyr
232                                          'origin loc)))
233                          addlyrics-list)))
234     (make-simultaneous-music (cons voice lyricstos))))
235
236 (define-ly-syntax (make-mark-set parser location label)
237   "Make the music for the \\mark command."
238   (let* ((set (and (integer? label)
239                    (context-spec-music (make-property-set 'rehearsalMark label)
240                                       'Score)))
241          (ev (make-music 'MarkEvent
242                          'origin location)))
243
244     (if set
245         (make-sequential-music (list set ev))
246         (begin
247           (set! (ly:music-property ev 'label) label)
248           ev))))