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 (partial-music-function fun args)
64 (let* ((sig (ly:music-function-signature fun))
65 (args (and (list args) (reverse! args))))
67 (ly:make-music-function
68 (cons (car sig) (list-tail (cdr sig) (length args)))
70 (apply (ly:music-function-extract fun)
71 (append args rest)))))))
73 (define-public (void-music)
74 (ly:set-origin! (make-music 'Music)))
76 (define-public (sequential-music mlist)
77 (ly:set-origin! (make-sequential-music mlist)))
79 (define-public (simultaneous-music mlist)
80 (ly:set-origin! (make-simultaneous-music mlist)))
82 (define-public (event-chord mlist)
83 (ly:set-origin! (make-music 'EventChord
86 (define-public (unrelativable-music mus)
87 (ly:set-origin! (make-music 'UnrelativableMusic
90 (define-public (context-change type id)
91 (ly:set-origin! (make-music 'ContextChange
95 (define-public (tempo text . rest)
96 (let* ((unit (and (pair? rest)
100 (range-tempo? (pair? count))
101 (tempo-change (ly:set-origin! (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-public (repeat type num body alts)
124 (ly:set-origin! (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-public (multi-measure-rest duration articulations)
135 (ly:set-origin! (make-music 'MultiMeasureRestMusic
136 'articulations (map script-to-mmrest-text articulations)
137 'duration duration)))
139 (define-public (repetition-chord duration articulations)
140 (ly:set-origin! (make-music 'EventChord
142 'elements articulations)))
144 (define-public (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))
148 (ly:set-origin! csm)))
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 (ly:set-origin! (apply make-music music-type
194 (ly:set-origin! (make-music 'ContextSpeccedMusic
196 'context-type ctx))))
198 (define (get-first-context-id! mus)
199 "Find the name of a ContextSpeccedMusic, possibly naming it"
200 (let ((id (ly:music-property mus 'context-id)))
201 (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
202 (if (and (string? id)
203 (not (string-null? id)))
205 ;; We may reliably give a new context a unique name, but
206 ;; not an existing one
207 (if (ly:music-property mus 'create-new #f)
208 (let ((id (get-next-unique-voice-name)))
209 (set! (ly:music-property mus 'context-id) id)
214 (define-public (lyric-event text duration)
215 (ly:set-origin! (make-lyric-event text duration)))
217 (define-public (lyric-combine sync sync-type music)
218 ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
219 ;; to signal to the Extender_engraver that any pending extender should
220 ;; be completed if the lyrics end before the associated voice.
221 (append! (ly:music-property music 'elements)
222 (list (make-music 'CompletizeExtenderEvent)))
224 (make-music 'LyricCombineMusic
226 'associated-context sync
227 'associated-context-type sync-type)))
229 (define-public (add-lyrics music addlyrics-list)
230 (let* ((existing-voice-name (get-first-context-id! music))
231 (voice-name (if (string? existing-voice-name)
233 (get-next-unique-voice-name)))
234 (voice (if (string? existing-voice-name)
236 (make-music 'ContextSpeccedMusic
239 'context-id voice-name
240 'origin (ly:music-property music 'origin))))
241 (voice-type (ly:music-property voice 'context-type))
245 (ly:music-property mus 'origin)
246 (ly:set-origin! (make-music 'ContextSpeccedMusic
248 'context-type 'Lyrics
251 voice-name voice-type mus)))))
253 (make-simultaneous-music (cons voice lyricstos))))