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