]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
Issue 4487/2: Allow for chaining of several partial functions in a row
[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 (property-operation ctx music-type symbol . args)
202   (let* ((props (case music-type
203                   ((PropertySet) (list 'value (car args)))
204                   ((PropertyUnset) '())
205                   ((OverrideProperty) (list 'grob-value (car args)
206                                             'grob-property-path (if (list? (cadr args))
207                                                                     (cadr args)
208                                                                     (cdr args))
209                                             'pop-first #t))
210                   ((RevertProperty)
211                    (if (list? (car args))
212                        (list 'grob-property-path (car args))
213                        (list 'grob-property-path args)))
214                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
215          (m (ly:set-origin! (apply make-music music-type
216                                    'symbol symbol
217                                    props))))
218     (ly:set-origin! (make-music 'ContextSpeccedMusic
219                                 'element m
220                                 'context-type ctx))))
221
222 (define (get-first-context-id! mus)
223   "Find the name of a ContextSpeccedMusic, possibly naming it"
224   (let ((id (ly:music-property mus 'context-id)))
225     (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
226         (if (and (string? id)
227                  (not (string-null? id)))
228             id
229             ;; We may reliably give a new context a unique name, but
230             ;; not an existing one
231             (if (ly:music-property mus 'create-new #f)
232                 (let ((id (get-next-unique-voice-name)))
233                   (set! (ly:music-property mus 'context-id) id)
234                   id)
235                 '()))
236         '())))
237
238 (define-public (lyric-event text duration)
239   (ly:set-origin! (make-lyric-event text duration)))
240
241 (define-public (lyric-combine sync sync-type music)
242   ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
243   ;; to signal to the Extender_engraver that any pending extender should
244   ;; be completed if the lyrics end before the associated voice.
245   (append! (ly:music-property music 'elements)
246            (list (make-music 'CompletizeExtenderEvent)))
247   (ly:set-origin!
248    (make-music 'LyricCombineMusic
249                'element music
250                'associated-context sync
251                'associated-context-type sync-type)))
252
253 (define-public (add-lyrics music addlyrics-list)
254   (let* ((existing-voice-name (get-first-context-id! music))
255          (voice-name (if (string? existing-voice-name)
256                          existing-voice-name
257                          (get-next-unique-voice-name)))
258          (voice (if (string? existing-voice-name)
259                     music
260                     (make-music 'ContextSpeccedMusic
261                                 'element music
262                                 'context-type 'Voice
263                                 'context-id voice-name
264                                 'origin (ly:music-property music 'origin))))
265          (voice-type (ly:music-property voice 'context-type))
266          (lyricstos (map
267                      (lambda (mus)
268                        (with-location
269                         (ly:music-property mus 'origin)
270                         (ly:set-origin! (make-music 'ContextSpeccedMusic
271                                                     'create-new #t
272                                                     'context-type 'Lyrics
273                                                     'element
274                                                     (lyric-combine
275                                                      voice-name voice-type mus)))))
276                      addlyrics-list)))
277     (make-simultaneous-music (cons voice lyricstos))))