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 "dispatcher.hh"
13 #include "international.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<Global_context const *> (daddy_context_);
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 &src)
50 : key_manager_ (src.key_manager_)
56 Context::properties_dict () const
58 return Scheme_hash_table::unsmob (properties_scm_);
62 Context::add_context (Context *t)
64 SCM ts = t->self_scm ();
65 context_list_ = ly_append2 (context_list_,
66 scm_cons (ts, SCM_EOL));
68 t->daddy_context_ = this;
74 Context_def *td = unsmob_context_def (t->definition_);
76 /* This cannot move before add_context (), because \override
77 operations require that we are in the hierarchy. */
78 td->apply_default_property_operations (t);
80 recurse_over_translators (t,
81 &Translator::initialize,
82 &Translator_group::initialize,
88 Context::Context (Object_key const *key)
96 properties_scm_ = SCM_EOL;
97 accepts_list_ = SCM_EOL;
98 context_list_ = SCM_EOL;
99 definition_ = SCM_EOL;
100 definition_mods_ = SCM_EOL;
107 Scheme_hash_table *tab = new Scheme_hash_table;
108 properties_scm_ = tab->unprotect ();
109 event_source_ = new Dispatcher ();
110 event_source_->unprotect ();
111 events_below_ = new Dispatcher ();
112 events_below_->unprotect ();
118 key_manager_.unprotect();
121 /* TODO: this shares code with find_create_context (). */
123 Context::create_unique_context (SCM name, string id, SCM operations)
126 Don't create multiple score contexts.
128 Global_context *gthis = dynamic_cast<Global_context *> (this);
129 if (gthis && gthis->get_score_context ())
130 return gthis->get_score_context ()->create_unique_context (name, id, operations);
133 TODO: use accepts_list_.
135 vector<Context_def*> path
136 = unsmob_context_def (definition_)->path_to_acceptable_context (name, get_output_def ());
140 Context *current = this;
142 // start at 1. The first one (index 0) will be us.
143 for (vsize i = 0; i < path.size (); i++)
146 string id_str = "\\new";
147 if (i == path.size () - 1)
152 current = current->create_context (path[i],
161 Don't go up to Global_context, because global goes down to
165 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
166 ret = daddy_context_->create_unique_context (name, id, operations);
169 warning (_f ("can't find or create new `%s'",
170 ly_symbol2string (name).c_str ()));
177 Context::find_create_context (SCM n, string id, SCM operations)
180 Don't create multiple score contexts.
182 Global_context *gthis = dynamic_cast<Global_context *> (this);
183 if (gthis && gthis->get_score_context ())
184 return gthis->get_score_context ()->find_create_context (n, id, operations);
186 if (Context *existing = find_context_below (this, n, id))
189 if (n == ly_symbol2scm ("Bottom"))
191 Context *tg = get_default_interpreter ();
196 TODO: use accepts_list_.
198 vector<Context_def*> path
199 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
203 Context *current = this;
205 // start at 1. The first one (index 0) will be us.
206 for (vsize i = 0; i < path.size (); i++)
208 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
211 if (i == path.size () -1)
214 current = current->create_context (path[i],
223 Don't go up to Global_context, because global goes down to
227 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
228 ret = daddy_context_->find_create_context (n, id, operations);
231 warning (_f ("can't find or create `%s' called `%s'",
232 ly_symbol2string (n).c_str (), id));
239 Context::create_context (Context_def *cdef,
243 int unique = get_global_context()->new_unique();
245 // TODO: The following should be carried out by a listener.
246 string type = ly_symbol2string (cdef->get_context_name ());
247 Object_key const *key = key_manager_.get_context_key (now_mom(), type, id);
249 = cdef->instantiate (ops, key);
251 new_context->id_string_ = id;
252 new_context->unique_ = unique;
254 new_context->events_below_->register_as_listener (new_context->event_source_);
256 add_context (new_context);
257 apply_property_operations (new_context, ops);
258 events_below_->register_as_listener (new_context->events_below_);
260 // TODO: The above operations should be performed by a listener to the following event.
261 send_stream_event (this, "CreateContext",
262 ly_symbol2scm ("unique"), scm_int2num (unique),
263 ly_symbol2scm ("ops"), ops,
264 ly_symbol2scm ("type"), cdef->get_context_name (),
265 ly_symbol2scm ("id"), scm_makfrom0str (id.c_str ()));
271 Default child context as a SCM string, or something else if there is
275 Context::default_child_context_name () const
277 return scm_is_pair (accepts_list_)
278 ? scm_car (accepts_list_)
283 Context::is_bottom_context () const
285 return !scm_is_symbol (default_child_context_name ());
289 Context::get_default_interpreter ()
291 if (!is_bottom_context ())
293 SCM nm = default_child_context_name ();
294 SCM st = find_context_def (get_output_def (), nm);
296 string name = ly_symbol2string (nm);
297 Context_def *t = unsmob_context_def (st);
300 warning (_f ("can't find or create: `%s'", name.c_str ()));
301 t = unsmob_context_def (this->definition_);
304 Context *tg = create_context (t, "", SCM_EOL);
305 return tg->get_default_interpreter ();
314 Context::where_defined (SCM sym, SCM *value) const
317 if (profile_property_accesses)
318 note_property_access (&context_property_lookup_table, sym);
321 if (properties_dict ()->try_retrieve (sym, value))
322 return (Context *)this;
324 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
328 return SCM_EOL when not found.
331 Context::internal_get_property (SCM sym) const
334 if (profile_property_accesses)
335 note_property_access (&context_property_lookup_table, sym);
339 if (properties_dict ()->try_retrieve (sym, &val))
343 return daddy_context_->internal_get_property (sym);
349 Called by the send_stream_event macro. props is a 0-terminated array of
350 properties and corresponding values, interleaved. This method should not
351 be called from any other place than the send_stream_event macro.
354 Context::internal_send_stream_event (SCM type, SCM props[])
356 Stream_event *e = new Stream_event (this, type);
357 for (int i = 0; props[i]; i += 2)
359 e->internal_set_property (props[i], props[i+1]);
361 event_source_->broadcast (e);
366 Context::is_alias (SCM sym) const
368 if (sym == ly_symbol2scm ("Bottom")
369 && !scm_is_pair (accepts_list_))
371 if (sym == unsmob_context_def (definition_)->get_context_name ())
374 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
378 Context::add_alias (SCM sym)
380 aliases_ = scm_cons (sym, aliases_);
384 Context::internal_set_property (SCM sym, SCM val)
387 if (do_internal_type_checking_global)
388 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
391 properties_dict ()->set (sym, val);
395 TODO: look up to check whether we have inherited var?
398 Context::unset_property (SCM sym)
400 properties_dict ()->remove (sym);
404 Remove a context from the hierarchy.
407 Context::remove_context (Context *trans)
411 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
412 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 find_context_below (Context *where,
446 if (where->get_unique () == unique)
450 for (SCM s = where->children_contexts ();
451 !found && scm_is_pair (s); s = scm_cdr (s))
453 Context *tr = unsmob_context (scm_car (s));
455 found = find_context_below (tr, unique);
462 Context::properties_as_alist () const
464 return properties_dict ()->to_alist ();
468 Context::context_name_symbol () const
470 Context_def *td = unsmob_context_def (definition_);
471 return td->get_context_name ();
475 Context::context_name () const
477 return ly_symbol2string (context_name_symbol ());
481 Context::get_score_context () const
483 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
485 else if (daddy_context_)
486 return daddy_context_->get_score_context ();
492 Context::get_output_def () const
494 return daddy_context_ ? daddy_context_->get_output_def () : 0;
502 Context::now_mom () const
504 Context const *p = this;
505 while (p->daddy_context_)
506 p = p->daddy_context_;
508 return p->now_mom ();
512 Context::print_smob (SCM s, SCM port, scm_print_state *)
514 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
516 scm_puts ("#<", port);
517 scm_puts (sc->class_name (), port);
518 if (Context_def *d = unsmob_context_def (sc->definition_))
520 scm_puts (" ", port);
521 scm_display (d->get_context_name (), port);
524 if (!sc->id_string_.empty ())
526 scm_puts ("=", port);
527 scm_puts (sc->id_string_.c_str (), port);
530 scm_puts (" ", port);
532 scm_display (sc->context_list_, port);
533 scm_puts (" >", port);
539 Context::get_grob_key (string name)
541 return key_manager_.get_grob_key (now_mom (), name);
545 Context::get_context_key (string name, string id)
547 return key_manager_.get_context_key (now_mom (), name, id);
551 Context::mark_smob (SCM sm)
553 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
554 me->key_manager_.gc_mark();
556 scm_gc_mark (me->context_list_);
557 scm_gc_mark (me->aliases_);
558 scm_gc_mark (me->definition_);
559 scm_gc_mark (me->definition_mods_);
560 scm_gc_mark (me->properties_scm_);
561 scm_gc_mark (me->accepts_list_);
562 if (me->implementation_)
563 scm_gc_mark (me->implementation_->self_scm ());
564 if (me->event_source_) scm_gc_mark (me->event_source_->self_scm ());
565 if (me->events_below_) scm_gc_mark (me->events_below_->self_scm ());
567 return me->properties_scm_;
570 IMPLEMENT_SMOBS (Context);
571 IMPLEMENT_DEFAULT_EQUAL_P (Context);
572 IMPLEMENT_TYPE_P (Context, "ly:context?");
575 Context::try_music (Music *m)
577 Translator_group *t = implementation ();
581 bool b = t->try_music (m);
582 if (!b && daddy_context_)
583 b = daddy_context_->try_music (m);
589 Context::get_global_context () const
591 if (dynamic_cast<Global_context *> ((Context *) this))
592 return dynamic_cast<Global_context *> ((Context *) this);
595 return daddy_context_->get_global_context ();
597 programming_error ("no Global context");
602 Context::get_parent_context () const
604 return daddy_context_;
608 Context::clear_key_disambiguations ()
610 if (!use_object_keys)
613 key_manager_.clear ();
614 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
615 unsmob_context (scm_car (s))->clear_key_disambiguations ();
619 Ugh. Where to put this?
622 measure_length (Context const *context)
624 SCM l = context->get_property ("measureLength");
626 if (unsmob_moment (l))
627 length = unsmob_moment (l)->main_part_;
632 measure_position (Context const *context)
634 SCM sm = context->get_property ("measurePosition");
637 if (unsmob_moment (sm))
639 m = *unsmob_moment (sm);
641 if (m.main_part_ < Rational (0))
643 Rational length (measure_length (context));
644 while (m.main_part_ < Rational (0))
645 m.main_part_ += length;
654 set_context_property_on_children (Context *trans, SCM sym, SCM val)
656 trans->internal_set_property (sym, ly_deep_copy (val));
657 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
659 Context *trg = unsmob_context (scm_car (p));
660 set_context_property_on_children (trg, sym, ly_deep_copy (val));
665 melisma_busy (Context *tr)
667 SCM melisma_properties = tr->get_property ("melismaBusyProperties");
670 for (; scm_is_pair (melisma_properties);
671 melisma_properties = scm_cdr (melisma_properties))
672 busy = busy || to_boolean (tr->internal_get_property (scm_car (melisma_properties)));