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