* lily/paper-book.cc: remove copyright & tagline. Remove
[lilypond.git] / lily / context.cc
blob3a4a875ec1f05cde55eaced15f517b577fdbd4db
1 /*
2 context.cc -- implement Context
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
10 #include "translator-group.hh"
11 #include "context-def.hh"
12 #include "context.hh"
13 #include "warn.hh"
14 #include "output-def.hh"
15 #include "scm-hash.hh"
16 #include "main.hh"
17 #include "ly-smobs.icc"
18 #include "score-context.hh"
20 bool
21 Context::is_removable () const
23 return context_list_ == SCM_EOL && ! iterator_count_ &&
24 !dynamic_cast<Score_context const*> (this)
28 void
29 Context::check_removal ()
31 for (SCM p = context_list_; ly_c_pair_p (p); p = ly_cdr (p))
33 Context *trg = unsmob_context (ly_car (p));
35 trg->check_removal ();
36 if (trg->is_removable ())
38 recurse_over_translators (trg, &Translator::finalize, UP);
39 remove_context (trg);
44 Context::Context (Context const&)
46 assert (false);
49 Scheme_hash_table*
50 Context::properties_dict () const
52 return Scheme_hash_table::unsmob (properties_scm_);
55 void
56 Context::add_context (Context*t)
58 SCM ts = t->self_scm ();
59 context_list_ = ly_append2 (context_list_,
60 scm_cons (ts, SCM_EOL));
62 t->daddy_context_ = this;
63 if (!t->init_)
65 t->init_ = true;
67 scm_gc_unprotect_object (ts);
68 Context_def * td = unsmob_context_def (t->definition_);
71 this can not move before add_context (), because \override
72 operations require that we are in the hierarchy.
74 td->apply_default_property_operations (t);
76 recurse_over_translators (t, &Translator::initialize, DOWN);
80 Context::Context ()
82 daddy_context_ = 0;
83 init_ = false;
84 aliases_ = SCM_EOL;
85 iterator_count_ = 0;
86 implementation_ = SCM_EOL;
87 properties_scm_ = SCM_EOL;
88 accepts_list_ = SCM_EOL;
89 context_list_ = SCM_EOL;
90 definition_ = SCM_EOL;
92 smobify_self ();
94 properties_scm_ = (new Scheme_hash_table)->self_scm ();
95 scm_gc_unprotect_object (properties_scm_);
100 Context*
101 Context::find_create_context (SCM n, String id,
102 SCM operations)
105 Don't create multiple score contexts.
107 if (dynamic_cast<Global_context*> (this)
108 && dynamic_cast<Global_context*> (this)->get_score_context ())
109 return get_score_context ()->find_create_context (n, id, operations);
112 Context * existing = find_context_below (this, n,id);
113 if (existing)
114 return existing;
116 if (n == ly_symbol2scm ("Bottom"))
118 Context* tg = get_default_interpreter ();
119 tg->id_string_ = id;
120 return tg;
124 TODO: use accepts_list_.
126 Link_array<Context_def> path
127 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
129 if (path.size ())
131 Context * current = this;
133 // start at 1. The first one (index 0) will be us.
134 for (int i=0; i < path.size (); i++)
136 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
138 Context * new_group
139 = path[i]->instantiate (ops);
141 if (i == path.size () -1)
143 new_group->id_string_ = id;
146 current->add_context (new_group);
147 apply_property_operations (new_group, ops);
149 current = new_group;
152 return current;
156 Don't go up to Global_context, because global goes down to
157 Score_context
159 Context *ret = 0;
160 if (daddy_context_ && !dynamic_cast<Global_context*> (daddy_context_))
161 ret = daddy_context_->find_create_context (n, id, operations);
162 else
164 warning (_f ("Cannot find or create `%s' called `%s'",
165 ly_symbol2string (n).to_str0 (), id));
166 ret =0;
168 return ret;
172 Default child context as a SCM string, or something else if there is
173 none.
176 Context::default_child_context_name () const
178 return ly_c_pair_p (accepts_list_)
179 ? ly_car (scm_last_pair (accepts_list_))
180 : SCM_EOL;
184 bool
185 Context::is_bottom_context () const
187 return !ly_c_symbol_p (default_child_context_name ());
190 Context*
191 Context::get_default_interpreter ()
193 if (!is_bottom_context ())
195 SCM nm = default_child_context_name ();
196 SCM st = find_context_def (get_output_def (), nm);
198 Context_def *t = unsmob_context_def (st);
199 if (!t)
201 warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
202 t = unsmob_context_def (this->definition_);
204 Context *tg = t->instantiate (SCM_EOL);
205 add_context (tg);
206 if (!tg->is_bottom_context ())
207 return tg->get_default_interpreter ();
208 else
209 return tg;
211 return this;
215 PROPERTIES
217 Context*
218 Context::where_defined (SCM sym) const
220 if (properties_dict ()->contains (sym))
222 return (Context*)this;
225 return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
229 return SCM_EOL when not found.
232 Context::internal_get_property (SCM sym) const
234 SCM val =SCM_EOL;
235 if (properties_dict ()->try_retrieve (sym, &val))
236 return val;
238 if (daddy_context_)
239 return daddy_context_->internal_get_property (sym);
241 return val;
244 bool
245 Context::is_alias (SCM sym) const
247 if (sym == ly_symbol2scm ("Bottom")
248 && !ly_c_pair_p (accepts_list_))
249 return true;
250 if (sym == unsmob_context_def (definition_)->get_context_name ())
251 return true;
253 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
256 void
257 Context::add_alias (SCM sym)
259 aliases_ = scm_cons (sym, aliases_);
264 void
265 Context::internal_set_property (SCM sym, SCM val)
267 #ifndef NDEBUG
268 if (internal_type_checking_global_b)
269 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
270 #endif
272 properties_dict ()->set (sym, val);
276 TODO: look up to check whether we have inherited var?
278 void
279 Context::unset_property (SCM sym)
281 properties_dict ()->remove (sym);
285 Remove a context from the hierarchy.
287 Context *
288 Context::remove_context (Context*trans)
290 assert (trans);
292 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
293 trans->daddy_context_ = 0;
294 return trans;
298 ID == "" means accept any ID.
300 Context *
301 find_context_below (Context * where,
302 SCM type, String id)
304 if (where->is_alias (type))
306 if (id == "" || where->id_string () == id)
307 return where;
310 Context * found = 0;
311 for (SCM s = where->children_contexts ();
312 !found && ly_c_pair_p (s); s = ly_cdr (s))
314 Context * tr = unsmob_context (ly_car (s));
316 found = find_context_below (tr, type, id);
319 return found;
323 Context::properties_as_alist () const
325 return properties_dict ()->to_alist ();
328 String
329 Context::context_name () const
331 Context_def * td = unsmob_context_def (definition_ );
332 return ly_symbol2string (td->get_context_name ());
336 Score_context*
337 Context::get_score_context () const
339 if (Score_context *sc =dynamic_cast<Score_context*> ((Context*)this))
340 return sc;
341 else if (daddy_context_)
342 return daddy_context_->get_score_context ();
343 else
344 return 0;
347 Output_def *
348 Context::get_output_def () const
350 return (daddy_context_)
351 ? daddy_context_->get_output_def () : 0;
354 Context::~Context ()
359 Moment
360 Context::now_mom () const
362 return daddy_context_->now_mom ();
366 Context::print_smob (SCM s, SCM port, scm_print_state *)
368 Context *sc = (Context *) ly_cdr (s);
370 scm_puts ("#<", port);
371 scm_puts (classname (sc), port);
372 if (Context_def *d=unsmob_context_def (sc->definition_))
374 scm_puts (" ", port);
375 scm_display (d->get_context_name (), port);
378 if (Context *td=dynamic_cast<Context *> (sc))
380 scm_puts ("=", port);
381 scm_puts (td->id_string_.to_str0 (), port);
385 scm_puts (" ", port);
387 scm_display (sc->context_list_, port);
388 scm_puts (" >", port);
390 return 1;
394 Context::mark_smob (SCM sm)
396 Context * me = (Context*) SCM_CELL_WORD_1 (sm);
398 scm_gc_mark (me->context_list_);
399 scm_gc_mark (me->aliases_);
400 scm_gc_mark (me->definition_);
401 scm_gc_mark (me->properties_scm_);
402 scm_gc_mark (me->accepts_list_);
403 scm_gc_mark (me->implementation_);
405 return me->properties_scm_;
408 IMPLEMENT_SMOBS (Context);
409 IMPLEMENT_DEFAULT_EQUAL_P (Context);
410 IMPLEMENT_TYPE_P (Context,"ly:context?");
412 bool
413 Context::try_music (Music* m)
415 Translator* t = implementation ();
416 if (!t)
417 return false;
419 bool b = t->try_music (m);
420 if (!b && daddy_context_)
421 b = daddy_context_->try_music (m);
423 return b;
427 Global_context*
428 Context::get_global_context () const
430 if (dynamic_cast<Global_context *>((Context*) this))
431 return dynamic_cast<Global_context *> ((Context*) this);
433 if (daddy_context_)
434 return daddy_context_->get_global_context ();
436 programming_error ("No Global context!");
437 return 0;
440 Context*
441 Context::get_parent_context () const
443 return daddy_context_;
446 Translator_group*
447 Context::implementation () const
449 return dynamic_cast<Translator_group*> (unsmob_translator (implementation_));