2.3.4 Intermediate substitution functions

Intermediate substitution functions involve a mix of Scheme code and LilyPond code in the music expression to be returned.

Some \override commands require an argument consisting of a pair of numbers (called a cons cell in Scheme).

The pair can be directly passed into the music function, using a pair? variable:

manualBeam =
#(define-music-function
     (beg-end)
     (pair?)
   #{
     \once \override Beam.positions = #beg-end
   #})

\relative c' {
  \manualBeam #'(3 . 6) c8 d e f
}

Alternatively, the numbers making up the pair can be passed as separate arguments, and the Scheme code used to create the pair can be included in the music expression:

manualBeam =
#(define-music-function
     (beg end)
     (number? number?)
   #{
     \once \override Beam.positions = #(cons beg end)
   #})

\relative c' {
  \manualBeam #3 #6 c8 d e f
}

[image of music]

Properties are maintained conceptually using one stack per property per grob per context. Music functions may need to override one or several properties for the duration of the function, restoring them to their previous value before exiting. However, normal overrides pop and discard the top of the current property stack before pushing to it, so the previous value of the property is lost when it is overridden. When the previous value must be preserved, prefix the \override command with \temporary, like this:

\temporary \override …

The use of \temporary causes the (usually set) pop-first property in the override to be cleared, so the previous value is not popped off the property stack before pushing the new value onto it. When a subsequent \revert pops off the temporarily overriden value, the previous value will re-emerge.

In other words, calling \temporary \override and \revert in succession on the same property will have a net effect of zero. Similarly, pairing \temporary and \undo on the same music containing overrides will have a net effect of zero.

Here is an example of a music function which makes use of this. The use of \temporary ensures the values of the cross-staff and style properties are restored on exit to whatever values they had when the crossStaff function was called. Without \temporary the default values would have been set on exit.

crossStaff =
#(define-music-function (notes) (ly:music?)
  (_i "Create cross-staff stems")
  #{
  \temporary \override Stem.cross-staff = #cross-staff-connect
  \temporary \override Flag.style = #'no-flag
  #notes
  \revert Stem.cross-staff
  \revert Flag.style
#})

LilyPond — Extending v2.23.82 (development-branch).