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 "lilypond-key.hh"
14 #include "ly-smobs.icc"
16 #include "output-def.hh"
18 #include "program-option.hh"
19 #include "scm-hash.hh"
20 #include "score-context.hh"
21 #include "translator-group.hh"
25 Context::is_removable () const
27 return context_list_ == SCM_EOL && ! iterator_count_
28 && !dynamic_cast<Score_context const *> (this);
32 Context::check_removal ()
34 for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
36 Context *trg = unsmob_context (scm_car (p));
38 trg->check_removal ();
39 if (trg->is_removable ())
41 recurse_over_translators (trg, &Translator::finalize,
42 &Translator_group::finalize,
49 Context::Context (Context const &)
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 ();
110 ((Object_key *)key)->unprotect ();
113 /* TODO: this shares code with find_create_context (). */
115 Context::create_unique_context (SCM n, 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 (n, operations);
125 TODO: use accepts_list_.
127 vector<Context_def*> path
128 = unsmob_context_def (definition_)->path_to_acceptable_context (n, 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++)
137 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
139 current = current->create_context (path[i],
148 Don't go up to Global_context, because global goes down to
152 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
153 ret = daddy_context_->create_unique_context (n, operations);
156 warning (_f ("can't find or create new `%s'",
157 ly_symbol2string (n).c_str ()));
164 Context::find_create_context (SCM n, string id, SCM operations)
167 Don't create multiple score contexts.
169 if (dynamic_cast<Global_context *> (this)
170 && dynamic_cast<Global_context *> (this)->get_score_context ())
171 return get_score_context ()->find_create_context (n, id, operations);
173 if (Context *existing = find_context_below (this, n, id))
176 if (n == ly_symbol2scm ("Bottom"))
178 Context *tg = get_default_interpreter ();
183 TODO: use accepts_list_.
185 vector<Context_def*> path
186 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
190 Context *current = this;
192 // start at 1. The first one (index 0) will be us.
193 for (vsize i = 0; i < path.size (); i++)
195 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
198 if (i == path.size () -1)
201 current = current->create_context (path[i],
210 Don't go up to Global_context, because global goes down to
214 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
215 ret = daddy_context_->find_create_context (n, id, operations);
218 warning (_f ("can't find or create `%s' called `%s'",
219 ly_symbol2string (n).c_str (), id));
226 Context::create_context (Context_def *cdef,
230 string type = ly_symbol2string (cdef->get_context_name ());
231 Object_key const *key = get_context_key (type, id);
233 = cdef->instantiate (ops, key);
235 new_context->id_string_ = id;
236 add_context (new_context);
237 apply_property_operations (new_context, ops);
243 Context::get_context_key (string type, string id)
245 if (!use_object_keys)
248 string now_key = type + "@" + id;
250 int disambiguation_count = 0;
251 if (context_counts_.find (now_key) != context_counts_.end ())
252 disambiguation_count = context_counts_[now_key];
254 context_counts_[now_key] = disambiguation_count + 1;
256 return new Lilypond_context_key (key (),
259 disambiguation_count);
263 Context::get_grob_key (string name)
265 if (!use_object_keys)
268 return create_grob_key (name);
272 We want to have a key for some objects anyway, so we can invent a
273 unique identifier for each (book,score) tuple.
276 Context::create_grob_key (string name)
278 int disambiguation_count = 0;
279 if (grob_counts_.find (name) != grob_counts_.end ())
280 disambiguation_count = grob_counts_[name];
281 grob_counts_[name] = disambiguation_count + 1;
283 Object_key *k = new Lilypond_grob_key (key (),
286 disambiguation_count);
292 Default child context as a SCM string, or something else if there is
296 Context::default_child_context_name () const
298 return scm_is_pair (accepts_list_)
299 ? scm_car (accepts_list_)
304 Context::is_bottom_context () const
306 return !scm_is_symbol (default_child_context_name ());
310 Context::get_default_interpreter ()
312 if (!is_bottom_context ())
314 SCM nm = default_child_context_name ();
315 SCM st = find_context_def (get_output_def (), nm);
317 string name = ly_symbol2string (nm);
318 Context_def *t = unsmob_context_def (st);
321 warning (_f ("can't find or create: `%s'", name.c_str ()));
322 t = unsmob_context_def (this->definition_);
325 Context *tg = create_context (t, "", SCM_EOL);
326 if (!tg->is_bottom_context ())
327 return tg->get_default_interpreter ();
338 Context::where_defined (SCM sym, SCM *value) const
341 if (profile_property_accesses)
342 note_property_access (&context_property_lookup_table, sym);
345 if (properties_dict ()->try_retrieve (sym, value))
346 return (Context *)this;
348 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
352 return SCM_EOL when not found.
355 Context::internal_get_property (SCM sym) const
358 if (profile_property_accesses)
359 note_property_access (&context_property_lookup_table, sym);
363 if (properties_dict ()->try_retrieve (sym, &val))
367 return daddy_context_->internal_get_property (sym);
373 Context::is_alias (SCM sym) const
375 if (sym == ly_symbol2scm ("Bottom")
376 && !scm_is_pair (accepts_list_))
378 if (sym == unsmob_context_def (definition_)->get_context_name ())
381 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
385 Context::add_alias (SCM sym)
387 aliases_ = scm_cons (sym, aliases_);
391 Context::internal_set_property (SCM sym, SCM val)
394 if (do_internal_type_checking_global)
395 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
398 properties_dict ()->set (sym, val);
402 TODO: look up to check whether we have inherited var?
405 Context::unset_property (SCM sym)
407 properties_dict ()->remove (sym);
411 Remove a context from the hierarchy.
414 Context::remove_context (Context *trans)
418 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
419 trans->daddy_context_ = 0;
424 ID == "" means accept any ID.
427 find_context_below (Context *where,
430 if (where->is_alias (type))
432 if (id == "" || where->id_string () == id)
437 for (SCM s = where->children_contexts ();
438 !found && scm_is_pair (s); s = scm_cdr (s))
440 Context *tr = unsmob_context (scm_car (s));
442 found = find_context_below (tr, type, id);
449 Context::properties_as_alist () const
451 return properties_dict ()->to_alist ();
455 Context::context_name_symbol () const
457 Context_def *td = unsmob_context_def (definition_);
458 return td->get_context_name ();
462 Context::context_name () const
464 return ly_symbol2string (context_name_symbol ());
468 Context::get_score_context () const
470 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
472 else if (daddy_context_)
473 return daddy_context_->get_score_context ();
479 Context::get_output_def () const
481 return daddy_context_ ? daddy_context_->get_output_def () : 0;
489 Context::now_mom () const
491 Context const *p = this;
492 while (p->daddy_context_)
493 p = p->daddy_context_;
495 return p->now_mom ();
499 Context::print_smob (SCM s, SCM port, scm_print_state *)
501 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
503 scm_puts ("#<", port);
504 scm_puts (sc->class_name (), port);
505 if (Context_def *d = unsmob_context_def (sc->definition_))
507 scm_puts (" ", port);
508 scm_display (d->get_context_name (), port);
511 if (!sc->id_string_.empty ())
513 scm_puts ("=", port);
514 scm_puts (sc->id_string_.c_str (), port);
517 scm_puts (" ", port);
519 scm_display (sc->context_list_, port);
520 scm_puts (" >", port);
526 Context::mark_smob (SCM sm)
528 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
530 scm_gc_mark (me->key_->self_scm ());
532 scm_gc_mark (me->context_list_);
533 scm_gc_mark (me->aliases_);
534 scm_gc_mark (me->definition_);
535 scm_gc_mark (me->properties_scm_);
536 scm_gc_mark (me->accepts_list_);
537 if (me->implementation_)
538 scm_gc_mark (me->implementation_->self_scm ());
540 return me->properties_scm_;
543 IMPLEMENT_SMOBS (Context);
544 IMPLEMENT_DEFAULT_EQUAL_P (Context);
545 IMPLEMENT_TYPE_P (Context, "ly:context?");
548 Context::try_music (Music *m)
550 Translator_group *t = implementation ();
554 bool b = t->try_music (m);
555 if (!b && daddy_context_)
556 b = daddy_context_->try_music (m);
562 Context::get_global_context () const
564 if (dynamic_cast<Global_context *> ((Context *) this))
565 return dynamic_cast<Global_context *> ((Context *) this);
568 return daddy_context_->get_global_context ();
570 programming_error ("no Global context");
575 Context::get_parent_context () const
577 return daddy_context_;
581 Context::clear_key_disambiguations ()
583 if (!use_object_keys)
586 grob_counts_.clear ();
587 context_counts_.clear ();
588 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
589 unsmob_context (scm_car (s))->clear_key_disambiguations ();
593 Ugh. Where to put this?
596 measure_length (Context const *context)
598 SCM l = context->get_property ("measureLength");
600 if (unsmob_moment (l))
601 length = unsmob_moment (l)->main_part_;
606 measure_position (Context const *context)
608 SCM sm = context->get_property ("measurePosition");
611 if (unsmob_moment (sm))
613 m = *unsmob_moment (sm);
615 if (m.main_part_ < Rational (0))
617 Rational length (measure_length (context));
618 while (m.main_part_ < Rational (0))
619 m.main_part_ += length;
628 set_context_property_on_children (Context *trans, SCM sym, SCM val)
630 trans->internal_set_property (sym, ly_deep_copy (val));
631 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
633 Context *trg = unsmob_context (scm_car (p));
634 set_context_property_on_children (trg, sym, ly_deep_copy (val));