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