]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
Revert "Apply scripts/auxiliar/fixscm.sh"
[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-simple (voice-separator)
102   (make-music 'VoiceSeparator))
103
104 (define-ly-syntax-simple (bar-check)
105   (make-music 'BarCheck))
106
107 (define-ly-syntax (tempo parser location text . rest)
108   (let* ((unit (and (pair? rest)
109                     (car rest)))
110          (count (and unit
111                      (cadr rest)))
112          (range-tempo? (pair? count))
113          (tempo-change (make-music 'TempoChangeEvent
114                                    'origin location
115                                    'text text
116                                    'tempo-unit unit
117                                    'metronome-count count))
118          (tempo-set
119           (and unit
120                (context-spec-music
121                 (make-property-set 'tempoWholesPerMinute
122                                    (ly:moment-mul
123                                     (ly:make-moment
124                                      (if range-tempo?
125                                          (round (/ (+ (car count) (cdr count))
126                                                    2))
127                                          count)
128                                      1)
129                                     (ly:duration-length unit)))
130                 'Score))))
131
132     (if tempo-set
133         (make-sequential-music (list tempo-change tempo-set))
134         tempo-change)))
135
136 (define-ly-syntax-simple (repeat type num body alts)
137   (make-repeat type num body alts))
138
139 (define (script-to-mmrest-text music)
140   "Extract @code{'direction} and @code{'text} from @var{music}, and transform
141 into a @code{MultiMeasureTextEvent}."
142
143   (if (memq 'script-event (ly:music-property music 'types))
144       (apply make-music 'MultiMeasureTextEvent
145              (flatten-alist (ly:music-mutable-properties music)))
146       music))
147
148 (define-ly-syntax (multi-measure-rest parser location duration articulations)
149   (make-music 'MultiMeasureRestMusic
150               'articulations (map script-to-mmrest-text articulations)
151               'duration duration
152               'origin location))
153
154 (define-ly-syntax (repetition-chord parser location duration articulations)
155   (make-music 'EventChord
156               'duration duration
157               'elements articulations
158               'origin location))
159
160 (define-ly-syntax-simple (context-specification type id ops create-new mus)
161   (let* ((type-sym (if (symbol? type) type (string->symbol type)))
162          (csm (context-spec-music mus type-sym id)))
163     (set! (ly:music-property csm 'property-operations) ops)
164     (if create-new (set! (ly:music-property csm 'create-new) #t))
165     csm))
166
167 (define-ly-syntax (composed-markup-list parser location commands markups)
168 ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
169 ;; and `commands' a list of commands with their scheme arguments, in reverse order,
170 ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
171 ;;  ((bold (raise 4 (italic markup1)))
172 ;;   (bold (raise 4 (italic markup2)))
173 ;;   (bold (raise 4 (italic markup3))))
174
175   (define (compose arg)
176     (fold
177      (lambda (cmd prev) (append cmd (list prev)))
178      arg
179      commands))
180   (let loop ((markups markups) (completed '()))
181     (cond ((null? markups) (reverse! completed))
182           ((markup? (car markups))
183            (loop (cdr markups)
184                  (cons (compose (car markups)) completed)))
185           (else
186            (call-with-values
187                (lambda () (break! markup? markups))
188              (lambda (complex rest)
189                (loop rest
190                      (reverse!
191                       (make-map-markup-commands-markup-list
192                        compose complex) completed))))))))
193
194 (define-ly-syntax (property-operation parser location ctx music-type symbol . args)
195   (let* ((props (case music-type
196                   ((PropertySet) (list 'value (car args)))
197                   ((PropertyUnset) '())
198                   ((OverrideProperty) (list 'grob-value (car args)
199                                             'grob-property-path (if (list? (cadr args))
200                                                                     (cadr args)
201                                                                     (cdr args))
202                                             'pop-first #t))
203                   ((RevertProperty)
204                    (if (list? (car args))
205                        (list 'grob-property-path (car args))
206                        (list 'grob-property-path args)))
207                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
208          (m (apply make-music music-type
209                    'symbol symbol
210                    'origin location
211                    props)))
212     (make-music 'ContextSpeccedMusic
213                 'element m
214                 'context-type ctx
215                 'origin location)))
216
217 ;; TODO: It seems that this function rarely returns anything useful.
218 (define (get-first-context-id type mus)
219   "Find the name of a ContextSpeccedMusic with given type"
220   (let ((id (ly:music-property mus 'context-id)))
221     (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
222              (eq? (ly:music-property mus 'context-type) type)
223              (string? id)
224              (not (string-null? id)))
225         id
226         '())))
227
228 (define unique-counter -1)
229 (define (get-next-unique-voice-name)
230   (set! unique-counter (1+ unique-counter))
231   (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
232
233 (define-ly-syntax-simple (lyric-event text duration)
234   (make-lyric-event text duration))
235
236 (define (lyric-combine-music sync music loc)
237   ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
238   ;; to signal to the Extender_engraver that any pending extender should
239   ;; be completed if the lyrics end before the associated voice.
240   (append! (ly:music-property music 'elements)
241            (list (make-music 'CompletizeExtenderEvent)))
242   (make-music 'LyricCombineMusic
243               'element music
244               'associated-context sync
245               'origin loc))
246
247 (define-ly-syntax (lyric-combine parser location voice music)
248   (lyric-combine-music voice music location))
249
250 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
251   (let* ((existing-voice-name (get-first-context-id 'Voice music))
252          (voice-name (if (string? existing-voice-name)
253                          existing-voice-name
254                          (get-next-unique-voice-name)))
255          (voice (if (string? existing-voice-name)
256                     (music)
257                     (make-music 'ContextSpeccedMusic
258                                 'element music
259                                 'context-type 'Voice
260                                 'context-id voice-name
261                                 'origin (ly:music-property music 'origin))))
262          (lyricstos (map (lambda (mus)
263                            (let* ((loc (ly:music-property mus 'origin))
264                                   (lyr (lyric-combine-music voice-name mus loc)))
265                              (make-music 'ContextSpeccedMusic
266                                          'create-new #t
267                                          'context-type 'Lyrics
268                                          'element lyr
269                                          'origin loc)))
270                          addlyrics-list)))
271     (make-simultaneous-music (cons voice lyricstos))))