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