]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
Doc-de: Update to the Learning manual
[lilypond.git] / scm / ly-syntax-constructors.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2006--2009 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 (define define-ly-syntax define-public)
21
22 ;; A ly-syntax constructor takes two extra parameters, parser and
23 ;; location. These are mainly used for reporting errors and
24 ;; warnings. This function is a syntactic sugar which uses the
25 ;; location arg to set the origin of the returned music object; this
26 ;; behaviour is usually desired
27 (defmacro define-ly-syntax-loc (args . body)
28   (primitive-eval `(define-ly-syntax ,args
29                      (let ((m ,(cons 'begin body)))
30                        (set! (ly:music-property m 'origin) ,(third args))
31                        m))))
32
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   (primitive-eval `(define-ly-syntax ,(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 fun args)
47   (let ((m (apply fun (cons* parser loc args))))
48     (if (ly:music? m)
49         m
50         (begin
51           (ly:parser-error parser (_ "Music head function must return Music object") loc)
52           (make-music 'Music)))))
53
54 (define-ly-syntax-simple (void-music)
55   (make-music 'Music))
56
57 (define-ly-syntax-simple (sequential-music mlist)
58   (make-sequential-music mlist))
59
60 (define-ly-syntax-simple (simultaneous-music mlist)
61   (make-simultaneous-music mlist))
62
63 (define-ly-syntax-simple (event-chord mlist)
64   (make-music 'EventChord
65               'elements mlist))
66
67 (define-ly-syntax-simple (unrelativable-music mus)
68   (make-music 'UnrelativableMusic
69               'element mus))
70
71 (define-ly-syntax-simple (context-change type id)
72   (make-music 'ContextChange
73               'change-to-type type
74               'change-to-id id))
75
76 (define-ly-syntax-simple (voice-separator)
77   (make-music 'VoiceSeparator))
78
79 (define-ly-syntax-simple (bar-check)
80   (make-music 'BarCheck))
81
82 (define-ly-syntax-simple (time-scaled-music fraction music)
83   (make-music 'TimeScaledMusic
84               'element (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction)))
85               'numerator (car fraction)
86               'denominator (cdr fraction)))
87
88 (define-ly-syntax-simple (transpose-music pitch music)
89   (make-music 'TransposedMusic
90               'element (ly:music-transpose music pitch)))
91
92 (define-ly-syntax-simple (tempo text duration tempo)
93   (let ((props (list
94                   (make-property-set 'tempoWholesPerMinute
95                         (ly:moment-mul (ly:make-moment tempo 1)
96                                        (ly:duration-length duration)))
97                   (make-property-set 'tempoUnitDuration duration)
98                   (make-property-set 'tempoUnitCount tempo))))
99     (set! props (cons 
100             (if text (make-property-set 'tempoText text)
101                      (make-property-unset 'tempoText)) 
102             props))
103     (context-spec-music
104       (make-sequential-music props)
105       'Score)))
106
107 (define-ly-syntax-simple (tempoText text)
108   (context-spec-music
109    (make-sequential-music
110     (list
111      (make-property-unset 'tempoUnitDuration)
112      (make-property-unset 'tempoUnitCount)
113      (make-property-set 'tempoText text)))
114    'Score))
115
116 (define-ly-syntax-simple (skip-music dur)
117   (make-music 'SkipMusic
118               'duration dur))
119
120 (define-ly-syntax-simple (repeat type num body alts)
121   (make-repeat type num body alts))
122
123 (define (script-to-mmrest-text music)
124   "Extract 'direction and 'text from SCRIPT-MUSIC, and transform MultiMeasureTextEvent"
125
126   (if (memq 'script-event (ly:music-property music 'types))
127       
128       (let*
129           ((dir (ly:music-property music 'direction))
130            (tags (ly:music-property music 'tags))
131            (p   (make-music 'MultiMeasureTextEvent
132                              'tags tags
133                              'text (ly:music-property music 'text))))
134         (if (ly:dir? dir)
135             (set! (ly:music-property p 'direction) dir))
136         p)
137       music))
138
139 (define-ly-syntax (multi-measure-rest parser location duration articulations)
140   (make-music 'MultiMeasureRestMusic
141               'articulations (map script-to-mmrest-text articulations)
142               'duration duration
143               'origin location))
144
145 (define-ly-syntax (repetition-chord parser location previous-chord repetition-function duration articulations)
146   (make-music 'RepeatedChord
147               'original-chord previous-chord
148               'element (repetition-function previous-chord location duration articulations)
149               'origin location))
150
151 (define-ly-syntax-simple (context-specification type id mus ops create-new)
152   (let* ((type-sym (if (symbol? type) type (string->symbol type)))
153          (csm (context-spec-music mus type-sym id)))
154     (set! (ly:music-property csm 'property-operations) ops)
155     (if create-new (set! (ly:music-property csm 'create-new) #t))
156     csm))
157
158 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
159   (let* ((props (case music-type
160                   ((PropertySet) (list 'value (car args)))
161                   ((PropertyUnset) '())
162                   ((OverrideProperty) (list 'grob-value (car args)
163                                             'grob-property-path (if (list? (cadr args))
164                                                                     (cadr args)
165                                                                     (cdr args))
166                                             'pop-first #t))
167                   ((RevertProperty)
168                    (if (list? (car args))
169                        (list 'grob-property-path (car args))
170                        (list 'grob-property-path args)))
171                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
172          (oprops (if once (cons* 'once once props) props))
173          (m (apply make-music music-type
174                    'symbol symbol
175                    'origin location
176                    oprops)))
177     (make-music 'ContextSpeccedMusic
178                 'element m
179                 'context-type ctx
180                 'origin location)))
181
182 ;; TODO: It seems that this function rarely returns anything useful.
183 (define (get-first-context-id type mus)
184   "Find the name of a ContextSpeccedMusic with given type"
185   (let ((id (ly:music-property mus 'context-id)))
186     (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
187              (eq? (ly:music-property mus 'context-type) type)
188              (string? id)
189              (not (string-null? id)))
190         id
191         '())))
192
193 (define unique-counter -1)
194 (define (get-next-unique-voice-name)
195   (set! unique-counter (1+ unique-counter))
196   (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
197
198 (define (lyric-combine-music sync music loc)
199   (make-music 'LyricCombineMusic
200               'element music
201               'associated-context sync
202               'origin loc))
203
204 (define-ly-syntax (lyric-combine parser location voice music)
205   (lyric-combine-music voice music location))
206
207 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
208   (let* ((existing-voice-name (get-first-context-id 'Voice music))
209          (voice-name (if (string? existing-voice-name)
210                          existing-voice-name
211                          (get-next-unique-voice-name)))
212          (voice (if (string? existing-voice-name)
213                     (music)
214                     (make-music 'ContextSpeccedMusic
215                                 'element music
216                                 'context-type 'Voice
217                                 'context-id voice-name
218                                 'origin (ly:music-property music 'origin))))
219          (lyricstos (map (lambda (mus)
220                            (let* ((loc (ly:music-property mus 'origin))
221                                   (lyr (lyric-combine-music voice-name mus loc)))
222                              (make-music 'ContextSpeccedMusic
223                                          'create-new #t
224                                          'context-type 'Lyrics
225                                          'element lyr
226                                          'origin loc)))
227                          addlyrics-list)))
228     (make-simultaneous-music (cons voice lyricstos))))