]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
Merge branch 'release/unstable'
[lilypond.git] / scm / ly-syntax-constructors.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2006--2011 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 ,(cons 'begin 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 ;; Scheme function: Apply function, return value can be anything
46 (define-ly-syntax (scheme-function parser loc fun args)
47       (apply fun parser loc args))
48
49 ;; Music function: Apply function and check return value.
50 (define-ly-syntax-loc (music-function parser loc fun args)
51   (let ((m (apply fun parser loc args)))
52     (if (ly:music? m)
53         m
54         (begin
55           (ly:parser-error parser (_ "Music head function must return Music object") loc)
56           (make-music 'Music)))))
57
58 (define-ly-syntax-simple (void-music)
59   (make-music 'Music))
60
61 (define-ly-syntax-simple (sequential-music mlist)
62   (make-sequential-music mlist))
63
64 (define-ly-syntax-simple (simultaneous-music mlist)
65   (make-simultaneous-music mlist))
66
67 (define-ly-syntax-simple (event-chord mlist)
68   (make-music 'EventChord
69               'elements mlist))
70
71 (define-ly-syntax-simple (unrelativable-music mus)
72   (make-music 'UnrelativableMusic
73               'element mus))
74
75 (define-ly-syntax-simple (context-change type id)
76   (make-music 'ContextChange
77               'change-to-type type
78               'change-to-id id))
79
80 (define-ly-syntax-simple (voice-separator)
81   (make-music 'VoiceSeparator))
82
83 (define-ly-syntax-simple (bar-check)
84   (make-music 'BarCheck))
85
86 (define-ly-syntax-simple (time-scaled-music fraction music)
87   (make-music 'TimeScaledMusic
88               'element (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction)))
89               'numerator (car fraction)
90               'denominator (cdr fraction)))
91
92 (define-ly-syntax-simple (transpose-music pitch music)
93   (make-music 'TransposedMusic
94               'element (ly:music-transpose music pitch)))
95
96 (define-ly-syntax (tempo parser location text . rest)
97   (let* ((unit (and (pair? rest)
98                     (car rest)))
99          (count (and unit
100                      (cadr rest)))
101          (range-tempo? (pair? count))
102          (tempo-change (make-music 'TempoChangeEvent
103                                    'origin location
104                                    'text text
105                                    'tempo-unit unit
106                                    'metronome-count count))
107          (tempo-set
108           (and unit
109                (context-spec-music
110                 (make-property-set 'tempoWholesPerMinute
111                                    (ly:moment-mul
112                                     (ly:make-moment
113                                      (if range-tempo?
114                                          (round (/ (+ (car count) (cdr count))
115                                                    2))
116                                          count)
117                                      1)
118                                     (ly:duration-length unit)))
119                 'Score))))
120
121     (if tempo-set
122         (make-sequential-music (list tempo-change tempo-set))
123         tempo-change)))
124
125 (define-ly-syntax-simple (skip-music dur)
126   (make-music 'SkipMusic
127               'duration dur))
128
129 (define-ly-syntax-simple (repeat type num body alts)
130   (make-repeat type num body alts))
131
132 (define (script-to-mmrest-text music)
133   "Extract @code{'direction} and @code{'text} from @var{music}, and transform
134 into a @code{MultiMeasureTextEvent}."
135
136   (if (memq 'script-event (ly:music-property music 'types))
137       (let* ((location (ly:music-property music 'origin))
138              (dir (ly:music-property music 'direction))
139              (tags (ly:music-property music 'tags))
140              (p (make-music 'MultiMeasureTextEvent
141                             'origin location
142                             'tags tags
143                             'text (ly:music-property music 'text))))
144         (if (ly:dir? dir)
145             (set! (ly:music-property p 'direction) dir))
146         p)
147       music))
148
149 (define-ly-syntax (multi-measure-rest parser location duration articulations)
150   (make-music 'MultiMeasureRestMusic
151               'articulations (map script-to-mmrest-text articulations)
152               'duration duration
153               'origin location))
154
155 (define-ly-syntax (repetition-chord parser location previous-chord repetition-function duration articulations)
156   (make-music 'RepeatedChord
157               'original-chord previous-chord
158               'element (repetition-function previous-chord location duration articulations)
159               'origin location))
160
161 (define-ly-syntax-simple (context-specification type id mus ops create-new)
162   (let* ((type-sym (if (symbol? type) type (string->symbol type)))
163          (csm (context-spec-music mus type-sym id)))
164     (set! (ly:music-property csm 'property-operations) ops)
165     (if create-new (set! (ly:music-property csm 'create-new) #t))
166     csm))
167
168 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
169   (let* ((props (case music-type
170                   ((PropertySet) (list 'value (car args)))
171                   ((PropertyUnset) '())
172                   ((OverrideProperty) (list 'grob-value (car args)
173                                             'grob-property-path (if (list? (cadr args))
174                                                                     (cadr args)
175                                                                     (cdr args))
176                                             'pop-first #t))
177                   ((RevertProperty)
178                    (if (list? (car args))
179                        (list 'grob-property-path (car args))
180                        (list 'grob-property-path args)))
181                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
182          (oprops (if once (cons* 'once once props) props))
183          (m (apply make-music music-type
184                    'symbol symbol
185                    'origin location
186                    oprops)))
187     (make-music 'ContextSpeccedMusic
188                 'element m
189                 'context-type ctx
190                 'origin location)))
191
192 ;; TODO: It seems that this function rarely returns anything useful.
193 (define (get-first-context-id type mus)
194   "Find the name of a ContextSpeccedMusic with given type"
195   (let ((id (ly:music-property mus 'context-id)))
196     (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
197              (eq? (ly:music-property mus 'context-type) type)
198              (string? id)
199              (not (string-null? id)))
200         id
201         '())))
202
203 (define unique-counter -1)
204 (define (get-next-unique-voice-name)
205   (set! unique-counter (1+ unique-counter))
206   (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
207
208 (define (lyric-combine-music sync music loc)
209   ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
210   ;; to signal to the Extender_engraver that any pending extender should
211   ;; be completed if the lyrics end before the associated voice.
212   (append! (ly:music-property music 'elements)
213            (list (make-music 'CompletizeExtenderEvent)))
214   (make-music 'LyricCombineMusic
215               'element music
216               'associated-context sync
217               'origin loc))
218
219 (define-ly-syntax (lyric-combine parser location voice music)
220   (lyric-combine-music voice music location))
221
222 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
223   (let* ((existing-voice-name (get-first-context-id 'Voice music))
224          (voice-name (if (string? existing-voice-name)
225                          existing-voice-name
226                          (get-next-unique-voice-name)))
227          (voice (if (string? existing-voice-name)
228                     (music)
229                     (make-music 'ContextSpeccedMusic
230                                 'element music
231                                 'context-type 'Voice
232                                 'context-id voice-name
233                                 'origin (ly:music-property music 'origin))))
234          (lyricstos (map (lambda (mus)
235                            (let* ((loc (ly:music-property mus 'origin))
236                                   (lyr (lyric-combine-music voice-name mus loc)))
237                              (make-music 'ContextSpeccedMusic
238                                          'create-new #t
239                                          'context-type 'Lyrics
240                                          'element lyr
241                                          'origin loc)))
242                          addlyrics-list)))
243     (make-simultaneous-music (cons voice lyricstos))))
244
245 (define-ly-syntax (make-mark-set parser location label)
246   "Make the music for the \\mark command."
247   (let* ((set (and (integer? label)
248                    (context-spec-music (make-property-set 'rehearsalMark label)
249                                       'Score)))
250          (ev (make-music 'MarkEvent
251                          'origin location)))
252
253     (if set
254         (make-sequential-music (list set ev))
255         (begin
256           (set! (ly:music-property ev 'label) label)
257           ev))))
258
259 (define-ly-syntax (partial parser location dur)
260   "Make a partial measure."
261
262   ;; We use `descend-to-context' here instead of `context-spec-music' to
263   ;; ensure \partial still works if the Timing_translator is moved
264     (descend-to-context
265      (context-spec-music (make-music 'PartialSet
266                                      'origin location
267                                      'partial-duration dur)
268                          'Timing)
269      'Score))