2 context.cc -- implement Context
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "context-def.hh"
13 #include "ly-smobs.icc"
15 #include "output-def.hh"
16 #include "scm-hash.hh"
17 #include "score-context.hh"
18 #include "translator-group.hh"
20 #include "lilypond-key.hh"
23 Context::is_removable () const
25 return context_list_ == SCM_EOL && ! iterator_count_ &&
26 !dynamic_cast<Score_context const*> (this);
30 Context::check_removal ()
32 for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
34 Context *trg = unsmob_context (scm_car (p));
36 trg->check_removal ();
37 if (trg->is_removable ())
39 recurse_over_translators (trg, &Translator::finalize, UP);
45 Context::Context (Context const&)
51 Context::properties_dict () const
53 return Scheme_hash_table::unsmob (properties_scm_);
57 Context::add_context (Context *t)
59 SCM ts = t->self_scm ();
60 context_list_ = ly_append2 (context_list_,
61 scm_cons (ts, SCM_EOL));
63 t->daddy_context_ = this;
68 scm_gc_unprotect_object (ts);
69 Context_def *td = unsmob_context_def (t->definition_);
72 this can not move before add_context (), because \override
73 operations require that we are in the hierarchy.
75 td->apply_default_property_operations (t);
77 recurse_over_translators (t, &Translator::initialize, DOWN);
82 Context::get_key () const
87 Context::Context (Object_key const* key)
94 implementation_ = SCM_EOL;
95 properties_scm_ = SCM_EOL;
96 accepts_list_ = SCM_EOL;
97 context_list_ = SCM_EOL;
98 definition_ = SCM_EOL;
102 properties_scm_ = (new Scheme_hash_table)->self_scm ();
103 scm_gc_unprotect_object (properties_scm_);
109 Context::find_create_context (SCM n, String id, SCM operations)
112 Don't create multiple score contexts.
114 if (dynamic_cast<Global_context*> (this)
115 && dynamic_cast<Global_context*> (this)->get_score_context ())
116 return get_score_context ()->find_create_context (n, id, operations);
118 if (Context *existing = find_context_below (this, n, id))
121 if (n == ly_symbol2scm ("Bottom"))
123 Context* tg = get_default_interpreter ();
129 TODO: use accepts_list_.
131 Link_array<Context_def> path
132 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
136 Context * current = this;
138 // start at 1. The first one (index 0) will be us.
139 for (int i=0; i < path.size (); i++)
141 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
144 if (i == path.size () -1)
150 current = current->create_context (path[i],
159 Don't go up to Global_context, because global goes down to
163 if (daddy_context_ && !dynamic_cast<Global_context*> (daddy_context_))
164 ret = daddy_context_->find_create_context (n, id, operations);
167 warning (_f ("Cannot find or create `%s' called `%s'",
168 ly_symbol2string (n).to_str0 (), id));
176 Context::create_context (Context_def * cdef,
180 String type = ly_symbol2string (cdef->get_context_name());
181 Object_key const *key = get_context_key (type, id);
183 = cdef->instantiate (ops, key);
184 scm_gc_unprotect_object (key->self_scm ());
186 new_group->id_string_ = id;
187 add_context (new_group);
188 apply_property_operations (new_group, ops);
194 Context::get_context_key (String type, String id)
196 String now_key = type + "@" + id;
198 int disambiguation_count = 0;
199 if (context_counts_.find (now_key) != context_counts_.end ())
201 disambiguation_count = context_counts_[now_key];
204 context_counts_[now_key] = disambiguation_count + 1;
207 return new Lilypond_context_key (get_key (),
210 disambiguation_count);
214 Context::get_grob_key (String name)
216 int disambiguation_count = 0;
217 if (grob_counts_.find (name) != grob_counts_.end ())
219 disambiguation_count = grob_counts_[name];
221 grob_counts_[name] = disambiguation_count + 1;
223 Object_key * k = new Lilypond_grob_key (get_key(),
226 disambiguation_count);
234 Default child context as a SCM string, or something else if there is
238 Context::default_child_context_name () const
240 return scm_is_pair (accepts_list_)
241 ? scm_car (scm_last_pair (accepts_list_))
247 Context::is_bottom_context () const
249 return !scm_is_symbol (default_child_context_name ());
253 Context::get_default_interpreter ()
255 if (!is_bottom_context ())
257 SCM nm = default_child_context_name ();
258 SCM st = find_context_def (get_output_def (), nm);
260 String name = ly_symbol2string (nm);
261 Context_def *t = unsmob_context_def (st);
264 warning (_f ("can't find or create: `%s'", name.to_str0 ()));
265 t = unsmob_context_def (this->definition_);
268 Context *tg = create_context (t, "", SCM_EOL);
269 if (!tg->is_bottom_context ())
270 return tg->get_default_interpreter ();
281 Context::where_defined (SCM sym) const
283 if (properties_dict ()->contains (sym))
285 return (Context*)this;
288 return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
292 return SCM_EOL when not found.
295 Context::internal_get_property (SCM sym) const
298 if (properties_dict ()->try_retrieve (sym, &val))
302 return daddy_context_->internal_get_property (sym);
308 Context::is_alias (SCM sym) const
310 if (sym == ly_symbol2scm ("Bottom")
311 && !scm_is_pair (accepts_list_))
313 if (sym == unsmob_context_def (definition_)->get_context_name ())
316 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
320 Context::add_alias (SCM sym)
322 aliases_ = scm_cons (sym, aliases_);
328 Context::internal_set_property (SCM sym, SCM val)
331 if (internal_type_checking_global_b)
332 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
335 properties_dict ()->set (sym, val);
339 TODO: look up to check whether we have inherited var?
342 Context::unset_property (SCM sym)
344 properties_dict ()->remove (sym);
348 Remove a context from the hierarchy.
351 Context::remove_context (Context*trans)
355 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
356 trans->daddy_context_ = 0;
361 ID == "" means accept any ID.
364 find_context_below (Context * where,
367 if (where->is_alias (type))
369 if (id == "" || where->id_string () == id)
374 for (SCM s = where->children_contexts ();
375 !found && scm_is_pair (s); s = scm_cdr (s))
377 Context *tr = unsmob_context (scm_car (s));
379 found = find_context_below (tr, type, id);
386 Context::properties_as_alist () const
388 return properties_dict ()->to_alist ();
392 Context::context_name_symbol () const
394 Context_def *td = unsmob_context_def (definition_);
395 return td->get_context_name ();
399 Context::context_name () const
401 return ly_symbol2string (context_name_symbol ());
405 Context::get_score_context () const
407 if (Score_context *sc = dynamic_cast<Score_context*> ((Context*) this))
409 else if (daddy_context_)
410 return daddy_context_->get_score_context ();
416 Context::get_output_def () const
418 return daddy_context_ ? daddy_context_->get_output_def () : 0;
427 Context::now_mom () const
429 return daddy_context_->now_mom ();
433 Context::print_smob (SCM s, SCM port, scm_print_state *)
435 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
437 scm_puts ("#<", port);
438 scm_puts (classname (sc), port);
439 if (Context_def *d = unsmob_context_def (sc->definition_))
441 scm_puts (" ", port);
442 scm_display (d->get_context_name (), port);
445 if (Context *td=dynamic_cast<Context *> (sc))
447 scm_puts ("=", port);
448 scm_puts (td->id_string_.to_str0 (), port);
452 scm_puts (" ", port);
454 scm_display (sc->context_list_, port);
455 scm_puts (" >", port);
461 Context::mark_smob (SCM sm)
463 Context *me = (Context*) SCM_CELL_WORD_1 (sm);
464 scm_gc_mark (me->key_->self_scm ());
465 scm_gc_mark (me->context_list_);
466 scm_gc_mark (me->aliases_);
467 scm_gc_mark (me->definition_);
468 scm_gc_mark (me->properties_scm_);
469 scm_gc_mark (me->accepts_list_);
470 scm_gc_mark (me->implementation_);
472 return me->properties_scm_;
475 IMPLEMENT_SMOBS (Context);
476 IMPLEMENT_DEFAULT_EQUAL_P (Context);
477 IMPLEMENT_TYPE_P (Context,"ly:context?");
480 Context::try_music (Music* m)
482 Translator* t = implementation ();
486 bool b = t->try_music (m);
487 if (!b && daddy_context_)
488 b = daddy_context_->try_music (m);
495 Context::get_global_context () const
497 if (dynamic_cast<Global_context *>((Context*) this))
498 return dynamic_cast<Global_context *> ((Context*) this);
501 return daddy_context_->get_global_context ();
503 programming_error ("No Global context!");
508 Context::get_parent_context () const
510 return daddy_context_;
514 Context::implementation () const
516 return dynamic_cast<Translator_group*> (unsmob_translator (implementation_));
520 Context::clear_key_disambiguations ()
522 grob_counts_.clear();
523 context_counts_.clear();
524 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
526 unsmob_context (scm_car (s))->clear_key_disambiguations();