[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Setting default arguments for music function?
From: |
Jean Abou Samra |
Subject: |
Re: Setting default arguments for music function? |
Date: |
Wed, 1 Feb 2023 01:28:45 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 |
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
OpenPGP_signature
Description: OpenPGP digital signature