2 context.cc -- implement Context
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "context-def.hh"
12 #include "dispatcher.hh"
13 #include "global-context.hh"
14 #include "international.hh"
15 #include "ly-smobs.icc"
17 #include "output-def.hh"
19 #include "program-option.hh"
20 #include "scm-hash.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 *ctx = unsmob_context (scm_car (p));
38 ctx->check_removal ();
39 if (ctx->is_removable ())
41 recurse_over_translators (ctx, &Translator::finalize,
42 &Translator_group::finalize,
44 send_stream_event (ctx, "RemoveContext", 0, 0);
49 Context::Context (Context const &src)
56 Context::properties_dict () const
58 return Scheme_hash_table::unsmob (properties_scm_);
62 Context::add_context (Context *child)
64 context_list_ = ly_append2 (context_list_,
65 scm_cons (child->self_scm (), SCM_EOL));
67 child->daddy_context_ = this;
68 this->events_below_->register_as_listener (child->events_below_);
78 properties_scm_ = SCM_EOL;
79 accepts_list_ = SCM_EOL;
80 context_list_ = SCM_EOL;
81 definition_ = SCM_EOL;
82 definition_mods_ = SCM_EOL;
88 Scheme_hash_table *tab = new Scheme_hash_table;
89 properties_scm_ = tab->unprotect ();
90 event_source_ = new Dispatcher ();
91 event_source_->unprotect ();
92 events_below_ = new Dispatcher ();
93 events_below_->unprotect ();
96 /* TODO: this shares code with find_create_context (). */
98 Context::create_unique_context (SCM name, string id, SCM operations)
101 Don't create multiple score contexts.
103 Global_context *gthis = dynamic_cast<Global_context *> (this);
104 if (gthis && gthis->get_score_context ())
105 return gthis->get_score_context ()->create_unique_context (name, id, operations);
107 vector<Context_def*> path = path_to_acceptable_context (name);
110 Context *current = this;
112 // Iterate through the path and create all of the implicit contexts.
113 for (vsize i = 0; i < path.size (); i++)
116 string id_str = "\\new";
117 if (i == path.size () - 1)
122 current = current->create_context (path[i],
131 Don't go up to Global_context, because global goes down to the
135 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
136 ret = daddy_context_->create_unique_context (name, id, operations);
139 warning (_f ("cannot find or create new `%s'",
140 ly_symbol2string (name).c_str ()));
147 Context::find_create_context (SCM n, string id, SCM operations)
150 Don't create multiple score contexts.
152 Global_context *gthis = dynamic_cast<Global_context *> (this);
153 if (gthis && gthis->get_score_context ())
154 return gthis->get_score_context ()->find_create_context (n, id, operations);
156 if (Context *existing = find_context_below (this, n, id))
159 if (n == ly_symbol2scm ("Bottom"))
161 Context *tg = get_default_interpreter ();
166 TODO: use accepts_list_.
168 vector<Context_def*> path = path_to_acceptable_context (n);
172 Context *current = this;
174 // start at 1. The first one (index 0) will be us.
175 for (vsize i = 0; i < path.size (); i++)
177 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
180 if (i == path.size () -1)
183 current = current->create_context (path[i],
192 Don't go up to Global_context, because global goes down to the
196 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
197 ret = daddy_context_->find_create_context (n, id, operations);
200 warning (_f ("cannot find or create `%s' called `%s'",
201 ly_symbol2string (n).c_str (), id));
207 IMPLEMENT_LISTENER (Context, acknowledge_infant);
209 Context::acknowledge_infant (SCM sev)
211 infant_event_ = unsmob_stream_event (sev);
214 IMPLEMENT_LISTENER (Context, set_property_from_event);
216 Context::set_property_from_event (SCM sev)
218 Stream_event *ev = unsmob_stream_event (sev);
220 SCM sym = ev->get_property ("symbol");
221 if (scm_is_symbol (sym))
223 SCM val = ev->get_property ("value");
226 ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
228 set_property (sym, val);
232 IMPLEMENT_LISTENER (Context, unset_property_from_event);
234 Context::unset_property_from_event (SCM sev)
236 Stream_event *ev = unsmob_stream_event (sev);
238 SCM sym = ev->get_property ("symbol");
239 type_check_assignment (sym, SCM_EOL, ly_symbol2scm ("translation-type?"));
240 unset_property (sym);
244 Creates a new context from a CreateContext event, and sends an
245 AnnounceNewContext event to this context.
247 IMPLEMENT_LISTENER (Context, create_context_from_event);
249 Context::create_context_from_event (SCM sev)
251 Stream_event *ev = unsmob_stream_event (sev);
253 string id = ly_scm2string (ev->get_property ("id"));
254 SCM ops = ev->get_property ("ops");
255 SCM type_scm = ev->get_property ("type");
256 string type = ly_symbol2string (type_scm);
258 vector<Context_def*> path = path_to_acceptable_context (type_scm);
260 if (path.size () != 1)
262 programming_error (_f ("Invalid CreateContext event: Cannot create %s context", type.c_str ()));
265 Context_def *cdef = path[0];
267 Context *new_context = cdef->instantiate (ops);
269 new_context->id_string_ = id;
271 /* Register various listeners:
272 - Make the new context hear events that universally affect contexts
273 - connect events_below etc. properly */
274 /* We want to be the first ones to hear our own events. Therefore, wait
275 before registering events_below_ */
276 new_context->event_source ()->
277 add_listener (GET_LISTENER (new_context->create_context_from_event),
278 ly_symbol2scm ("CreateContext"));
279 new_context->event_source ()->
280 add_listener (GET_LISTENER (new_context->remove_context),
281 ly_symbol2scm ("RemoveContext"));
282 new_context->event_source ()->
283 add_listener (GET_LISTENER (new_context->change_parent),
284 ly_symbol2scm ("ChangeParent"));
285 new_context->event_source ()->
286 add_listener (GET_LISTENER (new_context->set_property_from_event),
287 ly_symbol2scm ("SetProperty"));
288 new_context->event_source ()->
289 add_listener (GET_LISTENER (new_context->unset_property_from_event),
290 ly_symbol2scm ("UnsetProperty"));
292 new_context->events_below_->register_as_listener (new_context->event_source_);
293 this->add_context (new_context);
295 new_context->unprotect ();
297 Context_def *td = unsmob_context_def (new_context->definition_);
299 /* This cannot move before add_context (), because \override
300 operations require that we are in the hierarchy. */
301 td->apply_default_property_operations (new_context);
302 apply_property_operations (new_context, ops);
304 send_stream_event (this, "AnnounceNewContext", 0,
305 ly_symbol2scm ("context"), new_context->self_scm (),
306 ly_symbol2scm ("creator"), sev);
310 Context::path_to_acceptable_context (SCM name) const
312 // definition_mods_ is a list of (symbol string), but the Context_def expects
313 // to see a list of (symbol symbol).
314 SCM accepts = SCM_EOL;
315 for (SCM s = scm_reverse (definition_mods_); scm_is_pair (s); s = scm_cdr (s))
316 if (scm_caar (s) == ly_symbol2scm ("accepts"))
318 SCM elt = scm_list_2 (scm_caar (s), scm_string_to_symbol (scm_cadar (s)));
319 accepts = scm_cons (elt, accepts);
322 return unsmob_context_def (definition_)->path_to_acceptable_context (name,
329 Context::create_context (Context_def *cdef,
334 /* TODO: This is fairly misplaced. We can fix this when we have taken out all
335 iterator specific stuff from the Context class */
337 add_listener (GET_LISTENER (acknowledge_infant),
338 ly_symbol2scm ("AnnounceNewContext"));
339 /* The CreateContext creates a new context, and sends an announcement of the
340 new context through another event. That event will be stored in
341 infant_event_ to create a return value. */
342 send_stream_event (this, "CreateContext", 0,
343 ly_symbol2scm ("ops"), ops,
344 ly_symbol2scm ("type"), cdef->get_context_name (),
345 ly_symbol2scm ("id"), ly_string2scm (id));
347 remove_listener (GET_LISTENER (acknowledge_infant),
348 ly_symbol2scm ("AnnounceNewContext"));
350 assert (infant_event_);
351 SCM infant_scm = infant_event_->get_property ("context");
352 Context *infant = unsmob_context (infant_scm);
354 if (!infant || infant->get_parent_context () != this)
356 programming_error ("create_context: can't locate newly created context");
364 Default child context as a SCM string, or something else if there is
368 Context::default_child_context_name () const
370 return scm_is_pair (accepts_list_)
371 ? scm_car (accepts_list_)
376 Context::is_bottom_context () const
378 return !scm_is_symbol (default_child_context_name ());
382 Context::get_default_interpreter ()
384 if (!is_bottom_context ())
386 SCM nm = default_child_context_name ();
387 SCM st = find_context_def (get_output_def (), nm);
389 string name = ly_symbol2string (nm);
390 Context_def *t = unsmob_context_def (st);
393 warning (_f ("cannot find or create: `%s'", name.c_str ()));
394 t = unsmob_context_def (this->definition_);
397 Context *tg = create_context (t, "", SCM_EOL);
398 return tg->get_default_interpreter ();
407 Context::where_defined (SCM sym, SCM *value) const
410 if (profile_property_accesses)
411 note_property_access (&context_property_lookup_table, sym);
414 if (properties_dict ()->try_retrieve (sym, value))
415 return (Context *)this;
417 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
421 return SCM_EOL when not found.
424 Context::internal_get_property (SCM sym) const
427 if (profile_property_accesses)
428 note_property_access (&context_property_lookup_table, sym);
432 if (properties_dict ()->try_retrieve (sym, &val))
436 return daddy_context_->internal_get_property (sym);
442 Called by the send_stream_event macro. props is a 0-terminated array of
443 properties and corresponding values, interleaved. This method should not
444 be called from any other place than the send_stream_event macro.
447 Context::internal_send_stream_event (SCM type, Input *origin, SCM props[])
449 Stream_event *e = new Stream_event (type, origin);
450 for (int i = 0; props[i]; i += 2)
452 e->set_property (props[i], props[i+1]);
454 event_source_->broadcast (e);
459 Context::is_alias (SCM sym) const
461 if (sym == ly_symbol2scm ("Bottom")
462 && !scm_is_pair (accepts_list_))
464 if (sym == unsmob_context_def (definition_)->get_context_name ())
467 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
471 Context::add_alias (SCM sym)
473 aliases_ = scm_cons (sym, aliases_);
476 /* we don't (yet) instrument context properties */
478 Context::instrumented_set_property (SCM sym, SCM val, const char*, int, const char*)
480 internal_set_property (sym, val);
484 Context::internal_set_property (SCM sym, SCM val)
486 if (do_internal_type_checking_global)
487 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
489 properties_dict ()->set (sym, val);
493 TODO: look up to check whether we have inherited var?
496 Context::unset_property (SCM sym)
498 properties_dict ()->remove (sym);
501 IMPLEMENT_LISTENER (Context, change_parent);
503 Context::change_parent (SCM sev)
505 Stream_event *ev = unsmob_stream_event (sev);
506 Context *to = unsmob_context (ev->get_property ("context"));
508 disconnect_from_parent ();
509 to->add_context (this);
513 Die. The next GC sweep should take care of the actual death.
515 IMPLEMENT_LISTENER (Context, remove_context);
517 Context::remove_context (SCM)
519 /* ugh, the translator group should listen to RemoveContext events by itself */
520 Translator_group *impl = implementation ();
522 impl->disconnect_from_context ();
523 disconnect_from_parent ();
527 Context::disconnect_from_parent ()
529 daddy_context_->events_below_->unregister_as_listener (this->events_below_);
530 daddy_context_->context_list_ = scm_delq_x (this->self_scm (), daddy_context_->context_list_);
535 ID == "" means accept any ID.
538 find_context_below (Context *where,
541 if (where->is_alias (type))
543 if (id == "" || where->id_string () == id)
548 for (SCM s = where->children_contexts ();
549 !found && scm_is_pair (s); s = scm_cdr (s))
551 Context *tr = unsmob_context (scm_car (s));
553 found = find_context_below (tr, type, id);
560 Context::properties_as_alist () const
562 return properties_dict ()->to_alist ();
566 Context::context_name_symbol () const
568 Context_def *td = unsmob_context_def (definition_);
569 return td->get_context_name ();
573 Context::context_name () const
575 return ly_symbol2string (context_name_symbol ());
579 Context::get_score_context () const
582 return daddy_context_->get_score_context ();
588 Context::get_output_def () const
590 return daddy_context_ ? daddy_context_->get_output_def () : 0;
598 Context::now_mom () const
600 Context const *p = this;
601 while (p->daddy_context_)
602 p = p->daddy_context_;
604 return p->now_mom ();
608 Context::print_smob (SCM s, SCM port, scm_print_state *)
610 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
612 scm_puts ("#<", port);
613 scm_puts (sc->class_name (), port);
614 if (Context_def *d = unsmob_context_def (sc->definition_))
616 scm_puts (" ", port);
617 scm_display (d->get_context_name (), port);
620 if (!sc->id_string_.empty ())
622 scm_puts ("=", port);
623 scm_puts (sc->id_string_.c_str (), port);
626 scm_puts (" ", port);
628 scm_display (sc->context_list_, port);
629 scm_puts (" >", port);
635 Context::mark_smob (SCM sm)
637 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
639 scm_gc_mark (me->context_list_);
640 scm_gc_mark (me->aliases_);
641 scm_gc_mark (me->definition_);
642 scm_gc_mark (me->definition_mods_);
643 scm_gc_mark (me->properties_scm_);
644 scm_gc_mark (me->accepts_list_);
646 if (me->implementation_)
647 scm_gc_mark (me->implementation_->self_scm ());
649 if (me->event_source_)
650 scm_gc_mark (me->event_source_->self_scm ());
652 if (me->events_below_)
653 scm_gc_mark (me->events_below_->self_scm ());
655 return me->properties_scm_;
658 IMPLEMENT_SMOBS (Context);
659 IMPLEMENT_DEFAULT_EQUAL_P (Context);
660 IMPLEMENT_TYPE_P (Context, "ly:context?");
663 Context::get_global_context () const
665 if (dynamic_cast<Global_context *> ((Context *) this))
666 return dynamic_cast<Global_context *> ((Context *) this);
669 return daddy_context_->get_global_context ();
671 programming_error ("no Global context");
676 Context::get_parent_context () const
678 return daddy_context_;
682 Ugh. Where to put this?
685 measure_length (Context const *context)
687 SCM l = context->get_property ("measureLength");
689 if (unsmob_moment (l))
690 length = unsmob_moment (l)->main_part_;
695 measure_position (Context const *context)
697 SCM sm = context->get_property ("measurePosition");
700 if (unsmob_moment (sm))
702 m = *unsmob_moment (sm);
704 if (m.main_part_ < Rational (0))
706 Rational length (measure_length (context));
707 while (m.main_part_ < Rational (0))
708 m.main_part_ += length;
717 set_context_property_on_children (Context *trans, SCM sym, SCM val)
719 trans->set_property (sym, ly_deep_copy (val));
720 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
722 Context *trg = unsmob_context (scm_car (p));
723 set_context_property_on_children (trg, sym, ly_deep_copy (val));
728 melisma_busy (Context *tr)
730 SCM melisma_properties = tr->get_property ("melismaBusyProperties");
733 for (; !busy && scm_is_pair (melisma_properties);
734 melisma_properties = scm_cdr (melisma_properties))
735 busy = busy || to_boolean (tr->internal_get_property (scm_car (melisma_properties)));