Next: , Up: Programmer interfaces for input



C.1.1 Input variables and Scheme

The input format supports the notion of variable: in the following example, a music expression is assigned to a variable with the name traLaLa.

       traLaLa = \notes { c'4 d'4 }

There is also a form of scoping: in the following example, the \paper block also contains a traLaLa variable, which is independent of the outer \traLaLa.

       traLaLa = \notes { c'4 d'4 }
       \paper { traLaLa = 1.0 }

In effect, each input file is a scope, and all \header, \midi and \paper blocks are scopes nested inside that toplevel scope.

Both variables and scoping are implemented in the GUILE module system. An anonymous Scheme module is attached to each scope. An assignment of the form

      traLaLa = \notes { c'4 d'4 }

is internally converted to a Scheme definition

      (define traLaLa Scheme value of “\notes ... )

This means that input variables and Scheme variables may be freely mixed. In the following example, a music fragment is stored in the variable traLaLa, and duplicated using Scheme. The result is imported in a \score by means of a second variable twice:

       traLaLa = \notes { c'4 d'4 }
     
       #(define newLa (map ly:music-deep-copy
         (list traLaLa traLaLa)))
       #(define twice
         (make-sequential-music newLa))
     
       \score { \twice }

In the above example, music expressions can be `exported' from the input to the Scheme interpreter. The opposite is also possible. By wrapping a Scheme value in the function ly:export, a Scheme value is interpreted as if it were entered in LilyPond syntax: instead of defining \twice, the example above could also have been written as

       ...
       \score { #(ly:export (make-sequential-music newLa)) }

Read comments on this page, or add one.

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

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