Next: , Up: Programmer interfaces


12.2.1 Input variables and Scheme

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

traLaLa = { c'4 d'4 }

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

traLaLa = { c'4 d'4 }
\layout { traLaLa = 1.0 }

In effect, each input file is a scope, and all \header, \midi, and \layout 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 = { c'4 d'4 }

is internally converted to a Scheme definition

(define traLaLa Scheme value of this music expression)

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 block by means of a second variable twice:

traLaLa = { c'4 d'4 }

%% dummy action to deal with parser lookahead
#(display "this needs to be here, sorry!")

#(define newLa (map ly:music-deep-copy
  (list traLaLa traLaLa)))
#(define twice
  (make-sequential-music newLa))

{ \twice }

[image of music]

Due to parser lookahead

In this example, the assignment happens after parser has verified that nothing interesting happens after traLaLa = { ... }. Without the dummy statement in the above example, the newLa definition is executed before traLaLa is defined, leading to a syntax error.

The above example shows how to ‘export’ music expressions 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

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

Scheme code is evaluated as soon as the parser encounters it. To define some Scheme code in a macro (to be called later), use Void functions or

#(define (nopc)
  (ly:set-option 'point-and-click #f))

...
#(nopc)
{ c'4 }

Bugs

Mixing Scheme and LilyPond identifiers is not possible with the --safe option.


Next: , Up: Programmer interfaces

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

Report errors to http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs.