Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / scm / ly-syntax-constructors.scm
blobee798233611a6a65361f1fae414962848a9f3cfa
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>
7 ;; TODO: use separate module for syntax
8 ;; constructors. Also create wrapper around the constructor?
9 (define define-ly-syntax define-public)
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))))
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))))
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)))))
43 (define-ly-syntax-simple (void-music)
44   (make-music 'Music))
46 (define-ly-syntax-simple (sequential-music mlist)
47   (make-sequential-music mlist))
49 (define-ly-syntax-simple (simultaneous-music mlist)
50   (make-simultaneous-music mlist))
52 (define-ly-syntax-simple (event-chord mlist)
53   (make-music 'EventChord
54               'elements mlist))
56 (define-ly-syntax-simple (unrelativable-music mus)
57   (make-music 'UnrelativableMusic
58               'element mus))
60 (define-ly-syntax-simple (context-change type id)
61   (make-music 'ContextChange
62               'change-to-type type
63               'change-to-id id))
65 (define-ly-syntax-simple (voice-separator)
66   (make-music 'VoiceSeparator))
68 (define-ly-syntax-simple (bar-check)
69   (make-music 'BarCheck))
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)))
77 (define-ly-syntax-simple (transpose-music pitch music)
78   (make-music 'TransposedMusic
79               'element (ly:music-transpose music pitch)))
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)))
96 (define-ly-syntax-simple (tempoText text)
97   (context-spec-music
98    (make-sequential-music
99     (list
100      (make-property-unset 'tempoUnitDuration)
101      (make-property-unset 'tempoUnitCount)
102      (make-property-set 'tempoText text)))
103    'Score))
105 (define-ly-syntax-simple (skip-music dur)
106   (make-music 'SkipMusic
107               'duration dur))
109 (define-ly-syntax-simple (repeat type num body alts)
110   (make-repeat type num body alts))
112 (define (script-to-mmrest-text music)
113   "Extract 'direction and 'text from SCRIPT-MUSIC, and transform MultiMeasureTextEvent"
115   (if (memq 'script-event (ly:music-property music 'types))
116       
117       (let*
118           ((dir (ly:music-property music 'direction))
119            (tags (ly:music-property music 'tags))
120            (p   (make-music 'MultiMeasureTextEvent
121                              'tags tags
122                              'text (ly:music-property music 'text))))
123         (if (ly:dir? dir)
124             (set! (ly:music-property p 'direction) dir))
125         p)
126       music))
128 (define-ly-syntax (multi-measure-rest parser location duration articulations)
129   (make-music 'MultiMeasureRestMusic
130               'articulations (map script-to-mmrest-text articulations)
131               'duration duration
132               'origin location))
134 (define-ly-syntax-simple (context-specification type id mus ops create-new)
135   (let* ((type-sym (if (symbol? type) type (string->symbol type)))
136          (csm (context-spec-music mus type-sym id)))
137     (set! (ly:music-property csm 'property-operations) ops)
138     (if create-new (set! (ly:music-property csm 'create-new) #t))
139     csm))
141 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
142   (let* ((props (case music-type
143                   ((PropertySet) (list 'value (car args)))
144                   ((PropertyUnset) '())
145                   ((OverrideProperty) (list 'grob-value (car args)
146                                             'grob-property-path (if (list? (cadr args))
147                                                                     (cadr args)
148                                                                     (cdr args))
149                                             'pop-first #t))
150                   ((RevertProperty)
151                    (if (list? (car args))
152                        (list 'grob-property-path (car args))
153                        (list 'grob-property-path args)))
154                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
155          (oprops (if once (cons* 'once once props) props))
156          (m (apply make-music music-type
157                    'symbol symbol
158                    'origin location
159                    oprops)))
160     (make-music 'ContextSpeccedMusic
161                 'element m
162                 'context-type ctx
163                 'origin location)))
165 ;; TODO: It seems that this function rarely returns anything useful.
166 (define (get-first-context-id type mus)
167   "Find the name of a ContextSpeccedMusic with given type"
168   (let ((id (ly:music-property mus 'context-id)))
169     (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
170              (eq? (ly:music-property mus 'context-type) type)
171              (string? id)
172              (not (string-null? id)))
173         id
174         '())))
176 (define unique-counter -1)
177 (define (get-next-unique-voice-name)
178   (set! unique-counter (1+ unique-counter))
179   (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
181 (define (lyric-combine-music sync music loc)
182   (make-music 'LyricCombineMusic
183               'element music
184               'associated-context sync
185               'origin loc))
187 (define-ly-syntax (lyric-combine parser location voice music)
188   (lyric-combine-music voice music location))
190 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
191   (let* ((existing-voice-name (get-first-context-id 'Voice music))
192          (voice-name (if (string? existing-voice-name)
193                          existing-voice-name
194                          (get-next-unique-voice-name)))
195          (voice (if (string? existing-voice-name)
196                     (music)
197                     (make-music 'ContextSpeccedMusic
198                                 'element music
199                                 'context-type 'Voice
200                                 'context-id voice-name
201                                 'origin (ly:music-property music 'origin))))
202          (lyricstos (map (lambda (mus)
203                            (let* ((loc (ly:music-property mus 'origin))
204                                   (lyr (lyric-combine-music voice-name mus loc)))
205                              (make-music 'ContextSpeccedMusic
206                                          'create-new #t
207                                          'context-type 'Lyrics
208                                          'element lyr
209                                          'origin loc)))
210                          addlyrics-list)))
211     (make-simultaneous-music (cons voice lyricstos))))