2 quote-iterator.cc -- implement Quote_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "music-sequence.hh"
13 #include "lily-guile.hh"
14 #include "music-iterator.hh"
20 class Quote_iterator : public Music_iterator
30 DECLARE_SCHEME_CALLBACK(constructor, ());
33 virtual void construct_children ();
34 virtual Moment pending_moment () const;
35 virtual void process (Moment);
36 virtual bool ok () const;
39 Quote_iterator::Quote_iterator ()
41 event_vector_ = SCM_EOL;
47 moment_less (SCM a, SCM b)
49 return *unsmob_moment (a) < *unsmob_moment (b);
54 binsearch_scm_vector (SCM vec, SCM key, bool (*is_less)(SCM a,SCM b))
59 hi = SCM_VECTOR_LENGTH (vec);
64 int cmp = (lo + hi) / 2;
66 SCM when = gh_car (SCM_VECTOR_REF(vec, cmp));
67 bool result = (*is_less) (key, when);
80 Quote_iterator::construct_children ()
82 SCM dur = get_music ()->get_mus_property ("duration");
83 if (!unsmob_duration (dur))
86 set_translator (get_outlet ()->get_default_interpreter ());
88 Moment now = get_outlet ()->now_mom ();
89 Moment stop = now + unsmob_duration (dur)->get_length ();
92 event_vector_ = get_music ()->get_mus_property ("quoted-events");
94 if (scm_vector_p (event_vector_) == SCM_BOOL_T)
96 event_idx_ = binsearch_scm_vector (event_vector_, now.smobbed_copy (), &moment_less);
97 end_idx_ = binsearch_scm_vector (event_vector_, stop.smobbed_copy (), &moment_less);
103 Quote_iterator::ok () const
105 return (event_idx_ <= end_idx_);
110 Quote_iterator::pending_moment () const
112 SCM entry = SCM_VECTOR_REF (event_vector_, event_idx_);
113 return *unsmob_moment (gh_car (entry)) - start_moment_;
118 Quote_iterator::process (Moment m)
123 while (event_idx_ < end_idx_)
125 entry = SCM_VECTOR_REF (event_vector_, event_idx_);
127 Moment em = *unsmob_moment (gh_car (entry));
138 if (gh_pair_p (entry))
140 for (SCM s = gh_cdr (entry); gh_pair_p (s); s = gh_cdr (s))
142 SCM ev_acc = gh_car (s);
145 Music * mus = unsmob_music (gh_car (ev_acc));
148 bool b = get_outlet ()->try_music (mus);
151 mus->origin ()->warning (_f ("In quotation: junking event %s", mus->name()));
154 programming_error ("need music in quote.");
160 IMPLEMENT_CTOR_CALLBACK (Quote_iterator);