1 ;;;; define-syntax.scm -- Defines functions for syntax expressions
3 ;;;; source file of the GNU LilyPond music typesetter
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))
22 ;; Like define-ly-syntax-loc, but adds parser and location
23 ;; parameters. Useful for simple constructors that don't need to
25 (defmacro define-ly-syntax-simple (args . body)
26 (primitive-eval `(define-ly-syntax ,(cons* (car args)
30 (let ((m ,(cons 'begin body)))
31 (set! (ly:music-property m 'origin) location)
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))))
40 (ly:parser-error parser (_ "Music head function must return Music object") loc)
41 (make-music 'Music)))))
43 (define-ly-syntax-simple (void-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
56 (define-ly-syntax-simple (unrelativable-music mus)
57 (make-music 'UnrelativableMusic
60 (define-ly-syntax-simple (context-change type id)
61 (make-music 'ContextChange
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 duration tempo)
83 (make-sequential-music
85 (make-property-set 'tempoWholesPerMinute
86 (ly:moment-mul (ly:make-moment tempo 1)
87 (ly:duration-length duration)))
88 (make-property-set 'tempoUnitDuration duration)
89 (make-property-set 'tempoUnitCount tempo)))
92 (define-ly-syntax-simple (skip-music dur)
93 (make-music 'SkipMusic
96 (define-ly-syntax-simple (repeat type num body alts)
97 (make-repeat type num body alts))
99 (define (script-to-mmrest-text music)
100 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform MultiMeasureTextEvent"
102 (if (memq 'script-event (ly:music-property music 'types))
105 ((dir (ly:music-property music 'direction))
106 (tags (ly:music-property music 'tags))
107 (p (make-music 'MultiMeasureTextEvent
109 'text (ly:music-property music 'text))))
111 (set! (ly:music-property p 'direction) dir))
115 (define-ly-syntax (multi-measure-rest parser location duration articulations)
116 (make-music 'MultiMeasureRestMusic
117 'articulations (map script-to-mmrest-text articulations)
121 (define-ly-syntax-simple (context-specification type id mus ops create-new)
122 (let* ((type-sym (if (symbol? type) type (string->symbol type)))
123 (csm (context-spec-music mus type-sym id)))
124 (set! (ly:music-property csm 'property-operations) ops)
125 (if create-new (set! (ly:music-property csm 'create-new) #t))
128 (define-ly-syntax (property-operation parser location once ctx music-type symbol . args)
129 (let* ((props (case music-type
130 ((PropertySet) (list 'value (car args)))
131 ((PropertyUnset) '())
132 ((OverrideProperty) (list 'grob-value (car args)
133 'grob-property-path (cdr args)
135 ((RevertProperty) (list 'grob-property-path args))
136 (else (ly:error (_ "Invalid property operation ~a") music-type))))
137 (oprops (if once (cons* 'once once props) props))
138 (m (apply make-music music-type
142 (make-music 'ContextSpeccedMusic
147 ;; TODO: It seems that this function rarely returns anything useful.
148 (define (get-first-context-id type mus)
149 "Find the name of a ContextSpeccedMusic with given type"
150 (let ((id (ly:music-property mus 'context-id)))
151 (if (and (eq? (ly:music-property mus 'type) 'ContextSpeccedMusic)
152 (eq? (ly:music-property mus 'context-type) type)
154 (not (string-null? id)))
158 (define unique-counter -1)
159 (define (get-next-unique-voice-name)
160 (set! unique-counter (1+ unique-counter))
161 (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
163 (define (lyric-combine-music sync music loc)
164 (make-music 'LyricCombineMusic
166 'associated-context sync
169 (define-ly-syntax (lyric-combine parser location voice music)
170 (lyric-combine-music voice music location))
172 (define-ly-syntax (add-lyrics parser location music addlyrics-list)
173 (let* ((existing-voice-name (get-first-context-id 'Voice music))
174 (voice-name (if (string? existing-voice-name)
176 (get-next-unique-voice-name)))
177 (voice (if (string? existing-voice-name)
179 (make-music 'ContextSpeccedMusic
182 'context-id voice-name
183 'origin (ly:music-property music 'origin))))
184 (lyricstos (map (lambda (mus)
185 (let* ((loc (ly:music-property mus 'origin))
186 (lyr (lyric-combine-music voice-name mus loc)))
187 (make-music 'ContextSpeccedMusic
189 'context-type 'Lyrics
193 (make-simultaneous-music (cons voice lyricstos))))