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::Context (Object_key const *key)
94 properties_scm_ = SCM_EOL;
95 accepts_list_ = SCM_EOL;
96 context_list_ = SCM_EOL;
97 definition_ = SCM_EOL;
101 Scheme_hash_table *tab = new Scheme_hash_table;
102 properties_scm_ = tab->unprotect ();
109 ((Object_key *)key)->unprotect ();
112 /* TODO: this shares code with find_create_context (). */
114 Context::create_unique_context (SCM n, 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 (n, operations);
124 TODO: use accepts_list_.
126 Link_array<Context_def> path
127 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
131 Context *current = this;
133 // start at 1. The first one (index 0) will be us.
134 for (int i = 0; i < path.size (); i++)
136 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
138 current = current->create_context (path[i],
147 Don't go up to Global_context, because global goes down to
151 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
152 ret = daddy_context_->create_unique_context (n, operations);
155 warning (_f ("can't find or create new `%s'",
156 ly_symbol2string (n).to_str0 ()));
163 Context::find_create_context (SCM n, String id, SCM operations)
166 Don't create multiple score contexts.
168 if (dynamic_cast<Global_context *> (this)
169 && dynamic_cast<Global_context *> (this)->get_score_context ())
170 return get_score_context ()->find_create_context (n, id, operations);
172 if (Context *existing = find_context_below (this, n, id))
175 if (n == ly_symbol2scm ("Bottom"))
177 Context *tg = get_default_interpreter ();
182 TODO: use accepts_list_.
184 Link_array<Context_def> path
185 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
189 Context *current = this;
191 // start at 1. The first one (index 0) will be us.
192 for (int i = 0; i < path.size (); i++)
194 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
197 if (i == path.size () -1)
200 current = current->create_context (path[i],
209 Don't go up to Global_context, because global goes down to
213 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
214 ret = daddy_context_->find_create_context (n, id, operations);
217 warning (_f ("can't find or create `%s' called `%s'",
218 ly_symbol2string (n).to_str0 (), id));
225 Context::create_context (Context_def *cdef,
229 String type = ly_symbol2string (cdef->get_context_name ());
230 Object_key const *key = get_context_key (type, id);
232 = cdef->instantiate (ops, key);
234 new_context->id_string_ = id;
235 add_context (new_context);
236 apply_property_operations (new_context, ops);
242 Context::get_context_key (String type, String id)
244 if (!use_object_keys)
247 String now_key = type + "@" + id;
249 int disambiguation_count = 0;
250 if (context_counts_.find (now_key) != context_counts_.end ())
251 disambiguation_count = context_counts_[now_key];
253 context_counts_[now_key] = disambiguation_count + 1;
255 return new Lilypond_context_key (key (),
258 disambiguation_count);
262 Context::get_grob_key (String name)
264 if (!use_object_keys)
267 int disambiguation_count = 0;
268 if (grob_counts_.find (name) != grob_counts_.end ())
269 disambiguation_count = grob_counts_[name];
270 grob_counts_[name] = disambiguation_count + 1;
272 Object_key *k = new Lilypond_grob_key (key (),
275 disambiguation_count);
281 Default child context as a SCM string, or something else if there is
285 Context::default_child_context_name () const
287 return scm_is_pair (accepts_list_)
288 ? scm_car (accepts_list_)
293 Context::is_bottom_context () const
295 return !scm_is_symbol (default_child_context_name ());
299 Context::get_default_interpreter ()
301 if (!is_bottom_context ())
303 SCM nm = default_child_context_name ();
304 SCM st = find_context_def (get_output_def (), nm);
306 String name = ly_symbol2string (nm);
307 Context_def *t = unsmob_context_def (st);
310 warning (_f ("can't find or create: `%s'", name.to_str0 ()));
311 t = unsmob_context_def (this->definition_);
314 Context *tg = create_context (t, "", SCM_EOL);
315 if (!tg->is_bottom_context ())
316 return tg->get_default_interpreter ();
327 Context::where_defined (SCM sym, SCM *value) const
330 if (profile_property_accesses)
331 note_property_access (&context_property_lookup_table, sym);
334 if (properties_dict ()->try_retrieve (sym, value))
335 return (Context *)this;
337 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
341 return SCM_EOL when not found.
344 Context::internal_get_property (SCM sym) const
347 if (profile_property_accesses)
348 note_property_access (&context_property_lookup_table, sym);
352 if (properties_dict ()->try_retrieve (sym, &val))
356 return daddy_context_->internal_get_property (sym);
362 Context::is_alias (SCM sym) const
364 if (sym == ly_symbol2scm ("Bottom")
365 && !scm_is_pair (accepts_list_))
367 if (sym == unsmob_context_def (definition_)->get_context_name ())
370 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
374 Context::add_alias (SCM sym)
376 aliases_ = scm_cons (sym, aliases_);
380 Context::internal_set_property (SCM sym, SCM val)
383 if (do_internal_type_checking_global)
384 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
387 properties_dict ()->set (sym, val);
391 TODO: look up to check whether we have inherited var?
394 Context::unset_property (SCM sym)
396 properties_dict ()->remove (sym);
400 Remove a context from the hierarchy.
403 Context::remove_context (Context *trans)
407 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
408 trans->daddy_context_ = 0;
413 ID == "" means accept any ID.
416 find_context_below (Context *where,
419 if (where->is_alias (type))
421 if (id == "" || where->id_string () == id)
426 for (SCM s = where->children_contexts ();
427 !found && scm_is_pair (s); s = scm_cdr (s))
429 Context *tr = unsmob_context (scm_car (s));
431 found = find_context_below (tr, type, id);
438 Context::properties_as_alist () const
440 return properties_dict ()->to_alist ();
444 Context::context_name_symbol () const
446 Context_def *td = unsmob_context_def (definition_);
447 return td->get_context_name ();
451 Context::context_name () const
453 return ly_symbol2string (context_name_symbol ());
457 Context::get_score_context () const
459 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
461 else if (daddy_context_)
462 return daddy_context_->get_score_context ();
468 Context::get_output_def () const
470 return daddy_context_ ? daddy_context_->get_output_def () : 0;
478 Context::now_mom () const
480 Context const *p = this;
481 while (p->daddy_context_)
482 p = p->daddy_context_;
484 return p->now_mom ();
488 Context::print_smob (SCM s, SCM port, scm_print_state *)
490 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
492 scm_puts ("#<", port);
493 scm_puts (sc->class_name (), port);
494 if (Context_def *d = unsmob_context_def (sc->definition_))
496 scm_puts (" ", port);
497 scm_display (d->get_context_name (), port);
500 if (!sc->id_string_.is_empty ())
502 scm_puts ("=", port);
503 scm_puts (sc->id_string_.to_str0 (), port);
506 scm_puts (" ", port);
508 scm_display (sc->context_list_, port);
509 scm_puts (" >", port);
515 Context::mark_smob (SCM sm)
517 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
519 scm_gc_mark (me->key_->self_scm ());
521 scm_gc_mark (me->context_list_);
522 scm_gc_mark (me->aliases_);
523 scm_gc_mark (me->definition_);
524 scm_gc_mark (me->properties_scm_);
525 scm_gc_mark (me->accepts_list_);
526 if (me->implementation_)
527 scm_gc_mark (me->implementation_->self_scm ());
529 return me->properties_scm_;
532 IMPLEMENT_SMOBS (Context);
533 IMPLEMENT_DEFAULT_EQUAL_P (Context);
534 IMPLEMENT_TYPE_P (Context, "ly:context?");
537 Context::try_music (Music *m)
539 Translator_group *t = implementation ();
543 bool b = t->try_music (m);
544 if (!b && daddy_context_)
545 b = daddy_context_->try_music (m);
551 Context::get_global_context () const
553 if (dynamic_cast<Global_context *> ((Context *) this))
554 return dynamic_cast<Global_context *> ((Context *) this);
557 return daddy_context_->get_global_context ();
559 programming_error ("no Global context");
564 Context::get_parent_context () const
566 return daddy_context_;
570 Context::clear_key_disambiguations ()
572 if (!use_object_keys)
575 grob_counts_.clear ();
576 context_counts_.clear ();
577 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
578 unsmob_context (scm_car (s))->clear_key_disambiguations ();
582 Ugh. Where to put this?
585 measure_length (Context const *context)
587 SCM l = context->get_property ("measureLength");
589 if (unsmob_moment (l))
590 length = unsmob_moment (l)->main_part_;
595 measure_position (Context const *context)
597 SCM sm = context->get_property ("measurePosition");
600 if (unsmob_moment (sm))
602 m = *unsmob_moment (sm);
604 if (m.main_part_ < Rational (0))
606 Rational length (measure_length (context));
607 while (m.main_part_ < Rational (0))
608 m.main_part_ += length;