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