Fix flag snippet in NEWS.tely.
[lilypond.git] / lily / global-context.cc
blobf31a1283afa7fe1e4862736bf07630d503ff4172
1 /*
2 global-context.cc -- implement Global_context
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "global-context.hh"
11 #include <cstdio>
12 using namespace std;
14 #include "context-def.hh"
15 #include "dispatcher.hh"
16 #include "international.hh"
17 #include "music-iterator.hh"
18 #include "music.hh"
19 #include "output-def.hh"
20 #include "warn.hh"
22 Global_context::Global_context (Output_def *o)
23 : Context ()
25 output_def_ = o;
26 definition_ = find_context_def (o, ly_symbol2scm ("Global"));
28 now_mom_.set_infinite (-1);
29 prev_mom_.set_infinite (-1);
31 /* We only need the most basic stuff to bootstrap the context tree */
32 event_source ()->add_listener (GET_LISTENER (create_context_from_event),
33 ly_symbol2scm ("CreateContext"));
34 event_source ()->add_listener (GET_LISTENER (prepare),
35 ly_symbol2scm ("Prepare"));
36 events_below ()->register_as_listener (event_source_);
38 Context_def *globaldef = unsmob_context_def (definition_);
39 if (!globaldef)
40 programming_error ("no `Global' context found");
41 else
42 globaldef->apply_default_property_operations (this);
43 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
46 Output_def *
47 Global_context::get_output_def () const
49 return output_def_;
52 void
53 Global_context::add_moment_to_process (Moment m)
55 if (m < now_mom_)
56 programming_error ("trying to freeze in time");
58 for (vsize i = 0; i < extra_mom_pq_.size (); i++)
59 if (extra_mom_pq_[i] == m)
60 return;
61 extra_mom_pq_.insert (m);
64 Moment
65 Global_context::sneaky_insert_extra_moment (Moment w)
67 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
68 w = extra_mom_pq_.get ();
69 return w;
72 int
73 Global_context::get_moments_left () const
75 return extra_mom_pq_.size ();
78 IMPLEMENT_LISTENER (Global_context, prepare);
79 void
80 Global_context::prepare (SCM sev)
82 Stream_event *ev = unsmob_stream_event (sev);
83 Moment *mom = unsmob_moment (ev->get_property ("moment"));
85 assert (mom);
87 if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
88 prev_mom_ = *mom;
89 else
90 prev_mom_ = now_mom_;
91 now_mom_ = *mom;
94 Moment
95 Global_context::now_mom () const
97 return now_mom_;
100 Context *
101 Global_context::get_score_context () const
103 return (scm_is_pair (context_list_))
104 ? unsmob_context (scm_car (context_list_))
105 : 0;
109 Global_context::get_output ()
111 Context * c = get_score_context ();
112 if (c)
113 return c->get_property ("output");
114 else
115 return SCM_EOL;
118 void
119 Global_context::run_iterator_on_me (Music_iterator *iter)
121 prev_mom_.set_infinite (-1);
122 now_mom_.set_infinite (-1);
123 Moment final_mom = iter->get_music ()->get_length ();
125 bool first = true;
126 while (iter->ok () || get_moments_left ())
128 Moment w;
129 w.set_infinite (1);
130 if (iter->ok ())
131 w = iter->pending_moment ();
133 w = sneaky_insert_extra_moment (w);
134 if (w.main_part_.is_infinity () || w > final_mom)
135 break;
137 if (w == prev_mom_)
139 programming_error ("Moment is not increasing. Aborting interpretation.");
140 break ;
144 if (first)
147 Need this to get grace notes at start of a piece correct.
149 first = false;
150 set_property ("measurePosition", w.smobbed_copy ());
153 send_stream_event (this, "Prepare", 0,
154 ly_symbol2scm ("moment"), w.smobbed_copy ());
156 if (iter->ok ())
157 iter->process (w);
159 send_stream_event (this, "OneTimeStep", 0, 0);
160 apply_finalizations ();
161 check_removal ();
165 void
166 Global_context::apply_finalizations ()
168 SCM lst = get_property ("finalizations");
169 set_property ("finalizations", SCM_EOL);
170 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
172 /* TODO: make safe. */
173 scm_primitive_eval (scm_car (s));
176 /* Add a function to execute before stepping to the next time step. */
177 void
178 Global_context::add_finalization (SCM x)
180 SCM lst = get_property ("finalizations");
181 lst = scm_cons (x, lst);
182 set_property ("finalizations", lst);
185 Moment
186 Global_context::previous_moment () const
188 return prev_mom_;
191 Context *
192 Global_context::get_default_interpreter ()
194 if (get_score_context ())
195 return get_score_context ()->get_default_interpreter ();
196 else
197 return Context::get_default_interpreter ();
200 Global_context *
201 unsmob_global_context (SCM x)
203 return dynamic_cast<Global_context *> (unsmob_context (x));