]> git.donarmstrong.com Git - lilypond.git/blob - ly/music-functions-init.ly
Volta enhancements tranche 1
[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 crossStaff =
239 #(define-music-function (parser location notes) (ly:music?)
240   (_i "Create cross-staff stems")
241   #{
242   \override Stem #'cross-staff = #cross-staff-connect
243   \override Flag #'style = #'no-flag
244   $notes
245   \revert Stem #'cross-staff
246   \revert Flag #'style
247 #})
248
249 cueClef =
250 #(define-music-function (parser location type) (string?)
251   (_i "Set the current cue clef to @var{type}.")
252   (make-cue-clef-set type))
253
254 cueClefUnset =
255 #(define-music-function (parser location) ()
256   (_i "Unset the current cue clef.")
257   (make-cue-clef-unset))
258
259 cueDuring =
260 #(define-music-function
261    (parser location what dir main-music) (string? ly:dir? ly:music?)
262    (_i "Insert contents of quote @var{what} corresponding to @var{main-music},
263 in a CueVoice oriented by @var{dir}.")
264    (make-music 'QuoteMusic
265                'element main-music
266                'quoted-context-type 'Voice
267                'quoted-context-id "cue"
268                'quoted-music-name what
269                'quoted-voice-direction dir))
270
271 cueDuringWithClef =
272 #(define-music-function
273    (parser location what dir clef main-music) (string? ly:dir? string? ly:music?)
274    (_i "Insert contents of quote @var{what} corresponding to @var{main-music},
275 in a CueVoice oriented by @var{dir}.")
276    (make-music 'QuoteMusic
277                'element main-music
278                'quoted-context-type 'Voice
279                'quoted-context-id "cue"
280                'quoted-music-name what
281                'quoted-music-clef clef
282                'quoted-voice-direction dir))
283
284
285
286 displayLilyMusic =
287 #(define-music-function (parser location music) (ly:music?)
288    (_i "Display the LilyPond input representation of @var{music}
289 to the console.")
290    (newline)
291    (display-lily-music music parser)
292    music)
293
294 displayMusic =
295 #(define-music-function (parser location music) (ly:music?)
296    (_i "Display the internal representation of @var{music} to the console.")
297    (newline)
298    (display-scheme-music music)
299    music)
300
301
302
303 endSpanners =
304 #(define-music-function (parser location music) (ly:music?)
305    (_i "Terminate the next spanner prematurely after exactly one note
306 without the need of a specific end spanner.")
307    (if (memq (ly:music-property music 'name) '(EventChord NoteEvent))
308        (let* ((start-span-evs (filter (lambda (ev)
309                                         (equal? (ly:music-property ev 'span-direction)
310                                                 START))
311                                       (extract-typed-music music 'span-event)))
312               (stop-span-evs
313                (map (lambda (m)
314                       (let ((c (music-clone m)))
315                         (set! (ly:music-property c 'span-direction) STOP)
316                         c))
317                     start-span-evs))
318               (end-ev-chord (make-music 'EventChord
319                                         'elements stop-span-evs))
320               (total (make-music 'SequentialMusic
321                                  'elements (list music
322                                                  end-ev-chord))))
323          total)
324
325        (begin
326          (ly:input-message location (_ "argument endSpanners is not an EventChord: ~a") music)
327          music)))
328
329 eventChords =
330 #(define-music-function (parser location music) (ly:music?)
331    (_i "Compatibility function wrapping @code{EventChord} around
332 isolated rhythmic events occuring since version 2.15.28, after
333 expanding repeat chords @samp{q}.")
334    (event-chord-wrap! music parser))
335
336 featherDurations=
337 #(define-music-function (parser location factor argument) (ly:moment? ly:music?)
338    (_i "Adjust durations of music in @var{argument} by rational @var{factor}.")
339    (let ((orig-duration (ly:music-length argument))
340          (multiplier (ly:make-moment 1 1)))
341
342      (for-each
343       (lambda (mus)
344         (if (< 0 (ly:moment-main-denominator (ly:music-length mus)))
345             (begin
346               (ly:music-compress mus multiplier)
347               (set! multiplier (ly:moment-mul factor multiplier)))))
348       (extract-named-music argument '(EventChord NoteEvent RestEvent SkipEvent)))
349      (ly:music-compress
350       argument
351       (ly:moment-div orig-duration (ly:music-length argument)))
352
353      argument))
354
355 footnote =
356 #(define-music-function (parser location mark offset grob-name footnote music)
357    ((markup?) number-pair? (symbol?) markup? (ly:music?))
358    (_i "Make the markup @var{footnote} a footnote on @var{music}.  The
359 footnote is marked with a markup @var{mark} moved by @var{offset} with
360 respect to the marked music.
361
362 If @var{mark} is not given or specified as @var{\\default}, it is
363 replaced by an automatically generated sequence number.  If a symbol
364 @var{grob-name} is specified, then grobs of that type will be marked
365 if they have @var{music} as their ultimate cause; by default all grobs
366 having @var{music} as their @emph{direct} cause will be marked,
367 similar to the way @code{\\tweak} works.
368
369 If @var{music} is given as @code{\\default}, a footnote event
370 affecting @emph{all} grobs matching @var{grob-name} at a given time
371 step is generated.  This may be required for creating footnotes on
372 time signatures, clefs, and other items not cooperating with
373 @code{\\tweak}.
374
375 Like with @code{\\tweak}, if you use a footnote on a following
376 post-event, the @code{\\footnote} command itself needs to be attached
377 to the preceding note or rest as a post-event with @code{-}.")
378    (let ((mus (make-music
379                'FootnoteEvent
380                'X-offset (car offset)
381                'Y-offset (cdr offset)
382                'automatically-numbered (not mark)
383                'text (or mark (make-null-markup))
384                'footnote-text footnote
385                'symbol (or grob-name '()))))
386      (cond (music
387             (set! (ly:music-property music 'tweaks)
388                   (acons (if grob-name
389                              (cons grob-name 'footnote-music)
390                              'footnote-music)
391                          mus
392                          (ly:music-property music 'tweaks)))
393             music)
394            (grob-name mus)
395            (else
396             (ly:input-warning location
397                               (_ "\\footnote requires music or grob-name"))
398             (make-music 'Music)))))
399
400 grace =
401 #(def-grace-function startGraceMusic stopGraceMusic
402    (_i "Insert @var{music} as grace notes."))
403
404 grobdescriptions =
405 #(define-scheme-function (parser location descriptions) (list?)
406    (_i "Create a context modification from @var{descriptions}, a list
407 in the format of @code{all-grob-descriptions}.")
408    (ly:make-context-mod
409     (map (lambda (p)
410            (list 'assign (car p) (list (cdr p))))
411          descriptions)))
412
413 harmonicByFret = #(define-music-function (parser location fret music) (number? ly:music?)
414   (_i "Convert @var{music} into mixed harmonics; the resulting notes resemble
415 harmonics played on a fretted instrument by touching the strings at @var{fret}.")
416   #{
417     \set harmonicDots = ##t
418     \override TabNoteHead #'stencil = #(tab-note-head::print-custom-fret-label (number->string fret))
419     \override NoteHead #'Y-extent = #(ly:make-unpure-pure-container ly:grob::stencil-height
420                                        (lambda (grob start end)
421                                                (ly:grob::stencil-height grob)))
422     \override NoteHead #'stencil = #(lambda (grob) (ly:grob-set-property! grob 'style 'harmonic-mixed)
423                                             (ly:note-head::print grob))
424     $(make-harmonic
425        (calc-harmonic-pitch (fret->pitch (number->string fret)) music))
426     \unset harmonicDots
427     \revert TabNoteHead #'stencil
428     \revert NoteHead #'Y-extent
429     \revert NoteHead #'stencil
430   #})
431
432 harmonicByRatio = #(define-music-function (parser location ratio music) (number? ly:music?)
433     (_i "Convert @var{music} into mixed harmonics; the resulting notes resemble
434 harmonics played on a fretted instrument by touching the strings at the point
435 given through @var{ratio}.")
436   #{
437     \set harmonicDots = ##t
438     \override TabNoteHead #'stencil = #(tab-note-head::print-custom-fret-label (ratio->fret ratio))
439     \override NoteHead #'Y-extent = #(ly:make-unpure-pure-container ly:grob::stencil-height
440                                        (lambda (grob start end)
441                                                (ly:grob::stencil-height grob)))
442     \override NoteHead #'stencil = #(lambda (grob) (ly:grob-set-property! grob 'style 'harmonic-mixed)
443                                             (ly:note-head::print grob))
444     $(make-harmonic
445       (calc-harmonic-pitch (ratio->pitch ratio) music))
446     \unset harmonicDots
447     \revert TabNoteHead #'stencil
448     \revert NoteHead #'Y-extent
449     \revert NoteHead #'stencil
450   #})
451
452 inStaffSegno =
453 #(define-music-function (parser location) ()
454    (_i "Put the segno variant 'varsegno' at this position into the staff,
455 compatible with the repeat command.")
456    (make-music 'ApplyContext
457                'procedure
458                (lambda (ctx)
459                  (let ((score-ctx (ly:context-find ctx 'Score)))
460                    (if (ly:context? score-ctx)
461                      (let ((old-rc (ly:context-property score-ctx 'repeatCommands '())))
462                        (if (eq? (memq 'segno-display old-rc) #f)
463                          (ly:context-set-property! score-ctx 'repeatCommands (cons 'segno-display old-rc)))))))))
464
465 instrumentSwitch =
466 #(define-music-function
467    (parser location name) (string?)
468    (_i "Switch instrument to @var{name}, which must be predefined with
469 @code{\\addInstrumentDefinition}.")
470    (let* ((handle (assoc name instrument-definitions))
471           (instrument-def (if handle (cdr handle) '())))
472
473      (if (not handle)
474          (ly:input-warning location "No such instrument: ~a" name))
475      (context-spec-music
476       (make-music 'SimultaneousMusic
477                   'elements
478                   (map (lambda (kv)
479                          (make-property-set
480                           (car kv)
481                           (cdr kv)))
482                        instrument-def))
483       'Staff)))
484
485
486
487 keepWithTag =
488 #(define-music-function (parser location tag music) (symbol? ly:music?)
489    (_i "Include only elements of @var{music} that are tagged with @var{tag}.")
490    (music-filter
491     (lambda (m)
492       (let* ((tags (ly:music-property m 'tags))
493              (res (memq tag tags)))
494         (or
495          (eq? tags '())
496          res)))
497     music))
498
499 key =
500 #(define-music-function (parser location tonic pitch-alist)
501    ((ly:pitch? '()) (list? '()))
502    (_i "Set key to @var{tonic} and scale @var{pitch-alist}.
503 If both are null, just generate @code{KeyChangeEvent}.")
504    (cond ((null? tonic) (make-music 'KeyChangeEvent))
505          ((null? pitch-alist)
506           (ly:parser-error parser (_ "second argument must be pitch list")
507                            location)
508           (make-music 'SequentialMusic 'void #t))
509          (else
510           (ly:music-transpose
511            (make-music 'KeyChangeEvent
512                 'tonic (ly:make-pitch 0 0 0)
513                 'pitch-alist pitch-alist)
514            tonic))))
515
516 killCues =
517 #(define-music-function (parser location music) (ly:music?)
518    (_i "Remove cue notes from @var{music}.")
519    (music-map
520     (lambda (mus)
521       (if (and (string? (ly:music-property mus 'quoted-music-name))
522                (string=? (ly:music-property mus 'quoted-context-id "") "cue"))
523           (ly:music-property mus 'element)
524           mus))
525     music))
526
527
528
529 label =
530 #(define-music-function (parser location label) (symbol?)
531    (_i "Create @var{label} as a bookmarking label.")
532    (make-music 'EventChord
533                'page-marker #t
534                'page-label label
535                'elements (list (make-music 'LabelEvent
536                                            'page-label label))))
537
538
539 language =
540 #(define-void-function (parser location language) (string?)
541    (_i "Set note names for language @var{language}.")
542    (note-names-language parser language))
543
544 languageSaveAndChange =
545 #(define-void-function (parser location language) (string?)
546   (_i "Store the previous pitchnames alist, and set a new one.")
547   (set! previous-pitchnames pitchnames)
548   (note-names-language parser language))
549
550 languageRestore =
551 #(define-void-function (parser location) ()
552    (_i "Restore a previously-saved pitchnames alist.")
553    (if previous-pitchnames
554        (begin
555         (set! pitchnames previous-pitchnames)
556         (ly:parser-set-note-names parser pitchnames))
557       (ly:input-warning location (_ "No other language was defined previously. Ignoring."))))
558
559
560 makeClusters =
561 #(define-music-function (parser location arg) (ly:music?)
562    (_i "Display chords in @var{arg} as clusters.")
563    (music-map note-to-cluster arg))
564
565 modalInversion =
566 #(define-music-function (parser location around to scale music)
567     (ly:pitch? ly:pitch? ly:music? ly:music?)
568     (_i "Invert @var{music} about @var{around} using @var{scale} and
569 transpose from @var{around} to @var{to}.")
570     (let ((inverter (make-modal-inverter around to scale)))
571       (change-pitches music inverter)
572       music))
573
574 modalTranspose =
575 #(define-music-function (parser location from to scale music)
576     (ly:pitch? ly:pitch? ly:music? ly:music?)
577     (_i "Transpose @var{music} from pitch @var{from} to pitch @var{to}
578 using @var{scale}.")
579     (let ((transposer (make-modal-transposer from to scale)))
580       (change-pitches music transposer)
581       music))
582
583 inversion =
584 #(define-music-function
585    (parser location around to music) (ly:pitch? ly:pitch? ly:music?)
586    (_i "Invert @var{music} about @var{around} and
587 transpose from @var{around} to @var{to}.")
588    (music-invert around to music))
589
590 mark =
591 #(define-music-function
592    (parser location label) ((scheme? '()))
593   "Make the music for the \\mark command."
594   (let* ((set (and (integer? label)
595                    (context-spec-music (make-property-set 'rehearsalMark label)
596                                       'Score)))
597          (ev (make-music 'MarkEvent
598                          'origin location)))
599
600     (if set
601         (make-sequential-music (list set ev))
602         (begin
603           (set! (ly:music-property ev 'label) label)
604           ev))))
605
606 musicMap =
607 #(define-music-function (parser location proc mus) (procedure? ly:music?)
608    (_i "Apply @var{proc} to @var{mus} and all of the music it contains.")
609    (music-map proc mus))
610
611 %% noPageBreak and noPageTurn are music functions (not music indentifiers),
612 %% because music identifiers are not allowed at top-level.
613 noPageBreak =
614 #(define-music-function (location parser) ()
615    (_i "Forbid a page break.  May be used at toplevel (i.e., between scores or
616 markups), or inside a score.")
617    (make-music 'EventChord
618                'page-marker #t
619                'page-break-permission 'forbid
620                'elements (list (make-music 'PageBreakEvent
621                                            'break-permission '()))))
622
623 noPageTurn =
624 #(define-music-function (location parser) ()
625    (_i "Forbid a page turn.  May be used at toplevel (i.e., between scores or
626 markups), or inside a score.")
627    (make-music 'EventChord
628                'page-marker #t
629                'page-turn-permission 'forbid
630                'elements (list (make-music 'PageTurnEvent
631                                            'break-permission '()))))
632
633
634
635 octaveCheck =
636 #(define-music-function (parser location pitch) (ly:pitch?)
637    (_i "Octave check.")
638    (make-music 'RelativeOctaveCheck
639                'pitch pitch))
640
641 once =
642 #(define-music-function (parser location music) (ly:music?)
643    (_i "Set @code{once} to @code{#t} on all layout instruction events in @var{music}.")
644    (music-map
645     (lambda (m)
646       (cond ((music-is-of-type? m 'layout-instruction-event)
647              (set! (ly:music-property m 'once) #t))
648             ((ly:duration? (ly:music-property m 'duration))
649              (ly:music-warning m (_ "Cannot apply \\once to timed music"))))
650       m)
651     music))
652
653 ottava =
654 #(define-music-function (parser location octave) (integer?)
655    (_i "Set the octavation.")
656    (make-music 'OttavaMusic
657                'ottava-number octave))
658
659 overrideTimeSignatureSettings =
660 #(define-music-function
661    (parser location time-signature base-moment beat-structure beam-exceptions)
662    (pair? pair? cheap-list? cheap-list?)
663
664    (_i "Override @code{timeSignatureSettings}
665 for time signatures of @var{time-signature} to have settings
666 of @var{base-moment}, @var{beat-structure}, and @var{beam-exceptions}.")
667
668    ;; TODO -- add warning if largest value of grouping is
669    ;;       greater than time-signature.
670   (let ((setting (make-setting base-moment beat-structure beam-exceptions)))
671     (override-time-signature-setting time-signature setting)))
672
673 overrideProperty =
674 #(define-music-function (parser location name property value)
675    (string? symbol? scheme?)
676
677    (_i "Set @var{property} to @var{value} in all grobs named @var{name}.
678 The @var{name} argument is a string of the form @code{\"Context.GrobName\"}
679 or @code{\"GrobName\"}.")
680
681    (let ((name-components (string-split name #\.))
682          (context-name 'Bottom)
683          (grob-name #f))
684
685      (if (> 2 (length name-components))
686          (set! grob-name (string->symbol (car name-components)))
687          (begin
688            (set! grob-name (string->symbol (list-ref name-components 1)))
689            (set! context-name (string->symbol (list-ref name-components 0)))))
690
691      (make-music 'ApplyOutputEvent
692                  'context-type context-name
693                  'procedure
694                  (lambda (grob orig-context context)
695                    (if (equal?
696                         (cdr (assoc 'name (ly:grob-property grob 'meta)))
697                         grob-name)
698                        (set! (ly:grob-property grob property) value))))))
699
700
701
702 %% pageBreak and pageTurn are music functions (iso music indentifiers),
703 %% because music identifiers are not allowed at top-level.
704 pageBreak =
705 #(define-music-function (location parser) ()
706    (_i "Force a page break.  May be used at toplevel (i.e., between scores or
707 markups), or inside a score.")
708    (make-music 'EventChord
709                'page-marker #t
710                'line-break-permission 'force
711                'page-break-permission 'force
712                'elements (list (make-music 'LineBreakEvent
713                                            'break-permission 'force)
714                                (make-music 'PageBreakEvent
715                                            'break-permission 'force))))
716
717 pageTurn =
718 #(define-music-function (location parser) ()
719    (_i "Force a page turn between two scores or top-level markups.")
720    (make-music 'EventChord
721                'page-marker #t
722                'line-break-permission 'force
723                'page-break-permission 'force
724                'page-turn-permission 'force
725                'elements (list (make-music 'LineBreakEvent
726                                            'break-permission 'force)
727                                (make-music 'PageBreakEvent
728                                            'break-permission 'force)
729                                (make-music 'PageTurnEvent
730                                            'break-permission 'force))))
731
732 parallelMusic =
733 #(define-void-function (parser location voice-ids music) (list? ly:music?)
734    (_i "Define parallel music sequences, separated by '|' (bar check signs),
735 and assign them to the identifiers provided in @var{voice-ids}.
736
737 @var{voice-ids}: a list of music identifiers (symbols containing only letters)
738
739 @var{music}: a music sequence, containing BarChecks as limiting expressions.
740
741 Example:
742
743 @verbatim
744   \\parallelMusic #'(A B C) {
745     c c | d d | e e |
746     d d | e e | f f |
747   }
748 <==>
749   A = { c c | d d | }
750   B = { d d | e e | }
751   C = { e e | f f | }
752 @end verbatim
753 ")
754    (let* ((voices (apply circular-list (make-list (length voice-ids) (list))))
755           (current-voices voices)
756           (current-sequence (list))
757           (original music)
758           (wrapper #f))
759      ;;
760      ;; utilities
761      (define (push-music m)
762        "Push the music expression into the current sequence"
763        (set! current-sequence (cons m current-sequence)))
764      (define (change-voice)
765        "Stores the previously built sequence into the current voice and
766        change to the following voice."
767        (list-set! current-voices 0 (cons (make-music 'SequentialMusic
768                                                      'elements (reverse! current-sequence))
769                                          (car current-voices)))
770        (set! current-sequence (list))
771        (set! current-voices (cdr current-voices)))
772      (define (bar-check? m)
773        "Checks whether m is a bar check."
774        (eq? (ly:music-property m 'name) 'BarCheck))
775      (define (music-origin music)
776        "Recursively search an origin location stored in music."
777        (cond ((null? music) #f)
778              ((not (null? (ly:music-property music 'origin)))
779               (ly:music-property music 'origin))
780              (else (or (music-origin (ly:music-property music 'element))
781                        (let ((origins (remove not (map music-origin
782                                                        (ly:music-property music 'elements)))))
783                          (and (not (null? origins)) (car origins)))))))
784      (while (music-is-of-type? music 'music-wrapper-music)
785             (set! wrapper music)
786             (set! music (ly:music-property wrapper 'element)))
787      (if wrapper
788          (set! (ly:music-property wrapper 'element)
789                                   (make-music 'SequentialMusic
790                                               'origin location))
791          (set! original
792                (make-music 'SequentialMusic
793                            'origin location)))
794      ;;
795      ;; first, split the music and fill in voices
796      ;; We flatten direct layers of SequentialMusic since they are
797      ;; pretty much impossible to avoid when writing music functions.
798      (let rec ((music music))
799        (for-each (lambda (m)
800                    (if (eq? (ly:music-property m 'name) 'SequentialMusic)
801                        (rec m)
802                        (begin
803                          (push-music m)
804                          (if (bar-check? m) (change-voice)))))
805                  (ly:music-property music 'elements)))
806      (if (not (null? current-sequence)) (change-voice))
807      ;; un-circularize `voices' and reorder the voices
808      (set! voices (map-in-order (lambda (dummy seqs)
809                                   (reverse! seqs))
810                                 voice-ids voices))
811      ;;
812      ;; set origin location of each sequence in each voice
813      ;; for better type error tracking
814      (for-each (lambda (voice)
815                  (for-each (lambda (seq)
816                              (set! (ly:music-property seq 'origin)
817                                    (or (music-origin seq) location)))
818                            voice))
819                voices)
820      ;;
821      ;; check sequence length
822      (apply for-each (lambda* (#:rest seqs)
823                               (let ((moment-reference (ly:music-length (car seqs))))
824                                 (for-each (lambda (seq moment)
825                                             (if (not (equal? moment moment-reference))
826                                                 (ly:music-warning seq
827                                                                   "Bars in parallel music don't have the same length")))
828                                           seqs (map-in-order ly:music-length seqs))))
829             voices)
830      ;;
831      ;; bind voice identifiers to the voices
832      (for-each (lambda (voice-id voice)
833             (ly:parser-define! parser voice-id
834                                (let ((v (ly:music-deep-copy original)))
835                                  (set! (ly:music-property
836                                         (car (extract-named-music
837                                               v 'SequentialMusic))
838                                         'elements) voice)
839                                  v)))
840           voice-ids voices)))
841
842 parenthesize =
843 #(define-music-function (parser loc arg) (ly:music?)
844    (_i "Tag @var{arg} to be parenthesized.")
845
846    (if (memq 'event-chord (ly:music-property arg 'types))
847        ;; arg is an EventChord -> set the parenthesize property
848        ;; on all child notes and rests
849        (for-each
850         (lambda (ev)
851           (if (or (memq 'note-event (ly:music-property ev 'types))
852                   (memq 'rest-event (ly:music-property ev 'types)))
853               (set! (ly:music-property ev 'parenthesize) #t)))
854         (ly:music-property arg 'elements))
855        ;; No chord, simply set property for this expression:
856        (set! (ly:music-property arg 'parenthesize) #t))
857    arg)
858
859 partcombine =
860 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
861    (_i "Take the music in @var{part1} and @var{part2} and typeset so
862 that they share a staff.")
863    (make-part-combine-music parser
864                             (list part1 part2) #f))
865
866 partcombineUp =
867 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
868    (_i "Take the music in @var{part1} and @var{part2} and typeset so
869 that they share a staff with stems directed upward.")
870    (make-part-combine-music parser
871                             (list part1 part2) UP))
872
873 partcombineDown =
874 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
875    (_i "Take the music in @var{part1} and @var{part2} and typeset so
876 that they share a staff with stems directed downward.")
877    (make-part-combine-music parser
878                             (list part1 part2) DOWN))
879
880 partcombineForce =
881 #(define-music-function (location parser type once) (symbol-or-boolean? boolean?)
882    (_i "Override the part-combiner.")
883    (make-music 'EventChord
884                'elements (list (make-music 'PartCombineForceEvent
885                                            'forced-type type
886                                            'once once))))
887 partcombineApart = \partcombineForce #'apart ##f
888 partcombineApartOnce = \partcombineForce #'apart ##t
889 partcombineChords = \partcombineForce #'chords ##f
890 partcombineChordsOnce = \partcombineForce #'chords ##t
891 partcombineUnisono = \partcombineForce #'unisono ##f
892 partcombineUnisonoOnce = \partcombineForce #'unisono ##t
893 partcombineSoloI = \partcombineForce #'solo1 ##f
894 partcombineSoloIOnce = \partcombineForce #'solo1 ##t
895 partcombineSoloII = \partcombineForce #'solo2 ##f
896 partcombineSoloIIOnce = \partcombineForce #'solo2 ##t
897 partcombineAutomatic = \partcombineForce ##f ##f
898 partcombineAutomaticOnce = \partcombineForce ##f ##t
899
900 partial =
901 #(define-music-function (parser location dur) (ly:duration?)
902   (_i "Make a partial measure.")
903
904   ;; We use `descend-to-context' here instead of `context-spec-music' to
905   ;; ensure \partial still works if the Timing_translator is moved
906     (descend-to-context
907      (context-spec-music (make-music 'PartialSet
908                                      'origin location
909                                      'partial-duration dur)
910                          'Timing)
911      'Score))
912
913 pitchedTrill =
914 #(define-music-function
915    (parser location main-note secondary-note)
916    (ly:music? ly:music?)
917    (_i "Print a trill with @var{main-note} as the main note of the trill and
918 print @var{secondary-note} as a stemless note head in parentheses.")
919    (let* ((get-notes (lambda (ev-chord)
920                        (extract-named-music ev-chord 'NoteEvent)))
921           (sec-note-events (get-notes secondary-note))
922           (trill-events (extract-named-music main-note 'TrillSpanEvent)))
923      (if (pair? sec-note-events)
924          (begin
925            (let* ((trill-pitch (ly:music-property (car sec-note-events) 'pitch))
926                   (forced (ly:music-property (car sec-note-events) 'force-accidental)))
927
928              (if (ly:pitch? trill-pitch)
929                  (for-each (lambda (m)
930                              (ly:music-set-property! m 'pitch trill-pitch)) trill-events)
931                  (begin
932                    (ly:input-warning location (_ "Second argument of \\pitchedTrill should be single note: "))
933                    (display sec-note-events)))
934
935              (if (eq? forced #t)
936                  (for-each (lambda (m)
937                              (ly:music-set-property! m 'force-accidental forced))
938                            trill-events)))))
939      main-note))
940
941 pushToTag =
942 #(define-music-function (parser location tag more music)
943    (symbol? ly:music? ly:music?)
944    (_i "Add @var{more} to the front of @code{elements} of all music
945 expressions in @var{music} that are tagged with @var{tag}.")
946    (music-map (lambda (m)
947                 (if (memq tag (ly:music-property m 'tags))
948                     (set! (ly:music-property m 'elements)
949                           (cons more (ly:music-property m 'elements))))
950                 m)
951               music))
952
953 quoteDuring =
954 #(define-music-function (parser location what main-music) (string? ly:music?)
955    (_i "Indicate a section of music to be quoted.  @var{what} indicates the name
956 of the quoted voice, as specified in an @code{\\addQuote} command.
957 @var{main-music} is used to indicate the length of music to be quoted;
958 usually contains spacers or multi-measure rests.")
959    (make-music 'QuoteMusic
960                'element main-music
961                'quoted-music-name what))
962
963 relative =
964 #(define-music-function (parser location pitch music)
965    ((ly:pitch? (ly:make-pitch 0 0 0)) ly:music?)
966    (_i "Make @var{music} relative to @var{pitch} (default @code{c'}).")
967    (ly:make-music-relative! music pitch)
968    (make-music 'RelativeOctaveMusic
969                'element music))
970
971 removeWithTag =
972 #(define-music-function (parser location tag music) (symbol? ly:music?)
973    (_i "Remove elements of @var{music} that are tagged with @var{tag}.")
974    (music-filter
975     (lambda (m)
976       (let* ((tags (ly:music-property m 'tags))
977              (res (memq tag tags)))
978         (not res)))
979     music))
980
981 resetRelativeOctave =
982 #(define-music-function (parser location pitch) (ly:pitch?)
983    (_i "Set the octave inside a \\relative section.")
984
985    (make-music 'SequentialMusic
986                'to-relative-callback
987                (lambda (music last-pitch) pitch)))
988
989 retrograde =
990 #(define-music-function (parser location music)
991     (ly:music?)
992     (_i "Return @var{music} in reverse order.")
993     (retrograde-music music))
994
995 revertTimeSignatureSettings =
996 #(define-music-function
997    (parser location time-signature)
998    (pair?)
999
1000    (_i "Revert @code{timeSignatureSettings}
1001 for time signatures of @var{time-signature}.")
1002    (revert-time-signature-setting time-signature))
1003
1004 rightHandFinger =
1005 #(define-event-function (parser location finger) (number-or-string?)
1006    (_i "Apply @var{finger} as a fingering indication.")
1007
1008    (make-music
1009             'StrokeFingerEvent
1010             'origin location
1011             (if (string? finger) 'text 'digit)
1012             finger))
1013
1014 scaleDurations =
1015 #(define-music-function (parser location fraction music)
1016    (fraction? ly:music?)
1017    (_i "Multiply the duration of events in @var{music} by @var{fraction}.")
1018    (ly:music-compress music
1019                       (ly:make-moment (car fraction) (cdr fraction))))
1020
1021 settingsFrom =
1022 #(define-scheme-function (parser location ctx music)
1023    ((symbol?) ly:music?)
1024    (_i "Take the layout instruction events from @var{music}, optionally
1025 restricted to those applying to context type @var{ctx}, and return
1026 a context modification duplicating their effect.")
1027    (let ((mods (ly:make-context-mod)))
1028      (define (musicop m)
1029        (if (music-is-of-type? m 'layout-instruction-event)
1030            (ly:add-context-mod
1031             mods
1032             (case (ly:music-property m 'name)
1033               ((PropertySet)
1034                (list 'assign
1035                      (ly:music-property m 'symbol)
1036                      (ly:music-property m 'value)))
1037               ((PropertyUnset)
1038                (list 'unset
1039                      (ly:music-property m 'symbol)))
1040               ((OverrideProperty)
1041                (cons* 'push
1042                       (ly:music-property m 'symbol)
1043                       (ly:music-property m 'grob-value)
1044                       (ly:music-property m 'grob-property-path)))
1045               ((RevertProperty)
1046                (cons* 'pop
1047                       (ly:music-property m 'symbol)
1048                       (ly:music-property m 'grob-property-path)))))
1049            (case (ly:music-property m 'name)
1050              ((ApplyContext)
1051               (ly:add-context-mod mods
1052                                   (list 'apply
1053                                         (ly:music-property m 'procedure))))
1054              ((ContextSpeccedMusic)
1055               (if (or (not ctx)
1056                       (eq? ctx (ly:music-property m 'context-type)))
1057                   (musicop (ly:music-property m 'element))))
1058              (else
1059               (let ((callback (ly:music-property m 'elements-callback)))
1060                 (if (procedure? callback)
1061                     (for-each musicop (callback m))))))))
1062      (musicop music)
1063      mods))
1064
1065 shape =
1066 #(define-music-function (parser location grob offsets)
1067    (string? list?)
1068    (_i "Offset control-points of @var{grob} by @var{offsets}.  The argument
1069 is a list of number pairs or list of such lists.  Each element of a pair
1070 represents an offset to one of the coordinates of a control-point.")
1071    (define ((shape-curve offsets) grob)
1072      (let* ((orig (ly:grob-original grob))
1073             (siblings (if (ly:spanner? grob)
1074                           (ly:spanner-broken-into orig) '()))
1075             (total-found (length siblings))
1076             (function (assoc-get 'control-points
1077                                  (reverse (ly:grob-basic-properties grob))))
1078             (coords (function grob)))
1079
1080        (define (offset-control-points offsets)
1081          (if (null? offsets)
1082              coords
1083              (map
1084                (lambda (x y) (coord-translate x y))
1085                coords offsets)))
1086
1087        (define (helper sibs offs)
1088          (if (pair? offs)
1089              (if (eq? (car sibs) grob)
1090                  (offset-control-points (car offs))
1091                  (helper (cdr sibs) (cdr offs)))
1092              coords))
1093
1094        ;; we work with lists of lists
1095        (if (or (null? offsets)
1096                (not (list? (car offsets))))
1097            (set! offsets (list offsets)))
1098
1099        (if (>= total-found 2)
1100            (helper siblings offsets)
1101            (offset-control-points (car offsets)))))
1102
1103    #{
1104      \once \override $grob #'control-points = #(shape-curve offsets)
1105    #})
1106
1107 shiftDurations =
1108 #(define-music-function (parser location dur dots arg)
1109    (integer? integer? ly:music?)
1110    (_i "Change the duration of @var{arg} by adding @var{dur} to the
1111 @code{durlog} of @var{arg} and @var{dots} to the @code{dots} of @var{arg}.")
1112
1113    (music-map
1114     (lambda (x)
1115       (shift-one-duration-log x dur dots)) arg))
1116
1117 skip =
1118 #(define-music-function (parser location dur) (ly:duration?)
1119   (_i "Skip forward by @var{dur}.")
1120   (make-music 'SkipMusic
1121               'duration dur))
1122
1123
1124 slashedGrace =
1125 #(def-grace-function startSlashedGraceMusic stopSlashedGraceMusic
1126    (_i "Create slashed graces (slashes through stems, but no slur) from
1127 the following music expression"))
1128
1129 spacingTweaks =
1130 #(define-music-function (parser location parameters) (list?)
1131    (_i "Set the system stretch, by reading the 'system-stretch property of
1132 the `parameters' assoc list.")
1133    #{
1134      \overrideProperty #"Score.NonMusicalPaperColumn"
1135      #'line-break-system-details
1136      #(list (cons 'alignment-extra-space (cdr (assoc 'system-stretch parameters)))
1137              (cons 'system-Y-extent (cdr (assoc 'system-Y-extent parameters))))
1138    #})
1139
1140 styledNoteHeads =
1141 #(define-music-function (parser location style heads music)
1142    (symbol? list-or-symbol? ly:music?)
1143    (_i "Set @var{heads} in @var{music} to @var{style}.")
1144    (style-note-heads heads style music))
1145
1146 tag =
1147 #(define-music-function (parser location tag arg) (symbol? ly:music?)
1148
1149    (_i "Add @var{tag} to the @code{tags} property of @var{arg}.")
1150
1151    (set!
1152     (ly:music-property arg 'tags)
1153     (cons tag
1154           (ly:music-property arg 'tags)))
1155    arg)
1156
1157 time =
1158 #(define-music-function (parser location beat-structure fraction)
1159    ((number-list? '()) fraction?)
1160    (_i "Set @var{fraction} as time signature, with optional
1161 number list @var{beat-structure} before it.")
1162   (make-music 'TimeSignatureMusic
1163               'numerator (car fraction)
1164               'denominator (cdr fraction)
1165               'beat-structure beat-structure))
1166
1167 times =
1168 #(define-music-function (parser location fraction music)
1169    (fraction? ly:music?)
1170    (_i "Scale @var{music} in time by @var{fraction}.")
1171   (make-music 'TimeScaledMusic
1172               'element (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction)))
1173               'numerator (car fraction)
1174               'denominator (cdr fraction)))
1175
1176 transpose =
1177 #(define-music-function
1178    (parser location from to music)
1179    (ly:pitch? ly:pitch? ly:music?)
1180
1181    (_i "Transpose @var{music} from pitch @var{from} to pitch @var{to}.")
1182    (make-music 'TransposedMusic
1183                'element (ly:music-transpose music (ly:pitch-diff to from))))
1184
1185 transposedCueDuring =
1186 #(define-music-function
1187    (parser location what dir pitch main-music)
1188    (string? ly:dir? ly:pitch? ly:music?)
1189
1190    (_i "Insert notes from the part @var{what} into a voice called @code{cue},
1191 using the transposition defined by @var{pitch}.  This happens
1192 simultaneously with @var{main-music}, which is usually a rest.  The
1193 argument @var{dir} determines whether the cue notes should be notated
1194 as a first or second voice.")
1195
1196    (make-music 'QuoteMusic
1197                'element main-music
1198                'quoted-context-type 'Voice
1199                'quoted-context-id "cue"
1200                'quoted-music-name what
1201                'quoted-voice-direction dir
1202                'quoted-transposition pitch))
1203
1204 transposition =
1205 #(define-music-function (parser location pitch) (ly:pitch?)
1206    (_i "Set instrument transposition")
1207
1208    (context-spec-music
1209     (make-property-set 'instrumentTransposition
1210                        (ly:pitch-negate pitch))
1211     'Staff))
1212
1213 tweak =
1214 #(define-music-function (parser location grob prop value music)
1215    ((string?) symbol? scheme? ly:music?)
1216    (_i "Add a tweak to the following @var{music}.
1217 Layout objects created by @var{music} get their property @var{prop}
1218 set to @var{value}.  If @var{grob} is specified, like with
1219 @example
1220 \\tweak Accidental #'color #red cis'
1221 @end example
1222 an indirectly created grob (@samp{Accidental} is caused by
1223 @samp{NoteHead}) can be tweaked; otherwise only directly created grobs
1224 are affected.")
1225    (if (not (object-property prop 'backend-type?))
1226        (begin
1227          (ly:input-warning location (_ "cannot find property type-check for ~a") prop)
1228          (ly:warning (_ "doing assignment anyway"))))
1229    (set!
1230     (ly:music-property music 'tweaks)
1231     (acons (if grob (cons (string->symbol grob) prop) prop)
1232            value
1233            (ly:music-property music 'tweaks)))
1234    music)
1235
1236 unfoldRepeats =
1237 #(define-music-function (parser location music) (ly:music?)
1238    (_i "Force any @code{\\repeat volta}, @code{\\repeat tremolo} or
1239 @code{\\repeat percent} commands in @var{music} to be interpreted
1240 as @code{\\repeat unfold}.")
1241    (unfold-repeats music))
1242
1243 void =
1244 #(define-void-function (parser location arg) (scheme?)
1245    (_i "Accept a scheme argument, return a void expression.
1246 Use this if you want to have a scheme expression evaluated
1247 because of its side-effects, but its value ignored."))
1248
1249 withMusicProperty =
1250 #(define-music-function (parser location sym val music)
1251    (symbol? scheme? ly:music?)
1252    (_i "Set @var{sym} to @var{val} in @var{music}.")
1253
1254    (set! (ly:music-property music sym) val)
1255    music)