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