Extending music syntax - GNU LilyPond
Description: GNU LilyPond

Next: Manipulating music expressions, Previous: Internal music representation, Up: Programmer interfaces for input

#### 11.1.3 Extending music syntax

The syntax of composite music expressions, like `\repeat`, `\transpose`, and `\context` follows the general form of

    \keyword non-music-arguments music-arguments

Such syntax can also be defined as user code. To do this, it is necessary to create a music function. This is a specially marked Scheme function. For example, the music function `\applyMusic` applies a user-defined function to a music expression. Its syntax is

    \applyMusic #func music

A music function is created with `ly:make-music-function`,

    (ly:make-music-function

`\applyMusic` takes a Scheme function and a Music expression as arguments. This is encoded in its parameter list,

    (list procedure? ly:music?)

The function itself takes another argument, an Input location object. That object is used to provide error messages with file names and line numbers. The definition is the second argument of `ly:make-music-function`. The body simply calls the function

    (lambda (where func music)
     (func music))

The above Scheme code only defines the functionality. The tag `\applyMusic` is selected by defining

    applyMusic = #(ly:make-music-function
                    (list procedure? ly:music?)
                    (lambda (parser location func music)
                      (func music)))

A `define-music-function` macro is introduced on top of `ly:make-music-function` to ease the definition of music functions:

    applyMusic = #(define-music-function (parser location func music)
                    (procedure? ly:music?)
                    (func music))

Examples of the use of `\applyMusic` are in the next section.

##### See also

`ly/music-functions-init.ly`.

This page is for LilyPond-2.8.8 (stable-branch).

Other languages: English.
Using automatic language selection.