lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting default arguments for music function?


From: Ahanu Banerjee
Subject: Re: Setting default arguments for music function?
Date: Tue, 31 Jan 2023 19:32:05 -0500

Fantastic! Thanks again.

On Tue, Jan 31, 2023 at 7:28 PM Jean Abou Samra <jean@abou-samra.fr> wrote:
On 01/02/2023 01:23, Ahanu Banerjee wrote:
> Is it possible to have one of the arguments rely on a property of another argument? 
>
> In my example, I want the default value for "parenColor" to be the same as the color of the "parenItem":
>
> \version "2.24"
> \language "english" 
> altParen = #(define-music-function
>      (parenColor parenSize parenItem)
>      ( (color? "black") (number? -4) ly:music?)
>    #{
>      \tweak Parentheses.font-size #parenSize \tweak Parentheses.color #parenColor \parenthesize #parenItem
>    #})
> { c \altParen -\tweak color "green" \upbow }


Well, the color you want to access isn't a property of parentItem.
parenItem is just a bit of music. Rather, it is a property of the
grob that will eventually be caused by the music parentItem. So,
in the music function, the color is not available. However, what
you can do is writing a callback which runs waay later in the process,
and can access it. Cf.

https://extending-lilypond.readthedocs.io/en/latest/extending/backend.html#understanding-callbacks

\version "2.24.0"

\language "english"

#(define (color-from-host grob)
   (ly:grob-property (ly:grob-object grob 'sticky-host) 'color))

altParen = #(define-music-function
     (parenColor parenSize parenItem)
     ( (color? color-from-host) (number? -4) ly:music?)
   #{
     \tweak Parentheses.font-size #parenSize \tweak Parentheses.color #parenColor \parenthesize #parenItem
   #})
{ c \altParen -\tweak color "green" \upbow }



Actually, because this is a common pattern for grobs like parentheses
which attach to another grob, there is a shortcut:

\version "2.24.0"

\language "english"

altParen = #(define-music-function
     (parenColor parenSize parenItem)
     ( (color? (sticky-grob-interface::inherit-property 'color)) (number? -4) ly:music?)
   #{
     \tweak Parentheses.font-size #parenSize \tweak Parentheses.color #parenColor \parenthesize #parenItem
   #})
{ c \altParen -\tweak color "green" \upbow }



Jean



reply via email to

[Prev in Thread] Current Thread [Next in Thread]