]> git.donarmstrong.com Git - lilypond.git/blob - ly/music-functions-init.ly
Make \footnote a post-event
[lilypond.git] / ly / music-functions-init.ly
1 %%%% -*- Mode: Scheme -*-
2
3 %%%% This file is part of LilyPond, the GNU music typesetter.
4 %%%%
5 %%%% Copyright (C) 2003--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 %%%%                          Jan Nieuwenhuizen <janneke@gnu.org>
7 %%%%
8 %%%% LilyPond is free software: you can redistribute it and/or modify
9 %%%% it under the terms of the GNU General Public License as published by
10 %%%% the Free Software Foundation, either version 3 of the License, or
11 %%%% (at your option) any later version.
12 %%%%
13 %%%% LilyPond is distributed in the hope that it will be useful,
14 %%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
15 %%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 %%%% GNU General Public License for more details.
17 %%%%
18 %%%% You should have received a copy of the GNU General Public License
19 %%%% along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20
21 \version "2.15.18"
22
23
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25 %% this file is alphabetically sorted.
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27
28 %% need SRFI-1 for filter; optargs for lambda*
29 #(use-modules (srfi srfi-1)
30               (ice-9 optargs))
31
32 %% TODO: using define-music-function in a .scm causes crash.
33
34 acciaccatura =
35 #(def-grace-function startAcciaccaturaMusic stopAcciaccaturaMusic
36    (_i "Create an acciaccatura from the following music expression"))
37
38 %% keep these two together
39 "instrument-definitions" = #'()
40 addInstrumentDefinition =
41 #(define-void-function
42    (parser location name lst) (string? list?)
43    (_i "Create instrument @var{name} with properties @var{list}.")
44    (set! instrument-definitions (acons name lst instrument-definitions)))
45
46 addQuote =
47 #(define-void-function (parser location name music) (string? ly:music?)
48    (_i "Define @var{music} as a quotable music expression named
49 @var{name}")
50    (add-quotable parser name music))
51
52 %% keep these two together
53 afterGraceFraction = #(cons 6 8)
54 afterGrace =
55 #(define-music-function (parser location main grace) (ly:music? ly:music?)
56    (_i "Create @var{grace} note(s) after a @var{main} music expression.")
57    (let ((main-length (ly:music-length main))
58          (fraction  (ly:parser-lookup parser 'afterGraceFraction)))
59      (make-simultaneous-music
60       (list
61        main
62        (make-sequential-music
63         (list
64
65          (make-music 'SkipMusic
66                      'duration (ly:make-duration
67                                 0 0
68                                 (* (ly:moment-main-numerator main-length)
69                                    (car fraction))
70                                 (* (ly:moment-main-denominator main-length)
71                                    (cdr fraction))))
72          (make-music 'GraceMusic
73                      'element grace)))))))
74
75
76 %% music identifiers not allowed at top-level,
77 %% so this is a music-function instead.
78 allowPageTurn =
79 #(define-music-function (location parser) ()
80    (_i "Allow a page turn. May be used at toplevel (ie between scores or
81 markups), or inside a score.")
82    (make-music 'EventChord
83                'page-marker #t
84                'page-turn-permission 'allow
85                'elements (list (make-music 'PageTurnEvent
86                                            'break-permission 'allow))))
87
88 appendToTag =
89 #(define-music-function (parser location tag more music)
90    (symbol? ly:music? ly:music?)
91    (_i "Append @var{more} to the @code{elements} of all music
92 expressions in @var{music} that are tagged with @var{tag}.")
93    (music-map (lambda (m)
94                 (if (memq tag (ly:music-property m 'tags))
95                     (set! (ly:music-property m 'elements)
96                           (append (ly:music-property m 'elements)
97                                   (list more))))
98                 m)
99               music))
100
101 applyContext =
102 #(define-music-function (parser location proc) (procedure?)
103    (_i "Modify context properties with Scheme procedure @var{proc}.")
104    (make-music 'ApplyContext
105                'procedure proc))
106
107 applyMusic =
108 #(define-music-function (parser location func music) (procedure? ly:music?)
109    (_i"Apply procedure @var{func} to @var{music}.")
110    (func music))
111
112 applyOutput =
113 #(define-music-function (parser location ctx proc) (symbol? procedure?)
114    (_i "Apply function @code{proc} to every layout object in context @code{ctx}")
115    (make-music 'ApplyOutputEvent
116                'procedure proc
117                'context-type ctx))
118
119 appoggiatura =
120 #(def-grace-function startAppoggiaturaMusic stopAppoggiaturaMusic
121    (_i "Create an appoggiatura from @var{music}"))
122
123 % for regression testing purposes.
124 assertBeamQuant =
125 #(define-music-function (parser location l r) (pair? pair?)
126    (_i "Testing function: check whether the beam quants @var{l} and @var{r} are correct")
127    (make-grob-property-override 'Beam 'positions (check-quant-callbacks l r)))
128
129 % for regression testing purposes.
130 assertBeamSlope =
131 #(define-music-function (parser location comp) (procedure?)
132    (_i "Testing function: check whether the slope of the beam is the same as @code{comp}")
133    (make-grob-property-override 'Beam 'positions (check-slope-callbacks comp)))
134
135 autochange =
136 #(define-music-function (parser location music) (ly:music?)
137    (_i "Make voices that switch between staves automatically")
138    (make-autochange-music parser music))
139
140
141
142 balloonGrobText =
143 #(define-music-function (parser location grob-name offset text)
144    (symbol? number-pair? markup?)
145    (_i "Attach @var{text} to @var{grob-name} at offset @var{offset}
146  (use like @code{\\once})")
147    (make-music 'AnnotateOutputEvent
148                'symbol grob-name
149                'X-offset (car offset)
150                'Y-offset (cdr offset)
151                'text text))
152
153 balloonText =
154 #(define-music-function (parser location offset text) (number-pair? markup?)
155    (_i "Attach @var{text} at @var{offset} (use like @code{\\tweak})")
156    (make-music 'AnnotateOutputEvent
157                'X-offset (car offset)
158                'Y-offset (cdr offset)
159                'text text))
160
161 bar =
162 #(define-music-function (parser location type) (string?)
163    (_i "Insert a bar line of type @var{type}")
164    (context-spec-music
165     (make-property-set 'whichBar type)
166     'Timing))
167
168 barNumberCheck =
169 #(define-music-function (parser location n) (integer?)
170    (_i "Print a warning if the current bar number is not @var{n}.")
171    (make-music 'ApplyContext
172                'procedure
173                (lambda (c)
174                  (let ((cbn (ly:context-property c 'currentBarNumber)))
175                    (if (and  (number? cbn) (not (= cbn n)))
176                        (ly:input-warning location
177                                          "Barcheck failed got ~a expect ~a"
178                                          cbn n))))))
179
180 bendAfter =
181 #(define-event-function (parser location delta) (real?)
182    (_i "Create a fall or doit of pitch interval @var{delta}.")
183    (make-music 'BendAfterEvent
184                'delta-step delta))
185
186 bookOutputName =
187 #(define-void-function (parser location newfilename) (string?)
188    (_i "Direct output for the current book block to @var{newfilename}.")
189    (set! (paper-variable parser #f 'output-filename) newfilename))
190
191 bookOutputSuffix =
192 #(define-void-function (parser location newsuffix) (string?)
193    (_i "Set the output filename suffix for the current book block to
194 @var{newsuffix}.")
195    (set! (paper-variable parser #f 'output-suffix) newsuffix))
196
197 %% \breathe is defined as a music function rather than an event identifier to
198 %% ensure it gets useful input location information: as an event identifier,
199 %% it would have to be wrapped in an EventChord to prevent it from being
200 %% treated as a post_event by the parser
201 breathe =
202 #(define-music-function (parser location) ()
203    (_i "Insert a breath mark.")
204    (make-music 'BreathingEvent))
205
206 clef =
207 #(define-music-function (parser location type) (string?)
208    (_i "Set the current clef to @var{type}.")
209    (make-clef-set type))
210
211
212 compoundMeter =
213 #(define-music-function (parser location args) (pair?)
214   (_i "Create compound time signatures. The argument is a Scheme list of
215 lists. Each list describes one fraction, with the last entry being the
216 denominator, while the first entries describe the summands in the
217 enumerator. If the time signature consists of just one fraction,
218 the list can be given directly, i.e. not as a list containing a single list.
219 For example, a time signature of (3+1)/8 + 2/4 would be created as
220 @code{\\compoundMeter #'((3 1 8) (2 4))}, and a time signature of (3+2)/8
221 as @code{\\compoundMeter #'((3 2 8))} or shorter
222 @code{\\compoundMeter #'(3 2 8)}.")
223   (let* ((mlen (calculate-compound-measure-length args))
224          (beat (calculate-compound-base-beat args))
225          (beatGrouping (calculate-compound-beat-grouping args))
226          (timesig (cons (ly:moment-main-numerator mlen)
227                         (ly:moment-main-denominator mlen))))
228   #{
229     \once \override Staff.TimeSignature #'stencil = #(lambda (grob)
230                 (grob-interpret-markup grob (format-compound-time args)))
231     \set Timing.timeSignatureFraction = $timesig
232     \set Timing.baseMoment = $beat
233     \set Timing.beatStructure = $beatGrouping
234     \set Timing.beamExceptions = #'()
235     \set Timing.measureLength = $mlen
236   #} ))
237
238
239 cueClef =
240 #(define-music-function (parser location type) (string?)
241   (_i "Set the current cue clef to @var{type}.")
242   (make-cue-clef-set type))
243 cueClefUnset =
244 #(define-music-function (parser location) ()
245   (_i "Unset the current cue clef.")
246   (make-cue-clef-unset))
247
248 cueDuring =
249 #(define-music-function
250    (parser location what dir main-music) (string? ly:dir? ly:music?)
251    (_i "Insert contents of quote @var{what} corresponding to @var{main-music},
252 in a CueVoice oriented by @var{dir}.")
253    (make-music 'QuoteMusic
254                'element main-music
255                'quoted-context-type 'Voice
256                'quoted-context-id "cue"
257                'quoted-music-name what
258                'quoted-voice-direction dir))
259
260 cueDuringWithClef =
261 #(define-music-function
262    (parser location what dir clef main-music) (string? ly:dir? string? ly:music?)
263    (_i "Insert contents of quote @var{what} corresponding to @var{main-music},
264 in a CueVoice oriented by @var{dir}.")
265    (make-music 'QuoteMusic
266                'element main-music
267                'quoted-context-type 'Voice
268                'quoted-context-id "cue"
269                'quoted-music-name what
270                'quoted-music-clef clef
271                'quoted-voice-direction dir))
272
273
274
275 displayLilyMusic =
276 #(define-music-function (parser location music) (ly:music?)
277    (_i "Display the LilyPond input representation of @var{music}
278 to the console.")
279    (newline)
280    (display-lily-music music parser)
281    music)
282
283 displayMusic =
284 #(define-music-function (parser location music) (ly:music?)
285    (_i "Display the internal representation of @var{music} to the console.")
286    (newline)
287    (display-scheme-music music)
288    music)
289
290
291
292 endSpanners =
293 #(define-music-function (parser location music) (ly:music?)
294    (_i "Terminate the next spanner prematurely after exactly one note
295 without the need of a specific end spanner.")
296    (if (memq (ly:music-property music 'name) '(EventChord NoteEvent))
297        (let* ((start-span-evs (filter (lambda (ev)
298                                         (equal? (ly:music-property ev 'span-direction)
299                                                 START))
300                                       (extract-typed-music music 'span-event)))
301               (stop-span-evs
302                (map (lambda (m)
303                       (let ((c (music-clone m)))
304                         (set! (ly:music-property c 'span-direction) STOP)
305                         c))
306                     start-span-evs))
307               (end-ev-chord (make-music 'EventChord
308                                         'elements stop-span-evs))
309               (total (make-music 'SequentialMusic
310                                  'elements (list music
311                                                  end-ev-chord))))
312          total)
313
314        (begin
315          (ly:input-message location (_ "argument endSpanners is not an EventChord: ~a") music)
316          music)))
317
318 eventChords =
319 #(define-music-function (parser location music) (ly:music?)
320    (_i "Compatibility function wrapping @code{EventChord} around
321 isolated rhythmic events occuring since version 2.15.28, after
322 expanding repeat chords @samp{q}.")
323    (event-chord-wrap! music parser))
324
325 featherDurations=
326 #(define-music-function (parser location factor argument) (ly:moment? ly:music?)
327    (_i "Adjust durations of music in @var{argument} by rational @var{factor}.")
328    (let ((orig-duration (ly:music-length argument))
329          (multiplier (ly:make-moment 1 1)))
330
331      (for-each
332       (lambda (mus)
333         (if (< 0 (ly:moment-main-denominator (ly:music-length mus)))
334             (begin
335               (ly:music-compress mus multiplier)
336               (set! multiplier (ly:moment-mul factor multiplier)))))
337       (extract-named-music argument '(EventChord NoteEvent RestEvent SkipEvent)))
338      (ly:music-compress
339       argument
340       (ly:moment-div orig-duration (ly:music-length argument)))
341
342      argument))
343
344 footnote =
345 #(define-event-function (parser location text offset grob-name footnote)
346    ((markup?) number-pair? (symbol? '()) markup?)
347    (_i "Attach @var{text} at @var{offset} with @var{text} referring to
348 @var{footnote}.  If @var{text} is given as @code{\\default}, use
349 autonumbering instead.  Note that, for this to take effect,
350 auto-numbering must be turned on in the paper block.  Otherwise, no
351 number will appear.  Footnotes are applied like articulations.  If a
352 symbol @var{grob-name} is specified, all grobs of that kind at the
353 current time step are affected.")
354    (make-music
355     'FootnoteEvent
356     'X-offset (car offset)
357     'Y-offset (cdr offset)
358     'automatically-numbered (not text)
359     'text (or text (make-null-markup))
360     'footnote-text footnote
361     'symbol grob-name))
362
363 grace =
364 #(def-grace-function startGraceMusic stopGraceMusic
365    (_i "Insert @var{music} as grace notes."))
366
367 grobdescriptions =
368 #(define-scheme-function (parser location descriptions) (list?)
369    (_i "Create a context modification from @var{descriptions}, a list
370 in the format of @code{all-grob-descriptions}.")
371    (ly:make-context-mod
372     (map (lambda (p)
373            (list 'assign (car p) (list (cdr p))))
374          descriptions)))
375
376 harmonicByFret = #(define-music-function (parser location fret music) (number? ly:music?)
377   (_i "Convert @var{music} into mixed harmonics; the resulting notes resemble
378 harmonics played on a fretted instrument by touching the strings at @var{fret}.")
379   #{
380     \set harmonicDots = ##t
381     \override TabNoteHead #'stencil = #(tab-note-head::print-custom-fret-label (number->string fret))
382     \override NoteHead #'Y-extent = #(ly:make-unpure-pure-container ly:grob::stencil-height
383                                        (lambda (grob start end)
384                                                (ly:grob::stencil-height grob)))
385     \override NoteHead #'stencil = #(lambda (grob) (ly:grob-set-property! grob 'style 'harmonic-mixed)
386                                             (ly:note-head::print grob))
387     $(make-harmonic
388        (calc-harmonic-pitch (fret->pitch (number->string fret)) music))
389     \unset harmonicDots
390     \revert TabNoteHead #'stencil
391     \revert NoteHead #'Y-extent
392     \revert NoteHead #'stencil
393   #})
394
395 harmonicByRatio = #(define-music-function (parser location ratio music) (number? ly:music?)
396     (_i "Convert @var{music} into mixed harmonics; the resulting notes resemble
397 harmonics played on a fretted instrument by touching the strings at the point
398 given through @var{ratio}.")
399   #{
400     \set harmonicDots = ##t
401     \override TabNoteHead #'stencil = #(tab-note-head::print-custom-fret-label (ratio->fret ratio))
402     \override NoteHead #'Y-extent = #(ly:make-unpure-pure-container ly:grob::stencil-height
403                                        (lambda (grob start end)
404                                                (ly:grob::stencil-height grob)))
405     \override NoteHead #'stencil = #(lambda (grob) (ly:grob-set-property! grob 'style 'harmonic-mixed)
406                                             (ly:note-head::print grob))
407     $(make-harmonic
408       (calc-harmonic-pitch (ratio->pitch ratio) music))
409     \unset harmonicDots
410     \revert TabNoteHead #'stencil
411     \revert NoteHead #'Y-extent
412     \revert NoteHead #'stencil
413   #})
414
415 instrumentSwitch =
416 #(define-music-function
417    (parser location name) (string?)
418    (_i "Switch instrument to @var{name}, which must be predefined with
419 @code{\\addInstrumentDefinition}.")
420    (let* ((handle (assoc name instrument-definitions))
421           (instrument-def (if handle (cdr handle) '())))
422
423      (if (not handle)
424          (ly:input-warning location "No such instrument: ~a" name))
425      (context-spec-music
426       (make-music 'SimultaneousMusic
427                   'elements
428                   (map (lambda (kv)
429                          (make-property-set
430                           (car kv)
431                           (cdr kv)))
432                        instrument-def))
433       'Staff)))
434
435
436
437 keepWithTag =
438 #(define-music-function (parser location tag music) (symbol? ly:music?)
439    (_i "Include only elements of @var{music} that are tagged with @var{tag}.")
440    (music-filter
441     (lambda (m)
442       (let* ((tags (ly:music-property m 'tags))
443              (res (memq tag tags)))
444         (or
445          (eq? tags '())
446          res)))
447     music))
448
449 key =
450 #(define-music-function (parser location tonic pitch-alist)
451    ((ly:pitch? '()) (list? '()))
452    (_i "Set key to @var{tonic} and scale @var{pitch-alist}.
453 If both are null, just generate @code{KeyChangeEvent}.")
454    (cond ((null? tonic) (make-music 'KeyChangeEvent))
455          ((null? pitch-alist)
456           (ly:parser-error parser (_ "second argument must be pitch list")
457                            location)
458           (make-music 'SequentialMusic 'void #t))
459          (else
460           (ly:music-transpose
461            (make-music 'KeyChangeEvent
462                 'tonic (ly:make-pitch 0 0 0)
463                 'pitch-alist pitch-alist)
464            tonic))))
465
466 killCues =
467 #(define-music-function (parser location music) (ly:music?)
468    (_i "Remove cue notes from @var{music}.")
469    (music-map
470     (lambda (mus)
471       (if (and (string? (ly:music-property mus 'quoted-music-name))
472                (string=? (ly:music-property mus 'quoted-context-id "") "cue"))
473           (ly:music-property mus 'element)
474           mus))
475     music))
476
477
478
479 label =
480 #(define-music-function (parser location label) (symbol?)
481    (_i "Create @var{label} as a bookmarking label.")
482    (make-music 'EventChord
483                'page-marker #t
484                'page-label label
485                'elements (list (make-music 'LabelEvent
486                                            'page-label label))))
487
488
489 language =
490 #(define-void-function (parser location language) (string?)
491    (_i "Set note names for language @var{language}.")
492    (note-names-language parser language))
493
494 languageSaveAndChange =
495 #(define-void-function (parser location language) (string?)
496   (_i "Store the previous pitchnames alist, and set a new one.")
497   (set! previous-pitchnames pitchnames)
498   (note-names-language parser language))
499
500 languageRestore =
501 #(define-void-function (parser location) ()
502    (_i "Restore a previously-saved pitchnames alist.")
503    (if previous-pitchnames
504        (begin
505         (set! pitchnames previous-pitchnames)
506         (ly:parser-set-note-names parser pitchnames))
507       (ly:input-warning location (_ "No other language was defined previously. Ignoring."))))
508
509
510 makeClusters =
511 #(define-music-function (parser location arg) (ly:music?)
512    (_i "Display chords in @var{arg} as clusters.")
513    (music-map note-to-cluster arg))
514
515 modalInversion =
516 #(define-music-function (parser location around to scale music)
517     (ly:pitch? ly:pitch? ly:music? ly:music?)
518     (_i "Invert @var{music} about @var{around} using @var{scale} and
519 transpose from @var{around} to @var{to}.")
520     (let ((inverter (make-modal-inverter around to scale)))
521       (change-pitches music inverter)
522       music))
523
524 modalTranspose =
525 #(define-music-function (parser location from to scale music)
526     (ly:pitch? ly:pitch? ly:music? ly:music?)
527     (_i "Transpose @var{music} from pitch @var{from} to pitch @var{to}
528 using @var{scale}.")
529     (let ((transposer (make-modal-transposer from to scale)))
530       (change-pitches music transposer)
531       music))
532
533 inversion =
534 #(define-music-function
535    (parser location around to music) (ly:pitch? ly:pitch? ly:music?)
536    (_i "Invert @var{music} about @var{around} and
537 transpose from @var{around} to @var{to}.")
538    (music-invert around to music))
539
540 mark =
541 #(define-music-function
542    (parser location label) ((scheme? '()))
543   "Make the music for the \\mark command."
544   (let* ((set (and (integer? label)
545                    (context-spec-music (make-property-set 'rehearsalMark label)
546                                       'Score)))
547          (ev (make-music 'MarkEvent
548                          'origin location)))
549
550     (if set
551         (make-sequential-music (list set ev))
552         (begin
553           (set! (ly:music-property ev 'label) label)
554           ev))))
555
556 musicMap =
557 #(define-music-function (parser location proc mus) (procedure? ly:music?)
558    (_i "Apply @var{proc} to @var{mus} and all of the music it contains.")
559    (music-map proc mus))
560
561 %% noPageBreak and noPageTurn are music functions (not music indentifiers),
562 %% because music identifiers are not allowed at top-level.
563 noPageBreak =
564 #(define-music-function (location parser) ()
565    (_i "Forbid a page break.  May be used at toplevel (i.e., between scores or
566 markups), or inside a score.")
567    (make-music 'EventChord
568                'page-marker #t
569                'page-break-permission 'forbid
570                'elements (list (make-music 'PageBreakEvent
571                                            'break-permission '()))))
572
573 noPageTurn =
574 #(define-music-function (location parser) ()
575    (_i "Forbid a page turn.  May be used at toplevel (i.e., between scores or
576 markups), or inside a score.")
577    (make-music 'EventChord
578                'page-marker #t
579                'page-turn-permission 'forbid
580                'elements (list (make-music 'PageTurnEvent
581                                            'break-permission '()))))
582
583
584
585 octaveCheck =
586 #(define-music-function (parser location pitch) (ly:pitch?)
587    (_i "Octave check.")
588    (make-music 'RelativeOctaveCheck
589                'pitch pitch))
590
591 once =
592 #(define-music-function (parser location music) (ly:music?)
593    (_i "Set @code{once} to @code{#t} on all layout instruction events in @var{music}.")
594    (music-map
595     (lambda (m)
596       (cond ((music-is-of-type? m 'layout-instruction-event)
597              (set! (ly:music-property m 'once) #t))
598             ((ly:duration? (ly:music-property m 'duration))
599              (ly:music-warning m (_ "Cannot apply \\once to timed music"))))
600       m)
601     music))
602
603 ottava =
604 #(define-music-function (parser location octave) (integer?)
605    (_i "Set the octavation.")
606    (make-music 'OttavaMusic
607                'ottava-number octave))
608
609 overrideTimeSignatureSettings =
610 #(define-music-function
611    (parser location time-signature base-moment beat-structure beam-exceptions)
612    (pair? pair? cheap-list? cheap-list?)
613
614    (_i "Override @code{timeSignatureSettings}
615 for time signatures of @var{time-signature} to have settings
616 of @var{base-moment}, @var{beat-structure}, and @var{beam-exceptions}.")
617
618    ;; TODO -- add warning if largest value of grouping is
619    ;;       greater than time-signature.
620   (let ((setting (make-setting base-moment beat-structure beam-exceptions)))
621     (override-time-signature-setting time-signature setting)))
622
623 overrideProperty =
624 #(define-music-function (parser location name property value)
625    (string? symbol? scheme?)
626
627    (_i "Set @var{property} to @var{value} in all grobs named @var{name}.
628 The @var{name} argument is a string of the form @code{\"Context.GrobName\"}
629 or @code{\"GrobName\"}.")
630
631    (let ((name-components (string-split name #\.))
632          (context-name 'Bottom)
633          (grob-name #f))
634
635      (if (> 2 (length name-components))
636          (set! grob-name (string->symbol (car name-components)))
637          (begin
638            (set! grob-name (string->symbol (list-ref name-components 1)))
639            (set! context-name (string->symbol (list-ref name-components 0)))))
640
641      (make-music 'ApplyOutputEvent
642                  'context-type context-name
643                  'procedure
644                  (lambda (grob orig-context context)
645                    (if (equal?
646                         (cdr (assoc 'name (ly:grob-property grob 'meta)))
647                         grob-name)
648                        (set! (ly:grob-property grob property) value))))))
649
650
651
652 %% pageBreak and pageTurn are music functions (iso music indentifiers),
653 %% because music identifiers are not allowed at top-level.
654 pageBreak =
655 #(define-music-function (location parser) ()
656    (_i "Force a page break.  May be used at toplevel (i.e., between scores or
657 markups), or inside a score.")
658    (make-music 'EventChord
659                'page-marker #t
660                'line-break-permission 'force
661                'page-break-permission 'force
662                'elements (list (make-music 'LineBreakEvent
663                                            'break-permission 'force)
664                                (make-music 'PageBreakEvent
665                                            'break-permission 'force))))
666
667 pageTurn =
668 #(define-music-function (location parser) ()
669    (_i "Force a page turn between two scores or top-level markups.")
670    (make-music 'EventChord
671                'page-marker #t
672                'line-break-permission 'force
673                'page-break-permission 'force
674                'page-turn-permission 'force
675                'elements (list (make-music 'LineBreakEvent
676                                            'break-permission 'force)
677                                (make-music 'PageBreakEvent
678                                            'break-permission 'force)
679                                (make-music 'PageTurnEvent
680                                            'break-permission 'force))))
681
682 parallelMusic =
683 #(define-void-function (parser location voice-ids music) (list? ly:music?)
684    (_i "Define parallel music sequences, separated by '|' (bar check signs),
685 and assign them to the identifiers provided in @var{voice-ids}.
686
687 @var{voice-ids}: a list of music identifiers (symbols containing only letters)
688
689 @var{music}: a music sequence, containing BarChecks as limiting expressions.
690
691 Example:
692
693 @verbatim
694   \\parallelMusic #'(A B C) {
695     c c | d d | e e |
696     d d | e e | f f |
697   }
698 <==>
699   A = { c c | d d | }
700   B = { d d | e e | }
701   C = { e e | f f | }
702 @end verbatim
703 ")
704    (let* ((voices (apply circular-list (make-list (length voice-ids) (list))))
705           (current-voices voices)
706           (current-sequence (list))
707           (original music)
708           (wrapper #f))
709      ;;
710      ;; utilities
711      (define (push-music m)
712        "Push the music expression into the current sequence"
713        (set! current-sequence (cons m current-sequence)))
714      (define (change-voice)
715        "Stores the previously built sequence into the current voice and
716        change to the following voice."
717        (list-set! current-voices 0 (cons (make-music 'SequentialMusic
718                                                      'elements (reverse! current-sequence))
719                                          (car current-voices)))
720        (set! current-sequence (list))
721        (set! current-voices (cdr current-voices)))
722      (define (bar-check? m)
723        "Checks whether m is a bar check."
724        (eq? (ly:music-property m 'name) 'BarCheck))
725      (define (music-origin music)
726        "Recursively search an origin location stored in music."
727        (cond ((null? music) #f)
728              ((not (null? (ly:music-property music 'origin)))
729               (ly:music-property music 'origin))
730              (else (or (music-origin (ly:music-property music 'element))
731                        (let ((origins (remove not (map music-origin
732                                                        (ly:music-property music 'elements)))))
733                          (and (not (null? origins)) (car origins)))))))
734      (while (music-is-of-type? music 'music-wrapper-music)
735             (set! wrapper music)
736             (set! music (ly:music-property wrapper 'element)))
737      (if wrapper
738          (set! (ly:music-property wrapper 'element)
739                                   (make-music 'SequentialMusic
740                                               'origin location))
741          (set! original
742                (make-music 'SequentialMusic
743                            'origin location)))
744      ;;
745      ;; first, split the music and fill in voices
746      ;; We flatten direct layers of SequentialMusic since they are
747      ;; pretty much impossible to avoid when writing music functions.
748      (let rec ((music music))
749        (for-each (lambda (m)
750                    (if (eq? (ly:music-property m 'name) 'SequentialMusic)
751                        (rec m)
752                        (begin
753                          (push-music m)
754                          (if (bar-check? m) (change-voice)))))
755                  (ly:music-property music 'elements)))
756      (if (not (null? current-sequence)) (change-voice))
757      ;; un-circularize `voices' and reorder the voices
758      (set! voices (map-in-order (lambda (dummy seqs)
759                                   (reverse! seqs))
760                                 voice-ids voices))
761      ;;
762      ;; set origin location of each sequence in each voice
763      ;; for better type error tracking
764      (for-each (lambda (voice)
765                  (for-each (lambda (seq)
766                              (set! (ly:music-property seq 'origin)
767                                    (or (music-origin seq) location)))
768                            voice))
769                voices)
770      ;;
771      ;; check sequence length
772      (apply for-each (lambda* (#:rest seqs)
773                               (let ((moment-reference (ly:music-length (car seqs))))
774                                 (for-each (lambda (seq moment)
775                                             (if (not (equal? moment moment-reference))
776                                                 (ly:music-warning seq
777                                                                   "Bars in parallel music don't have the same length")))
778                                           seqs (map-in-order ly:music-length seqs))))
779             voices)
780      ;;
781      ;; bind voice identifiers to the voices
782      (for-each (lambda (voice-id voice)
783             (ly:parser-define! parser voice-id
784                                (let ((v (ly:music-deep-copy original)))
785                                  (set! (ly:music-property
786                                         (car (extract-named-music
787                                               v 'SequentialMusic))
788                                         'elements) voice)
789                                  v)))
790           voice-ids voices)))
791
792 parenthesize =
793 #(define-music-function (parser loc arg) (ly:music?)
794    (_i "Tag @var{arg} to be parenthesized.")
795
796    (if (memq 'event-chord (ly:music-property arg 'types))
797        ;; arg is an EventChord -> set the parenthesize property
798        ;; on all child notes and rests
799        (for-each
800         (lambda (ev)
801           (if (or (memq 'note-event (ly:music-property ev 'types))
802                   (memq 'rest-event (ly:music-property ev 'types)))
803               (set! (ly:music-property ev 'parenthesize) #t)))
804         (ly:music-property arg 'elements))
805        ;; No chord, simply set property for this expression:
806        (set! (ly:music-property arg 'parenthesize) #t))
807    arg)
808
809 partcombine =
810 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
811    (_i "Take the music in @var{part1} and @var{part2} and typeset so
812 that they share a staff.")
813    (make-part-combine-music parser
814                             (list part1 part2) #f))
815
816 partcombineUp =
817 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
818    (_i "Take the music in @var{part1} and @var{part2} and typeset so
819 that they share a staff with stems directed upward.")
820    (make-part-combine-music parser
821                             (list part1 part2) UP))
822
823 partcombineDown =
824 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
825    (_i "Take the music in @var{part1} and @var{part2} and typeset so
826 that they share a staff with stems directed downward.")
827    (make-part-combine-music parser
828                             (list part1 part2) DOWN))
829
830 partcombineForce =
831 #(define-music-function (location parser type once) (symbol-or-boolean? boolean?)
832    (_i "Override the part-combiner.")
833    (make-music 'EventChord
834                'elements (list (make-music 'PartCombineForceEvent
835                                            'forced-type type
836                                            'once once))))
837 partcombineApart = \partcombineForce #'apart ##f
838 partcombineApartOnce = \partcombineForce #'apart ##t
839 partcombineChords = \partcombineForce #'chords ##f
840 partcombineChordsOnce = \partcombineForce #'chords ##t
841 partcombineUnisono = \partcombineForce #'unisono ##f
842 partcombineUnisonoOnce = \partcombineForce #'unisono ##t
843 partcombineSoloI = \partcombineForce #'solo1 ##f
844 partcombineSoloIOnce = \partcombineForce #'solo1 ##t
845 partcombineSoloII = \partcombineForce #'solo2 ##f
846 partcombineSoloIIOnce = \partcombineForce #'solo2 ##t
847 partcombineAutomatic = \partcombineForce ##f ##f
848 partcombineAutomaticOnce = \partcombineForce ##f ##t
849
850 partial =
851 #(define-music-function (parser location dur) (ly:duration?)
852   (_i "Make a partial measure.")
853
854   ;; We use `descend-to-context' here instead of `context-spec-music' to
855   ;; ensure \partial still works if the Timing_translator is moved
856     (descend-to-context
857      (context-spec-music (make-music 'PartialSet
858                                      'origin location
859                                      'partial-duration dur)
860                          'Timing)
861      'Score))
862
863 pitchedTrill =
864 #(define-music-function
865    (parser location main-note secondary-note)
866    (ly:music? ly:music?)
867    (_i "Print a trill with @var{main-note} as the main note of the trill and
868 print @var{secondary-note} as a stemless note head in parentheses.")
869    (let* ((get-notes (lambda (ev-chord)
870                        (extract-named-music ev-chord 'NoteEvent)))
871           (sec-note-events (get-notes secondary-note))
872           (trill-events (extract-named-music main-note 'TrillSpanEvent)))
873      (if (pair? sec-note-events)
874          (begin
875            (let* ((trill-pitch (ly:music-property (car sec-note-events) 'pitch))
876                   (forced (ly:music-property (car sec-note-events) 'force-accidental)))
877
878              (if (ly:pitch? trill-pitch)
879                  (for-each (lambda (m)
880                              (ly:music-set-property! m 'pitch trill-pitch)) trill-events)
881                  (begin
882                    (ly:input-warning location (_ "Second argument of \\pitchedTrill should be single note: "))
883                    (display sec-note-events)))
884
885              (if (eq? forced #t)
886                  (for-each (lambda (m)
887                              (ly:music-set-property! m 'force-accidental forced))
888                            trill-events)))))
889      main-note))
890
891 pushToTag =
892 #(define-music-function (parser location tag more music)
893    (symbol? ly:music? ly:music?)
894    (_i "Add @var{more} to the front of @code{elements} of all music
895 expressions in @var{music} that are tagged with @var{tag}.")
896    (music-map (lambda (m)
897                 (if (memq tag (ly:music-property m 'tags))
898                     (set! (ly:music-property m 'elements)
899                           (cons more (ly:music-property m 'elements))))
900                 m)
901               music))
902
903 quoteDuring =
904 #(define-music-function (parser location what main-music) (string? ly:music?)
905    (_i "Indicate a section of music to be quoted.  @var{what} indicates the name
906 of the quoted voice, as specified in an @code{\\addQuote} command.
907 @var{main-music} is used to indicate the length of music to be quoted;
908 usually contains spacers or multi-measure rests.")
909    (make-music 'QuoteMusic
910                'element main-music
911                'quoted-music-name what))
912
913 relative =
914 #(define-music-function (parser location pitch music)
915    ((ly:pitch? (ly:make-pitch 0 0 0)) ly:music?)
916    (_i "Make @var{music} relative to @var{pitch} (default @code{c'}).")
917    (ly:make-music-relative! music pitch)
918    (make-music 'RelativeOctaveMusic
919                'element music))
920
921 removeWithTag =
922 #(define-music-function (parser location tag music) (symbol? ly:music?)
923    (_i "Remove elements of @var{music} that are tagged with @var{tag}.")
924    (music-filter
925     (lambda (m)
926       (let* ((tags (ly:music-property m 'tags))
927              (res (memq tag tags)))
928         (not res)))
929     music))
930
931 resetRelativeOctave =
932 #(define-music-function (parser location pitch) (ly:pitch?)
933    (_i "Set the octave inside a \\relative section.")
934
935    (make-music 'SequentialMusic
936                'to-relative-callback
937                (lambda (music last-pitch) pitch)))
938
939 retrograde =
940 #(define-music-function (parser location music)
941     (ly:music?)
942     (_i "Return @var{music} in reverse order.")
943     (retrograde-music music))
944
945 revertTimeSignatureSettings =
946 #(define-music-function
947    (parser location time-signature)
948    (pair?)
949
950    (_i "Revert @code{timeSignatureSettings}
951 for time signatures of @var{time-signature}.")
952    (revert-time-signature-setting time-signature))
953
954 rightHandFinger =
955 #(define-event-function (parser location finger) (number-or-string?)
956    (_i "Apply @var{finger} as a fingering indication.")
957
958    (make-music
959             'StrokeFingerEvent
960             'origin location
961             (if (string? finger) 'text 'digit)
962             finger))
963
964 scaleDurations =
965 #(define-music-function (parser location fraction music)
966    (fraction? ly:music?)
967    (_i "Multiply the duration of events in @var{music} by @var{fraction}.")
968    (ly:music-compress music
969                       (ly:make-moment (car fraction) (cdr fraction))))
970
971 settingsFrom =
972 #(define-scheme-function (parser location ctx music)
973    ((symbol?) ly:music?)
974    (_i "Take the layout instruction events from @var{music}, optionally
975 restricted to those applying to context type @var{ctx}, and return
976 a context modification duplicating their effect.")
977    (let ((mods (ly:make-context-mod)))
978      (define (musicop m)
979        (if (music-is-of-type? m 'layout-instruction-event)
980            (ly:add-context-mod
981             mods
982             (case (ly:music-property m 'name)
983               ((PropertySet)
984                (list 'assign
985                      (ly:music-property m 'symbol)
986                      (ly:music-property m 'value)))
987               ((PropertyUnset)
988                (list 'unset
989                      (ly:music-property m 'symbol)))
990               ((OverrideProperty)
991                (cons* 'push
992                       (ly:music-property m 'symbol)
993                       (ly:music-property m 'grob-value)
994                       (ly:music-property m 'grob-property-path)))
995               ((RevertProperty)
996                (cons* 'pop
997                       (ly:music-property m 'symbol)
998                       (ly:music-property m 'grob-property-path)))))
999            (case (ly:music-property m 'name)
1000              ((ApplyContext)
1001               (ly:add-context-mod mods
1002                                   (list 'apply
1003                                         (ly:music-property m 'procedure))))
1004              ((ContextSpeccedMusic)
1005               (if (or (not ctx)
1006                       (eq? ctx (ly:music-property m 'context-type)))
1007                   (musicop (ly:music-property m 'element))))
1008              (else
1009               (let ((callback (ly:music-property m 'elements-callback)))
1010                 (if (procedure? callback)
1011                     (for-each musicop (callback m))))))))
1012      (musicop music)
1013      mods))
1014
1015 shiftDurations =
1016 #(define-music-function (parser location dur dots arg)
1017    (integer? integer? ly:music?)
1018    (_i "Change the duration of @var{arg} by adding @var{dur} to the
1019 @code{durlog} of @var{arg} and @var{dots} to the @code{dots} of @var{arg}.")
1020
1021    (music-map
1022     (lambda (x)
1023       (shift-one-duration-log x dur dots)) arg))
1024
1025 skip =
1026 #(define-music-function (parser location dur) (ly:duration?)
1027   (_i "Skip forward by @var{dur}.")
1028   (make-music 'SkipMusic
1029               'duration dur))
1030
1031
1032 slashedGrace =
1033 #(def-grace-function startSlashedGraceMusic stopSlashedGraceMusic
1034    (_i "Create slashed graces (slashes through stems, but no slur) from
1035 the following music expression"))
1036
1037 spacingTweaks =
1038 #(define-music-function (parser location parameters) (list?)
1039    (_i "Set the system stretch, by reading the 'system-stretch property of
1040 the `parameters' assoc list.")
1041    #{
1042      \overrideProperty #"Score.NonMusicalPaperColumn"
1043      #'line-break-system-details
1044      #(list (cons 'alignment-extra-space (cdr (assoc 'system-stretch parameters)))
1045              (cons 'system-Y-extent (cdr (assoc 'system-Y-extent parameters))))
1046    #})
1047
1048 styledNoteHeads =
1049 #(define-music-function (parser location style heads music)
1050    (symbol? list-or-symbol? ly:music?)
1051    (_i "Set @var{heads} in @var{music} to @var{style}.")
1052    (style-note-heads heads style music))
1053
1054 tag =
1055 #(define-music-function (parser location tag arg) (symbol? ly:music?)
1056
1057    (_i "Add @var{tag} to the @code{tags} property of @var{arg}.")
1058
1059    (set!
1060     (ly:music-property arg 'tags)
1061     (cons tag
1062           (ly:music-property arg 'tags)))
1063    arg)
1064
1065 time =
1066 #(define-music-function (parser location beat-structure fraction)
1067    ((number-list? '()) fraction?)
1068    (_i "Set @var{fraction} as time signature, with optional
1069 number list @var{beat-structure} before it.")
1070   (make-music 'TimeSignatureMusic
1071               'numerator (car fraction)
1072               'denominator (cdr fraction)
1073               'beat-structure beat-structure))
1074
1075 times =
1076 #(define-music-function (parser location fraction music)
1077    (fraction? ly:music?)
1078    (_i "Scale @var{music} in time by @var{fraction}.")
1079   (make-music 'TimeScaledMusic
1080               'element (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction)))
1081               'numerator (car fraction)
1082               'denominator (cdr fraction)))
1083
1084 transpose =
1085 #(define-music-function
1086    (parser location from to music)
1087    (ly:pitch? ly:pitch? ly:music?)
1088
1089    (_i "Transpose @var{music} from pitch @var{from} to pitch @var{to}.")
1090    (make-music 'TransposedMusic
1091                'element (ly:music-transpose music (ly:pitch-diff to from))))
1092
1093 transposedCueDuring =
1094 #(define-music-function
1095    (parser location what dir pitch main-music)
1096    (string? ly:dir? ly:pitch? ly:music?)
1097
1098    (_i "Insert notes from the part @var{what} into a voice called @code{cue},
1099 using the transposition defined by @var{pitch}.  This happens
1100 simultaneously with @var{main-music}, which is usually a rest.  The
1101 argument @var{dir} determines whether the cue notes should be notated
1102 as a first or second voice.")
1103
1104    (make-music 'QuoteMusic
1105                'element main-music
1106                'quoted-context-type 'Voice
1107                'quoted-context-id "cue"
1108                'quoted-music-name what
1109                'quoted-voice-direction dir
1110                'quoted-transposition pitch))
1111
1112 transposition =
1113 #(define-music-function (parser location pitch) (ly:pitch?)
1114    (_i "Set instrument transposition")
1115
1116    (context-spec-music
1117     (make-property-set 'instrumentTransposition
1118                        (ly:pitch-negate pitch))
1119     'Staff))
1120
1121 tweak =
1122 #(define-music-function (parser location sym val arg)
1123    (symbol? scheme? ly:music?)
1124    (_i "Add @code{sym . val} to the @code{tweaks} property of @var{arg}.")
1125
1126    (if (equal? (object-property sym 'backend-type?) #f)
1127        (begin
1128          (ly:input-warning location (_ "cannot find property type-check for ~a") sym)
1129          (ly:warning (_ "doing assignment anyway"))))
1130    (set!
1131     (ly:music-property arg 'tweaks)
1132     (acons sym val
1133            (ly:music-property arg 'tweaks)))
1134    arg)
1135
1136
1137
1138 unfoldRepeats =
1139 #(define-music-function (parser location music) (ly:music?)
1140    (_i "Force any @code{\\repeat volta}, @code{\\repeat tremolo} or
1141 @code{\\repeat percent} commands in @var{music} to be interpreted
1142 as @code{\\repeat unfold}.")
1143    (unfold-repeats music))
1144
1145 void =
1146 #(define-void-function (parser location arg) (scheme?)
1147    (_i "Accept a scheme argument, return a void expression.
1148 Use this if you want to have a scheme expression evaluated
1149 because of its side-effects, but its value ignored."))
1150
1151 withMusicProperty =
1152 #(define-music-function (parser location sym val music)
1153    (symbol? scheme? ly:music?)
1154    (_i "Set @var{sym} to @var{val} in @var{music}.")
1155
1156    (set! (ly:music-property music sym) val)
1157    music)