1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2006--2012 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
48 ;; If args is not a proper list, an error has been flagged earlier
49 ;; and no fallback value had been available. In this case,
50 ;; we don't call the function but rather return the general
52 (define-ly-syntax (music-function parser loc fun args . rest)
53 (let* ((sig (ly:music-function-signature fun))
54 (pred (if (pair? (car sig)) (caar sig) (car sig)))
55 (good (proper-list? args))
56 (m (and good (apply (ly:music-function-extract fun)
57 parser loc (reverse! args rest)))))
58 (if (and good (pred m))
61 (set! (ly:music-property m 'origin) loc))
65 (ly:parser-error parser
66 (format #f (_ "~a function cannot return ~a")
69 (and (pair? (car sig)) (cdar sig))))))
71 (define-ly-syntax (argument-error parser location n pred arg)
75 (_ "wrong type for argument ~a. Expecting ~a, found ~s")
76 n (type-name pred) arg)
79 (define-ly-syntax-simple (void-music)
82 (define-ly-syntax-simple (sequential-music mlist)
83 (make-sequential-music mlist))
85 (define-ly-syntax-simple (simultaneous-music mlist)
86 (make-simultaneous-music mlist))
88 (define-ly-syntax-simple (event-chord mlist)
89 (make-music 'EventChord
92 (define-ly-syntax-simple (unrelativable-music mus)
93 (make-music 'UnrelativableMusic
96 (define-ly-syntax-simple (context-change type id)
97 (make-music 'ContextChange
101 (define-ly-syntax-simple (voice-separator)
102 (make-music 'VoiceSeparator))
104 (define-ly-syntax-simple (bar-check)
105 (make-music 'BarCheck))
107 (define-ly-syntax (tempo parser location text . rest)
108 (let* ((unit (and (pair? rest)
112 (range-tempo? (pair? count))
113 (tempo-change (make-music 'TempoChangeEvent
117 'metronome-count count))
121 (make-property-set 'tempoWholesPerMinute
125 (round (/ (+ (car count) (cdr count))
129 (ly:duration-length unit)))
133 (make-sequential-music (list tempo-change tempo-set))
136 (define-ly-syntax-simple (repeat type num body alts)
137 (make-repeat type num body alts))
139 (define (script-to-mmrest-text music)
140 "Extract @code{'direction} and @code{'text} from @var{music}, and transform
141 into a @code{MultiMeasureTextEvent}."
143 (if (memq 'script-event (ly:music-property music 'types))
144 (let* ((location (ly:music-property music 'origin))
145 (dir (ly:music-property music 'direction))
146 (tags (ly:music-property music 'tags))
147 (p (make-music 'MultiMeasureTextEvent
150 'text (ly:music-property music 'text))))
152 (set! (ly:music-property p 'direction) dir))
156 (define-ly-syntax (multi-measure-rest parser location duration articulations)
157 (make-music 'MultiMeasureRestMusic
158 'articulations (map script-to-mmrest-text articulations)
162 (define-ly-syntax (repetition-chord parser location duration articulations)
163 (make-music 'EventChord
165 'elements articulations
168 (define-ly-syntax-simple (context-specification type id ops create-new mus)
169 (let* ((type-sym (if (symbol? type) type (string->symbol type)))
170 (csm (context-spec-music mus type-sym id)))
171 (set! (ly:music-property csm 'property-operations) ops)
172 (if create-new (set! (ly:music-property csm 'create-new) #t))
175 (define-ly-syntax (property-operation parser location ctx music-type symbol . args)
176 (let* ((props (case music-type
177 ((PropertySet) (list 'value (car args)))
178 ((PropertyUnset) '())
179 ((OverrideProperty) (list 'grob-value (car args)
180 'grob-property-path (if (list? (cadr args))
185 (if (list? (car args))
186 (list 'grob-property-path (car args))
187 (list 'grob-property-path args)))
188 (else (ly:error (_ "Invalid property operation ~a") music-type))))
189 (m (apply make-music music-type
193 (make-music 'ContextSpeccedMusic
198 ;; TODO: It seems that this function rarely returns anything useful.
199 (define (get-first-context-id type mus)
200 "Find the name of a ContextSpeccedMusic with given type"
201 (let ((id (ly:music-property mus 'context-id)))
202 (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
203 (eq? (ly:music-property mus 'context-type) type)
205 (not (string-null? id)))
209 (define unique-counter -1)
210 (define (get-next-unique-voice-name)
211 (set! unique-counter (1+ unique-counter))
212 (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
214 (define-ly-syntax-simple (lyric-event text duration)
215 (make-lyric-event text duration))
217 (define (lyric-combine-music sync music loc)
218 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
219 ;; to signal to the Extender_engraver that any pending extender should
220 ;; be completed if the lyrics end before the associated voice.
221 (append! (ly:music-property music 'elements)
222 (list (make-music 'CompletizeExtenderEvent)))
223 (make-music 'LyricCombineMusic
225 'associated-context sync
228 (define-ly-syntax (lyric-combine parser location voice music)
229 (lyric-combine-music voice music location))
231 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
232 (let* ((existing-voice-name (get-first-context-id 'Voice music))
233 (voice-name (if (string? existing-voice-name)
235 (get-next-unique-voice-name)))
236 (voice (if (string? existing-voice-name)
238 (make-music 'ContextSpeccedMusic
241 'context-id voice-name
242 'origin (ly:music-property music 'origin))))
243 (lyricstos (map (lambda (mus)
244 (let* ((loc (ly:music-property mus 'origin))
245 (lyr (lyric-combine-music voice-name mus loc)))
246 (make-music 'ContextSpeccedMusic
248 'context-type 'Lyrics
252 (make-simultaneous-music (cons voice lyricstos))))