2 context.cc -- implement Context
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "context-def.hh"
12 #include "international.hh"
13 #include "ly-smobs.icc"
15 #include "output-def.hh"
17 #include "program-option.hh"
18 #include "scm-hash.hh"
19 #include "score-context.hh"
20 #include "translator-group.hh"
24 Context::is_removable () const
26 return context_list_ == SCM_EOL && ! iterator_count_
27 && !dynamic_cast<Score_context const *> (this);
31 Context::check_removal ()
33 for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
35 Context *trg = unsmob_context (scm_car (p));
37 trg->check_removal ();
38 if (trg->is_removable ())
40 recurse_over_translators (trg, &Translator::finalize,
41 &Translator_group::finalize,
48 Context::Context (Context const &src)
49 : key_manager_ (src.key_manager_)
55 Context::properties_dict () const
57 return Scheme_hash_table::unsmob (properties_scm_);
61 Context::add_context (Context *t)
63 SCM ts = t->self_scm ();
64 context_list_ = ly_append2 (context_list_,
65 scm_cons (ts, SCM_EOL));
67 t->daddy_context_ = this;
73 Context_def *td = unsmob_context_def (t->definition_);
75 /* This cannot move before add_context (), because \override
76 operations require that we are in the hierarchy. */
77 td->apply_default_property_operations (t);
79 recurse_over_translators (t,
80 &Translator::initialize,
81 &Translator_group::initialize,
87 Context::Context (Object_key const *key)
95 properties_scm_ = SCM_EOL;
96 accepts_list_ = SCM_EOL;
97 context_list_ = SCM_EOL;
98 definition_ = SCM_EOL;
102 Scheme_hash_table *tab = new Scheme_hash_table;
103 properties_scm_ = tab->unprotect ();
109 key_manager_.unprotect();
112 /* TODO: this shares code with find_create_context (). */
114 Context::create_unique_context (SCM name, string id, SCM operations)
117 Don't create multiple score contexts.
119 if (dynamic_cast<Global_context *> (this)
120 && dynamic_cast<Global_context *> (this)->get_score_context ())
121 return get_score_context ()->create_unique_context (name, id, operations);
124 TODO: use accepts_list_.
126 vector<Context_def*> path
127 = unsmob_context_def (definition_)->path_to_acceptable_context (name, get_output_def ());
131 Context *current = this;
133 // start at 1. The first one (index 0) will be us.
134 for (vsize i = 0; i < path.size (); i++)
137 string id_str = "\\new";
138 if (i == path.size () - 1)
143 current = current->create_context (path[i],
152 Don't go up to Global_context, because global goes down to
156 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
157 ret = daddy_context_->create_unique_context (name, id, operations);
160 warning (_f ("can't find or create new `%s'",
161 ly_symbol2string (name).c_str ()));
168 Context::find_create_context (SCM n, string id, SCM operations)
171 Don't create multiple score contexts.
173 if (dynamic_cast<Global_context *> (this)
174 && dynamic_cast<Global_context *> (this)->get_score_context ())
175 return get_score_context ()->find_create_context (n, id, operations);
177 if (Context *existing = find_context_below (this, n, id))
180 if (n == ly_symbol2scm ("Bottom"))
182 Context *tg = get_default_interpreter ();
187 TODO: use accepts_list_.
189 vector<Context_def*> path
190 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
194 Context *current = this;
196 // start at 1. The first one (index 0) will be us.
197 for (vsize i = 0; i < path.size (); i++)
199 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
202 if (i == path.size () -1)
205 current = current->create_context (path[i],
214 Don't go up to Global_context, because global goes down to
218 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
219 ret = daddy_context_->find_create_context (n, id, operations);
222 warning (_f ("can't find or create `%s' called `%s'",
223 ly_symbol2string (n).c_str (), id));
230 Context::create_context (Context_def *cdef,
234 string type = ly_symbol2string (cdef->get_context_name ());
235 Object_key const *key = key_manager_.get_context_key (now_mom(), type, id);
237 = cdef->instantiate (ops, key);
239 new_context->id_string_ = id;
240 add_context (new_context);
241 apply_property_operations (new_context, ops);
247 Default child context as a SCM string, or something else if there is
251 Context::default_child_context_name () const
253 return scm_is_pair (accepts_list_)
254 ? scm_car (accepts_list_)
259 Context::is_bottom_context () const
261 return !scm_is_symbol (default_child_context_name ());
265 Context::get_default_interpreter ()
267 if (!is_bottom_context ())
269 SCM nm = default_child_context_name ();
270 SCM st = find_context_def (get_output_def (), nm);
272 string name = ly_symbol2string (nm);
273 Context_def *t = unsmob_context_def (st);
276 warning (_f ("can't find or create: `%s'", name.c_str ()));
277 t = unsmob_context_def (this->definition_);
280 Context *tg = create_context (t, "", SCM_EOL);
281 if (!tg->is_bottom_context ())
282 return tg->get_default_interpreter ();
293 Context::where_defined (SCM sym, SCM *value) const
296 if (profile_property_accesses)
297 note_property_access (&context_property_lookup_table, sym);
300 if (properties_dict ()->try_retrieve (sym, value))
301 return (Context *)this;
303 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
307 return SCM_EOL when not found.
310 Context::internal_get_property (SCM sym) const
313 if (profile_property_accesses)
314 note_property_access (&context_property_lookup_table, sym);
318 if (properties_dict ()->try_retrieve (sym, &val))
322 return daddy_context_->internal_get_property (sym);
328 Context::is_alias (SCM sym) const
330 if (sym == ly_symbol2scm ("Bottom")
331 && !scm_is_pair (accepts_list_))
333 if (sym == unsmob_context_def (definition_)->get_context_name ())
336 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
340 Context::add_alias (SCM sym)
342 aliases_ = scm_cons (sym, aliases_);
346 Context::internal_set_property (SCM sym, SCM val)
349 if (do_internal_type_checking_global)
350 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
353 properties_dict ()->set (sym, val);
357 TODO: look up to check whether we have inherited var?
360 Context::unset_property (SCM sym)
362 properties_dict ()->remove (sym);
366 Remove a context from the hierarchy.
369 Context::remove_context (Context *trans)
373 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
374 trans->daddy_context_ = 0;
379 ID == "" means accept any ID.
382 find_context_below (Context *where,
385 if (where->is_alias (type))
387 if (id == "" || where->id_string () == id)
392 for (SCM s = where->children_contexts ();
393 !found && scm_is_pair (s); s = scm_cdr (s))
395 Context *tr = unsmob_context (scm_car (s));
397 found = find_context_below (tr, type, id);
404 Context::properties_as_alist () const
406 return properties_dict ()->to_alist ();
410 Context::context_name_symbol () const
412 Context_def *td = unsmob_context_def (definition_);
413 return td->get_context_name ();
417 Context::context_name () const
419 return ly_symbol2string (context_name_symbol ());
423 Context::get_score_context () const
425 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
427 else if (daddy_context_)
428 return daddy_context_->get_score_context ();
434 Context::get_output_def () const
436 return daddy_context_ ? daddy_context_->get_output_def () : 0;
444 Context::now_mom () const
446 Context const *p = this;
447 while (p->daddy_context_)
448 p = p->daddy_context_;
450 return p->now_mom ();
454 Context::print_smob (SCM s, SCM port, scm_print_state *)
456 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
458 scm_puts ("#<", port);
459 scm_puts (sc->class_name (), port);
460 if (Context_def *d = unsmob_context_def (sc->definition_))
462 scm_puts (" ", port);
463 scm_display (d->get_context_name (), port);
466 if (!sc->id_string_.empty ())
468 scm_puts ("=", port);
469 scm_puts (sc->id_string_.c_str (), port);
472 scm_puts (" ", port);
474 scm_display (sc->context_list_, port);
475 scm_puts (" >", port);
481 Context::get_grob_key (string name)
483 return key_manager_.get_grob_key (now_mom (), name);
487 Context::get_context_key (string name, string id)
489 return key_manager_.get_context_key (now_mom (), name, id);
493 Context::mark_smob (SCM sm)
495 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
496 me->key_manager_.gc_mark();
498 scm_gc_mark (me->context_list_);
499 scm_gc_mark (me->aliases_);
500 scm_gc_mark (me->definition_);
501 scm_gc_mark (me->properties_scm_);
502 scm_gc_mark (me->accepts_list_);
503 if (me->implementation_)
504 scm_gc_mark (me->implementation_->self_scm ());
506 return me->properties_scm_;
509 IMPLEMENT_SMOBS (Context);
510 IMPLEMENT_DEFAULT_EQUAL_P (Context);
511 IMPLEMENT_TYPE_P (Context, "ly:context?");
514 Context::try_music (Music *m)
516 Translator_group *t = implementation ();
520 bool b = t->try_music (m);
521 if (!b && daddy_context_)
522 b = daddy_context_->try_music (m);
528 Context::get_global_context () const
530 if (dynamic_cast<Global_context *> ((Context *) this))
531 return dynamic_cast<Global_context *> ((Context *) this);
534 return daddy_context_->get_global_context ();
536 programming_error ("no Global context");
541 Context::get_parent_context () const
543 return daddy_context_;
547 Context::clear_key_disambiguations ()
549 if (!use_object_keys)
552 key_manager_.clear ();
553 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
554 unsmob_context (scm_car (s))->clear_key_disambiguations ();
558 Ugh. Where to put this?
561 measure_length (Context const *context)
563 SCM l = context->get_property ("measureLength");
565 if (unsmob_moment (l))
566 length = unsmob_moment (l)->main_part_;
571 measure_position (Context const *context)
573 SCM sm = context->get_property ("measurePosition");
576 if (unsmob_moment (sm))
578 m = *unsmob_moment (sm);
580 if (m.main_part_ < Rational (0))
582 Rational length (measure_length (context));
583 while (m.main_part_ < Rational (0))
584 m.main_part_ += length;
593 set_context_property_on_children (Context *trans, SCM sym, SCM val)
595 trans->internal_set_property (sym, ly_deep_copy (val));
596 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
598 Context *trg = unsmob_context (scm_car (p));
599 set_context_property_on_children (trg, sym, ly_deep_copy (val));