]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
Issue 4487/3: Implement partial markups
[lilypond.git] / scm / ly-syntax-constructors.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2006--2015 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 (define-module (scm ly-syntax-constructors)
19   #:use-module (lily)
20   #:use-module (srfi srfi-1))
21
22 (define-public (music-function-call-error fun m)
23   (let* ((sigcar (car (ly:music-function-signature fun)))
24          (pred? (if (pair? sigcar) (car sigcar) sigcar)))
25     (ly:parser-error
26      (format #f (_ "~a function cannot return ~a")
27              (type-name pred?)
28              (value->lily-string m))
29      (*location*))
30     (and (pair? sigcar)
31          (if (ly:music? (cdr sigcar))
32              (ly:music-deep-copy (cdr sigcar) (*location*))
33              (cdr sigcar)))))
34
35 ;; Music function: Apply function and check return value.
36 ;; args are in reverse order.
37 ;;
38 ;; If args is not a proper list, an error has been flagged earlier
39 ;; and no fallback value had been available.  In this case,
40 ;; we don't call the function but rather return the general
41 ;; fallback.
42 (define-public (music-function fun args)
43   (let* ((sigcar (car (ly:music-function-signature fun)))
44          (pred? (if (pair? sigcar) (car sigcar) sigcar))
45          (good (list? args))
46          (m (and good (apply (ly:music-function-extract fun) (reverse! args)))))
47     (if good
48         (if (pred? m)
49             (if (ly:music? m) (ly:set-origin! m) m)
50             (music-function-call-error fun m))
51         (and (pair? sigcar)
52              (if (ly:music (cdr sigcar))
53                  (ly:music-deep-copy (cdr sigcar) (*location*))
54                  (cdr sigcar))))))
55
56 (define-public (argument-error n pred arg)
57   (ly:parser-error
58    (format #f
59            (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
60            n (type-name pred) (music->make-music arg))
61    (*location*)))
62
63 ;; Used for chaining several music functions together.  `final'
64 ;; contains the last argument and still needs typechecking.
65 (define (music-function-chain fun args final)
66   (let* ((siglast (last (ly:music-function-signature fun)))
67          (pred? (if (pair? siglast) (car siglast) siglast)))
68     (if (pred? final)
69         (music-function fun (cons final args))
70         (begin
71           (argument-error (1+ (length args)) pred? final)
72           ;; call music function just for the error return value
73           (music-function fun #f)))))
74
75 (define-public (partial-music-function fun-list arg-list)
76   (let* ((good (every list? arg-list))
77          (sig (ly:music-function-signature (car fun-list))))
78     (and good
79          (ly:make-music-function
80           (cons (car sig) (list-tail (cdr sig) (length (car arg-list))))
81           (lambda rest
82             ;; Every time we use music-function, it destructively
83             ;; reverses its list of arguments.  Changing the calling
84             ;; convention would be non-trivial since we do error
85             ;; propagation to the reversed argument list by making it
86             ;; a non-proper list.  So we just create a fresh copy of
87             ;; all argument lists for each call.  We also want to
88             ;; avoid reusing any music expressions without copying and
89             ;; want to let them point to the location of the music
90             ;; function call rather than its definition.
91             (let ((arg-list (ly:music-deep-copy arg-list (*location*))))
92               (fold music-function-chain
93                     (music-function (car fun-list)
94                                     (reverse! rest (car arg-list)))
95                     (cdr fun-list) (cdr arg-list))))))))
96
97 (define-public (void-music)
98   (ly:set-origin! (make-music 'Music)))
99
100 (define-public (sequential-music mlist)
101   (ly:set-origin! (make-sequential-music mlist)))
102
103 (define-public (simultaneous-music mlist)
104   (ly:set-origin! (make-simultaneous-music mlist)))
105
106 (define-public (event-chord mlist)
107   (ly:set-origin! (make-music 'EventChord
108                               'elements mlist)))
109
110 (define-public (unrelativable-music mus)
111   (ly:set-origin! (make-music 'UnrelativableMusic
112                               'element mus)))
113
114 (define-public (context-change type id)
115   (ly:set-origin! (make-music 'ContextChange
116                               'change-to-type type
117                               'change-to-id id)))
118
119 (define-public (tempo text . rest)
120   (let* ((unit (and (pair? rest)
121                     (car rest)))
122          (count (and unit
123                      (cadr rest)))
124          (range-tempo? (pair? count))
125          (tempo-change (ly:set-origin! (make-music 'TempoChangeEvent
126                                                    'text text
127                                                    'tempo-unit unit
128                                                    'metronome-count count)))
129          (tempo-set
130           (and unit
131                (context-spec-music
132                 (make-property-set 'tempoWholesPerMinute
133                                    (ly:moment-mul
134                                     (ly:make-moment
135                                      (if range-tempo?
136                                          (round (/ (+ (car count) (cdr count))
137                                                    2))
138                                          count)
139                                      1)
140                                     (ly:duration-length unit)))
141                 'Score))))
142
143     (if tempo-set
144         (make-sequential-music (list tempo-change tempo-set))
145         tempo-change)))
146
147 (define-public (repeat type num body alts)
148   (ly:set-origin! (make-repeat type num body alts)))
149
150 (define (script-to-mmrest-text music)
151   "Extract @code{'direction} and @code{'text} from @var{music}, and transform
152 into a @code{MultiMeasureTextEvent}."
153
154   (if (music-is-of-type? music 'script-event)
155       (make-music 'MultiMeasureTextEvent music)
156       music))
157
158 (define-public (multi-measure-rest duration articulations)
159   (ly:set-origin! (make-music 'MultiMeasureRestMusic
160                               'articulations (map script-to-mmrest-text articulations)
161                               'duration duration)))
162
163 (define-public (repetition-chord duration articulations)
164   (ly:set-origin! (make-music 'EventChord
165                               'duration duration
166                               'elements articulations)))
167
168 (define-public (context-specification type id ops create-new mus)
169   (let ((csm (context-spec-music mus type id)))
170     (set! (ly:music-property csm 'property-operations) ops)
171     (if create-new (set! (ly:music-property csm 'create-new) #t))
172     (ly:set-origin! csm)))
173
174 (define-public (composed-markup-list commands markups)
175   ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
176   ;; and `commands' a list of commands with their scheme arguments, in reverse order,
177   ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
178   ;;  ((bold (raise 4 (italic markup1)))
179   ;;   (bold (raise 4 (italic markup2)))
180   ;;   (bold (raise 4 (italic markup3))))
181
182   (define (compose arg)
183     (fold
184      (lambda (cmd prev) (append cmd (list prev)))
185      arg
186      commands))
187   (let loop ((markups markups) (completed '()))
188     (cond ((null? markups) (reverse! completed))
189           ((markup? (car markups))
190            (loop (cdr markups)
191                  (cons (compose (car markups)) completed)))
192           (else
193            (call-with-values
194                (lambda () (break! markup? markups))
195              (lambda (complex rest)
196                (loop rest
197                      (reverse!
198                       (make-map-markup-commands-markup-list
199                        compose complex) completed))))))))
200
201 (define-public (partial-markup commands)
202   ;; Like composed-markup-list, except that the result is a single
203   ;; markup command that can be applied to one markup
204   (define (compose arg)
205     (fold
206      (lambda (cmd prev) (append cmd (list prev)))
207      arg
208      commands))
209   (let ((chain (lambda (layout props arg)
210                  (interpret-markup layout props (compose arg)))))
211     (set-object-property! chain 'markup-signature (list markup?))
212     chain))
213
214 (define-public (property-operation ctx music-type symbol . args)
215   (let* ((props (case music-type
216                   ((PropertySet) (list 'value (car args)))
217                   ((PropertyUnset) '())
218                   ((OverrideProperty) (list 'grob-value (car args)
219                                             'grob-property-path (if (list? (cadr args))
220                                                                     (cadr args)
221                                                                     (cdr args))
222                                             'pop-first #t))
223                   ((RevertProperty)
224                    (if (list? (car args))
225                        (list 'grob-property-path (car args))
226                        (list 'grob-property-path args)))
227                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
228          (m (ly:set-origin! (apply make-music music-type
229                                    'symbol symbol
230                                    props))))
231     (ly:set-origin! (make-music 'ContextSpeccedMusic
232                                 'element m
233                                 'context-type ctx))))
234
235 (define (get-first-context-id! mus)
236   "Find the name of a ContextSpeccedMusic, possibly naming it"
237   (let ((id (ly:music-property mus 'context-id)))
238     (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
239         (if (and (string? id)
240                  (not (string-null? id)))
241             id
242             ;; We may reliably give a new context a unique name, but
243             ;; not an existing one
244             (if (ly:music-property mus 'create-new #f)
245                 (let ((id (get-next-unique-voice-name)))
246                   (set! (ly:music-property mus 'context-id) id)
247                   id)
248                 '()))
249         '())))
250
251 (define-public (lyric-event text duration)
252   (ly:set-origin! (make-lyric-event text duration)))
253
254 (define-public (lyric-combine sync sync-type music)
255   ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
256   ;; to signal to the Extender_engraver that any pending extender should
257   ;; be completed if the lyrics end before the associated voice.
258   (append! (ly:music-property music 'elements)
259            (list (make-music 'CompletizeExtenderEvent)))
260   (ly:set-origin!
261    (make-music 'LyricCombineMusic
262                'element music
263                'associated-context sync
264                'associated-context-type sync-type)))
265
266 (define-public (add-lyrics music addlyrics-list)
267   (let* ((existing-voice-name (get-first-context-id! music))
268          (voice-name (if (string? existing-voice-name)
269                          existing-voice-name
270                          (get-next-unique-voice-name)))
271          (voice (if (string? existing-voice-name)
272                     music
273                     (make-music 'ContextSpeccedMusic
274                                 'element music
275                                 'context-type 'Voice
276                                 'context-id voice-name
277                                 'origin (ly:music-property music 'origin))))
278          (voice-type (ly:music-property voice 'context-type))
279          (lyricstos (map
280                      (lambda (mus)
281                        (with-location
282                         (ly:music-property mus 'origin)
283                         (ly:set-origin! (make-music 'ContextSpeccedMusic
284                                                     'create-new #t
285                                                     'context-type 'Lyrics
286                                                     'element
287                                                     (lyric-combine
288                                                      voice-name voice-type mus)))))
289                      addlyrics-list)))
290     (make-simultaneous-music (cons voice lyricstos))))