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