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;
36 virtual void derived_mark () const;
37 virtual void construct_children ();
38 virtual Moment pending_moment () const;
39 virtual void process (Moment);
40 virtual void do_quit ();
41 virtual bool ok () const;
45 Quote_iterator::do_quit ()
47 Music_wrapper_iterator::do_quit ();
48 quote_outlet_.set_context (0);
52 Quote_iterator::accept_music_type (Music *mus) const
54 SCM accept = get_outlet ()->get_property ("quotedEventTypes");
55 for (SCM s = mus->get_property ("types");
56 scm_is_pair (s); s = scm_cdr (s))
58 if (scm_memq (scm_car (s), accept) != SCM_BOOL_F)
66 Quote_iterator::derived_mark () const
68 Music_wrapper_iterator::derived_mark ();
69 scm_gc_mark (transposed_musics_);
72 Quote_iterator::Quote_iterator ()
74 transposed_musics_ = SCM_EOL;
75 event_vector_ = SCM_EOL;
81 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less) (SCM a, SCM b))
84 int hi = scm_c_vector_length (vec);
89 int cmp = (lo + hi) / 2;
91 SCM when = scm_caar (scm_c_vector_ref (vec, cmp));
92 bool result = (*is_less) (key, when);
104 Quote_iterator::construct_children ()
106 Music_wrapper_iterator::construct_children ();
108 SCM name = get_music ()->get_property ("quoted-context-type");
109 SCM id = get_music ()->get_property ("quoted-context-id");
111 if (scm_is_string (id)
112 && scm_is_symbol (name))
114 Context *cue_context = get_outlet ()->find_create_context (name,
115 ly_scm2string (id), SCM_EOL);
116 quote_outlet_.set_context (cue_context);
120 quote_outlet_.set_context (get_outlet ());
123 event_vector_ = get_music ()->get_property ("quoted-events");
126 We have to delay initting event_idx_ , since we have to
127 take starting grace notes into account. Those may offset
134 Quote_iterator::ok () const
137 Music_wrapper_iterator::ok ()
142 Quote_iterator::quote_ok () const
144 return (event_idx_ >= 0
145 && scm_is_vector (event_vector_)
146 && event_idx_ <= end_idx_
149 Don't quote the grace notes leading to an unquoted note.
151 && vector_moment (event_idx_).main_part_ < stop_moment_.main_part_);
155 Quote_iterator::pending_moment () const
158 infty.set_infinite (1);
161 if (Music_wrapper_iterator::ok ())
162 m = m <? Music_wrapper_iterator::pending_moment ();
165 In case event_idx_ < 0, we're not initted yet, and the wrapped
166 music expression determines the starting moment.
169 m = m <? vector_moment (event_idx_) - start_moment_;
175 Quote_iterator::vector_moment (int idx) const
177 SCM entry = scm_c_vector_ref (event_vector_, idx);
178 return *unsmob_moment (scm_caar (entry));
182 Quote_iterator::process (Moment m)
184 if (Music_wrapper_iterator::ok ())
185 Music_wrapper_iterator::process (m);
187 if (!scm_is_vector (event_vector_))
192 event_idx_ = binsearch_scm_vector (event_vector_,
193 get_outlet ()->now_mom ().smobbed_copy (),
195 start_moment_ = get_outlet ()->now_mom () - music_start_mom ();
196 stop_moment_ = start_moment_ + get_music ()->get_length ();
198 end_idx_ = binsearch_scm_vector (event_vector_,
199 stop_moment_.smobbed_copy (),
204 while (event_idx_ <= end_idx_)
206 Moment em = vector_moment (event_idx_);
218 SCM entry = scm_c_vector_ref (event_vector_, event_idx_);
219 Pitch *quote_pitch = unsmob_pitch (scm_cdar (entry));
222 The pitch that sounds like central C
224 Pitch *me_pitch = unsmob_pitch (get_outlet ()->get_property ("instrumentTransposition"));
226 for (SCM s = scm_cdr (entry); scm_is_pair (s); s = scm_cdr (s))
228 SCM ev_acc = scm_car (s);
230 Music *mus = unsmob_music (scm_car (ev_acc));
232 programming_error ("no music found in quote");
233 else if (accept_music_type (mus))
235 if (quote_pitch || me_pitch)
243 Pitch diff = pitch_interval (qp, mp);
245 SCM copy = ly_music_deep_copy (mus->self_scm ());
246 mus = unsmob_music (copy);
248 transposed_musics_ = scm_cons (copy, transposed_musics_);
249 mus->transpose (diff);
252 bool b = quote_outlet_.get_outlet ()->try_music (mus);
254 mus->origin ()->warning (_f ("in quotation: junking event %s",
263 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);