On Thu, Nov 3, 2011 at 12:04 PM, David Kastrup <d...@gnu.org> wrote: > 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. >
Thanks David, I appreciate the detailed explanation but I'm still struggling with how to do what, in most programming languages, is a fairly straightforward task: define a variable in an outer scope and temporarily change its value in an inner scope. Over the past year or so, I've built up an include file with lots of handy functions for transcribing individual choral parts from printed scores. As a particular example, I have a function named cueNotes that prints notes inline in a teeny font and different color and specifies an alternate midi instrument and restores the defaults before exiting. Up until now, my preferences have been hard-coded, e.g. cueNotes= #(define-music-function (p l music) (ly:music?) "for printing cue notes in teeny font in color" #{ \set midiInstrument = #"acoustic grand" \override Accidental #'color = #(x11-color 'maroon) \override Beam #'color = #(x11-color 'maroon) \override NoteHead #'color = #(x11-color 'maroon) \override Rest #'color = #(x11-color 'maroon) \override Slur #'color = #(x11-color 'maroon) \override Stem #'color = #(x11-color 'maroon) \override Tie #'color = #(x11-color 'maroon) \teeny $music \normalsize \revert Accidental #'color \revert Beam #'color \revert NoteHead #'color \revert Rest #'color \revert Slur #'color \revert Stem #'color \revert Tie #'color \set midiInstrument = #"cello" #}) But recently, other singers in my choral groups are starting to use LilyPond and want to make use of my templates and include files but with the ability to easily change instrument and color preferences without having to hack the files. So, with that as background, let me re-ask the question in a different form: What's the best way to support changing a set of default values? _______________________________________________ lilypond-user mailing list lilypond-user@gnu.org https://lists.gnu.org/mailman/listinfo/lilypond-user