2 context.cc -- implement Context
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "context-def.hh"
12 #include "ly-smobs.icc"
14 #include "output-def.hh"
15 #include "scm-hash.hh"
16 #include "score-context.hh"
17 #include "translator-group.hh"
19 #include "lilypond-key.hh"
22 Context::is_removable () const
24 return context_list_ == SCM_EOL && ! iterator_count_
25 && !dynamic_cast<Score_context const *> (this);
29 Context::check_removal ()
31 for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
33 Context *trg = unsmob_context (scm_car (p));
35 trg->check_removal ();
36 if (trg->is_removable ())
38 recurse_over_translators (trg, &Translator::finalize, UP);
44 Context::Context (Context const &)
50 Context::properties_dict () const
52 return Scheme_hash_table::unsmob (properties_scm_);
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;
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);
81 Context::get_key () const
86 Context::Context (Object_key const *key)
93 implementation_ = SCM_EOL;
94 properties_scm_ = SCM_EOL;
95 accepts_list_ = SCM_EOL;
96 context_list_ = SCM_EOL;
97 definition_ = SCM_EOL;
100 properties_scm_ = (new Scheme_hash_table)->self_scm ();
101 scm_gc_unprotect_object (properties_scm_);
102 scm_gc_unprotect_object (key_->self_scm ());
105 /* TODO: this shares code with find_create_context (). */
107 Context::create_unique_context (SCM n, SCM operations)
110 Don't create multiple score contexts.
112 if (dynamic_cast<Global_context *> (this)
113 && dynamic_cast<Global_context *> (this)->get_score_context ())
114 return get_score_context ()->create_unique_context (n, operations);
117 TODO: use accepts_list_.
119 Link_array<Context_def> path
120 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
124 Context *current = this;
126 // start at 1. The first one (index 0) will be us.
127 for (int i = 0; i < path.size (); i++)
129 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
131 current = current->create_context (path[i],
140 Don't go up to Global_context, because global goes down to
144 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
145 ret = daddy_context_->create_unique_context (n, operations);
148 warning (_f ("Cannot find or create new `%s'",
149 ly_symbol2string (n).to_str0 ()));
156 Context::find_create_context (SCM n, String id, SCM operations)
159 Don't create multiple score contexts.
161 if (dynamic_cast<Global_context *> (this)
162 && dynamic_cast<Global_context *> (this)->get_score_context ())
163 return get_score_context ()->find_create_context (n, id, operations);
165 if (Context *existing = find_context_below (this, n, id))
168 if (n == ly_symbol2scm ("Bottom"))
170 Context *tg = get_default_interpreter ();
175 TODO: use accepts_list_.
177 Link_array<Context_def> path
178 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
182 Context *current = this;
184 // start at 1. The first one (index 0) will be us.
185 for (int i = 0; i < path.size (); i++)
187 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
190 if (i == path.size () -1)
195 current = current->create_context (path[i],
204 Don't go up to Global_context, because global goes down to
208 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
209 ret = daddy_context_->find_create_context (n, id, operations);
212 warning (_f ("Cannot find or create `%s' called `%s'",
213 ly_symbol2string (n).to_str0 (), id));
220 Context::create_context (Context_def *cdef,
224 String type = ly_symbol2string (cdef->get_context_name ());
225 Object_key const *key = get_context_key (type, id);
227 = cdef->instantiate (ops, key);
229 new_group->id_string_ = id;
230 add_context (new_group);
231 apply_property_operations (new_group, ops);
237 Context::get_context_key (String type, String id)
239 String now_key = type + "@" + id;
241 int disambiguation_count = 0;
242 if (context_counts_.find (now_key) != context_counts_.end ())
244 disambiguation_count = context_counts_[now_key];
247 context_counts_[now_key] = disambiguation_count + 1;
249 return new Lilypond_context_key (get_key (),
252 disambiguation_count);
256 Context::get_grob_key (String name)
258 int disambiguation_count = 0;
259 if (grob_counts_.find (name) != grob_counts_.end ())
261 disambiguation_count = grob_counts_[name];
263 grob_counts_[name] = disambiguation_count + 1;
265 Object_key *k = new Lilypond_grob_key (get_key (),
268 disambiguation_count);
274 Default child context as a SCM string, or something else if there is
278 Context::default_child_context_name () const
280 return scm_is_pair (accepts_list_)
281 ? scm_car (scm_last_pair (accepts_list_))
286 Context::is_bottom_context () const
288 return !scm_is_symbol (default_child_context_name ());
292 Context::get_default_interpreter ()
294 if (!is_bottom_context ())
296 SCM nm = default_child_context_name ();
297 SCM st = find_context_def (get_output_def (), nm);
299 String name = ly_symbol2string (nm);
300 Context_def *t = unsmob_context_def (st);
303 warning (_f ("can't find or create: `%s'", name.to_str0 ()));
304 t = unsmob_context_def (this->definition_);
307 Context *tg = create_context (t, "", SCM_EOL);
308 if (!tg->is_bottom_context ())
309 return tg->get_default_interpreter ();
320 Context::where_defined (SCM sym) const
322 if (properties_dict ()->contains (sym))
324 return (Context *)this;
327 return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
331 return SCM_EOL when not found.
334 Context::internal_get_property (SCM sym) const
337 if (properties_dict ()->try_retrieve (sym, &val))
341 return daddy_context_->internal_get_property (sym);
347 Context::is_alias (SCM sym) const
349 if (sym == ly_symbol2scm ("Bottom")
350 && !scm_is_pair (accepts_list_))
352 if (sym == unsmob_context_def (definition_)->get_context_name ())
355 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
359 Context::add_alias (SCM sym)
361 aliases_ = scm_cons (sym, aliases_);
365 Context::internal_set_property (SCM sym, SCM val)
368 if (do_internal_type_checking_global)
369 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
372 properties_dict ()->set (sym, val);
376 TODO: look up to check whether we have inherited var?
379 Context::unset_property (SCM sym)
381 properties_dict ()->remove (sym);
385 Remove a context from the hierarchy.
388 Context::remove_context (Context *trans)
392 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
393 trans->daddy_context_ = 0;
398 ID == "" means accept any ID.
401 find_context_below (Context *where,
404 if (where->is_alias (type))
406 if (id == "" || where->id_string () == id)
411 for (SCM s = where->children_contexts ();
412 !found && scm_is_pair (s); s = scm_cdr (s))
414 Context *tr = unsmob_context (scm_car (s));
416 found = find_context_below (tr, type, id);
423 Context::properties_as_alist () const
425 return properties_dict ()->to_alist ();
429 Context::context_name_symbol () const
431 Context_def *td = unsmob_context_def (definition_);
432 return td->get_context_name ();
436 Context::context_name () const
438 return ly_symbol2string (context_name_symbol ());
442 Context::get_score_context () const
444 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
446 else if (daddy_context_)
447 return daddy_context_->get_score_context ();
453 Context::get_output_def () const
455 return daddy_context_ ? daddy_context_->get_output_def () : 0;
463 Context::now_mom () const
465 return daddy_context_->now_mom ();
469 Context::print_smob (SCM s, SCM port, scm_print_state *)
471 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
473 scm_puts ("#<", port);
474 scm_puts (classname (sc), port);
475 if (Context_def *d = unsmob_context_def (sc->definition_))
477 scm_puts (" ", port);
478 scm_display (d->get_context_name (), port);
481 if (Context *td = dynamic_cast<Context *> (sc))
483 scm_puts ("=", port);
484 scm_puts (td->id_string_.to_str0 (), port);
487 scm_puts (" ", port);
489 scm_display (sc->context_list_, port);
490 scm_puts (" >", port);
496 Context::mark_smob (SCM sm)
498 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
499 scm_gc_mark (me->key_->self_scm ());
500 scm_gc_mark (me->context_list_);
501 scm_gc_mark (me->aliases_);
502 scm_gc_mark (me->definition_);
503 scm_gc_mark (me->properties_scm_);
504 scm_gc_mark (me->accepts_list_);
505 scm_gc_mark (me->implementation_);
507 return me->properties_scm_;
510 IMPLEMENT_SMOBS (Context);
511 IMPLEMENT_DEFAULT_EQUAL_P (Context);
512 IMPLEMENT_TYPE_P (Context, "ly:context?");
515 Context::try_music (Music *m)
517 Translator *t = implementation ();
521 bool b = t->try_music (m);
522 if (!b && daddy_context_)
523 b = daddy_context_->try_music (m);
529 Context::get_global_context () const
531 if (dynamic_cast<Global_context *> ((Context *) this))
532 return dynamic_cast<Global_context *> ((Context *) this);
535 return daddy_context_->get_global_context ();
537 programming_error ("No Global context!");
542 Context::get_parent_context () const
544 return daddy_context_;
548 Context::implementation () const
550 return dynamic_cast<Translator_group *> (unsmob_translator (implementation_));
554 Context::clear_key_disambiguations ()
556 grob_counts_.clear ();
557 context_counts_.clear ();
558 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
560 unsmob_context (scm_car (s))->clear_key_disambiguations ();