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"
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 &)
54 Context::properties_dict () const
56 return Scheme_hash_table::unsmob (properties_scm_);
60 Context::add_context (Context *t)
62 SCM ts = t->self_scm ();
63 context_list_ = ly_append2 (context_list_,
64 scm_cons (ts, SCM_EOL));
66 t->daddy_context_ = this;
72 Context_def *td = unsmob_context_def (t->definition_);
74 /* This cannot move before add_context (), because \override
75 operations require that we are in the hierarchy. */
76 td->apply_default_property_operations (t);
78 recurse_over_translators (t,
79 &Translator::initialize,
80 &Translator_group::initialize,
86 Context::get_key () const
91 Context::Context (Object_key const *key)
99 properties_scm_ = SCM_EOL;
100 accepts_list_ = SCM_EOL;
101 context_list_ = SCM_EOL;
102 definition_ = SCM_EOL;
106 Scheme_hash_table *tab = new Scheme_hash_table;
107 properties_scm_ = tab->unprotect ();
114 ((Object_key *)key)->unprotect ();
117 /* TODO: this shares code with find_create_context (). */
119 Context::create_unique_context (SCM n, SCM operations)
122 Don't create multiple score contexts.
124 if (dynamic_cast<Global_context *> (this)
125 && dynamic_cast<Global_context *> (this)->get_score_context ())
126 return get_score_context ()->create_unique_context (n, operations);
129 TODO: use accepts_list_.
131 Link_array<Context_def> path
132 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
136 Context *current = this;
138 // start at 1. The first one (index 0) will be us.
139 for (int i = 0; i < path.size (); i++)
141 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
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 (n, operations);
160 warning (_f ("can't find or create new `%s'",
161 ly_symbol2string (n).to_str0 ()));
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 Link_array<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 (int 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).to_str0 (), id));
230 Context::create_context (Context_def *cdef,
234 String type = ly_symbol2string (cdef->get_context_name ());
235 Object_key const *key = get_context_key (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 Context::get_context_key (String type, String id)
249 if (!use_object_keys)
252 String now_key = type + "@" + id;
254 int disambiguation_count = 0;
255 if (context_counts_.find (now_key) != context_counts_.end ())
256 disambiguation_count = context_counts_[now_key];
258 context_counts_[now_key] = disambiguation_count + 1;
260 return new Lilypond_context_key (get_key (),
263 disambiguation_count);
267 Context::get_grob_key (String name)
269 if (!use_object_keys)
272 int disambiguation_count = 0;
273 if (grob_counts_.find (name) != grob_counts_.end ())
274 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
335 if (profile_property_accesses)
336 note_property_access (&context_property_lookup_table, sym);
339 if (properties_dict ()->try_retrieve (sym, value))
340 return (Context *)this;
342 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
346 return SCM_EOL when not found.
349 Context::internal_get_property (SCM sym) const
352 if (profile_property_accesses)
353 note_property_access (&context_property_lookup_table, sym);
357 if (properties_dict ()->try_retrieve (sym, &val))
361 return daddy_context_->internal_get_property (sym);
367 Context::is_alias (SCM sym) const
369 if (sym == ly_symbol2scm ("Bottom")
370 && !scm_is_pair (accepts_list_))
372 if (sym == unsmob_context_def (definition_)->get_context_name ())
375 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
379 Context::add_alias (SCM sym)
381 aliases_ = scm_cons (sym, aliases_);
385 Context::internal_set_property (SCM sym, SCM val)
388 if (do_internal_type_checking_global)
389 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
392 properties_dict ()->set (sym, val);
396 TODO: look up to check whether we have inherited var?
399 Context::unset_property (SCM sym)
401 properties_dict ()->remove (sym);
405 Remove a context from the hierarchy.
408 Context::remove_context (Context *trans)
412 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
413 trans->daddy_context_ = 0;
418 ID == "" means accept any ID.
421 find_context_below (Context *where,
424 if (where->is_alias (type))
426 if (id == "" || where->id_string () == id)
431 for (SCM s = where->children_contexts ();
432 !found && scm_is_pair (s); s = scm_cdr (s))
434 Context *tr = unsmob_context (scm_car (s));
436 found = find_context_below (tr, type, id);
443 Context::properties_as_alist () const
445 return properties_dict ()->to_alist ();
449 Context::context_name_symbol () const
451 Context_def *td = unsmob_context_def (definition_);
452 return td->get_context_name ();
456 Context::context_name () const
458 return ly_symbol2string (context_name_symbol ());
462 Context::get_score_context () const
464 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
466 else if (daddy_context_)
467 return daddy_context_->get_score_context ();
473 Context::get_output_def () const
475 return daddy_context_ ? daddy_context_->get_output_def () : 0;
483 Context::now_mom () const
485 Context const *p = this;
486 while (p->daddy_context_)
487 p = p->daddy_context_;
489 return p->now_mom ();
493 Context::print_smob (SCM s, SCM port, scm_print_state *)
495 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
497 scm_puts ("#<", port);
498 scm_puts (classname (sc), port);
499 if (Context_def *d = unsmob_context_def (sc->definition_))
501 scm_puts (" ", port);
502 scm_display (d->get_context_name (), port);
505 if (!sc->id_string_.is_empty ())
507 scm_puts ("=", port);
508 scm_puts (sc->id_string_.to_str0 (), port);
511 scm_puts (" ", port);
513 scm_display (sc->context_list_, port);
514 scm_puts (" >", port);
520 Context::mark_smob (SCM sm)
522 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
524 scm_gc_mark (me->key_->self_scm ());
526 scm_gc_mark (me->context_list_);
527 scm_gc_mark (me->aliases_);
528 scm_gc_mark (me->definition_);
529 scm_gc_mark (me->properties_scm_);
530 scm_gc_mark (me->accepts_list_);
531 if (me->implementation_)
532 scm_gc_mark (me->implementation_->self_scm ());
534 return me->properties_scm_;
537 IMPLEMENT_SMOBS (Context);
538 IMPLEMENT_DEFAULT_EQUAL_P (Context);
539 IMPLEMENT_TYPE_P (Context, "ly:context?");
542 Context::try_music (Music *m)
544 Translator_group *t = implementation ();
548 bool b = t->try_music (m);
549 if (!b && daddy_context_)
550 b = daddy_context_->try_music (m);
556 Context::get_global_context () const
558 if (dynamic_cast<Global_context *> ((Context *) this))
559 return dynamic_cast<Global_context *> ((Context *) this);
562 return daddy_context_->get_global_context ();
564 programming_error ("no Global context");
569 Context::get_parent_context () const
571 return daddy_context_;
575 Context::clear_key_disambiguations ()
577 if (!use_object_keys)
580 grob_counts_.clear ();
581 context_counts_.clear ();
582 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
583 unsmob_context (scm_car (s))->clear_key_disambiguations ();
587 Ugh. Where to put this?
590 measure_length (Context const *context)
592 SCM l = context->get_property ("measureLength");
594 if (unsmob_moment (l))
595 length = unsmob_moment (l)->main_part_;
600 measure_position (Context const *context)
602 SCM sm = context->get_property ("measurePosition");
605 if (unsmob_moment (sm))
607 m = *unsmob_moment (sm);
609 if (m.main_part_ < Rational (0))
611 Rational length (measure_length (context));
612 while (m.main_part_ < Rational (0))
613 m.main_part_ += length;