Michael Ellis <michael.f.el...@gmail.com> writes:

> What's the right way to define a function that assigns string values
> to LilyPond variables? I'm trying to create a function in an include
> file that allows me to assign different values to variables used as
> midi instrument specifiers.
> %--------------------------------------------------------
> setMainCueClapInstruments=
> #(define-music-function (p l main cue clap) (string? string? string?)
>    #{
>        mainInstrument = #$main
>        cueInstrument  = #$cue
>        clapInstrument = #$clap
>    #}
>    (make-music 'SequentialMusic 'void #t))
> \setMainCueClapInstruments #"cello" #"acoustic grand" #"woodblock"
> %--------------------------------------------------------
> But the parser throws errors, starting with the following ...
> Parsing...
> <string>:2:7: error: syntax error, unexpected STRING
>
>        mainInstrument = #lilyvartmpbg

A music function can only do things you could also do inside of music.
Assignments are _not_ permitted in music.  Music is something you can
put into music variables and shuffle around.  Assignments are acted on
immediately.

What you _can_ put into music are property overrides and sets: those
happen at the time they are replayed, and are wrapped into music events.

You can, of course, just use ly:parser-define! inside of your function
to manipulate variables.  But they will get changed at the _location_
you call the music function, not at the _time_ the music expression is
executed.  If you put setMainCueClapInstruments into a music variable
then, the effect will occur at the time you define the music variable,
not at the time you use it.

If you want the latter, you need to go through properties.  Or even
\ApplyToContext, but that's really obscure.

-- 
David Kastrup


_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to