1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2006--2011 Erik Sandberg <mandolaerik@gmail.com>
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.
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.
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/>.
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)))
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)
30 (let ((m ,(cons 'begin body)))
31 (set! (ly:music-property m 'origin) ,(third args))
33 ;; Like define-ly-syntax-loc, but adds parser and location
34 ;; parameters. Useful for simple constructors that don't need to
36 (defmacro define-ly-syntax-simple (args . body)
37 `(define-public ,(cons* (car args)
41 (let ((m ,(cons 'begin body)))
42 (set! (ly:music-property m 'origin) location)
45 ;; Music function: Apply function and check return value.
46 (define-ly-syntax-loc (music-function parser loc pred fun args)
47 (let ((m (apply fun parser loc args)))
50 (cond ((eq? pred ly:music?)
51 (ly:parser-error parser (_ "Music syntax function must return Music object") loc)
54 (ly:parser-error parser
55 (format #f (_ "Scheme function must return ~a object") (type-name pred))
59 (define-ly-syntax-simple (void-music)
62 (define-ly-syntax-simple (sequential-music mlist)
63 (make-sequential-music mlist))
65 (define-ly-syntax-simple (simultaneous-music mlist)
66 (make-simultaneous-music mlist))
68 (define-ly-syntax-simple (event-chord mlist)
69 (make-music 'EventChord
72 (define-ly-syntax-simple (unrelativable-music mus)
73 (make-music 'UnrelativableMusic
76 (define-ly-syntax-simple (context-change type id)
77 (make-music 'ContextChange
81 (define-ly-syntax-simple (voice-separator)
82 (make-music 'VoiceSeparator))
84 (define-ly-syntax-simple (bar-check)
85 (make-music 'BarCheck))
87 (define-ly-syntax-simple (time-scaled-music fraction music)
88 (make-music 'TimeScaledMusic
89 'element (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction)))
90 'numerator (car fraction)
91 'denominator (cdr fraction)))
93 (define-ly-syntax (tempo parser location text . rest)
94 (let* ((unit (and (pair? rest)
98 (range-tempo? (pair? count))
99 (tempo-change (make-music 'TempoChangeEvent
103 'metronome-count count))
107 (make-property-set 'tempoWholesPerMinute
111 (round (/ (+ (car count) (cdr count))
115 (ly:duration-length unit)))
119 (make-sequential-music (list tempo-change tempo-set))
122 (define-ly-syntax-simple (repeat type num body alts)
123 (make-repeat type num body alts))
125 (define (script-to-mmrest-text music)
126 "Extract @code{'direction} and @code{'text} from @var{music}, and transform
127 into a @code{MultiMeasureTextEvent}."
129 (if (memq 'script-event (ly:music-property music 'types))
130 (let* ((location (ly:music-property music 'origin))
131 (dir (ly:music-property music 'direction))
132 (tags (ly:music-property music 'tags))
133 (p (make-music 'MultiMeasureTextEvent
136 'text (ly:music-property music 'text))))
138 (set! (ly:music-property p 'direction) dir))
142 (define-ly-syntax (multi-measure-rest parser location duration articulations)
143 (make-music 'MultiMeasureRestMusic
144 'articulations (map script-to-mmrest-text articulations)
148 (define-ly-syntax (repetition-chord parser location previous-chord repetition-function duration articulations)
149 (make-music 'RepeatedChord
150 'original-chord previous-chord
151 'element (repetition-function previous-chord location duration articulations)
154 (define-ly-syntax-simple (context-specification type id mus ops create-new)
155 (let* ((type-sym (if (symbol? type) type (string->symbol type)))
156 (csm (context-spec-music mus type-sym id)))
157 (set! (ly:music-property csm 'property-operations) ops)
158 (if create-new (set! (ly:music-property csm 'create-new) #t))
161 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
162 (let* ((props (case music-type
163 ((PropertySet) (list 'value (car args)))
164 ((PropertyUnset) '())
165 ((OverrideProperty) (list 'grob-value (car args)
166 'grob-property-path (if (list? (cadr args))
171 (if (list? (car args))
172 (list 'grob-property-path (car args))
173 (list 'grob-property-path args)))
174 (else (ly:error (_ "Invalid property operation ~a") music-type))))
175 (oprops (if once (cons* 'once once props) props))
176 (m (apply make-music music-type
180 (make-music 'ContextSpeccedMusic
185 ;; TODO: It seems that this function rarely returns anything useful.
186 (define (get-first-context-id type mus)
187 "Find the name of a ContextSpeccedMusic with given type"
188 (let ((id (ly:music-property mus 'context-id)))
189 (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
190 (eq? (ly:music-property mus 'context-type) type)
192 (not (string-null? id)))
196 (define unique-counter -1)
197 (define (get-next-unique-voice-name)
198 (set! unique-counter (1+ unique-counter))
199 (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
201 (define (lyric-combine-music sync music loc)
202 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
203 ;; to signal to the Extender_engraver that any pending extender should
204 ;; be completed if the lyrics end before the associated voice.
205 (append! (ly:music-property music 'elements)
206 (list (make-music 'CompletizeExtenderEvent)))
207 (make-music 'LyricCombineMusic
209 'associated-context sync
212 (define-ly-syntax (lyric-combine parser location voice music)
213 (lyric-combine-music voice music location))
215 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
216 (let* ((existing-voice-name (get-first-context-id 'Voice music))
217 (voice-name (if (string? existing-voice-name)
219 (get-next-unique-voice-name)))
220 (voice (if (string? existing-voice-name)
222 (make-music 'ContextSpeccedMusic
225 'context-id voice-name
226 'origin (ly:music-property music 'origin))))
227 (lyricstos (map (lambda (mus)
228 (let* ((loc (ly:music-property mus 'origin))
229 (lyr (lyric-combine-music voice-name mus loc)))
230 (make-music 'ContextSpeccedMusic
232 'context-type 'Lyrics
236 (make-simultaneous-music (cons voice lyricstos))))
238 (define-ly-syntax (make-mark-set parser location label)
239 "Make the music for the \\mark command."
240 (let* ((set (and (integer? label)
241 (context-spec-music (make-property-set 'rehearsalMark label)
243 (ev (make-music 'MarkEvent
247 (make-sequential-music (list set ev))
249 (set! (ly:music-property ev 'label) label)