1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2006--2015 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 ,@body))
23 ;; A ly-syntax constructor takes one extra parameter,
24 ;; location. This is 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) ,(second args))
33 ;; Like define-ly-syntax-loc, but adds location
34 ;; parameter. Useful for simple constructors that don't need to
36 (defmacro define-ly-syntax-simple (args . body)
37 `(define-public ,(cons* (car args)
40 (let ((m ,(cons 'begin body)))
41 (set! (ly:music-property m 'origin) location)
44 (define (music-function-call-error loc fun m)
45 (let* ((sig (ly:music-function-signature fun))
46 (pred (if (pair? (car sig)) (caar sig) (car sig))))
48 (format #f (_ "~a function cannot return ~a")
50 (value->lily-string m))
52 (and (pair? (car sig)) (cdar sig))))
54 ;; Music function: Apply function and check return value.
55 ;; args are in reverse order, rest may specify additional ones
57 ;; If args is not a proper list, an error has been flagged earlier
58 ;; and no fallback value had been available. In this case,
59 ;; we don't call the function but rather return the general
61 (define-ly-syntax (music-function loc fun args . rest)
62 (let* ((sig (ly:music-function-signature fun))
63 (pred (if (pair? (car sig)) (caar sig) (car sig)))
64 (good (proper-list? args))
65 (m (and good (with-fluids ((%location loc))
66 (apply (ly:music-function-extract fun)
67 (reverse! args rest))))))
68 (if (and good (pred m))
71 (set! (ly:music-property m 'origin) loc))
74 (music-function-call-error loc fun m)
75 (and (pair? (car sig)) (cdar sig))))))
77 (define-ly-syntax (argument-error location n pred arg)
80 (_ "wrong type for argument ~a. Expecting ~a, found ~s")
81 n (type-name pred) (music->make-music arg))
84 (define-ly-syntax-simple (void-music)
87 (define-ly-syntax-simple (sequential-music mlist)
88 (make-sequential-music mlist))
90 (define-ly-syntax-simple (simultaneous-music mlist)
91 (make-simultaneous-music mlist))
93 (define-ly-syntax-simple (event-chord mlist)
94 (make-music 'EventChord
97 (define-ly-syntax-simple (unrelativable-music mus)
98 (make-music 'UnrelativableMusic
101 (define-ly-syntax-simple (context-change type id)
102 (make-music 'ContextChange
106 (define-ly-syntax (tempo 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 (music-is-of-type? music 'script-event)
143 (make-music 'MultiMeasureTextEvent music)
146 (define-ly-syntax (multi-measure-rest location duration articulations)
147 (make-music 'MultiMeasureRestMusic
148 'articulations (map script-to-mmrest-text articulations)
152 (define-ly-syntax (repetition-chord location duration articulations)
153 (make-music 'EventChord
155 'elements articulations
158 (define-ly-syntax-simple (context-specification type id ops create-new mus)
159 (let ((csm (context-spec-music mus type id)))
160 (set! (ly:music-property csm 'property-operations) ops)
161 (if create-new (set! (ly:music-property csm 'create-new) #t))
164 (define-ly-syntax (composed-markup-list location commands markups)
165 ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
166 ;; and `commands' a list of commands with their scheme arguments, in reverse order,
167 ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
168 ;; ((bold (raise 4 (italic markup1)))
169 ;; (bold (raise 4 (italic markup2)))
170 ;; (bold (raise 4 (italic markup3))))
172 (define (compose arg)
174 (lambda (cmd prev) (append cmd (list prev)))
177 (let loop ((markups markups) (completed '()))
178 (cond ((null? markups) (reverse! completed))
179 ((markup? (car markups))
181 (cons (compose (car markups)) completed)))
184 (lambda () (break! markup? markups))
185 (lambda (complex rest)
188 (make-map-markup-commands-markup-list
189 compose complex) completed))))))))
191 (define-ly-syntax (property-operation location ctx music-type symbol . args)
192 (let* ((props (case music-type
193 ((PropertySet) (list 'value (car args)))
194 ((PropertyUnset) '())
195 ((OverrideProperty) (list 'grob-value (car args)
196 'grob-property-path (if (list? (cadr args))
201 (if (list? (car args))
202 (list 'grob-property-path (car args))
203 (list 'grob-property-path args)))
204 (else (ly:error (_ "Invalid property operation ~a") music-type))))
205 (m (apply make-music music-type
209 (make-music 'ContextSpeccedMusic
214 (define (get-first-context-id! mus)
215 "Find the name of a ContextSpeccedMusic, possibly naming it"
216 (let ((id (ly:music-property mus 'context-id)))
217 (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
218 (if (and (string? id)
219 (not (string-null? id)))
221 ;; We may reliably give a new context a unique name, but
222 ;; not an existing one
223 (if (ly:music-property mus 'create-new #f)
224 (let ((id (get-next-unique-voice-name)))
225 (set! (ly:music-property mus 'context-id) id)
230 (define unique-counter -1)
231 (define (get-next-unique-voice-name)
232 (set! unique-counter (1+ unique-counter))
233 (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
235 (define-ly-syntax-simple (lyric-event text duration)
236 (make-lyric-event text duration))
238 (define (lyric-combine-music sync sync-type music loc)
239 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
240 ;; to signal to the Extender_engraver that any pending extender should
241 ;; be completed if the lyrics end before the associated voice.
242 (append! (ly:music-property music 'elements)
243 (list (make-music 'CompletizeExtenderEvent)))
244 (make-music 'LyricCombineMusic
246 'associated-context sync
247 'associated-context-type sync-type
250 (define-ly-syntax (lyric-combine location voice typ music)
251 (lyric-combine-music voice typ music location))
253 (define-ly-syntax (add-lyrics location music addlyrics-list)
254 (let* ((existing-voice-name (get-first-context-id! music))
255 (voice-name (if (string? existing-voice-name)
257 (get-next-unique-voice-name)))
258 (voice (if (string? existing-voice-name)
260 (make-music 'ContextSpeccedMusic
263 'context-id voice-name
264 'origin (ly:music-property music 'origin))))
265 (voice-type (ly:music-property voice 'context-type))
266 (lyricstos (map (lambda (mus)
267 (let* ((loc (ly:music-property mus 'origin))
268 (lyr (lyric-combine-music
269 voice-name voice-type mus loc)))
270 (make-music 'ContextSpeccedMusic
272 'context-type 'Lyrics
276 (make-simultaneous-music (cons voice lyricstos))))