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
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 (object-property fun 'music-function-signature))
54 (pred (if (pair? (car sig)) (caar sig) (car sig)))
55 (good (proper-list? args))
56 (m (and good (apply fun parser loc (reverse! args rest)))))
57 (if (and good (pred m))
60 (set! (ly:music-property m 'origin) loc))
64 (ly:parser-error parser
65 (format #f (_ "~a function cannot return ~a")
68 (and (pair? (car sig)) (cdar sig))))))
70 (define-ly-syntax (argument-error parser location n pred arg)
74 (_ "wrong type for argument ~a. Expecting ~a, found ~s")
75 n (type-name pred) arg)
78 (define-ly-syntax-simple (void-music)
81 (define-ly-syntax-simple (sequential-music mlist)
82 (make-sequential-music mlist))
84 (define-ly-syntax-simple (simultaneous-music mlist)
85 (make-simultaneous-music mlist))
87 (define-ly-syntax-simple (event-chord mlist)
88 (make-music 'EventChord
91 (define-ly-syntax-simple (unrelativable-music mus)
92 (make-music 'UnrelativableMusic
95 (define-ly-syntax-simple (context-change type id)
96 (make-music 'ContextChange
100 (define-ly-syntax-simple (voice-separator)
101 (make-music 'VoiceSeparator))
103 (define-ly-syntax-simple (bar-check)
104 (make-music 'BarCheck))
106 (define-ly-syntax (tempo parser location text . rest)
107 (let* ((unit (and (pair? rest)
111 (range-tempo? (pair? count))
112 (tempo-change (make-music 'TempoChangeEvent
116 'metronome-count count))
120 (make-property-set 'tempoWholesPerMinute
124 (round (/ (+ (car count) (cdr count))
128 (ly:duration-length unit)))
132 (make-sequential-music (list tempo-change tempo-set))
135 (define-ly-syntax-simple (repeat type num body alts)
136 (make-repeat type num body alts))
138 (define (script-to-mmrest-text music)
139 "Extract @code{'direction} and @code{'text} from @var{music}, and transform
140 into a @code{MultiMeasureTextEvent}."
142 (if (memq 'script-event (ly:music-property music 'types))
143 (let* ((location (ly:music-property music 'origin))
144 (dir (ly:music-property music 'direction))
145 (tags (ly:music-property music 'tags))
146 (p (make-music 'MultiMeasureTextEvent
149 'text (ly:music-property music 'text))))
151 (set! (ly:music-property p 'direction) dir))
155 (define-ly-syntax (multi-measure-rest parser location duration articulations)
156 (make-music 'MultiMeasureRestMusic
157 'articulations (map script-to-mmrest-text articulations)
161 (define-ly-syntax (repetition-chord parser location previous-chord repetition-function duration articulations)
162 (make-music 'RepeatedChord
163 'original-chord previous-chord
164 'element (repetition-function previous-chord location duration articulations)
167 (define-ly-syntax-simple (context-specification type id ops create-new mus)
168 (let* ((type-sym (if (symbol? type) type (string->symbol type)))
169 (csm (context-spec-music mus type-sym id)))
170 (set! (ly:music-property csm 'property-operations) ops)
171 (if create-new (set! (ly:music-property csm 'create-new) #t))
174 (define-ly-syntax (property-operation parser location ctx music-type symbol . args)
175 (let* ((props (case music-type
176 ((PropertySet) (list 'value (car args)))
177 ((PropertyUnset) '())
178 ((OverrideProperty) (list 'grob-value (car args)
179 'grob-property-path (if (list? (cadr args))
184 (if (list? (car args))
185 (list 'grob-property-path (car args))
186 (list 'grob-property-path args)))
187 (else (ly:error (_ "Invalid property operation ~a") music-type))))
188 (m (apply make-music music-type
192 (make-music 'ContextSpeccedMusic
197 ;; TODO: It seems that this function rarely returns anything useful.
198 (define (get-first-context-id type mus)
199 "Find the name of a ContextSpeccedMusic with given type"
200 (let ((id (ly:music-property mus 'context-id)))
201 (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
202 (eq? (ly:music-property mus 'context-type) type)
204 (not (string-null? id)))
208 (define unique-counter -1)
209 (define (get-next-unique-voice-name)
210 (set! unique-counter (1+ unique-counter))
211 (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
213 (define-ly-syntax-simple (lyric-event text duration)
214 (make-lyric-event text duration))
216 (define (lyric-combine-music sync music loc)
217 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
218 ;; to signal to the Extender_engraver that any pending extender should
219 ;; be completed if the lyrics end before the associated voice.
220 (append! (ly:music-property music 'elements)
221 (list (make-music 'CompletizeExtenderEvent)))
222 (make-music 'LyricCombineMusic
224 'associated-context sync
227 (define-ly-syntax (lyric-combine parser location voice music)
228 (lyric-combine-music voice music location))
230 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
231 (let* ((existing-voice-name (get-first-context-id 'Voice music))
232 (voice-name (if (string? existing-voice-name)
234 (get-next-unique-voice-name)))
235 (voice (if (string? existing-voice-name)
237 (make-music 'ContextSpeccedMusic
240 'context-id voice-name
241 'origin (ly:music-property music 'origin))))
242 (lyricstos (map (lambda (mus)
243 (let* ((loc (ly:music-property mus 'origin))
244 (lyr (lyric-combine-music voice-name mus loc)))
245 (make-music 'ContextSpeccedMusic
247 'context-type 'Lyrics
251 (make-simultaneous-music (cons voice lyricstos))))