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