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 ;; args are in reverse order, rest may specify additional ones
47 (define-ly-syntax (music-function parser loc fun args . rest)
48 (let* ((sig (object-property fun 'music-function-signature))
49 (pred (if (pair? (car sig)) (caar sig) (car sig)))
50 (m (apply fun parser loc (reverse! args rest))))
54 (set! (ly:music-property m 'origin) loc))
57 (ly:parser-error parser
58 (format #f (_ "~a function cannot return ~a")
61 (and (pair? (car sig)) (cdar sig))))))
63 (define-ly-syntax-simple (void-music)
66 (define-ly-syntax-simple (sequential-music mlist)
67 (make-sequential-music mlist))
69 (define-ly-syntax-simple (simultaneous-music mlist)
70 (make-simultaneous-music mlist))
72 (define-ly-syntax-simple (event-chord mlist)
73 (make-music 'EventChord
76 (define-ly-syntax-simple (unrelativable-music mus)
77 (make-music 'UnrelativableMusic
80 (define-ly-syntax-simple (context-change type id)
81 (make-music 'ContextChange
85 (define-ly-syntax-simple (voice-separator)
86 (make-music 'VoiceSeparator))
88 (define-ly-syntax-simple (bar-check)
89 (make-music 'BarCheck))
91 (define-ly-syntax-simple (time-scaled-music fraction music)
92 (make-music 'TimeScaledMusic
93 'element (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction)))
94 'numerator (car fraction)
95 'denominator (cdr fraction)))
97 (define-ly-syntax (tempo parser location text . rest)
98 (let* ((unit (and (pair? rest)
102 (range-tempo? (pair? count))
103 (tempo-change (make-music 'TempoChangeEvent
107 'metronome-count count))
111 (make-property-set 'tempoWholesPerMinute
115 (round (/ (+ (car count) (cdr count))
119 (ly:duration-length unit)))
123 (make-sequential-music (list tempo-change tempo-set))
126 (define-ly-syntax-simple (repeat type num body alts)
127 (make-repeat type num body alts))
129 (define (script-to-mmrest-text music)
130 "Extract @code{'direction} and @code{'text} from @var{music}, and transform
131 into a @code{MultiMeasureTextEvent}."
133 (if (memq 'script-event (ly:music-property music 'types))
134 (let* ((location (ly:music-property music 'origin))
135 (dir (ly:music-property music 'direction))
136 (tags (ly:music-property music 'tags))
137 (p (make-music 'MultiMeasureTextEvent
140 'text (ly:music-property music 'text))))
142 (set! (ly:music-property p 'direction) dir))
146 (define-ly-syntax (multi-measure-rest parser location duration articulations)
147 (make-music 'MultiMeasureRestMusic
148 'articulations (map script-to-mmrest-text articulations)
152 (define-ly-syntax (repetition-chord parser location previous-chord repetition-function duration articulations)
153 (make-music 'RepeatedChord
154 'original-chord previous-chord
155 'element (repetition-function previous-chord location duration articulations)
158 (define-ly-syntax-simple (context-specification type id ops create-new mus)
159 (let* ((type-sym (if (symbol? type) type (string->symbol type)))
160 (csm (context-spec-music mus type-sym id)))
161 (set! (ly:music-property csm 'property-operations) ops)
162 (if create-new (set! (ly:music-property csm 'create-new) #t))
165 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
166 (let* ((props (case music-type
167 ((PropertySet) (list 'value (car args)))
168 ((PropertyUnset) '())
169 ((OverrideProperty) (list 'grob-value (car args)
170 'grob-property-path (if (list? (cadr args))
175 (if (list? (car args))
176 (list 'grob-property-path (car args))
177 (list 'grob-property-path args)))
178 (else (ly:error (_ "Invalid property operation ~a") music-type))))
179 (oprops (if once (cons* 'once once props) props))
180 (m (apply make-music music-type
184 (make-music 'ContextSpeccedMusic
189 ;; TODO: It seems that this function rarely returns anything useful.
190 (define (get-first-context-id type mus)
191 "Find the name of a ContextSpeccedMusic with given type"
192 (let ((id (ly:music-property mus 'context-id)))
193 (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
194 (eq? (ly:music-property mus 'context-type) type)
196 (not (string-null? id)))
200 (define unique-counter -1)
201 (define (get-next-unique-voice-name)
202 (set! unique-counter (1+ unique-counter))
203 (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
205 (define (lyric-combine-music sync music loc)
206 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
207 ;; to signal to the Extender_engraver that any pending extender should
208 ;; be completed if the lyrics end before the associated voice.
209 (append! (ly:music-property music 'elements)
210 (list (make-music 'CompletizeExtenderEvent)))
211 (make-music 'LyricCombineMusic
213 'associated-context sync
216 (define-ly-syntax (lyric-combine parser location voice music)
217 (lyric-combine-music voice music location))
219 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
220 (let* ((existing-voice-name (get-first-context-id 'Voice music))
221 (voice-name (if (string? existing-voice-name)
223 (get-next-unique-voice-name)))
224 (voice (if (string? existing-voice-name)
226 (make-music 'ContextSpeccedMusic
229 'context-id voice-name
230 'origin (ly:music-property music 'origin))))
231 (lyricstos (map (lambda (mus)
232 (let* ((loc (ly:music-property mus 'origin))
233 (lyr (lyric-combine-music voice-name mus loc)))
234 (make-music 'ContextSpeccedMusic
236 'context-type 'Lyrics
240 (make-simultaneous-music (cons voice lyricstos))))
242 (define-ly-syntax (make-mark-set parser location label)
243 "Make the music for the \\mark command."
244 (let* ((set (and (integer? label)
245 (context-spec-music (make-property-set 'rehearsalMark label)
247 (ev (make-music 'MarkEvent
251 (make-sequential-music (list set ev))
253 (set! (ly:music-property ev 'label) label)