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 (define ly-syntax-module (resolve-module '(ly-syntax)))
20 (defmacro define-ly-syntax (args . body)
22 `(module-define! ,ly-syntax-module ',(car args)
23 (lambda ,(cdr args) ,@body))
24 `(module-define! ,ly-syntax-module ',args ,@body)))
26 ;; A ly-syntax constructor can access location data as (*location*).
27 ;; This is mainly used for reporting errors and warnings. This
28 ;; function is a syntactic sugar which uses (*location*) to set the
29 ;; origin of the returned music object; this behaviour is usually
31 (defmacro define-ly-syntax-loc (args . body)
32 `(define-ly-syntax ,args
33 (let ((m ,(cons 'begin body)))
34 (set! (ly:music-property m 'origin) (*location*))
37 (define (music-function-call-error fun m)
38 (let* ((sig (ly:music-function-signature fun))
39 (pred (if (pair? (car sig)) (caar sig) (car sig))))
41 (format #f (_ "~a function cannot return ~a")
43 (value->lily-string m))
45 (and (pair? (car sig)) (cdar sig))))
47 (define-ly-syntax music-function-call-error music-function-call-error)
49 ;; Music function: Apply function and check return value.
50 ;; args are in reverse order, rest may specify additional ones
52 ;; If args is not a proper list, an error has been flagged earlier
53 ;; and no fallback value had been available. In this case,
54 ;; we don't call the function but rather return the general
56 (define-ly-syntax (music-function fun args . rest)
57 (let* ((sig (ly:music-function-signature fun))
58 (pred (if (pair? (car sig)) (caar sig) (car sig)))
59 (good (proper-list? args))
60 (m (and good (apply (ly:music-function-extract fun)
61 (reverse! args rest)))))
62 (if (and good (pred m))
65 (set! (ly:music-property m 'origin) (*location*)))
68 (music-function-call-error fun m)
69 (and (pair? (car sig)) (cdar sig))))))
71 (define-ly-syntax (argument-error n pred arg)
74 (_ "wrong type for argument ~a. Expecting ~a, found ~s")
75 n (type-name pred) (music->make-music arg))
78 (define-ly-syntax-loc (void-music)
81 (define-ly-syntax-loc (sequential-music mlist)
82 (make-sequential-music mlist))
84 (define-ly-syntax-loc (simultaneous-music mlist)
85 (make-simultaneous-music mlist))
87 (define-ly-syntax-loc (event-chord mlist)
88 (make-music 'EventChord
91 (define-ly-syntax-loc (unrelativable-music mus)
92 (make-music 'UnrelativableMusic
95 (define-ly-syntax-loc (context-change type id)
96 (make-music 'ContextChange
100 (define-ly-syntax (tempo text . rest)
101 (let* ((unit (and (pair? rest)
105 (range-tempo? (pair? count))
106 (tempo-change (make-music 'TempoChangeEvent
110 'metronome-count count))
114 (make-property-set 'tempoWholesPerMinute
118 (round (/ (+ (car count) (cdr count))
122 (ly:duration-length unit)))
126 (make-sequential-music (list tempo-change tempo-set))
129 (define-ly-syntax-loc (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 (music-is-of-type? music 'script-event)
137 (make-music 'MultiMeasureTextEvent music)
140 (define-ly-syntax-loc (multi-measure-rest duration articulations)
141 (make-music 'MultiMeasureRestMusic
142 'articulations (map script-to-mmrest-text articulations)
145 (define-ly-syntax-loc (repetition-chord duration articulations)
146 (make-music 'EventChord
148 'elements articulations))
150 (define-ly-syntax-loc (context-specification type id ops create-new mus)
151 (let ((csm (context-spec-music mus type id)))
152 (set! (ly:music-property csm 'property-operations) ops)
153 (if create-new (set! (ly:music-property csm 'create-new) #t))
156 (define-ly-syntax (composed-markup-list commands markups)
157 ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
158 ;; and `commands' a list of commands with their scheme arguments, in reverse order,
159 ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
160 ;; ((bold (raise 4 (italic markup1)))
161 ;; (bold (raise 4 (italic markup2)))
162 ;; (bold (raise 4 (italic markup3))))
164 (define (compose arg)
166 (lambda (cmd prev) (append cmd (list prev)))
169 (let loop ((markups markups) (completed '()))
170 (cond ((null? markups) (reverse! completed))
171 ((markup? (car markups))
173 (cons (compose (car markups)) completed)))
176 (lambda () (break! markup? markups))
177 (lambda (complex rest)
180 (make-map-markup-commands-markup-list
181 compose complex) completed))))))))
183 (define-ly-syntax (property-operation ctx music-type symbol . args)
184 (let* ((props (case music-type
185 ((PropertySet) (list 'value (car args)))
186 ((PropertyUnset) '())
187 ((OverrideProperty) (list 'grob-value (car args)
188 'grob-property-path (if (list? (cadr args))
193 (if (list? (car args))
194 (list 'grob-property-path (car args))
195 (list 'grob-property-path args)))
196 (else (ly:error (_ "Invalid property operation ~a") music-type))))
197 (m (apply make-music music-type
201 (make-music 'ContextSpeccedMusic
204 'origin (*location*))))
206 (define (get-first-context-id! mus)
207 "Find the name of a ContextSpeccedMusic, possibly naming it"
208 (let ((id (ly:music-property mus 'context-id)))
209 (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
210 (if (and (string? id)
211 (not (string-null? id)))
213 ;; We may reliably give a new context a unique name, but
214 ;; not an existing one
215 (if (ly:music-property mus 'create-new #f)
216 (let ((id (get-next-unique-voice-name)))
217 (set! (ly:music-property mus 'context-id) id)
222 (define unique-counter -1)
223 (define (get-next-unique-voice-name)
224 (set! unique-counter (1+ unique-counter))
225 (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
227 (define-ly-syntax-loc (lyric-event text duration)
228 (make-lyric-event text duration))
230 (define (lyric-combine-music sync sync-type music loc)
231 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
232 ;; to signal to the Extender_engraver that any pending extender should
233 ;; be completed if the lyrics end before the associated voice.
234 (append! (ly:music-property music 'elements)
235 (list (make-music 'CompletizeExtenderEvent)))
236 (make-music 'LyricCombineMusic
238 'associated-context sync
239 'associated-context-type sync-type
242 (define-ly-syntax (lyric-combine voice typ music)
243 (lyric-combine-music voice typ music (*location*)))
245 (define-ly-syntax (add-lyrics music addlyrics-list)
246 (let* ((existing-voice-name (get-first-context-id! music))
247 (voice-name (if (string? existing-voice-name)
249 (get-next-unique-voice-name)))
250 (voice (if (string? existing-voice-name)
252 (make-music 'ContextSpeccedMusic
255 'context-id voice-name
256 'origin (ly:music-property music 'origin))))
257 (voice-type (ly:music-property voice 'context-type))
258 (lyricstos (map (lambda (mus)
259 (let* ((loc (ly:music-property mus 'origin))
260 (lyr (lyric-combine-music
261 voice-name voice-type mus loc)))
262 (make-music 'ContextSpeccedMusic
264 'context-type 'Lyrics
268 (make-simultaneous-music (cons voice lyricstos))))