2.7 Callback functions

Properties (like thickness, direction, etc.) can be set at fixed values with \override, e.g.

\override Stem.thickness = #2.0

Properties can also be set to a Scheme procedure:

\override Stem.thickness = #(lambda (grob)
    (if (= UP (ly:grob-property grob 'direction))
\relative { c'' b a g b a g b }

[image of music]

In this case, the procedure is executed as soon as the value of the property is requested during the formatting process.

Most of the typesetting engine is driven by such callbacks. Properties that typically use callbacks include


The printing routine, that constructs a drawing for the symbol


The routine that sets the horizontal position


The routine that computes the width of an object

The procedure always takes a single argument, being the grob.

That procedure may access the usual value of the property, by first calling the function that is the usual callback for that property, which can by found in the Internals Reference or the file ’define-grobs.scm’:

\relative {
  \override Flag.X-offset = #(lambda (flag)
    (let ((default (ly:flag::calc-x-offset flag)))
      (* default 4.0)))
  c''4. d8 a4. g8

It is also possible to get the value of the existing default by employing the function grob-transformer:

\relative {
  \override Flag.X-offset = #(grob-transformer 'X-offset
    (lambda (flag default) (* default 4.0)))
  c''4. d8 a4. g8

[image of music]

From within a callback, the easiest method for evaluating a markup is to use grob-interpret-markup. For example:

my-callback = #(lambda (grob)
                 (grob-interpret-markup grob (markup "foo")))

Extending LilyPond v2.25.16 (development-branch).