(process_acknowledged_grobs):
[lilypond.git] / lily / unfolded-repeat-iterator.cc
blob27141d0e27f5a4b56cdca686eb76919a9924c10a
1 /*
2 unfolded-repeat-iterator.cc -- implement Unfolded_repeat_iterator, Volta_repeat_iterator
4 source file of the GNU LilyPond music typesetter
7 (c) 2002--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
11 #include "music.hh"
12 #include "sequential-iterator.hh"
13 #include "translator-group.hh"
15 class Unfolded_repeat_iterator : public Sequential_iterator
17 public:
18 DECLARE_SCHEME_CALLBACK(constructor, ());
19 VIRTUAL_COPY_CONS (Music_iterator);
20 protected:
21 virtual SCM get_music_list () const;
25 SCM
26 Unfolded_repeat_iterator::get_music_list () const
28 SCM l = SCM_EOL;
29 SCM *tail = &l;
31 SCM body = get_music ()->get_mus_property ("element");
32 SCM alts = get_music ()->get_mus_property ("elements");
33 int alt_count = scm_ilength (alts);
34 int rep_count = gh_scm2int (get_music ()->get_mus_property ("repeat-count"));
36 for (int i = 0; i < rep_count; i++)
38 if (unsmob_music (body))
39 *tail = gh_cons (body, SCM_EOL) ;
41 tail = SCM_CDRLOC (*tail);
43 if (alt_count)
45 *tail = gh_cons (gh_car (alts), SCM_EOL);
46 tail = SCM_CDRLOC (*tail);
47 if (i >= rep_count - alt_count)
49 alts = gh_cdr (alts);
53 return l;
56 class Volta_repeat_iterator : public Sequential_iterator
58 public:
59 DECLARE_SCHEME_CALLBACK(constructor, ());
60 VIRTUAL_COPY_CONS (Music_iterator);
61 Volta_repeat_iterator();
63 void add_repeat_command (SCM);
64 protected:
65 virtual SCM get_music_list () const;
66 virtual void next_element (bool);
67 virtual void construct_children();
68 virtual void process (Moment);
70 bool first_time_;
71 int alt_count_;
72 int rep_count_;
73 int done_count_;
77 Volta_repeat_iterator::Volta_repeat_iterator()
79 done_count_ = alt_count_ = rep_count_= 0;
80 first_time_ = true;
83 SCM
84 Volta_repeat_iterator::get_music_list()const
86 return gh_cons (get_music ()->get_mus_property ("element"),
87 get_music ()->get_mus_property ("elements"));
90 void
91 Volta_repeat_iterator::construct_children ()
93 Sequential_iterator::construct_children();
95 SCM alts = get_music ()->get_mus_property ("elements");
97 alt_count_ = scm_ilength (alts);
98 rep_count_ = gh_scm2int (get_music ()->get_mus_property ("repeat-count"));
99 done_count_ = 0;
104 TODO: add source information for debugging
106 void
107 Volta_repeat_iterator::add_repeat_command (SCM what)
109 SCM reps = ly_symbol2scm ("repeatCommands");
110 SCM current_reps = report_to ()->internal_get_property (reps);
112 Translator_group * where = report_to ()->where_defined (reps);
113 if (where
114 && current_reps == SCM_EOL || gh_pair_p (current_reps))
116 current_reps = gh_cons (what, current_reps);
117 where->internal_set_property (reps, current_reps);
122 void
123 Volta_repeat_iterator::next_element (bool side_effect)
125 done_count_ ++;
127 Sequential_iterator::next_element (side_effect);
129 if (side_effect)
131 if (alt_count_)
133 String repstr = to_string (rep_count_ - alt_count_ + done_count_) + ".";
134 if (done_count_ > 1)
136 add_repeat_command (scm_list_n (ly_symbol2scm ("volta"), SCM_BOOL_F, SCM_UNDEFINED));
138 if (done_count_ - 1 < alt_count_)
139 add_repeat_command (ly_symbol2scm ("end-repeat"));
144 if (done_count_ == 1 && alt_count_ < rep_count_)
146 repstr = "1.--" + to_string (rep_count_ - alt_count_ + done_count_) + ".";
149 if (done_count_ <= alt_count_)
150 add_repeat_command (scm_list_n (ly_symbol2scm ("volta"),
151 scm_makfrom0str (repstr.to_str0 ()), SCM_UNDEFINED));
153 else
155 add_repeat_command (ly_symbol2scm ("end-repeat"));
161 void
162 Volta_repeat_iterator::process (Moment m)
164 if (first_time_)
166 add_repeat_command (ly_symbol2scm ("start-repeat"));
167 first_time_ = false;
169 Sequential_iterator::process(m);
173 IMPLEMENT_CTOR_CALLBACK(Volta_repeat_iterator);
174 IMPLEMENT_CTOR_CALLBACK(Unfolded_repeat_iterator);