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