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