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>
11 #include "music-sequence.hh"
12 #include "lily-guile.hh"
13 #include "music-wrapper-iterator.hh"
16 class Quote_iterator : public Music_wrapper_iterator
20 Moment vector_moment (int idx) const;
21 Interpretation_context_handle quote_outlet_;
29 SCM transposed_musics_;
31 DECLARE_SCHEME_CALLBACK (constructor, ());
32 bool quote_ok () const;
33 bool accept_music_type (Music *) const;
35 virtual void derived_mark () const;
36 virtual void construct_children ();
37 virtual Moment pending_moment () const;
38 virtual void process (Moment);
39 virtual bool ok () const;
43 Quote_iterator::accept_music_type (Music *mus) const
45 SCM accept = get_outlet ()->get_property ("quotedEventTypes");
46 for (SCM s = mus->get_property ("types");
47 scm_is_pair (s); s = scm_cdr (s))
49 if (scm_memq (scm_car (s), accept) != SCM_BOOL_F)
57 Quote_iterator::derived_mark () const
59 Music_wrapper_iterator::derived_mark ();
60 scm_gc_mark (transposed_musics_);
63 Quote_iterator::Quote_iterator ()
65 transposed_musics_ = SCM_EOL;
66 event_vector_ = SCM_EOL;
72 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less) (SCM a, SCM b))
75 int hi = scm_c_vector_length (vec);
80 int cmp = (lo + hi) / 2;
82 SCM when = scm_caar (scm_c_vector_ref (vec, cmp));
83 bool result = (*is_less) (key, when);
95 Quote_iterator::construct_children ()
97 Music_wrapper_iterator::construct_children ();
99 SCM name = get_music ()->get_property ("quoted-context-type");
100 SCM id = get_music ()->get_property ("quoted-context-id");
102 if (scm_is_string (id)
103 && scm_is_symbol (name))
105 Context *cue_context = get_outlet ()->find_create_context (name,
106 ly_scm2string (id), SCM_EOL);
107 quote_outlet_.set_context (cue_context);
111 quote_outlet_.set_context (get_outlet ());
114 event_vector_ = get_music ()->get_property ("quoted-events");
117 We have to delay initting event_idx_ , since we have to
118 take starting grace notes into account. Those may offset
125 Quote_iterator::ok () const
128 Music_wrapper_iterator::ok ()
133 Quote_iterator::quote_ok () const
135 return (event_idx_ >= 0
136 && scm_is_vector (event_vector_)
137 && event_idx_ <= end_idx_
140 Don't quote the grace notes leading to an unquoted note.
142 && vector_moment (event_idx_).main_part_ < stop_moment_.main_part_);
146 Quote_iterator::pending_moment () const
149 infty.set_infinite (1);
152 if (Music_wrapper_iterator::ok ())
153 m = m <? Music_wrapper_iterator::pending_moment ();
156 In case event_idx_ < 0, we're not initted yet, and the wrapped
157 music expression determines the starting moment.
160 m = m <? vector_moment (event_idx_) - start_moment_;
166 Quote_iterator::vector_moment (int idx) const
168 SCM entry = scm_c_vector_ref (event_vector_, idx);
169 return *unsmob_moment (scm_caar (entry));
173 Quote_iterator::process (Moment m)
175 if (Music_wrapper_iterator::ok ())
176 Music_wrapper_iterator::process (m);
178 if (!scm_is_vector (event_vector_))
183 event_idx_ = binsearch_scm_vector (event_vector_,
184 get_outlet ()->now_mom ().smobbed_copy (),
186 start_moment_ = get_outlet ()->now_mom () - music_start_mom ();
187 stop_moment_ = start_moment_ + get_music ()->get_length ();
189 end_idx_ = binsearch_scm_vector (event_vector_,
190 stop_moment_.smobbed_copy (),
195 while (event_idx_ <= end_idx_)
197 Moment em = vector_moment (event_idx_);
209 SCM entry = scm_c_vector_ref (event_vector_, event_idx_);
210 Pitch *quote_pitch = unsmob_pitch (scm_cdar (entry));
213 The pitch that sounds like central C
215 Pitch *me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
217 for (SCM s = scm_cdr (entry); scm_is_pair (s); s = scm_cdr (s))
219 SCM ev_acc = scm_car (s);
221 Music *mus = unsmob_music (scm_car (ev_acc));
223 programming_error ("need music in quote.");
224 else if (accept_music_type (mus))
226 if (quote_pitch || me_pitch)
234 Pitch diff = pitch_interval (qp, mp);
236 SCM copy = ly_music_deep_copy (mus->self_scm ());
237 mus = unsmob_music (copy);
239 transposed_musics_ = scm_cons (copy, transposed_musics_);
240 mus->transpose (diff);
243 bool b = quote_outlet_.get_outlet ()->try_music (mus);
245 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name ()));
253 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);