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