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 ;; Scheme function: Apply function, return value can be anything
46 (define-ly-syntax (scheme-function parser loc fun args)
47 (apply fun parser loc args))
49 ;; Music function: Apply function and check return value.
50 (define-ly-syntax-loc (music-function parser loc fun args)
51 (let ((m (apply fun parser loc args)))
55 (ly:parser-error parser (_ "Music head function must return Music object") loc)
56 (make-music 'Music)))))
58 (define-ly-syntax-simple (void-music)
61 (define-ly-syntax-simple (sequential-music mlist)
62 (make-sequential-music mlist))
64 (define-ly-syntax-simple (simultaneous-music mlist)
65 (make-simultaneous-music mlist))
67 (define-ly-syntax-simple (event-chord mlist)
68 (make-music 'EventChord
71 (define-ly-syntax-simple (unrelativable-music mus)
72 (make-music 'UnrelativableMusic
75 (define-ly-syntax-simple (context-change type id)
76 (make-music 'ContextChange
80 (define-ly-syntax-simple (voice-separator)
81 (make-music 'VoiceSeparator))
83 (define-ly-syntax-simple (bar-check)
84 (make-music 'BarCheck))
86 (define-ly-syntax-simple (time-scaled-music fraction music)
87 (make-music 'TimeScaledMusic
88 'element (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction)))
89 'numerator (car fraction)
90 'denominator (cdr fraction)))
92 (define-ly-syntax-simple (transpose-music pitch music)
93 (make-music 'TransposedMusic
94 'element (ly:music-transpose music pitch)))
96 (define-ly-syntax (tempo parser location text . rest)
97 (let* ((unit (and (pair? rest)
101 (range-tempo? (pair? count))
102 (tempo-change (make-music 'TempoChangeEvent
106 'metronome-count count))
110 (make-property-set 'tempoWholesPerMinute
114 (round (/ (+ (car count) (cdr count))
118 (ly:duration-length unit)))
122 (make-sequential-music (list tempo-change tempo-set))
125 (define-ly-syntax-simple (skip-music dur)
126 (make-music 'SkipMusic
129 (define-ly-syntax-simple (repeat type num body alts)
130 (make-repeat type num body alts))
132 (define (script-to-mmrest-text music)
133 "Extract @code{'direction} and @code{'text} from @var{music}, and transform
134 into a @code{MultiMeasureTextEvent}."
136 (if (memq 'script-event (ly:music-property music 'types))
137 (let* ((location (ly:music-property music 'origin))
138 (dir (ly:music-property music 'direction))
139 (tags (ly:music-property music 'tags))
140 (p (make-music 'MultiMeasureTextEvent
143 'text (ly:music-property music 'text))))
145 (set! (ly:music-property p 'direction) dir))
149 (define-ly-syntax (multi-measure-rest parser location duration articulations)
150 (make-music 'MultiMeasureRestMusic
151 'articulations (map script-to-mmrest-text articulations)
155 (define-ly-syntax (repetition-chord parser location previous-chord repetition-function duration articulations)
156 (make-music 'RepeatedChord
157 'original-chord previous-chord
158 'element (repetition-function previous-chord location duration articulations)
161 (define-ly-syntax-simple (context-specification type id mus ops create-new)
162 (let* ((type-sym (if (symbol? type) type (string->symbol type)))
163 (csm (context-spec-music mus type-sym id)))
164 (set! (ly:music-property csm 'property-operations) ops)
165 (if create-new (set! (ly:music-property csm 'create-new) #t))
168 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
169 (let* ((props (case music-type
170 ((PropertySet) (list 'value (car args)))
171 ((PropertyUnset) '())
172 ((OverrideProperty) (list 'grob-value (car args)
173 'grob-property-path (if (list? (cadr args))
178 (if (list? (car args))
179 (list 'grob-property-path (car args))
180 (list 'grob-property-path args)))
181 (else (ly:error (_ "Invalid property operation ~a") music-type))))
182 (oprops (if once (cons* 'once once props) props))
183 (m (apply make-music music-type
187 (make-music 'ContextSpeccedMusic
192 ;; TODO: It seems that this function rarely returns anything useful.
193 (define (get-first-context-id type mus)
194 "Find the name of a ContextSpeccedMusic with given type"
195 (let ((id (ly:music-property mus 'context-id)))
196 (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
197 (eq? (ly:music-property mus 'context-type) type)
199 (not (string-null? id)))
203 (define unique-counter -1)
204 (define (get-next-unique-voice-name)
205 (set! unique-counter (1+ unique-counter))
206 (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
208 (define (lyric-combine-music sync music loc)
209 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
210 ;; to signal to the Extender_engraver that any pending extender should
211 ;; be completed if the lyrics end before the associated voice.
212 (append! (ly:music-property music 'elements)
213 (list (make-music 'CompletizeExtenderEvent)))
214 (make-music 'LyricCombineMusic
216 'associated-context sync
219 (define-ly-syntax (lyric-combine parser location voice music)
220 (lyric-combine-music voice music location))
222 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
223 (let* ((existing-voice-name (get-first-context-id 'Voice music))
224 (voice-name (if (string? existing-voice-name)
226 (get-next-unique-voice-name)))
227 (voice (if (string? existing-voice-name)
229 (make-music 'ContextSpeccedMusic
232 'context-id voice-name
233 'origin (ly:music-property music 'origin))))
234 (lyricstos (map (lambda (mus)
235 (let* ((loc (ly:music-property mus 'origin))
236 (lyr (lyric-combine-music voice-name mus loc)))
237 (make-music 'ContextSpeccedMusic
239 'context-type 'Lyrics
243 (make-simultaneous-music (cons voice lyricstos))))
245 (define-ly-syntax (make-mark-set parser location label)
246 "Make the music for the \\mark command."
247 (let* ((set (and (integer? label)
248 (context-spec-music (make-property-set 'rehearsalMark label)
250 (ev (make-music 'MarkEvent
254 (make-sequential-music (list set ev))
256 (set! (ly:music-property ev 'label) label)
259 (define-ly-syntax (partial parser location dur)
260 "Make a partial measure."
262 ;; We use `descend-to-context' here instead of `context-spec-music' to
263 ;; ensure \partial still works if the Timing_translator is moved
265 (context-spec-music (make-music 'PartialSet
267 'partial-duration dur)