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;
103 Scheme_hash_table *tab = new Scheme_hash_table;
104 properties_scm_ = tab->unprotect ();
110 key_manager_.unprotect();
113 /* TODO: this shares code with find_create_context (). */
115 Context::create_unique_context (SCM name, string id, SCM operations)
118 Don't create multiple score contexts.
120 if (dynamic_cast<Global_context *> (this)
121 && dynamic_cast<Global_context *> (this)->get_score_context ())
122 return get_score_context ()->create_unique_context (name, id, operations);
125 TODO: use accepts_list_.
127 vector<Context_def*> path
128 = unsmob_context_def (definition_)->path_to_acceptable_context (name, get_output_def ());
132 Context *current = this;
134 // start at 1. The first one (index 0) will be us.
135 for (vsize i = 0; i < path.size (); i++)
138 string id_str = "\\new";
139 if (i == path.size () - 1)
144 current = current->create_context (path[i],
153 Don't go up to Global_context, because global goes down to
157 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
158 ret = daddy_context_->create_unique_context (name, id, operations);
161 warning (_f ("can't find or create new `%s'",
162 ly_symbol2string (name).c_str ()));
169 Context::find_create_context (SCM n, string id, SCM operations)
172 Don't create multiple score contexts.
174 if (dynamic_cast<Global_context *> (this)
175 && dynamic_cast<Global_context *> (this)->get_score_context ())
176 return get_score_context ()->find_create_context (n, id, operations);
178 if (Context *existing = find_context_below (this, n, id))
181 if (n == ly_symbol2scm ("Bottom"))
183 Context *tg = get_default_interpreter ();
188 TODO: use accepts_list_.
190 vector<Context_def*> path
191 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
195 Context *current = this;
197 // start at 1. The first one (index 0) will be us.
198 for (vsize i = 0; i < path.size (); i++)
200 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
203 if (i == path.size () -1)
206 current = current->create_context (path[i],
215 Don't go up to Global_context, because global goes down to
219 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
220 ret = daddy_context_->find_create_context (n, id, operations);
223 warning (_f ("can't find or create `%s' called `%s'",
224 ly_symbol2string (n).c_str (), id));
231 Context::create_context (Context_def *cdef,
235 string type = ly_symbol2string (cdef->get_context_name ());
236 Object_key const *key = key_manager_.get_context_key (now_mom(), type, id);
238 = cdef->instantiate (ops, key);
240 new_context->unique_ = get_global_context()->new_unique();
241 new_context->id_string_ = id;
242 add_context (new_context);
243 apply_property_operations (new_context, ops);
249 Default child context as a SCM string, or something else if there is
253 Context::default_child_context_name () const
255 return scm_is_pair (accepts_list_)
256 ? scm_car (accepts_list_)
261 Context::is_bottom_context () const
263 return !scm_is_symbol (default_child_context_name ());
267 Context::get_default_interpreter ()
269 if (!is_bottom_context ())
271 SCM nm = default_child_context_name ();
272 SCM st = find_context_def (get_output_def (), nm);
274 string name = ly_symbol2string (nm);
275 Context_def *t = unsmob_context_def (st);
278 warning (_f ("can't find or create: `%s'", name.c_str ()));
279 t = unsmob_context_def (this->definition_);
282 Context *tg = create_context (t, "", SCM_EOL);
283 if (!tg->is_bottom_context ())
284 return tg->get_default_interpreter ();
295 Context::where_defined (SCM sym, SCM *value) const
298 if (profile_property_accesses)
299 note_property_access (&context_property_lookup_table, sym);
302 if (properties_dict ()->try_retrieve (sym, value))
303 return (Context *)this;
305 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
309 return SCM_EOL when not found.
312 Context::internal_get_property (SCM sym) const
315 if (profile_property_accesses)
316 note_property_access (&context_property_lookup_table, sym);
320 if (properties_dict ()->try_retrieve (sym, &val))
324 return daddy_context_->internal_get_property (sym);
330 Context::is_alias (SCM sym) const
332 if (sym == ly_symbol2scm ("Bottom")
333 && !scm_is_pair (accepts_list_))
335 if (sym == unsmob_context_def (definition_)->get_context_name ())
338 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
342 Context::add_alias (SCM sym)
344 aliases_ = scm_cons (sym, aliases_);
348 Context::internal_set_property (SCM sym, SCM val)
351 if (do_internal_type_checking_global)
352 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
355 properties_dict ()->set (sym, val);
359 TODO: look up to check whether we have inherited var?
362 Context::unset_property (SCM sym)
364 properties_dict ()->remove (sym);
368 Remove a context from the hierarchy.
371 Context::remove_context (Context *trans)
375 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
376 trans->daddy_context_ = 0;
381 ID == "" means accept any ID.
384 find_context_below (Context *where,
387 if (where->is_alias (type))
389 if (id == "" || where->id_string () == id)
394 for (SCM s = where->children_contexts ();
395 !found && scm_is_pair (s); s = scm_cdr (s))
397 Context *tr = unsmob_context (scm_car (s));
399 found = find_context_below (tr, type, id);
406 Context::properties_as_alist () const
408 return properties_dict ()->to_alist ();
412 Context::context_name_symbol () const
414 Context_def *td = unsmob_context_def (definition_);
415 return td->get_context_name ();
419 Context::context_name () const
421 return ly_symbol2string (context_name_symbol ());
425 Context::get_score_context () const
427 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
429 else if (daddy_context_)
430 return daddy_context_->get_score_context ();
436 Context::get_output_def () const
438 return daddy_context_ ? daddy_context_->get_output_def () : 0;
446 Context::now_mom () const
448 Context const *p = this;
449 while (p->daddy_context_)
450 p = p->daddy_context_;
452 return p->now_mom ();
456 Context::print_smob (SCM s, SCM port, scm_print_state *)
458 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
460 scm_puts ("#<", port);
461 scm_puts (sc->class_name (), port);
462 if (Context_def *d = unsmob_context_def (sc->definition_))
464 scm_puts (" ", port);
465 scm_display (d->get_context_name (), port);
468 if (!sc->id_string_.empty ())
470 scm_puts ("=", port);
471 scm_puts (sc->id_string_.c_str (), port);
474 scm_puts (" ", port);
476 scm_display (sc->context_list_, port);
477 scm_puts (" >", port);
483 Context::get_grob_key (string name)
485 return key_manager_.get_grob_key (now_mom (), name);
489 Context::get_context_key (string name, string id)
491 return key_manager_.get_context_key (now_mom (), name, id);
495 Context::mark_smob (SCM sm)
497 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
498 me->key_manager_.gc_mark();
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 if (me->implementation_)
506 scm_gc_mark (me->implementation_->self_scm ());
508 return me->properties_scm_;
511 IMPLEMENT_SMOBS (Context);
512 IMPLEMENT_DEFAULT_EQUAL_P (Context);
513 IMPLEMENT_TYPE_P (Context, "ly:context?");
516 Context::try_music (Music *m)
518 Translator_group *t = implementation ();
522 bool b = t->try_music (m);
523 if (!b && daddy_context_)
524 b = daddy_context_->try_music (m);
530 Context::get_global_context () const
532 if (dynamic_cast<Global_context *> ((Context *) this))
533 return dynamic_cast<Global_context *> ((Context *) this);
536 return daddy_context_->get_global_context ();
538 programming_error ("no Global context");
543 Context::get_parent_context () const
545 return daddy_context_;
549 Context::clear_key_disambiguations ()
551 if (!use_object_keys)
554 key_manager_.clear ();
555 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
556 unsmob_context (scm_car (s))->clear_key_disambiguations ();
560 Ugh. Where to put this?
563 measure_length (Context const *context)
565 SCM l = context->get_property ("measureLength");
567 if (unsmob_moment (l))
568 length = unsmob_moment (l)->main_part_;
573 measure_position (Context const *context)
575 SCM sm = context->get_property ("measurePosition");
578 if (unsmob_moment (sm))
580 m = *unsmob_moment (sm);
582 if (m.main_part_ < Rational (0))
584 Rational length (measure_length (context));
585 while (m.main_part_ < Rational (0))
586 m.main_part_ += length;
595 set_context_property_on_children (Context *trans, SCM sym, SCM val)
597 trans->internal_set_property (sym, ly_deep_copy (val));
598 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
600 Context *trg = unsmob_context (scm_car (p));
601 set_context_property_on_children (trg, sym, ly_deep_copy (val));