Thank you Aaron and Jean, your code works!

But I have some trivial questions.
If I write:

\displayLilyMusic \chordmode {c}


I get:

{ < c' e' g' >4 }


It looks a good "compound music expression".
But Aaron code and Jean code behave differently. Have a look here
This does not work.

\version "2.23.2"
#(define (extract music)
   (first (ly:music-property music 'elements)))

{ #(first-element #{ \chordmode {c} #}) }


And this work as expected:

\version "2.23.2"
extract =
#(define-music-function
   (which what music)
   (symbol? symbol? ly:music?)
   (let ((extracted (extract-typed-music music what)))
    (case which
     ((first) (first extracted))
     ((last) (last extracted))
     ((all all-as-sequence) (make-sequential-music extracted))
     ((all-as-chord) (make-event-chord extracted))
     (else (ly:input-warning (*location*)
            "\\extract does not understand '~s'" which)
           music))))

{ \extract first event-chord \chordmode {c} }


why?
Sorry if the question is very trivial.
g.

On Fri, 23 Apr 2021 at 11:29, Aaron Hill <lilyp...@hillvisions.com> wrote:

> On 2021-04-23 1:58 am, Jean Abou Samra wrote:
> > If you have it at hand in Scheme:
> >
> > \version "2.23.
> >
> > #(define (first-element music)
> >    (first (ly:music-property music 'elements)))
> >
> > { #(first-element #{ { c'1 } #}) }
> >
> > It would be helpful to understand your use
> > case; this sounds like you may not necessarily
> > need to unpack a sequential music expression.
>
> In my usual fashion, here's an over-engineered option showing off some
> of the built-in functions in an easily extensible pattern:
>
> %%%%
> \version "2.22.0"
>
> extract =
> #(define-music-function
>    (which what music)
>    (symbol? symbol? ly:music?)
>    (let ((extracted (extract-typed-music music what)))
>     (case which
>      ((first) (first extracted))
>      ((last) (last extracted))
>      ((all all-as-sequence) (make-sequential-music extracted))
>      ((all-as-chord) (make-event-chord extracted))
>      (else (ly:input-warning (*location*)
>             "\\extract does not understand '~s'" which)
>            music))))
>
> foo = { c <e g> }
>
> \fixed c' {
>    \extract first event-chord \foo
>    \extract last note-event \foo
>    \extract all note-event \foo
>    \extract all-as-chord note-event \foo
> }
> %%%%
>
> -- Aaron Hill

Reply via email to