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



8.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 argument. This is encoded in its first argument,

(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 is function 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 (location func music)
                  (func music)))

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

applymusic = #(def-music-function (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.4.5 (stable-branch).

Report errors to <bug-lilypond@gnu.org>.

Other languages: English.
Using automatic language selection.