2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "context-def.hh"
23 #include "dispatcher.hh"
24 #include "global-context.hh"
25 #include "international.hh"
26 #include "ly-smobs.icc"
28 #include "output-def.hh"
30 #include "program-option.hh"
31 #include "scm-hash.hh"
32 #include "translator-group.hh"
36 Context::is_removable () const
38 return context_list_ == SCM_EOL && ! client_count_
39 && !dynamic_cast<Global_context const *> (daddy_context_);
43 Context::check_removal ()
45 for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
47 Context *ctx = unsmob_context (scm_car (p));
49 ctx->check_removal ();
50 if (ctx->is_removable ())
52 recurse_over_translators (ctx, &Translator::finalize,
53 &Translator_group::finalize,
55 send_stream_event (ctx, "RemoveContext", 0, 0);
60 Context::Context (Context const & /* src */)
66 Context::properties_dict () const
68 return Scheme_hash_table::unsmob (properties_scm_);
72 Context::add_context (Context *child)
74 context_list_ = ly_append2 (context_list_,
75 scm_cons (child->self_scm (), SCM_EOL));
77 child->daddy_context_ = this;
78 this->events_below_->register_as_listener (child->events_below_);
87 properties_scm_ = SCM_EOL;
88 accepts_list_ = SCM_EOL;
89 context_list_ = SCM_EOL;
90 definition_ = SCM_EOL;
91 definition_mods_ = SCM_EOL;
94 ancestor_lookup_ = SCM_UNDEFINED;
98 Scheme_hash_table *tab = new Scheme_hash_table;
99 properties_scm_ = tab->unprotect ();
100 event_source_ = new Dispatcher ();
101 event_source_->unprotect ();
102 events_below_ = new Dispatcher ();
103 events_below_->unprotect ();
106 /* TODO: this shares code with find_create_context (). */
108 Context::create_unique_context (SCM name, string id, SCM operations)
111 Don't create multiple score contexts.
113 Global_context *gthis = dynamic_cast<Global_context *> (this);
114 if (gthis && gthis->get_score_context ())
115 return gthis->get_score_context ()->create_unique_context (name, id, operations);
117 vector<Context_def *> path = path_to_acceptable_context (name);
120 Context *current = this;
122 // Iterate through the path and create all of the implicit contexts.
123 for (vsize i = 0; i < path.size (); i++)
126 string id_str = "\\new";
127 if (i == path.size () - 1)
132 current = current->create_context (path[i],
141 Don't go up to Global_context, because global goes down to the
145 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
146 ret = daddy_context_->create_unique_context (name, id, operations);
149 warning (_f ("cannot find or create new `%s'",
150 ly_symbol2string (name).c_str ()));
157 Context::find_create_context (SCM n, string id, SCM operations)
160 Don't create multiple score contexts.
162 Global_context *gthis = dynamic_cast<Global_context *> (this);
163 if (gthis && gthis->get_score_context ())
164 return gthis->get_score_context ()->find_create_context (n, id, operations);
166 if (Context *existing = find_context_below (this, n, id))
169 if (n == ly_symbol2scm ("Bottom"))
171 Context *tg = get_default_interpreter (id);
175 vector<Context_def *> path = path_to_acceptable_context (n);
179 Context *current = this;
181 // start at 1. The first one (index 0) will be us.
182 for (vsize i = 0; i < path.size (); i++)
184 SCM ops = (i == path.size () - 1) ? operations : SCM_EOL;
187 if (i == path.size () - 1)
190 current = current->create_context (path[i],
199 Don't go up to Global_context, because global goes down to the
203 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
204 ret = daddy_context_->find_create_context (n, id, operations);
207 warning (_f ("cannot find or create `%s' called `%s'",
208 ly_symbol2string (n).c_str (), id));
214 IMPLEMENT_LISTENER (Context, acknowledge_infant);
216 Context::acknowledge_infant (SCM sev)
218 infant_event_ = unsmob_stream_event (sev);
221 IMPLEMENT_LISTENER (Context, set_property_from_event);
223 Context::set_property_from_event (SCM sev)
225 Stream_event *ev = unsmob_stream_event (sev);
227 SCM sym = ev->get_property ("symbol");
228 if (scm_is_symbol (sym))
230 SCM val = ev->get_property ("value");
232 if (SCM_UNBNDP (val)) {
233 unset_property (sym);
239 ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
241 set_property (sym, val);
245 IMPLEMENT_LISTENER (Context, unset_property_from_event);
247 Context::unset_property_from_event (SCM sev)
249 Stream_event *ev = unsmob_stream_event (sev);
251 SCM sym = ev->get_property ("symbol");
252 type_check_assignment (sym, SCM_EOL, ly_symbol2scm ("translation-type?"));
253 unset_property (sym);
257 Creates a new context from a CreateContext event, and sends an
258 AnnounceNewContext event to this context.
260 IMPLEMENT_LISTENER (Context, create_context_from_event);
262 Context::create_context_from_event (SCM sev)
264 Stream_event *ev = unsmob_stream_event (sev);
266 string id = ly_scm2string (ev->get_property ("id"));
267 SCM ops = ev->get_property ("ops");
268 SCM type_scm = ev->get_property ("type");
269 string type = ly_symbol2string (type_scm);
271 vector<Context_def *> path = path_to_acceptable_context (type_scm);
273 if (path.size () != 1)
275 programming_error (to_string ("Invalid CreateContext event: Cannot create %s context", type.c_str ()));
278 Context_def *cdef = path[0];
280 Context *new_context = cdef->instantiate (ops);
282 new_context->id_string_ = id;
284 /* Register various listeners:
285 - Make the new context hear events that universally affect contexts
286 - connect events_below etc. properly */
287 /* We want to be the first ones to hear our own events. Therefore, wait
288 before registering events_below_ */
289 new_context->event_source ()->
290 add_listener (GET_LISTENER (new_context->create_context_from_event),
291 ly_symbol2scm ("CreateContext"));
292 new_context->event_source ()->
293 add_listener (GET_LISTENER (new_context->remove_context),
294 ly_symbol2scm ("RemoveContext"));
295 new_context->event_source ()->
296 add_listener (GET_LISTENER (new_context->change_parent),
297 ly_symbol2scm ("ChangeParent"));
298 new_context->event_source ()->
299 add_listener (GET_LISTENER (new_context->set_property_from_event),
300 ly_symbol2scm ("SetProperty"));
301 new_context->event_source ()->
302 add_listener (GET_LISTENER (new_context->unset_property_from_event),
303 ly_symbol2scm ("UnsetProperty"));
305 new_context->events_below_->register_as_listener (new_context->event_source_);
306 this->add_context (new_context);
308 new_context->unprotect ();
310 Context_def *td = unsmob_context_def (new_context->definition_);
312 /* This cannot move before add_context (), because \override
313 operations require that we are in the hierarchy. */
314 td->apply_default_property_operations (new_context);
315 apply_property_operations (new_context, ops);
317 send_stream_event (this, "AnnounceNewContext", 0,
318 ly_symbol2scm ("context"), new_context->self_scm (),
319 ly_symbol2scm ("creator"), sev);
322 vector<Context_def *>
323 Context::path_to_acceptable_context (SCM name) const
325 // The 'accepts elements in definition_mods_ is a list of ('accepts string),
326 // but the Context_def expects to see elements of the form ('accepts symbol).
327 SCM accepts = SCM_EOL;
328 for (SCM s = definition_mods_; scm_is_pair (s); s = scm_cdr (s))
329 if (scm_caar (s) == ly_symbol2scm ("accepts"))
331 SCM elt = scm_list_2 (scm_caar (s), scm_string_to_symbol (scm_cadar (s)));
332 accepts = scm_cons (elt, accepts);
335 return unsmob_context_def (definition_)->path_to_acceptable_context (name,
337 scm_reverse_x (accepts, SCM_EOL));
342 Context::create_context (Context_def *cdef,
347 /* TODO: This is fairly misplaced. We can fix this when we have taken out all
348 iterator specific stuff from the Context class */
350 add_listener (GET_LISTENER (acknowledge_infant),
351 ly_symbol2scm ("AnnounceNewContext"));
352 /* The CreateContext creates a new context, and sends an announcement of the
353 new context through another event. That event will be stored in
354 infant_event_ to create a return value. */
355 send_stream_event (this, "CreateContext", 0,
356 ly_symbol2scm ("ops"), ops,
357 ly_symbol2scm ("type"), cdef->get_context_name (),
358 ly_symbol2scm ("id"), ly_string2scm (id));
360 remove_listener (GET_LISTENER (acknowledge_infant),
361 ly_symbol2scm ("AnnounceNewContext"));
363 assert (infant_event_);
364 SCM infant_scm = infant_event_->get_property ("context");
365 Context *infant = unsmob_context (infant_scm);
367 if (!infant || infant->get_parent_context () != this)
369 programming_error ("create_context: can't locate newly created context");
377 Default child context as a SCM string, or something else if there is
381 Context::default_child_context_name () const
383 return scm_is_pair (accepts_list_)
384 ? scm_car (accepts_list_)
389 Context::is_bottom_context () const
391 return !scm_is_symbol (default_child_context_name ());
395 Context::get_default_interpreter (string context_id)
397 if (!is_bottom_context ())
399 SCM nm = default_child_context_name ();
400 SCM st = find_context_def (get_output_def (), nm);
402 string name = ly_symbol2string (nm);
403 Context_def *t = unsmob_context_def (st);
406 warning (_f ("cannot find or create: `%s'", name.c_str ()));
407 t = unsmob_context_def (this->definition_);
410 Context *tg = create_context (t, context_id, SCM_EOL);
411 return tg->get_default_interpreter (context_id);
420 Context::where_defined (SCM sym, SCM *value) const
423 if (profile_property_accesses)
424 note_property_access (&context_property_lookup_table, sym);
427 if (properties_dict ()->try_retrieve (sym, value))
428 return (Context *)this;
430 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
434 return SCM_EOL when not found.
437 Context::internal_get_property (SCM sym) const
440 if (profile_property_accesses)
441 note_property_access (&context_property_lookup_table, sym);
445 if (properties_dict ()->try_retrieve (sym, &val))
449 return daddy_context_->internal_get_property (sym);
455 Called by the send_stream_event macro. props is a 0-terminated array of
456 properties and corresponding values, interleaved. This method should not
457 be called from any other place than the send_stream_event macro.
460 Context::internal_send_stream_event (SCM type, Input *origin, SCM props[])
462 Stream_event *e = new Stream_event (make_event_class (type), origin);
463 for (int i = 0; props[i]; i += 2)
465 e->set_property (props[i], props[i + 1]);
467 event_source_->broadcast (e);
472 Context::is_alias (SCM sym) const
474 if (sym == ly_symbol2scm ("Bottom")
475 && !scm_is_pair (accepts_list_))
477 if (sym == unsmob_context_def (definition_)->get_context_name ())
480 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
484 Context::add_alias (SCM sym)
486 aliases_ = scm_cons (sym, aliases_);
489 /* we don't (yet) instrument context properties */
491 Context::instrumented_set_property (SCM sym, SCM val, const char *, int, const char *)
493 internal_set_property (sym, val);
497 Context::internal_set_property (SCM sym, SCM val)
499 bool type_check_ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
501 if (do_internal_type_checking_global)
502 assert (type_check_ok);
505 properties_dict ()->set (sym, val);
509 TODO: look up to check whether we have inherited var?
512 Context::unset_property (SCM sym)
514 properties_dict ()->remove (sym);
517 IMPLEMENT_LISTENER (Context, change_parent);
519 Context::change_parent (SCM sev)
521 Stream_event *ev = unsmob_stream_event (sev);
522 Context *to = unsmob_context (ev->get_property ("context"));
524 disconnect_from_parent ();
525 to->add_context (this);
529 Die. The next GC sweep should take care of the actual death.
531 IMPLEMENT_LISTENER (Context, remove_context);
533 Context::remove_context (SCM)
535 /* ugh, the translator group should listen to RemoveContext events by itself */
536 Translator_group *impl = implementation ();
538 impl->disconnect_from_context ();
539 disconnect_from_parent ();
543 Context::disconnect_from_parent ()
545 daddy_context_->events_below_->unregister_as_listener (this->events_below_);
546 daddy_context_->context_list_ = scm_delq_x (this->self_scm (), daddy_context_->context_list_);
551 ID == "" means accept any ID.
554 find_context_below (Context *where,
557 if (where->is_alias (type))
559 if (id == "" || where->id_string () == id)
564 for (SCM s = where->children_contexts ();
565 !found && scm_is_pair (s); s = scm_cdr (s))
567 Context *tr = unsmob_context (scm_car (s));
569 found = find_context_below (tr, type, id);
576 Context::properties_as_alist () const
578 return properties_dict ()->to_alist ();
582 Context::context_name_symbol () const
584 Context_def *td = unsmob_context_def (definition_);
585 return td->get_context_name ();
589 Context::context_name () const
591 return ly_symbol2string (context_name_symbol ());
595 Context::get_score_context () const
598 return daddy_context_->get_score_context ();
604 Context::make_event_class (SCM event_type)
606 if (SCM_UNBNDP (ancestor_lookup_))
607 ancestor_lookup_ = get_global_context ()->ancestor_lookup_;
608 return scm_hashq_ref (ancestor_lookup_, event_type, SCM_EOL);
612 Context::get_output_def () const
614 return daddy_context_ ? daddy_context_->get_output_def () : 0;
622 Context::now_mom () const
624 Context const *p = this;
625 while (p->daddy_context_)
626 p = p->daddy_context_;
628 return p->now_mom ();
632 Context::print_smob (SCM s, SCM port, scm_print_state *)
634 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
636 scm_puts ("#<", port);
637 scm_puts (sc->class_name (), port);
638 if (Context_def *d = unsmob_context_def (sc->definition_))
640 scm_puts (" ", port);
641 scm_display (d->get_context_name (), port);
644 if (!sc->id_string_.empty ())
646 scm_puts ("=", port);
647 scm_puts (sc->id_string_.c_str (), port);
650 scm_puts (" ", port);
652 scm_display (sc->context_list_, port);
653 scm_puts (" >", port);
659 Context::mark_smob (SCM sm)
661 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
663 scm_gc_mark (me->context_list_);
664 scm_gc_mark (me->aliases_);
665 scm_gc_mark (me->definition_);
666 scm_gc_mark (me->definition_mods_);
667 scm_gc_mark (me->properties_scm_);
668 scm_gc_mark (me->accepts_list_);
670 if (me->implementation_)
671 scm_gc_mark (me->implementation_->self_scm ());
673 if (me->event_source_)
674 scm_gc_mark (me->event_source_->self_scm ());
676 if (me->events_below_)
677 scm_gc_mark (me->events_below_->self_scm ());
679 scm_gc_mark (me->ancestor_lookup_);
681 return me->properties_scm_;
684 IMPLEMENT_SMOBS (Context);
685 IMPLEMENT_DEFAULT_EQUAL_P (Context);
686 IMPLEMENT_TYPE_P (Context, "ly:context?");
689 Context::get_global_context () const
691 if (dynamic_cast<Global_context *> ((Context *) this))
692 return dynamic_cast<Global_context *> ((Context *) this);
695 return daddy_context_->get_global_context ();
697 programming_error ("no Global context");
702 Context::get_parent_context () const
704 return daddy_context_;
708 Ugh. Where to put this?
711 measure_length (Context const *context)
713 SCM l = context->get_property ("measureLength");
715 if (unsmob_moment (l))
716 length = unsmob_moment (l)->main_part_;
721 measure_position (Context const *context)
723 SCM sm = context->get_property ("measurePosition");
726 if (unsmob_moment (sm))
728 m = *unsmob_moment (sm);
730 if (m.main_part_ < Rational (0))
732 Rational length (measure_length (context));
733 while (m.main_part_ < Rational (0))
734 m.main_part_ += length;
741 /* Finds the measure position after a note of length DUR that
742 begins at the current measure position. */
744 measure_position (Context const *context, Duration const *dur)
746 Moment pos = measure_position (context);
747 Rational dur_length = dur ? dur->get_length () : Rational (0);
749 Moment end_pos = pos.grace_part_ < Rational (0)
750 ? Moment (pos.main_part_, pos.grace_part_ + dur_length)
751 : Moment (pos.main_part_ + dur_length, 0);
757 measure_number (Context const *context)
759 SCM barnum = context->get_property ("internalBarNumber");
760 SCM smp = context->get_property ("measurePosition");
762 int bn = robust_scm2int (barnum, 0);
763 Moment mp = robust_scm2moment (smp, Moment (0));
764 if (mp.main_part_ < Rational (0))
771 set_context_property_on_children (Context *trans, SCM sym, SCM val)
773 trans->set_property (sym, ly_deep_copy (val));
774 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
776 Context *trg = unsmob_context (scm_car (p));
777 set_context_property_on_children (trg, sym, ly_deep_copy (val));
782 melisma_busy (Context *tr)
784 SCM melisma_properties = tr->get_property ("melismaBusyProperties");
787 for (; !busy && scm_is_pair (melisma_properties);
788 melisma_properties = scm_cdr (melisma_properties))
789 busy = busy || to_boolean (tr->internal_get_property (scm_car (melisma_properties)));
795 check_repeat_count_visibility (Context const *context, SCM count)
797 SCM proc = context->get_property ("repeatCountVisibility");
798 return (ly_is_procedure (proc)
799 && to_boolean (scm_call_2 (proc,
801 context->self_scm ())));