Paolo Prete <> writes:

> Hello,
> I try to re-post my previous question in a more generic way: is it
> possible to define local variables in a scheme function and use them
> in a lilypond code block?Something like (pseudo-code: it doesn't
> compile):
> func = #(define-music-function (parser location note) (ly:music)  
>  ((mypitch (ly:music-property note 'pitch '()))    (myduration
> (ly:music-property note 'duration '())))#{ ....  $mypitch $myduration
> .....  #}  )
> If not, is there a work-around to obtain that?

Is there a particular reason you don't just try it out?

Let me quote from the documentation (Extending LilyPond Guide):

2.1 LilyPond code blocks

Creating music expressions in Scheme can be tedious, as they are heavily
nested and the resulting Scheme code is large.  For some simple tasks
this can be avoided by using LilyPond code blocks, which enable common
LilyPond syntax to be used within Scheme.

   LilyPond code blocks look like

       #{ LILYPOND CODE #}

   Here is a trivial example:

     ritpp = #(define-event-function () ()
       #{ ^"rit." \pp #}
     { c'4 e'4\ritpp g'2 }
     [image src="lilypond/f3/lily-a1c2820d.png" alt="[image of music]" 
text="image of music"]

   LilyPond code blocks can be used anywhere where you can write Scheme
code.  The Scheme reader actually is changed for accommodating LilyPond
code blocks and can deal with embedded Scheme expressions starting with
‘$’ and ‘#’.

   The reader extracts the LilyPond code block and generates a runtime
call to the LilyPond parser to interpret the LilyPond code.  Scheme
expressions embedded in the LilyPond code are evaluated in the lexical
environment of the LilyPond code block, so all local variables and
function parameters available at the point the LilyPond code block is
written may be accessed.  Variables defined in other Scheme modules,
like the modules containing ‘\header’ and ‘\layout’ blocks, are not
accessible as Scheme variables, i.e.  prefixed with ‘#’, but they are
accessible as LilyPond variables, i.e.  prefixed with ‘\’.

David Kastrup

lilypond-user mailing list

Reply via email to