2 context.cc -- implement Context
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "translator-group.hh"
11 #include "context-def.hh"
14 #include "output-def.hh"
15 #include "scm-hash.hh"
17 #include "ly-smobs.icc"
18 #include "score-context.hh"
21 Context::is_removable () const
23 return context_list_ == SCM_EOL && ! iterator_count_ &&
24 !dynamic_cast<Score_context const*> (this);
28 Context::check_removal ()
30 for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
32 Context *trg = unsmob_context (scm_car (p));
34 trg->check_removal ();
35 if (trg->is_removable ())
37 recurse_over_translators (trg, &Translator::finalize, UP);
43 Context::Context (Context const&)
49 Context::properties_dict () const
51 return Scheme_hash_table::unsmob (properties_scm_);
55 Context::add_context (Context*t)
57 SCM ts = t->self_scm ();
58 context_list_ = ly_append2 (context_list_,
59 scm_cons (ts, SCM_EOL));
61 t->daddy_context_ = this;
66 scm_gc_unprotect_object (ts);
67 Context_def * td = unsmob_context_def (t->definition_);
70 this can not move before add_context (), because \override
71 operations require that we are in the hierarchy.
73 td->apply_default_property_operations (t);
75 recurse_over_translators (t, &Translator::initialize, DOWN);
85 implementation_ = SCM_EOL;
86 properties_scm_ = SCM_EOL;
87 accepts_list_ = SCM_EOL;
88 context_list_ = SCM_EOL;
89 definition_ = SCM_EOL;
93 properties_scm_ = (new Scheme_hash_table)->self_scm ();
94 scm_gc_unprotect_object (properties_scm_);
100 Context::find_create_context (SCM n, String id,
104 Don't create multiple score contexts.
106 if (dynamic_cast<Global_context*> (this)
107 && dynamic_cast<Global_context*> (this)->get_score_context ())
108 return get_score_context ()->find_create_context (n, id, operations);
111 Context * existing = find_context_below (this, n,id);
115 if (n == ly_symbol2scm ("Bottom"))
117 Context* tg = get_default_interpreter ();
123 TODO: use accepts_list_.
125 Link_array<Context_def> path
126 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
130 Context * current = this;
132 // start at 1. The first one (index 0) will be us.
133 for (int i=0; i < path.size (); i++)
135 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
138 = path[i]->instantiate (ops);
140 if (i == path.size () -1)
142 new_group->id_string_ = id;
145 current->add_context (new_group);
146 apply_property_operations (new_group, ops);
155 Don't go up to Global_context, because global goes down to
159 if (daddy_context_ && !dynamic_cast<Global_context*> (daddy_context_))
160 ret = daddy_context_->find_create_context (n, id, operations);
163 warning (_f ("Cannot find or create `%s' called `%s'",
164 ly_symbol2string (n).to_str0 (), id));
171 Default child context as a SCM string, or something else if there is
175 Context::default_child_context_name () const
177 return scm_is_pair (accepts_list_)
178 ? scm_car (scm_last_pair (accepts_list_))
184 Context::is_bottom_context () const
186 return !scm_is_symbol (default_child_context_name ());
190 Context::get_default_interpreter ()
192 if (!is_bottom_context ())
194 SCM nm = default_child_context_name ();
195 SCM st = find_context_def (get_output_def (), nm);
197 Context_def *t = unsmob_context_def (st);
200 warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
201 t = unsmob_context_def (this->definition_);
203 Context *tg = t->instantiate (SCM_EOL);
205 if (!tg->is_bottom_context ())
206 return tg->get_default_interpreter ();
217 Context::where_defined (SCM sym) const
219 if (properties_dict ()->contains (sym))
221 return (Context*)this;
224 return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
228 return SCM_EOL when not found.
231 Context::internal_get_property (SCM sym) const
234 if (properties_dict ()->try_retrieve (sym, &val))
238 return daddy_context_->internal_get_property (sym);
244 Context::is_alias (SCM sym) const
246 if (sym == ly_symbol2scm ("Bottom")
247 && !scm_is_pair (accepts_list_))
249 if (sym == unsmob_context_def (definition_)->get_context_name ())
252 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
256 Context::add_alias (SCM sym)
258 aliases_ = scm_cons (sym, aliases_);
264 Context::internal_set_property (SCM sym, SCM val)
267 if (internal_type_checking_global_b)
268 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
271 properties_dict ()->set (sym, val);
275 TODO: look up to check whether we have inherited var?
278 Context::unset_property (SCM sym)
280 properties_dict ()->remove (sym);
284 Remove a context from the hierarchy.
287 Context::remove_context (Context*trans)
291 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
292 trans->daddy_context_ = 0;
297 ID == "" means accept any ID.
300 find_context_below (Context * where,
303 if (where->is_alias (type))
305 if (id == "" || where->id_string () == id)
310 for (SCM s = where->children_contexts ();
311 !found && scm_is_pair (s); s = scm_cdr (s))
313 Context * tr = unsmob_context (scm_car (s));
315 found = find_context_below (tr, type, id);
322 Context::properties_as_alist () const
324 return properties_dict ()->to_alist ();
328 Context::context_name () const
330 Context_def * td = unsmob_context_def (definition_ );
331 return ly_symbol2string (td->get_context_name ());
336 Context::get_score_context () const
338 if (Score_context *sc =dynamic_cast<Score_context*> ((Context*)this))
340 else if (daddy_context_)
341 return daddy_context_->get_score_context ();
347 Context::get_output_def () const
349 return (daddy_context_)
350 ? daddy_context_->get_output_def () : 0;
359 Context::now_mom () const
361 return daddy_context_->now_mom ();
365 Context::print_smob (SCM s, SCM port, scm_print_state *)
367 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
369 scm_puts ("#<", port);
370 scm_puts (classname (sc), port);
371 if (Context_def *d=unsmob_context_def (sc->definition_))
373 scm_puts (" ", port);
374 scm_display (d->get_context_name (), port);
377 if (Context *td=dynamic_cast<Context *> (sc))
379 scm_puts ("=", port);
380 scm_puts (td->id_string_.to_str0 (), port);
384 scm_puts (" ", port);
386 scm_display (sc->context_list_, port);
387 scm_puts (" >", port);
393 Context::mark_smob (SCM sm)
395 Context * me = (Context*) SCM_CELL_WORD_1 (sm);
397 scm_gc_mark (me->context_list_);
398 scm_gc_mark (me->aliases_);
399 scm_gc_mark (me->definition_);
400 scm_gc_mark (me->properties_scm_);
401 scm_gc_mark (me->accepts_list_);
402 scm_gc_mark (me->implementation_);
404 return me->properties_scm_;
407 IMPLEMENT_SMOBS (Context);
408 IMPLEMENT_DEFAULT_EQUAL_P (Context);
409 IMPLEMENT_TYPE_P (Context,"ly:context?");
412 Context::try_music (Music* m)
414 Translator* t = implementation ();
418 bool b = t->try_music (m);
419 if (!b && daddy_context_)
420 b = daddy_context_->try_music (m);
427 Context::get_global_context () const
429 if (dynamic_cast<Global_context *>((Context*) this))
430 return dynamic_cast<Global_context *> ((Context*) this);
433 return daddy_context_->get_global_context ();
435 programming_error ("No Global context!");
440 Context::get_parent_context () const
442 return daddy_context_;
446 Context::implementation () const
448 return dynamic_cast<Translator_group*> (unsmob_translator (implementation_));