2 quote-iterator.cc -- implement Quote_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "music-sequence.hh"
13 #include "lily-guile.hh"
14 #include "music-wrapper-iterator.hh"
17 class Quote_iterator : public Music_wrapper_iterator
21 Moment vector_moment (int idx) const;
22 Interpretation_context_handle quote_outlet_;
30 SCM transposed_musics_;
32 DECLARE_SCHEME_CALLBACK (constructor, ());
33 bool quote_ok () const;
34 bool accept_music_type (Music*) const;
36 virtual void derived_mark () const;
37 virtual void construct_children ();
38 virtual Moment pending_moment () const;
39 virtual void process (Moment);
40 virtual bool ok () const;
44 Quote_iterator::accept_music_type (Music *mus) const
46 SCM accept = get_outlet()->get_property ("quotedEventTypes");
47 for (SCM s = mus->get_property ("types");
48 scm_is_pair (s); s = scm_cdr (s))
50 if (scm_memq (scm_car (s), accept) != SCM_BOOL_F)
59 Quote_iterator::derived_mark () const
61 scm_gc_mark (transposed_musics_ );
64 Quote_iterator::Quote_iterator ()
66 transposed_musics_ = SCM_EOL;
67 event_vector_ = SCM_EOL;
74 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less)(SCM a, SCM b))
77 int hi = scm_c_vector_length (vec);
82 int cmp = (lo + hi) / 2;
84 SCM when = scm_caar (scm_c_vector_ref (vec, cmp));
85 bool result = (*is_less) (key, when);
98 Quote_iterator::construct_children ()
100 Music_wrapper_iterator::construct_children ();
102 SCM name = get_music ()->get_property ("quoted-context-type");
103 SCM id = get_music ()->get_property ("quoted-context-id");
105 if (scm_is_string (id)
106 && scm_is_symbol (name))
108 Context *cue_context = get_outlet()->find_create_context (name,
109 ly_scm2string (id), SCM_EOL);
110 quote_outlet_.set_context (cue_context);
114 quote_outlet_.set_context (get_outlet ());
118 event_vector_ = get_music ()->get_property ("quoted-events");
121 We have to delay initting event_idx_ , since we have to
122 take starting grace notes into account. Those may offset
130 Quote_iterator::ok () const
133 Music_wrapper_iterator::ok()
138 Quote_iterator::quote_ok () const
140 return (event_idx_ >= 0
141 && scm_is_vector (event_vector_)
142 && event_idx_ <= end_idx_
145 Don't quote the grace notes leading to an unquoted note.
147 && vector_moment (event_idx_).main_part_ < stop_moment_.main_part_
152 Quote_iterator::pending_moment () const
155 infty.set_infinite (1);
158 if (Music_wrapper_iterator::ok())
159 m = m <? Music_wrapper_iterator::pending_moment();
162 In case event_idx_ < 0, we're not initted yet, and the wrapped
163 music expression determines the starting moment.
166 m = m <? vector_moment (event_idx_) - start_moment_;
172 Quote_iterator::vector_moment (int idx) const
174 SCM entry = scm_c_vector_ref (event_vector_, idx);
175 return *unsmob_moment (scm_caar (entry));
179 Quote_iterator::process (Moment m)
181 if (Music_wrapper_iterator::ok())
182 Music_wrapper_iterator::process (m);
184 if (!scm_is_vector (event_vector_))
189 event_idx_ = binsearch_scm_vector (event_vector_,
190 get_outlet ()->now_mom ().smobbed_copy (),
192 start_moment_ = get_outlet ()->now_mom () - music_start_mom();
193 stop_moment_ = start_moment_ + get_music()->get_length ();
195 end_idx_ = binsearch_scm_vector (event_vector_,
196 stop_moment_.smobbed_copy (),
201 while (event_idx_ <= end_idx_)
203 Moment em = vector_moment (event_idx_);
215 SCM entry = scm_c_vector_ref (event_vector_, event_idx_);
216 Pitch * quote_pitch = unsmob_pitch (scm_cdar (entry));
219 The pitch that sounds like central C
221 Pitch * me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
223 for (SCM s = scm_cdr (entry); scm_is_pair (s); s = scm_cdr (s))
225 SCM ev_acc = scm_car (s);
227 Music * mus = unsmob_music (scm_car (ev_acc));
229 programming_error ("need music in quote.");
230 else if (accept_music_type (mus))
232 if (quote_pitch || me_pitch)
240 Pitch diff = pitch_interval (qp, mp);
242 SCM copy = ly_music_deep_copy (mus->self_scm ());
243 mus = unsmob_music (copy);
245 transposed_musics_ = scm_cons (copy, transposed_musics_);
246 mus->transpose (diff);
249 bool b = quote_outlet_.get_outlet ()->try_music (mus);
251 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
259 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);