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 (define-public (void-music)
64 (ly:set-origin! (make-music 'Music)))
66 (define-public (sequential-music mlist)
67 (ly:set-origin! (make-sequential-music mlist)))
69 (define-public (simultaneous-music mlist)
70 (ly:set-origin! (make-simultaneous-music mlist)))
72 (define-public (event-chord mlist)
73 (ly:set-origin! (make-music 'EventChord
76 (define-public (unrelativable-music mus)
77 (ly:set-origin! (make-music 'UnrelativableMusic
80 (define-public (context-change type id)
81 (ly:set-origin! (make-music 'ContextChange
85 (define-public (tempo text . rest)
86 (let* ((unit (and (pair? rest)
90 (range-tempo? (pair? count))
91 (tempo-change (ly:set-origin! (make-music 'TempoChangeEvent
94 'metronome-count count)))
98 (make-property-set 'tempoWholesPerMinute
102 (round (/ (+ (car count) (cdr count))
106 (ly:duration-length unit)))
110 (make-sequential-music (list tempo-change tempo-set))
113 (define-public (repeat type num body alts)
114 (ly:set-origin! (make-repeat type num body alts)))
116 (define (script-to-mmrest-text music)
117 "Extract @code{'direction} and @code{'text} from @var{music}, and transform
118 into a @code{MultiMeasureTextEvent}."
120 (if (music-is-of-type? music 'script-event)
121 (make-music 'MultiMeasureTextEvent music)
124 (define-public (multi-measure-rest duration articulations)
125 (ly:set-origin! (make-music 'MultiMeasureRestMusic
126 'articulations (map script-to-mmrest-text articulations)
127 'duration duration)))
129 (define-public (repetition-chord duration articulations)
130 (ly:set-origin! (make-music 'EventChord
132 'elements articulations)))
134 (define-public (context-specification type id ops create-new mus)
135 (let ((csm (context-spec-music mus type id)))
136 (set! (ly:music-property csm 'property-operations) ops)
137 (if create-new (set! (ly:music-property csm 'create-new) #t))
138 (ly:set-origin! csm)))
140 (define-public (composed-markup-list commands markups)
141 ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
142 ;; and `commands' a list of commands with their scheme arguments, in reverse order,
143 ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
144 ;; ((bold (raise 4 (italic markup1)))
145 ;; (bold (raise 4 (italic markup2)))
146 ;; (bold (raise 4 (italic markup3))))
148 (define (compose arg)
150 (lambda (cmd prev) (append cmd (list prev)))
153 (let loop ((markups markups) (completed '()))
154 (cond ((null? markups) (reverse! completed))
155 ((markup? (car markups))
157 (cons (compose (car markups)) completed)))
160 (lambda () (break! markup? markups))
161 (lambda (complex rest)
164 (make-map-markup-commands-markup-list
165 compose complex) completed))))))))
167 (define-public (property-operation ctx music-type symbol . args)
168 (let* ((props (case music-type
169 ((PropertySet) (list 'value (car args)))
170 ((PropertyUnset) '())
171 ((OverrideProperty) (list 'grob-value (car args)
172 'grob-property-path (if (list? (cadr args))
177 (if (list? (car args))
178 (list 'grob-property-path (car args))
179 (list 'grob-property-path args)))
180 (else (ly:error (_ "Invalid property operation ~a") music-type))))
181 (m (ly:set-origin! (apply make-music music-type
184 (ly:set-origin! (make-music 'ContextSpeccedMusic
186 'context-type ctx))))
188 (define (get-first-context-id! mus)
189 "Find the name of a ContextSpeccedMusic, possibly naming it"
190 (let ((id (ly:music-property mus 'context-id)))
191 (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
192 (if (and (string? id)
193 (not (string-null? id)))
195 ;; We may reliably give a new context a unique name, but
196 ;; not an existing one
197 (if (ly:music-property mus 'create-new #f)
198 (let ((id (get-next-unique-voice-name)))
199 (set! (ly:music-property mus 'context-id) id)
204 (define-public (lyric-event text duration)
205 (ly:set-origin! (make-lyric-event text duration)))
207 (define-public (lyric-combine sync sync-type music)
208 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
209 ;; to signal to the Extender_engraver that any pending extender should
210 ;; be completed if the lyrics end before the associated voice.
211 (append! (ly:music-property music 'elements)
212 (list (make-music 'CompletizeExtenderEvent)))
214 (make-music 'LyricCombineMusic
216 'associated-context sync
217 'associated-context-type sync-type)))
219 (define-public (add-lyrics music addlyrics-list)
220 (let* ((existing-voice-name (get-first-context-id! music))
221 (voice-name (if (string? existing-voice-name)
223 (get-next-unique-voice-name)))
224 (voice (if (string? existing-voice-name)
226 (make-music 'ContextSpeccedMusic
229 'context-id voice-name
230 'origin (ly:music-property music 'origin))))
231 (voice-type (ly:music-property voice 'context-type))
235 (ly:music-property mus 'origin)
236 (ly:set-origin! (make-music 'ContextSpeccedMusic
238 'context-type 'Lyrics
241 voice-name voice-type mus)))))
243 (make-simultaneous-music (cons voice lyricstos))))