2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2015 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"
27 #include "output-def.hh"
29 #include "program-option.hh"
30 #include "scm-hash.hh"
31 #include "translator-group.hh"
35 Context::is_removable () const
37 return context_list_ == SCM_EOL && ! client_count_
38 && !dynamic_cast<Global_context const *> (daddy_context_);
42 Context::check_removal ()
44 for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
46 Context *ctx = Context::unsmob (scm_car (p));
48 ctx->check_removal ();
49 if (ctx->is_removable ())
51 recurse_over_translators (ctx, &Translator::finalize,
52 &Translator_group::finalize,
54 send_stream_event (ctx, "RemoveContext", 0, 0);
60 Context::properties_dict () const
62 return Scheme_hash_table::unsmob (properties_scm_);
66 Context::add_context (Context *child)
68 context_list_ = ly_append2 (context_list_,
69 scm_cons (child->self_scm (), SCM_EOL));
71 child->daddy_context_ = this;
72 this->events_below_->register_as_listener (child->events_below_);
81 properties_scm_ = SCM_EOL;
82 accepts_list_ = SCM_EOL;
83 default_child_ = SCM_EOL;
84 context_list_ = SCM_EOL;
85 definition_ = SCM_EOL;
86 definition_mods_ = SCM_EOL;
92 Scheme_hash_table *tab = new Scheme_hash_table;
93 properties_scm_ = tab->unprotect ();
94 event_source_ = new Dispatcher ();
95 event_source_->unprotect ();
96 events_below_ = new Dispatcher ();
97 events_below_->unprotect ();
100 /* TODO: this shares code with find_create_context (). */
102 Context::create_unique_context (SCM name, const string &id, SCM operations)
105 Don't create multiple score contexts.
107 Global_context *gthis = dynamic_cast<Global_context *> (this);
108 if (gthis && gthis->get_score_context ())
109 return gthis->get_score_context ()->create_unique_context (name, id, operations);
111 vector<Context_def *> path = path_to_acceptable_context (name);
114 Context *current = this;
116 // Iterate through the path and create all of the implicit contexts.
117 for (vsize i = 0; i < path.size (); i++)
120 string id_str = "\\new";
121 if (i == path.size () - 1)
126 current = current->create_context (path[i],
135 Don't go up to Global_context, because global goes down to the
139 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
140 ret = daddy_context_->create_unique_context (name, id, operations);
143 warning (_f ("cannot find or create new `%s'",
144 ly_symbol2string (name).c_str ()));
151 Context::find_create_context (SCM n, const string &id, SCM operations)
154 Don't create multiple score contexts.
156 Global_context *gthis = dynamic_cast<Global_context *> (this);
159 if (gthis->get_score_context ())
160 return gthis->get_score_context ()->find_create_context (n, id, operations);
162 // Special case: If we use \set Timing.xxx = whatever before
163 // Score is established, the alias of Score to Timing will not
164 // be taken into account. We check for this particular case
165 // here. Aliases apart from Score-level ones don't warrant
166 // context creation as they could create unwanted contexts, like
167 // RhythmicVoice instead of Voice. Creating a Score context,
168 // however, can't really do anything wrong.
170 SCM score_name = default_child_context_name ();
171 SCM score_def = find_context_def (get_output_def (), score_name);
173 if (Context_def *cd = Context_def::unsmob (score_def))
175 if (cd->is_alias (n))
176 return create_context (cd, id, operations);
181 if (Context *existing = find_context_below (this, n, id))
184 if (n == ly_symbol2scm ("Bottom"))
186 Context *tg = get_default_interpreter (id);
190 vector<Context_def *> path = path_to_acceptable_context (n);
194 Context *current = this;
196 // start at 1. The first one (index 0) will be us.
197 for (vsize 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 the
218 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
219 ret = daddy_context_->find_create_context (n, id, operations);
222 warning (_f ("cannot find or create `%s' called `%s'",
223 ly_symbol2string (n).c_str (), id));
229 IMPLEMENT_LISTENER (Context, acknowledge_infant);
231 Context::acknowledge_infant (SCM sev)
233 infant_event_ = Stream_event::unsmob (sev);
236 IMPLEMENT_LISTENER (Context, set_property_from_event);
238 Context::set_property_from_event (SCM sev)
240 Stream_event *ev = Stream_event::unsmob (sev);
242 SCM sym = ev->get_property ("symbol");
243 if (scm_is_symbol (sym))
245 SCM val = ev->get_property ("value");
247 if (SCM_UNBNDP (val)) {
248 unset_property (sym);
253 ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
256 set_property (sym, val);
260 IMPLEMENT_LISTENER (Context, unset_property_from_event);
262 Context::unset_property_from_event (SCM sev)
264 Stream_event *ev = Stream_event::unsmob (sev);
266 SCM sym = ev->get_property ("symbol");
267 type_check_assignment (sym, SCM_EOL, ly_symbol2scm ("translation-type?"));
268 unset_property (sym);
272 Creates a new context from a CreateContext event, and sends an
273 AnnounceNewContext event to this context.
275 IMPLEMENT_LISTENER (Context, create_context_from_event);
277 Context::create_context_from_event (SCM sev)
279 Stream_event *ev = Stream_event::unsmob (sev);
281 string id = ly_scm2string (ev->get_property ("id"));
282 SCM ops = ev->get_property ("ops");
283 SCM type_scm = ev->get_property ("type");
284 string type = ly_symbol2string (type_scm);
286 vector<Context_def *> path = path_to_acceptable_context (type_scm);
288 if (path.size () != 1)
290 programming_error (to_string ("Invalid CreateContext event: Cannot create %s context", type.c_str ()));
293 Context_def *cdef = path[0];
295 Context *new_context = cdef->instantiate (ops);
297 new_context->id_string_ = id;
299 /* Register various listeners:
300 - Make the new context hear events that universally affect contexts
301 - connect events_below etc. properly */
302 /* We want to be the first ones to hear our own events. Therefore, wait
303 before registering events_below_ */
304 new_context->event_source ()->
305 add_listener (GET_LISTENER (new_context->create_context_from_event),
306 ly_symbol2scm ("CreateContext"));
307 new_context->event_source ()->
308 add_listener (GET_LISTENER (new_context->remove_context),
309 ly_symbol2scm ("RemoveContext"));
310 new_context->event_source ()->
311 add_listener (GET_LISTENER (new_context->change_parent),
312 ly_symbol2scm ("ChangeParent"));
313 new_context->event_source ()->
314 add_listener (GET_LISTENER (new_context->set_property_from_event),
315 ly_symbol2scm ("SetProperty"));
316 new_context->event_source ()->
317 add_listener (GET_LISTENER (new_context->unset_property_from_event),
318 ly_symbol2scm ("UnsetProperty"));
320 new_context->events_below_->register_as_listener (new_context->event_source_);
321 this->add_context (new_context);
323 new_context->unprotect ();
325 Context_def *td = Context_def::unsmob (new_context->definition_);
327 /* This cannot move before add_context (), because \override
328 operations require that we are in the hierarchy. */
329 td->apply_default_property_operations (new_context);
330 apply_property_operations (new_context, ops);
332 send_stream_event (this, "AnnounceNewContext", 0,
333 ly_symbol2scm ("context"), new_context->self_scm (),
334 ly_symbol2scm ("creator"), sev);
337 vector<Context_def *>
338 Context::path_to_acceptable_context (SCM name) const
340 // The 'accepts elements in definition_mods_ is a list of ('accepts string),
341 // but the Context_def expects to see elements of the form ('accepts symbol).
342 SCM accepts = SCM_EOL;
343 for (SCM s = definition_mods_; scm_is_pair (s); s = scm_cdr (s))
344 if (scm_caar (s) == ly_symbol2scm ("accepts"))
346 SCM elt = scm_list_2 (scm_caar (s), scm_string_to_symbol (scm_cadar (s)));
347 accepts = scm_cons (elt, accepts);
350 return Context_def::unsmob (definition_)->path_to_acceptable_context (name,
352 scm_reverse_x (accepts, SCM_EOL));
357 Context::create_context (Context_def *cdef,
362 /* TODO: This is fairly misplaced. We can fix this when we have taken out all
363 iterator specific stuff from the Context class */
365 add_listener (GET_LISTENER (acknowledge_infant),
366 ly_symbol2scm ("AnnounceNewContext"));
367 /* The CreateContext creates a new context, and sends an announcement of the
368 new context through another event. That event will be stored in
369 infant_event_ to create a return value. */
370 send_stream_event (this, "CreateContext", 0,
371 ly_symbol2scm ("ops"), ops,
372 ly_symbol2scm ("type"), cdef->get_context_name (),
373 ly_symbol2scm ("id"), ly_string2scm (id));
375 remove_listener (GET_LISTENER (acknowledge_infant),
376 ly_symbol2scm ("AnnounceNewContext"));
378 assert (infant_event_);
379 SCM infant_scm = infant_event_->get_property ("context");
380 Context *infant = Context::unsmob (infant_scm);
382 if (!infant || infant->get_parent_context () != this)
384 programming_error ("create_context: can't locate newly created context");
392 Default child context as a SCM string, or something else if there is
396 Context::default_child_context_name () const
398 return default_child_;
402 Context::is_bottom_context () const
404 return !scm_is_symbol (default_child_context_name ());
408 Context::get_default_interpreter (const string &context_id)
410 if (!is_bottom_context ())
412 SCM nm = default_child_context_name ();
413 SCM st = find_context_def (get_output_def (), nm);
415 string name = ly_symbol2string (nm);
416 Context_def *t = Context_def::unsmob (st);
419 warning (_f ("cannot find or create: `%s'", name.c_str ()));
420 t = Context_def::unsmob (this->definition_);
422 if (scm_is_symbol (t->get_default_child (SCM_EOL)))
424 Context *tg = create_context (t, "\\new", SCM_EOL);
425 return tg->get_default_interpreter (context_id);
427 return create_context (t, context_id, SCM_EOL);
429 else if (!context_id.empty () && context_id != id_string ())
431 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
432 return daddy_context_->get_default_interpreter (context_id);
433 warning (_f ("cannot find or create new Bottom = \"%s\"",
434 context_id.c_str ()));
443 Context::where_defined (SCM sym, SCM *value) const
446 if (profile_property_accesses)
447 note_property_access (&context_property_lookup_table, sym);
450 if (properties_dict ()->try_retrieve (sym, value))
451 return (Context *)this;
453 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
456 /* Quick variant of where_defined. Checks only the context itself. */
459 Context::here_defined (SCM sym, SCM *value) const
462 if (profile_property_accesses)
463 note_property_access (&context_property_lookup_table, sym);
466 return properties_dict ()->try_retrieve (sym, value);
470 return SCM_EOL when not found.
473 Context::internal_get_property (SCM sym) const
476 if (profile_property_accesses)
477 note_property_access (&context_property_lookup_table, sym);
481 if (properties_dict ()->try_retrieve (sym, &val))
485 return daddy_context_->internal_get_property (sym);
491 Called by the send_stream_event macro. props is a 0-terminated array of
492 properties and corresponding values, interleaved. This method should not
493 be called from any other place than the send_stream_event macro.
496 Context::internal_send_stream_event (SCM type, Input *origin, SCM props[])
498 Stream_event *e = new Stream_event
499 (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"), type),
501 for (int i = 0; props[i]; i += 2)
503 e->set_property (props[i], props[i + 1]);
505 event_source_->broadcast (e);
510 Context::is_alias (SCM sym) const
512 if (scm_is_eq (sym, ly_symbol2scm ("Bottom")))
513 return is_bottom_context ();
514 if (scm_is_eq (sym, context_name_symbol ()))
517 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
521 Context::add_alias (SCM sym)
523 aliases_ = scm_cons (sym, aliases_);
526 /* we don't (yet) instrument context properties */
528 Context::instrumented_set_property (SCM sym, SCM val, const char *, int, const char *)
530 internal_set_property (sym, val);
534 Context::internal_set_property (SCM sym, SCM val)
536 bool type_check_ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
538 if (do_internal_type_checking_global)
539 assert (type_check_ok);
542 properties_dict ()->set (sym, val);
546 TODO: look up to check whether we have inherited var?
549 Context::unset_property (SCM sym)
551 properties_dict ()->remove (sym);
554 IMPLEMENT_LISTENER (Context, change_parent);
556 Context::change_parent (SCM sev)
558 Stream_event *ev = Stream_event::unsmob (sev);
559 Context *to = Context::unsmob (ev->get_property ("context"));
561 disconnect_from_parent ();
562 to->add_context (this);
566 Die. The next GC sweep should take care of the actual death.
568 IMPLEMENT_LISTENER (Context, remove_context);
570 Context::remove_context (SCM)
572 /* ugh, the translator group should listen to RemoveContext events by itself */
573 Translator_group *impl = implementation ();
575 impl->disconnect_from_context ();
576 disconnect_from_parent ();
580 Context::disconnect_from_parent ()
582 daddy_context_->events_below_->unregister_as_listener (this->events_below_);
583 daddy_context_->context_list_ = scm_delq_x (this->self_scm (), daddy_context_->context_list_);
588 ID == "" means accept any ID.
591 find_context_below (Context *where,
592 SCM type, const string &id)
594 if (where->is_alias (type))
596 if (id == "" || where->id_string () == id)
601 for (SCM s = where->children_contexts ();
602 !found && scm_is_pair (s); s = scm_cdr (s))
604 Context *tr = Context::unsmob (scm_car (s));
606 found = find_context_below (tr, type, id);
613 Context::properties_as_alist () const
615 return properties_dict ()->to_alist ();
619 Context::context_name_symbol () const
621 Context_def *td = Context_def::unsmob (definition_);
622 return td->get_context_name ();
626 Context::context_name () const
628 return ly_symbol2string (context_name_symbol ());
632 Context::get_score_context () const
635 return daddy_context_->get_score_context ();
641 Context::get_output_def () const
643 return daddy_context_ ? daddy_context_->get_output_def () : 0;
651 Context::now_mom () const
653 Context const *p = this;
654 while (p->daddy_context_)
655 p = p->daddy_context_;
657 return p->now_mom ();
661 Context::print_smob (SCM port, scm_print_state *)
663 scm_puts ("#<", port);
664 scm_puts (class_name (), port);
665 if (Context_def *d = Context_def::unsmob (definition_))
667 scm_puts (" ", port);
668 scm_display (d->get_context_name (), port);
671 if (!id_string_.empty ())
673 scm_puts ("=", port);
674 scm_puts (id_string_.c_str (), port);
677 scm_puts (" ", port);
679 scm_display (context_list_, port);
680 scm_puts (" >", port);
686 Context::mark_smob ()
688 scm_gc_mark (context_list_);
689 scm_gc_mark (aliases_);
690 scm_gc_mark (definition_);
691 scm_gc_mark (definition_mods_);
692 scm_gc_mark (properties_scm_);
693 scm_gc_mark (accepts_list_);
694 scm_gc_mark (default_child_);
697 scm_gc_mark (implementation_->self_scm ());
700 scm_gc_mark (event_source_->self_scm ());
703 scm_gc_mark (events_below_->self_scm ());
705 return properties_scm_;
708 const char Context::type_p_name_[] = "ly:context?";
711 Context::get_global_context () const
713 if (dynamic_cast<Global_context *> ((Context *) this))
714 return dynamic_cast<Global_context *> ((Context *) this);
717 return daddy_context_->get_global_context ();
719 programming_error ("no Global context");
724 Context::get_parent_context () const
726 return daddy_context_;
730 Ugh. Where to put this?
733 measure_length (Context const *context)
735 SCM l = context->get_property ("measureLength");
737 if (Moment::is_smob (l))
738 length = Moment::unsmob (l)->main_part_;
743 measure_position (Context const *context)
745 SCM sm = context->get_property ("measurePosition");
748 if (Moment::is_smob (sm))
750 m = *Moment::unsmob (sm);
752 if (m.main_part_ < Rational (0))
754 Rational length (measure_length (context));
755 while (m.main_part_ < Rational (0))
756 m.main_part_ += length;
763 /* Finds the measure position after a note of length DUR that
764 begins at the current measure position. */
766 measure_position (Context const *context, Duration const *dur)
768 Moment pos = measure_position (context);
769 Rational dur_length = dur ? dur->get_length () : Rational (0);
771 Moment end_pos = pos.grace_part_ < Rational (0)
772 ? Moment (pos.main_part_, pos.grace_part_ + dur_length)
773 : Moment (pos.main_part_ + dur_length, 0);
779 measure_number (Context const *context)
781 SCM barnum = context->get_property ("internalBarNumber");
782 SCM smp = context->get_property ("measurePosition");
784 int bn = robust_scm2int (barnum, 0);
785 Moment mp = robust_scm2moment (smp, Moment (0));
786 if (mp.main_part_ < Rational (0))
793 set_context_property_on_children (Context *trans, SCM sym, SCM val)
795 trans->set_property (sym, ly_deep_copy (val));
796 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
798 Context *trg = Context::unsmob (scm_car (p));
799 set_context_property_on_children (trg, sym, ly_deep_copy (val));
804 melisma_busy (Context *tr)
806 // When there are subcontexts, they are responsible for maintaining
808 SCM ch = tr->children_contexts ();
809 if (scm_is_pair (ch))
811 // all contexts need to have a busy melisma for this to evaluate
815 if (!melisma_busy (Context::unsmob (scm_car (ch))))
818 } while (scm_is_pair (ch));
822 for (SCM melisma_properties = tr->get_property ("melismaBusyProperties");
823 scm_is_pair (melisma_properties);
824 melisma_properties = scm_cdr (melisma_properties))
825 if (to_boolean (tr->get_property (scm_car (melisma_properties))))
832 check_repeat_count_visibility (Context const *context, SCM count)
834 SCM proc = context->get_property ("repeatCountVisibility");
835 return (ly_is_procedure (proc)
836 && to_boolean (scm_call_2 (proc,
838 context->self_scm ())));