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-module (scm ly-syntax-constructors)
20 #:use-module (srfi srfi-1))
22 ;; A ly-syntax constructor can access location data as (*location*).
23 ;; This is mainly used for reporting errors and warnings. This
24 ;; function is a syntactic sugar which uses (*location*) to set the
25 ;; origin of the returned music object; this behaviour is usually
27 (defmacro define-loc (args . body)
29 (let ((m ,(cons 'begin body)))
30 (set! (ly:music-property m 'origin) (*location*))
33 (define-public (music-function-call-error fun m)
34 (let* ((sig (ly:music-function-signature fun))
35 (pred (if (pair? (car sig)) (caar sig) (car sig))))
37 (format #f (_ "~a function cannot return ~a")
39 (value->lily-string m))
41 (and (pair? (car sig)) (cdar sig))))
43 ;; Music function: Apply function and check return value.
44 ;; args are in reverse order, rest may specify additional ones
46 ;; If args is not a proper list, an error has been flagged earlier
47 ;; and no fallback value had been available. In this case,
48 ;; we don't call the function but rather return the general
50 (define-public (music-function fun args . rest)
51 (let* ((sig (ly:music-function-signature fun))
52 (pred (if (pair? (car sig)) (caar sig) (car sig)))
53 (good (proper-list? args))
54 (m (and good (apply (ly:music-function-extract fun)
55 (reverse! args rest)))))
56 (if (and good (pred m))
59 (set! (ly:music-property m 'origin) (*location*)))
62 (music-function-call-error fun m)
63 (and (pair? (car sig)) (cdar sig))))))
65 (define-public (argument-error n pred arg)
68 (_ "wrong type for argument ~a. Expecting ~a, found ~s")
69 n (type-name pred) (music->make-music arg))
72 (define-loc (void-music)
75 (define-loc (sequential-music mlist)
76 (make-sequential-music mlist))
78 (define-loc (simultaneous-music mlist)
79 (make-simultaneous-music mlist))
81 (define-loc (event-chord mlist)
82 (make-music 'EventChord
85 (define-loc (unrelativable-music mus)
86 (make-music 'UnrelativableMusic
89 (define-loc (context-change type id)
90 (make-music 'ContextChange
94 (define-public (tempo text . rest)
95 (let* ((unit (and (pair? rest)
99 (range-tempo? (pair? count))
100 (tempo-change (make-music 'TempoChangeEvent
104 'metronome-count count))
108 (make-property-set 'tempoWholesPerMinute
112 (round (/ (+ (car count) (cdr count))
116 (ly:duration-length unit)))
120 (make-sequential-music (list tempo-change tempo-set))
123 (define-loc (repeat type num body alts)
124 (make-repeat type num body alts))
126 (define (script-to-mmrest-text music)
127 "Extract @code{'direction} and @code{'text} from @var{music}, and transform
128 into a @code{MultiMeasureTextEvent}."
130 (if (music-is-of-type? music 'script-event)
131 (make-music 'MultiMeasureTextEvent music)
134 (define-loc (multi-measure-rest duration articulations)
135 (make-music 'MultiMeasureRestMusic
136 'articulations (map script-to-mmrest-text articulations)
139 (define-loc (repetition-chord duration articulations)
140 (make-music 'EventChord
142 'elements articulations))
144 (define-loc (context-specification type id ops create-new mus)
145 (let ((csm (context-spec-music mus type id)))
146 (set! (ly:music-property csm 'property-operations) ops)
147 (if create-new (set! (ly:music-property csm 'create-new) #t))
150 (define-public (composed-markup-list commands markups)
151 ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
152 ;; and `commands' a list of commands with their scheme arguments, in reverse order,
153 ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
154 ;; ((bold (raise 4 (italic markup1)))
155 ;; (bold (raise 4 (italic markup2)))
156 ;; (bold (raise 4 (italic markup3))))
158 (define (compose arg)
160 (lambda (cmd prev) (append cmd (list prev)))
163 (let loop ((markups markups) (completed '()))
164 (cond ((null? markups) (reverse! completed))
165 ((markup? (car markups))
167 (cons (compose (car markups)) completed)))
170 (lambda () (break! markup? markups))
171 (lambda (complex rest)
174 (make-map-markup-commands-markup-list
175 compose complex) completed))))))))
177 (define-public (property-operation ctx music-type symbol . args)
178 (let* ((props (case music-type
179 ((PropertySet) (list 'value (car args)))
180 ((PropertyUnset) '())
181 ((OverrideProperty) (list 'grob-value (car args)
182 'grob-property-path (if (list? (cadr args))
187 (if (list? (car args))
188 (list 'grob-property-path (car args))
189 (list 'grob-property-path args)))
190 (else (ly:error (_ "Invalid property operation ~a") music-type))))
191 (m (apply make-music music-type
195 (make-music 'ContextSpeccedMusic
198 'origin (*location*))))
200 (define (get-first-context-id! mus)
201 "Find the name of a ContextSpeccedMusic, possibly naming it"
202 (let ((id (ly:music-property mus 'context-id)))
203 (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
204 (if (and (string? id)
205 (not (string-null? id)))
207 ;; We may reliably give a new context a unique name, but
208 ;; not an existing one
209 (if (ly:music-property mus 'create-new #f)
210 (let ((id (get-next-unique-voice-name)))
211 (set! (ly:music-property mus 'context-id) id)
216 (define-loc (lyric-event text duration)
217 (make-lyric-event text duration))
219 (define (lyric-combine-music sync sync-type music loc)
220 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
221 ;; to signal to the Extender_engraver that any pending extender should
222 ;; be completed if the lyrics end before the associated voice.
223 (append! (ly:music-property music 'elements)
224 (list (make-music 'CompletizeExtenderEvent)))
225 (make-music 'LyricCombineMusic
227 'associated-context sync
228 'associated-context-type sync-type
231 (define-public (lyric-combine voice typ music)
232 (lyric-combine-music voice typ music (*location*)))
234 (define-public (add-lyrics music addlyrics-list)
235 (let* ((existing-voice-name (get-first-context-id! music))
236 (voice-name (if (string? existing-voice-name)
238 (get-next-unique-voice-name)))
239 (voice (if (string? existing-voice-name)
241 (make-music 'ContextSpeccedMusic
244 'context-id voice-name
245 'origin (ly:music-property music 'origin))))
246 (voice-type (ly:music-property voice 'context-type))
247 (lyricstos (map (lambda (mus)
248 (let* ((loc (ly:music-property mus 'origin))
249 (lyr (lyric-combine-music
250 voice-name voice-type mus loc)))
251 (make-music 'ContextSpeccedMusic
253 'context-type 'Lyrics
257 (make-simultaneous-music (cons voice lyricstos))))