]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
Issue 4421/2: Factor out music-function-call-error
[lilypond.git] / scm / ly-syntax-constructors.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2006--2015 Erik Sandberg <mandolaerik@gmail.com>
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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/>.
17
18 ;; TODO: use separate module for syntax
19 ;; constructors. Also create wrapper around the constructor?
20 (defmacro define-ly-syntax (args . body)
21   `(define-public ,args ,@body))
22
23 ;; A ly-syntax constructor takes two extra parameters, parser and
24 ;; location. These are mainly used for reporting errors and
25 ;; warnings. This function is a syntactic sugar which uses the
26 ;; location arg to set the origin of the returned music object; this
27 ;; behaviour is usually desired
28 (defmacro define-ly-syntax-loc (args . body)
29   `(define-public ,args
30      (let ((m ,(cons 'begin body)))
31        (set! (ly:music-property m 'origin) ,(third args))
32        m)))
33 ;; Like define-ly-syntax-loc, but adds parser and location
34 ;; parameters. Useful for simple constructors that don't need to
35 ;; report errors.
36 (defmacro define-ly-syntax-simple (args . body)
37   `(define-public ,(cons* (car args)
38                           'parser
39                           'location
40                           (cdr args))
41      (let ((m ,(cons 'begin body)))
42        (set! (ly:music-property m 'origin) location)
43        m)))
44
45 (define (music-function-call-error parser loc fun m)
46   (let* ((sig (ly:music-function-signature fun))
47          (pred (if (pair? (car sig)) (caar sig) (car sig))))
48     (ly:parser-error parser
49                      (format #f (_ "~a function cannot return ~a")
50                              (type-name pred)
51                              (value->lily-string m parser))
52                      loc)
53     (and (pair? (car sig)) (cdar sig))))
54
55 ;; Music function: Apply function and check return value.
56 ;; args are in reverse order, rest may specify additional ones
57 ;;
58 ;; If args is not a proper list, an error has been flagged earlier
59 ;; and no fallback value had been available.  In this case,
60 ;; we don't call the function but rather return the general
61 ;; fallback.
62 (define-ly-syntax (music-function parser loc fun args . rest)
63   (let* ((sig (ly:music-function-signature fun))
64          (pred (if (pair? (car sig)) (caar sig) (car sig)))
65          (good (proper-list? args))
66          (m (and good (with-fluids ((%parser parser) (%location loc))
67                                    (apply (ly:music-function-extract fun)
68                                           (reverse! args rest))))))
69     (if (and good (pred m))
70         (begin
71           (if (ly:music? m)
72               (set! (ly:music-property m 'origin) loc))
73           m)
74         (if good
75             (music-function-call-error parser loc fun m)
76             (and (pair? (car sig)) (cdar sig))))))
77
78 (define-ly-syntax (argument-error parser location n pred arg)
79   (ly:parser-error
80    parser
81    (format #f
82            (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
83            n (type-name pred) (music->make-music arg))
84    location))
85
86 (define-ly-syntax-simple (void-music)
87   (make-music 'Music))
88
89 (define-ly-syntax-simple (sequential-music mlist)
90   (make-sequential-music mlist))
91
92 (define-ly-syntax-simple (simultaneous-music mlist)
93   (make-simultaneous-music mlist))
94
95 (define-ly-syntax-simple (event-chord mlist)
96   (make-music 'EventChord
97               'elements mlist))
98
99 (define-ly-syntax-simple (unrelativable-music mus)
100   (make-music 'UnrelativableMusic
101               'element mus))
102
103 (define-ly-syntax-simple (context-change type id)
104   (make-music 'ContextChange
105               'change-to-type type
106               'change-to-id id))
107
108 (define-ly-syntax (tempo parser location text . rest)
109   (let* ((unit (and (pair? rest)
110                     (car rest)))
111          (count (and unit
112                      (cadr rest)))
113          (range-tempo? (pair? count))
114          (tempo-change (make-music 'TempoChangeEvent
115                                    'origin location
116                                    'text text
117                                    'tempo-unit unit
118                                    'metronome-count count))
119          (tempo-set
120           (and unit
121                (context-spec-music
122                 (make-property-set 'tempoWholesPerMinute
123                                    (ly:moment-mul
124                                     (ly:make-moment
125                                      (if range-tempo?
126                                          (round (/ (+ (car count) (cdr count))
127                                                    2))
128                                          count)
129                                      1)
130                                     (ly:duration-length unit)))
131                 'Score))))
132
133     (if tempo-set
134         (make-sequential-music (list tempo-change tempo-set))
135         tempo-change)))
136
137 (define-ly-syntax-simple (repeat type num body alts)
138   (make-repeat type num body alts))
139
140 (define (script-to-mmrest-text music)
141   "Extract @code{'direction} and @code{'text} from @var{music}, and transform
142 into a @code{MultiMeasureTextEvent}."
143
144   (if (music-is-of-type? music 'script-event)
145       (make-music 'MultiMeasureTextEvent music)
146       music))
147
148 (define-ly-syntax (multi-measure-rest parser location duration articulations)
149   (make-music 'MultiMeasureRestMusic
150               'articulations (map script-to-mmrest-text articulations)
151               'duration duration
152               'origin location))
153
154 (define-ly-syntax (repetition-chord parser location duration articulations)
155   (make-music 'EventChord
156               'duration duration
157               'elements articulations
158               'origin location))
159
160 (define-ly-syntax-simple (context-specification type id ops create-new mus)
161   (let ((csm (context-spec-music mus type id)))
162     (set! (ly:music-property csm 'property-operations) ops)
163     (if create-new (set! (ly:music-property csm 'create-new) #t))
164     csm))
165
166 (define-ly-syntax (composed-markup-list parser location commands markups)
167   ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
168   ;; and `commands' a list of commands with their scheme arguments, in reverse order,
169   ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
170   ;;  ((bold (raise 4 (italic markup1)))
171   ;;   (bold (raise 4 (italic markup2)))
172   ;;   (bold (raise 4 (italic markup3))))
173
174   (define (compose arg)
175     (fold
176      (lambda (cmd prev) (append cmd (list prev)))
177      arg
178      commands))
179   (let loop ((markups markups) (completed '()))
180     (cond ((null? markups) (reverse! completed))
181           ((markup? (car markups))
182            (loop (cdr markups)
183                  (cons (compose (car markups)) completed)))
184           (else
185            (call-with-values
186                (lambda () (break! markup? markups))
187              (lambda (complex rest)
188                (loop rest
189                      (reverse!
190                       (make-map-markup-commands-markup-list
191                        compose complex) completed))))))))
192
193 (define-ly-syntax (property-operation parser location ctx music-type symbol . args)
194   (let* ((props (case music-type
195                   ((PropertySet) (list 'value (car args)))
196                   ((PropertyUnset) '())
197                   ((OverrideProperty) (list 'grob-value (car args)
198                                             'grob-property-path (if (list? (cadr args))
199                                                                     (cadr args)
200                                                                     (cdr args))
201                                             'pop-first #t))
202                   ((RevertProperty)
203                    (if (list? (car args))
204                        (list 'grob-property-path (car args))
205                        (list 'grob-property-path args)))
206                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
207          (m (apply make-music music-type
208                    'symbol symbol
209                    'origin location
210                    props)))
211     (make-music 'ContextSpeccedMusic
212                 'element m
213                 'context-type ctx
214                 'origin location)))
215
216 (define (get-first-context-id! mus)
217   "Find the name of a ContextSpeccedMusic, possibly naming it"
218   (let ((id (ly:music-property mus 'context-id)))
219     (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
220         (if (and (string? id)
221                  (not (string-null? id)))
222             id
223             ;; We may reliably give a new context a unique name, but
224             ;; not an existing one
225             (if (ly:music-property mus 'create-new #f)
226                 (let ((id (get-next-unique-voice-name)))
227                   (set! (ly:music-property mus 'context-id) id)
228                   id)
229                 '()))
230         '())))
231
232 (define unique-counter -1)
233 (define (get-next-unique-voice-name)
234   (set! unique-counter (1+ unique-counter))
235   (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
236
237 (define-ly-syntax-simple (lyric-event text duration)
238   (make-lyric-event text duration))
239
240 (define (lyric-combine-music sync sync-type music loc)
241   ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
242   ;; to signal to the Extender_engraver that any pending extender should
243   ;; be completed if the lyrics end before the associated voice.
244   (append! (ly:music-property music 'elements)
245            (list (make-music 'CompletizeExtenderEvent)))
246   (make-music 'LyricCombineMusic
247               'element music
248               'associated-context sync
249               'associated-context-type sync-type
250               'origin loc))
251
252 (define-ly-syntax (lyric-combine parser location voice typ music)
253   (lyric-combine-music voice typ music location))
254
255 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
256   (let* ((existing-voice-name (get-first-context-id! music))
257          (voice-name (if (string? existing-voice-name)
258                          existing-voice-name
259                          (get-next-unique-voice-name)))
260          (voice (if (string? existing-voice-name)
261                     music
262                     (make-music 'ContextSpeccedMusic
263                                 'element music
264                                 'context-type 'Voice
265                                 'context-id voice-name
266                                 'origin (ly:music-property music 'origin))))
267          (voice-type (ly:music-property voice 'context-type))
268          (lyricstos (map (lambda (mus)
269                            (let* ((loc (ly:music-property mus 'origin))
270                                   (lyr (lyric-combine-music
271                                         voice-name voice-type mus loc)))
272                              (make-music 'ContextSpeccedMusic
273                                          'create-new #t
274                                          'context-type 'Lyrics
275                                          'element lyr
276                                          'origin loc)))
277                          addlyrics-list)))
278     (make-simultaneous-music (cons voice lyricstos))))