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 "program-option.hh"
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,
40 &Translator_group::finalize,
47 Context::Context (Context const &)
53 Context::properties_dict () const
55 return Scheme_hash_table::unsmob (properties_scm_);
59 Context::add_context (Context *t)
61 SCM ts = t->self_scm ();
62 context_list_ = ly_append2 (context_list_,
63 scm_cons (ts, SCM_EOL));
65 t->daddy_context_ = this;
70 scm_gc_unprotect_object (ts);
71 Context_def *td = unsmob_context_def (t->definition_);
73 /* This cannot move before add_context (), because \override
74 operations require that we are in the hierarchy. */
75 td->apply_default_property_operations (t);
77 recurse_over_translators (t,
78 &Translator::initialize,
79 &Translator_group::initialize,
85 Context::get_key () const
90 Context::Context (Object_key const *key)
98 properties_scm_ = SCM_EOL;
99 accepts_list_ = SCM_EOL;
100 context_list_ = SCM_EOL;
101 definition_ = SCM_EOL;
104 properties_scm_ = (new Scheme_hash_table)->self_scm ();
105 scm_gc_unprotect_object (properties_scm_);
108 scm_gc_unprotect_object (key_->self_scm ());
111 /* TODO: this shares code with find_create_context (). */
113 Context::create_unique_context (SCM n, SCM operations)
116 Don't create multiple score contexts.
118 if (dynamic_cast<Global_context *> (this)
119 && dynamic_cast<Global_context *> (this)->get_score_context ())
120 return get_score_context ()->create_unique_context (n, operations);
123 TODO: use accepts_list_.
125 Link_array<Context_def> path
126 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
130 Context *current = this;
132 // start at 1. The first one (index 0) will be us.
133 for (int i = 0; i < path.size (); i++)
135 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
137 current = current->create_context (path[i],
146 Don't go up to Global_context, because global goes down to
150 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
151 ret = daddy_context_->create_unique_context (n, operations);
154 warning (_f ("can't find or create new `%s'",
155 ly_symbol2string (n).to_str0 ()));
162 Context::find_create_context (SCM n, String id, SCM operations)
165 Don't create multiple score contexts.
167 if (dynamic_cast<Global_context *> (this)
168 && dynamic_cast<Global_context *> (this)->get_score_context ())
169 return get_score_context ()->find_create_context (n, id, operations);
171 if (Context *existing = find_context_below (this, n, id))
174 if (n == ly_symbol2scm ("Bottom"))
176 Context *tg = get_default_interpreter ();
181 TODO: use accepts_list_.
183 Link_array<Context_def> path
184 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
188 Context *current = this;
190 // start at 1. The first one (index 0) will be us.
191 for (int i = 0; i < path.size (); i++)
193 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
196 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).to_str0 (), 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 ())
253 disambiguation_count = context_counts_[now_key];
256 context_counts_[now_key] = disambiguation_count + 1;
258 return new Lilypond_context_key (get_key (),
261 disambiguation_count);
265 Context::get_grob_key (String name)
267 if (!use_object_keys)
270 int disambiguation_count = 0;
271 if (grob_counts_.find (name) != grob_counts_.end ())
273 disambiguation_count = grob_counts_[name];
275 grob_counts_[name] = disambiguation_count + 1;
277 Object_key *k = new Lilypond_grob_key (get_key (),
280 disambiguation_count);
286 Default child context as a SCM string, or something else if there is
290 Context::default_child_context_name () const
292 return scm_is_pair (accepts_list_)
293 ? scm_car (accepts_list_)
298 Context::is_bottom_context () const
300 return !scm_is_symbol (default_child_context_name ());
304 Context::get_default_interpreter ()
306 if (!is_bottom_context ())
308 SCM nm = default_child_context_name ();
309 SCM st = find_context_def (get_output_def (), nm);
311 String name = ly_symbol2string (nm);
312 Context_def *t = unsmob_context_def (st);
315 warning (_f ("can't find or create: `%s'", name.to_str0 ()));
316 t = unsmob_context_def (this->definition_);
319 Context *tg = create_context (t, "", SCM_EOL);
320 if (!tg->is_bottom_context ())
321 return tg->get_default_interpreter ();
332 Context::where_defined (SCM sym, SCM *value) const
334 if (properties_dict ()->try_retrieve (sym, value))
336 return (Context *)this;
339 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
342 SCM context_property_lookup_table;
343 LY_DEFINE(ly_context_property_lookup_stats, "ly:context-property-lookup-stats",
347 return context_property_lookup_table ? context_property_lookup_table
348 : scm_c_make_hash_table (1);
353 return SCM_EOL when not found.
356 Context::internal_get_property (SCM sym) const
359 if (profile_property_accesses)
361 extern void note_property_access (SCM *table, SCM sym);
362 note_property_access (&context_property_lookup_table, sym);
367 if (properties_dict ()->try_retrieve (sym, &val))
371 return daddy_context_->internal_get_property (sym);
377 Context::is_alias (SCM sym) const
379 if (sym == ly_symbol2scm ("Bottom")
380 && !scm_is_pair (accepts_list_))
382 if (sym == unsmob_context_def (definition_)->get_context_name ())
385 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
389 Context::add_alias (SCM sym)
391 aliases_ = scm_cons (sym, aliases_);
395 Context::internal_set_property (SCM sym, SCM val)
398 if (do_internal_type_checking_global)
399 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
402 properties_dict ()->set (sym, val);
406 TODO: look up to check whether we have inherited var?
409 Context::unset_property (SCM sym)
411 properties_dict ()->remove (sym);
415 Remove a context from the hierarchy.
418 Context::remove_context (Context *trans)
422 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
423 trans->daddy_context_ = 0;
428 ID == "" means accept any ID.
431 find_context_below (Context *where,
434 if (where->is_alias (type))
436 if (id == "" || where->id_string () == id)
441 for (SCM s = where->children_contexts ();
442 !found && scm_is_pair (s); s = scm_cdr (s))
444 Context *tr = unsmob_context (scm_car (s));
446 found = find_context_below (tr, type, id);
453 Context::properties_as_alist () const
455 return properties_dict ()->to_alist ();
459 Context::context_name_symbol () const
461 Context_def *td = unsmob_context_def (definition_);
462 return td->get_context_name ();
466 Context::context_name () const
468 return ly_symbol2string (context_name_symbol ());
472 Context::get_score_context () const
474 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
476 else if (daddy_context_)
477 return daddy_context_->get_score_context ();
483 Context::get_output_def () const
485 return daddy_context_ ? daddy_context_->get_output_def () : 0;
493 Context::now_mom () const
495 return daddy_context_->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 (classname (sc), 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_.is_empty ())
513 scm_puts ("=", port);
514 scm_puts (sc->id_string_.to_str0 (), 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))
590 unsmob_context (scm_car (s))->clear_key_disambiguations ();
596 Ugh. Where to put this?
599 measure_length (Context const *context)
601 SCM l = context->get_property ("measureLength");
603 if (unsmob_moment (l))
604 length = unsmob_moment (l)->main_part_;
609 measure_position (Context const *context)
611 SCM sm = context->get_property ("measurePosition");
614 if (unsmob_moment (sm))
616 m = *unsmob_moment (sm);
618 if (m.main_part_ < Rational (0))
620 Rational length (measure_length (context));
621 while (m.main_part_ < Rational (0))
622 m.main_part_ += length;