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