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 (define-public (music-function-call-error fun m)
23 (let* ((sigcar (car (ly:music-function-signature fun)))
24 (pred? (if (pair? sigcar) (car sigcar) sigcar)))
26 (format #f (_ "~a function cannot return ~a")
28 (value->lily-string m))
31 (if (ly:music? (cdr sigcar))
32 (ly:music-deep-copy (cdr sigcar) (*location*))
35 ;; Music function: Apply function and check return value.
36 ;; args are in reverse order.
38 ;; If args is not a proper list, an error has been flagged earlier
39 ;; and no fallback value had been available. In this case,
40 ;; we don't call the function but rather return the general
42 (define-public (music-function fun args)
43 (let* ((sigcar (car (ly:music-function-signature fun)))
44 (pred? (if (pair? sigcar) (car sigcar) sigcar))
46 (m (and good (apply (ly:music-function-extract fun) (reverse! args)))))
49 (if (ly:music? m) (ly:set-origin! m) m)
50 (music-function-call-error fun m))
52 (if (ly:music (cdr sigcar))
53 (ly:music-deep-copy (cdr sigcar) (*location*))
56 (define-public (argument-error n pred arg)
59 (_ "wrong type for argument ~a. Expecting ~a, found ~s")
60 n (type-name pred) (music->make-music arg))
63 ;; Used for chaining several music functions together. `final'
64 ;; contains the last argument and still needs typechecking.
65 (define (music-function-chain fun args final)
66 (let* ((siglast (last (ly:music-function-signature fun)))
67 (pred? (if (pair? siglast) (car siglast) siglast)))
69 (music-function fun (cons final args))
71 (argument-error (1+ (length args)) pred? final)
72 ;; call music function just for the error return value
73 (music-function fun #f)))))
75 (define-public (partial-music-function fun-list arg-list)
76 (let* ((good (every list? arg-list))
77 (sig (ly:music-function-signature (car fun-list))))
79 (ly:make-music-function
80 (cons (car sig) (list-tail (cdr sig) (length (car arg-list))))
82 ;; Every time we use music-function, it destructively
83 ;; reverses its list of arguments. Changing the calling
84 ;; convention would be non-trivial since we do error
85 ;; propagation to the reversed argument list by making it
86 ;; a non-proper list. So we just create a fresh copy of
87 ;; all argument lists for each call. We also want to
88 ;; avoid reusing any music expressions without copying and
89 ;; want to let them point to the location of the music
90 ;; function call rather than its definition.
91 (let ((arg-list (ly:music-deep-copy arg-list (*location*))))
92 (fold music-function-chain
93 (music-function (car fun-list)
94 (reverse! rest (car arg-list)))
95 (cdr fun-list) (cdr arg-list))))))))
97 (define-public (void-music)
98 (ly:set-origin! (make-music 'Music)))
100 (define-public (sequential-music mlist)
101 (ly:set-origin! (make-sequential-music mlist)))
103 (define-public (simultaneous-music mlist)
104 (ly:set-origin! (make-simultaneous-music mlist)))
106 (define-public (event-chord mlist)
107 (ly:set-origin! (make-music 'EventChord
110 (define-public (unrelativable-music mus)
111 (ly:set-origin! (make-music 'UnrelativableMusic
114 (define-public (context-change type id)
115 (ly:set-origin! (make-music 'ContextChange
119 (define-public (tempo text . rest)
120 (let* ((unit (and (pair? rest)
124 (range-tempo? (pair? count))
125 (tempo-change (ly:set-origin! (make-music 'TempoChangeEvent
128 'metronome-count count)))
132 (make-property-set 'tempoWholesPerMinute
136 (round (/ (+ (car count) (cdr count))
140 (ly:duration-length unit)))
144 (make-sequential-music (list tempo-change tempo-set))
147 (define-public (repeat type num body alts)
148 (ly:set-origin! (make-repeat type num body alts)))
150 (define (script-to-mmrest-text music)
151 "Extract @code{'direction} and @code{'text} from @var{music}, and transform
152 into a @code{MultiMeasureTextEvent}."
154 (if (music-is-of-type? music 'script-event)
155 (make-music 'MultiMeasureTextEvent music)
158 (define-public (multi-measure-rest duration articulations)
159 (ly:set-origin! (make-music 'MultiMeasureRestMusic
160 'articulations (map script-to-mmrest-text articulations)
161 'duration duration)))
163 (define-public (repetition-chord duration articulations)
164 (ly:set-origin! (make-music 'EventChord
166 'elements articulations)))
168 (define-public (context-specification type id ops create-new mus)
169 (let ((csm (context-spec-music mus type id)))
170 (set! (ly:music-property csm 'property-operations) ops)
171 (if create-new (set! (ly:music-property csm 'create-new) #t))
172 (ly:set-origin! csm)))
174 (define-public (composed-markup-list commands markups)
175 ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
176 ;; and `commands' a list of commands with their scheme arguments, in reverse order,
177 ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
178 ;; ((bold (raise 4 (italic markup1)))
179 ;; (bold (raise 4 (italic markup2)))
180 ;; (bold (raise 4 (italic markup3))))
182 (define (compose arg)
184 (lambda (cmd prev) (append cmd (list prev)))
187 (let loop ((markups markups) (completed '()))
188 (cond ((null? markups) (reverse! completed))
189 ((markup? (car markups))
191 (cons (compose (car markups)) completed)))
194 (lambda () (break! markup? markups))
195 (lambda (complex rest)
198 (make-map-markup-commands-markup-list
199 compose complex) completed))))))))
201 (define-public (partial-markup commands)
202 ;; Like composed-markup-list, except that the result is a single
203 ;; markup command that can be applied to one markup
204 (define (compose arg)
206 (lambda (cmd prev) (append cmd (list prev)))
209 (let ((chain (lambda (layout props arg)
210 (interpret-markup layout props (compose arg)))))
211 (set-object-property! chain 'markup-signature (list markup?))
214 (define-public (property-operation ctx music-type symbol . args)
215 (let* ((props (case music-type
216 ((PropertySet) (list 'value (car args)))
217 ((PropertyUnset) '())
218 ((OverrideProperty) (list 'grob-value (car args)
219 'grob-property-path (if (list? (cadr args))
224 (if (list? (car args))
225 (list 'grob-property-path (car args))
226 (list 'grob-property-path args)))
227 (else (ly:error (_ "Invalid property operation ~a") music-type))))
228 (m (ly:set-origin! (apply make-music music-type
231 (ly:set-origin! (make-music 'ContextSpeccedMusic
233 'context-type ctx))))
235 (define (get-first-context-id! mus)
236 "Find the name of a ContextSpeccedMusic, possibly naming it"
237 (let ((id (ly:music-property mus 'context-id)))
238 (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
239 (if (and (string? id)
240 (not (string-null? id)))
242 ;; We may reliably give a new context a unique name, but
243 ;; not an existing one
244 (if (ly:music-property mus 'create-new #f)
245 (let ((id (get-next-unique-voice-name)))
246 (set! (ly:music-property mus 'context-id) id)
251 (define-public (lyric-event text duration)
252 (ly:set-origin! (make-lyric-event text duration)))
254 (define-public (lyric-combine sync sync-type music)
255 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
256 ;; to signal to the Extender_engraver that any pending extender should
257 ;; be completed if the lyrics end before the associated voice.
258 (append! (ly:music-property music 'elements)
259 (list (make-music 'CompletizeExtenderEvent)))
261 (make-music 'LyricCombineMusic
263 'associated-context sync
264 'associated-context-type sync-type)))
266 (define-public (add-lyrics music addlyrics-list)
267 (let* ((existing-voice-name (get-first-context-id! music))
268 (voice-name (if (string? existing-voice-name)
270 (get-next-unique-voice-name)))
271 (voice (if (string? existing-voice-name)
273 (make-music 'ContextSpeccedMusic
276 'context-id voice-name
277 'origin (ly:music-property music 'origin))))
278 (voice-type (ly:music-property voice 'context-type))
282 (ly:music-property mus 'origin)
283 (ly:set-origin! (make-music 'ContextSpeccedMusic
285 'context-type 'Lyrics
288 voice-name voice-type mus)))))
290 (make-simultaneous-music (cons voice lyricstos))))