]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
rename MultiMeasureRest->MultiMeasureRestMusic, to prevent name clash in documentation
[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 (skip-music dur)
82   (make-music 'SkipMusic
83               'duration dur))
84
85 (define-ly-syntax-simple (repeat type num body alts)
86   (make-repeat type num body alts))
87
88 (define (script-to-mmrest-text music)
89   "Extract 'direction and 'text from SCRIPT-MUSIC, and transform MultiMeasureTextEvent"
90   (if (memq 'script-event (ly:music-property music 'types))
91       
92       (let ((dir (ly:music-property music 'direction))
93             (p   (make-music 'MultiMeasureTextEvent
94                              'text (ly:music-property music 'text))))
95         (if (ly:dir? dir)
96             (set! (ly:music-property p 'direction) dir))
97         p)
98       music))
99
100 (define-ly-syntax (multi-measure-rest parser location duration articulations)
101   (make-music 'MultiMeasureRestMusic
102               'articulations (map script-to-mmrest-text articulations)
103               'duration duration
104               'origin location))
105
106 (define-ly-syntax-simple (context-specification type id mus ops create-new)
107   (let* ((type-sym (if (symbol? type) type (string->symbol type)))
108          (csm (context-spec-music mus type-sym id)))
109     (set! (ly:music-property csm 'property-operations) ops)
110     (if create-new (set! (ly:music-property csm 'create-new) #t))
111     csm))
112
113 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
114   (let* ((props (case music-type
115                   ((PropertySet) (list 'value (car args)))
116                   ((PropertyUnset) '())
117                   ((OverrideProperty) (list 'grob-value (car args)
118                                             'grob-property-path (cdr args)
119                                             'pop-first #t))
120                   ((RevertProperty) (list 'grob-property-path args))
121                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
122          (oprops (if once (cons* 'once once props) props))
123          (m (apply make-music music-type
124                    'symbol symbol
125                    'origin location
126                    oprops)))
127     (make-music 'ContextSpeccedMusic
128                 'element m
129                 'context-type ctx
130                 'origin location)))
131
132 ;; TODO: It seems that this function rarely returns anything useful.
133 (define (get-first-context-id type mus)
134   "Find the name of a ContextSpeccedMusic with given type"
135   (let ((id (ly:music-property mus 'context-id)))
136     (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
137              (eq? (ly:music-property mus 'context-type) type)
138              (string? id)
139              (not (string-null? id)))
140         id
141         '())))
142
143 (define unique-counter -1)
144 (define (get-next-unique-voice-name)
145   (set! unique-counter (1+ unique-counter))
146   (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
147
148 (define (lyric-combine-music sync music loc)
149   (make-music 'LyricCombineMusic
150               'element music
151               'associated-context sync
152               'origin loc))
153
154 (define-ly-syntax (lyric-combine parser location voice music)
155   (lyric-combine-music voice music location))
156
157 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
158   (let* ((existing-voice-name (get-first-context-id 'Voice music))
159          (voice-name (if (string? existing-voice-name)
160                          existing-voice-name
161                          (get-next-unique-voice-name)))
162          (voice (if (string? existing-voice-name)
163                     (music)
164                     (make-music 'ContextSpeccedMusic
165                                 'element music
166                                 'context-type 'Voice
167                                 'context-id voice-name
168                                 'origin (ly:music-property music 'origin))))
169          (lyricstos (map (lambda (mus)
170                            (let* ((loc (ly:music-property mus 'origin))
171                                   (lyr (lyric-combine-music voice-name mus loc)))
172                              (make-music 'ContextSpeccedMusic
173                                          'create-new #t
174                                          'context-type 'Lyrics
175                                          'element lyr
176                                          'origin loc)))
177                          addlyrics-list)))
178     (make-simultaneous-music (cons voice lyricstos))))