lilypond-user
[Top][All Lists]
Advanced

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

Getting the name of the context a scheme function was called from


From: Paul Morris
Subject: Getting the name of the context a scheme function was called from
Date: Fri, 11 Jan 2013 12:18:47 -0500

Hello everyone,  

Here's an obscure scheme function question: how do you get the name of the 
context that a scheme function was called from, from within that function?  
Assuming this is possible... 

I would like to write a scheme function that will do something (override 
horizontal placement of noteheads) when it is called from a given custom staff 
context, but do nothing when it is called from a standard staff context.  Then 
I could put the music, with calls to the function, into a variable, and use 
that variable in both custom and standard staves.  When called from the custom 
staff the function would make changes, but when called from the standard staff 
it would do nothing.

I've tried to do this by using ly:context-name:

  Function: ly:context-name context
  Return the name of context, i.e., for \context Voice = "one" … return the 
symbol Voice.

and maybe getting the "context" argument by using ly:translator-context:

  Function: ly:translator-context trans
  Return the context of the translator object trans.

http://www.lilypond.org/doc/v2.16/Documentation/internals/scheme-functions

But to do that I need the "translator object" (which seems to be an engraver?), 
and I'm not sure how to do that.  Maybe there is a way to get to it from the 
grob?  

Also, it seems like I might be able to get the name of the voice context since 
the note head engraver lives in the voice context, but what I really want is 
the staff context.  So that might be another hurdle.

See example below for where I'm stuck.  

(It now occurs to me that it would probably be better to put the context-lookup 
logic in displaceHeads rather than in #(define ((shift offsets)...)

Thanks for any advice or pointers,
-Paul


%%%% BEGIN TINY EXAMPLE %%%%

\version "2.16.1"
\layout {
  \context {
    \Staff
    \name TwinNoteStaff 
    \alias Staff
    % omitting customizations that go here (\consists, \override etc.)
  }
  \context { \Score \accepts TwinNoteStaff }
  \context { \ChoirStaff \accepts TwinNoteStaff }
  \context { \GrandStaff \accepts TwinNoteStaff }
  \context { \PianoStaff \accepts TwinNoteStaff }
  \context { \StaffGroup \accepts TwinNoteStaff }
}

#(define ((shift offsets) grob)
"Defines how NoteHeads should be moved according to the given list of offsets."
 (let* (
   (trans "how to get a Translator?  from the grob?") 
   (context (ly:translator-context trans))
   (name (ly:context-name context))
   ;; (name "TwinNoteStaff")
   )
   (display name) 
   (newline)
   (if (equal? name "TwinNoteStaff") 
       (display "Yes, TwinNote staff") 
       (display "No, standard staff")
   )
   (newline)
   ))
 

% shift note heads
displaceHeads =
#(define-music-function (parser location offsets) (list?)
"
 Moves the NoteHeads, using (shift offsets)
"
 #{
   \once \override NoteColumn #'before-line-breaking = #(shift offsets)
 #})

music = \relative c' { <c e> \displaceHeads #'(-1 0) <e g> }

<<
\new Staff {
  \music
}
\new TwinNoteStaff {
  \music
}
>>

%%%% END TINY EXAMPLE %%%%


reply via email to

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