2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2014 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);
59 Context::Context (Context const & /* src */)
65 Context::properties_dict () const
67 return Scheme_hash_table::unsmob (properties_scm_);
71 Context::add_context (Context *child)
73 context_list_ = ly_append2 (context_list_,
74 scm_cons (child->self_scm (), SCM_EOL));
76 child->daddy_context_ = this;
77 this->events_below_->register_as_listener (child->events_below_);
86 properties_scm_ = SCM_EOL;
87 accepts_list_ = SCM_EOL;
88 default_child_ = SCM_EOL;
89 context_list_ = SCM_EOL;
90 definition_ = SCM_EOL;
91 definition_mods_ = SCM_EOL;
97 Scheme_hash_table *tab = new Scheme_hash_table;
98 properties_scm_ = tab->unprotect ();
99 event_source_ = new Dispatcher ();
100 event_source_->unprotect ();
101 events_below_ = new Dispatcher ();
102 events_below_->unprotect ();
105 /* TODO: this shares code with find_create_context (). */
107 Context::create_unique_context (SCM name, const string &id, SCM operations)
110 Don't create multiple score contexts.
112 Global_context *gthis = dynamic_cast<Global_context *> (this);
113 if (gthis && gthis->get_score_context ())
114 return gthis->get_score_context ()->create_unique_context (name, id, operations);
116 vector<Context_def *> path = path_to_acceptable_context (name);
119 Context *current = this;
121 // Iterate through the path and create all of the implicit contexts.
122 for (vsize i = 0; i < path.size (); i++)
125 string id_str = "\\new";
126 if (i == path.size () - 1)
131 current = current->create_context (path[i],
140 Don't go up to Global_context, because global goes down to the
144 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
145 ret = daddy_context_->create_unique_context (name, id, operations);
148 warning (_f ("cannot find or create new `%s'",
149 ly_symbol2string (name).c_str ()));
156 Context::find_create_context (SCM n, const string &id, SCM operations)
159 Don't create multiple score contexts.
161 Global_context *gthis = dynamic_cast<Global_context *> (this);
164 if (gthis->get_score_context ())
165 return gthis->get_score_context ()->find_create_context (n, id, operations);
167 // Special case: If we use \set Timing.xxx = whatever before
168 // Score is established, the alias of Score to Timing will not
169 // be taken into account. We check for this particular case
170 // here. Aliases apart from Score-level ones don't warrant
171 // context creation as they could create unwanted contexts, like
172 // RhythmicVoice instead of Voice. Creating a Score context,
173 // however, can't really do anything wrong.
175 SCM score_name = default_child_context_name ();
176 SCM score_def = find_context_def (get_output_def (), score_name);
178 if (Context_def *cd = Context_def::unsmob (score_def))
180 if (cd->is_alias (n))
181 return create_context (cd, id, operations);
186 if (Context *existing = find_context_below (this, n, id))
189 if (n == ly_symbol2scm ("Bottom"))
191 Context *tg = get_default_interpreter (id);
195 vector<Context_def *> path = path_to_acceptable_context (n);
199 Context *current = this;
201 // start at 1. The first one (index 0) will be us.
202 for (vsize i = 0; i < path.size (); i++)
204 SCM ops = (i == path.size () - 1) ? operations : SCM_EOL;
207 if (i == path.size () - 1)
210 current = current->create_context (path[i],
219 Don't go up to Global_context, because global goes down to the
223 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
224 ret = daddy_context_->find_create_context (n, id, operations);
227 warning (_f ("cannot find or create `%s' called `%s'",
228 ly_symbol2string (n).c_str (), id));
234 IMPLEMENT_LISTENER (Context, acknowledge_infant);
236 Context::acknowledge_infant (SCM sev)
238 infant_event_ = Stream_event::unsmob (sev);
241 IMPLEMENT_LISTENER (Context, set_property_from_event);
243 Context::set_property_from_event (SCM sev)
245 Stream_event *ev = Stream_event::unsmob (sev);
247 SCM sym = ev->get_property ("symbol");
248 if (scm_is_symbol (sym))
250 SCM val = ev->get_property ("value");
252 if (SCM_UNBNDP (val)) {
253 unset_property (sym);
258 ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
261 set_property (sym, val);
265 IMPLEMENT_LISTENER (Context, unset_property_from_event);
267 Context::unset_property_from_event (SCM sev)
269 Stream_event *ev = Stream_event::unsmob (sev);
271 SCM sym = ev->get_property ("symbol");
272 type_check_assignment (sym, SCM_EOL, ly_symbol2scm ("translation-type?"));
273 unset_property (sym);
277 Creates a new context from a CreateContext event, and sends an
278 AnnounceNewContext event to this context.
280 IMPLEMENT_LISTENER (Context, create_context_from_event);
282 Context::create_context_from_event (SCM sev)
284 Stream_event *ev = Stream_event::unsmob (sev);
286 string id = ly_scm2string (ev->get_property ("id"));
287 SCM ops = ev->get_property ("ops");
288 SCM type_scm = ev->get_property ("type");
289 string type = ly_symbol2string (type_scm);
291 vector<Context_def *> path = path_to_acceptable_context (type_scm);
293 if (path.size () != 1)
295 programming_error (to_string ("Invalid CreateContext event: Cannot create %s context", type.c_str ()));
298 Context_def *cdef = path[0];
300 Context *new_context = cdef->instantiate (ops);
302 new_context->id_string_ = id;
304 /* Register various listeners:
305 - Make the new context hear events that universally affect contexts
306 - connect events_below etc. properly */
307 /* We want to be the first ones to hear our own events. Therefore, wait
308 before registering events_below_ */
309 new_context->event_source ()->
310 add_listener (GET_LISTENER (new_context->create_context_from_event),
311 ly_symbol2scm ("CreateContext"));
312 new_context->event_source ()->
313 add_listener (GET_LISTENER (new_context->remove_context),
314 ly_symbol2scm ("RemoveContext"));
315 new_context->event_source ()->
316 add_listener (GET_LISTENER (new_context->change_parent),
317 ly_symbol2scm ("ChangeParent"));
318 new_context->event_source ()->
319 add_listener (GET_LISTENER (new_context->set_property_from_event),
320 ly_symbol2scm ("SetProperty"));
321 new_context->event_source ()->
322 add_listener (GET_LISTENER (new_context->unset_property_from_event),
323 ly_symbol2scm ("UnsetProperty"));
325 new_context->events_below_->register_as_listener (new_context->event_source_);
326 this->add_context (new_context);
328 new_context->unprotect ();
330 Context_def *td = Context_def::unsmob (new_context->definition_);
332 /* This cannot move before add_context (), because \override
333 operations require that we are in the hierarchy. */
334 td->apply_default_property_operations (new_context);
335 apply_property_operations (new_context, ops);
337 send_stream_event (this, "AnnounceNewContext", 0,
338 ly_symbol2scm ("context"), new_context->self_scm (),
339 ly_symbol2scm ("creator"), sev);
342 vector<Context_def *>
343 Context::path_to_acceptable_context (SCM name) const
345 // The 'accepts elements in definition_mods_ is a list of ('accepts string),
346 // but the Context_def expects to see elements of the form ('accepts symbol).
347 SCM accepts = SCM_EOL;
348 for (SCM s = definition_mods_; scm_is_pair (s); s = scm_cdr (s))
349 if (scm_caar (s) == ly_symbol2scm ("accepts"))
351 SCM elt = scm_list_2 (scm_caar (s), scm_string_to_symbol (scm_cadar (s)));
352 accepts = scm_cons (elt, accepts);
355 return Context_def::unsmob (definition_)->path_to_acceptable_context (name,
357 scm_reverse_x (accepts, SCM_EOL));
362 Context::create_context (Context_def *cdef,
367 /* TODO: This is fairly misplaced. We can fix this when we have taken out all
368 iterator specific stuff from the Context class */
370 add_listener (GET_LISTENER (acknowledge_infant),
371 ly_symbol2scm ("AnnounceNewContext"));
372 /* The CreateContext creates a new context, and sends an announcement of the
373 new context through another event. That event will be stored in
374 infant_event_ to create a return value. */
375 send_stream_event (this, "CreateContext", 0,
376 ly_symbol2scm ("ops"), ops,
377 ly_symbol2scm ("type"), cdef->get_context_name (),
378 ly_symbol2scm ("id"), ly_string2scm (id));
380 remove_listener (GET_LISTENER (acknowledge_infant),
381 ly_symbol2scm ("AnnounceNewContext"));
383 assert (infant_event_);
384 SCM infant_scm = infant_event_->get_property ("context");
385 Context *infant = Context::unsmob (infant_scm);
387 if (!infant || infant->get_parent_context () != this)
389 programming_error ("create_context: can't locate newly created context");
397 Default child context as a SCM string, or something else if there is
401 Context::default_child_context_name () const
403 return default_child_;
407 Context::is_bottom_context () const
409 return !scm_is_symbol (default_child_context_name ());
413 Context::get_default_interpreter (const string &context_id)
415 if (!is_bottom_context ())
417 SCM nm = default_child_context_name ();
418 SCM st = find_context_def (get_output_def (), nm);
420 string name = ly_symbol2string (nm);
421 Context_def *t = Context_def::unsmob (st);
424 warning (_f ("cannot find or create: `%s'", name.c_str ()));
425 t = Context_def::unsmob (this->definition_);
427 if (scm_is_symbol (t->get_default_child (SCM_EOL)))
429 Context *tg = create_context (t, "\\new", SCM_EOL);
430 return tg->get_default_interpreter (context_id);
432 return create_context (t, context_id, SCM_EOL);
434 else if (!context_id.empty () && context_id != id_string ())
436 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
437 return daddy_context_->get_default_interpreter (context_id);
438 warning (_f ("cannot find or create new Bottom = \"%s\"",
439 context_id.c_str ()));
448 Context::where_defined (SCM sym, SCM *value) const
451 if (profile_property_accesses)
452 note_property_access (&context_property_lookup_table, sym);
455 if (properties_dict ()->try_retrieve (sym, value))
456 return (Context *)this;
458 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
461 /* Quick variant of where_defined. Checks only the context itself. */
464 Context::here_defined (SCM sym, SCM *value) const
467 if (profile_property_accesses)
468 note_property_access (&context_property_lookup_table, sym);
471 return properties_dict ()->try_retrieve (sym, value);
475 return SCM_EOL when not found.
478 Context::internal_get_property (SCM sym) const
481 if (profile_property_accesses)
482 note_property_access (&context_property_lookup_table, sym);
486 if (properties_dict ()->try_retrieve (sym, &val))
490 return daddy_context_->internal_get_property (sym);
496 Called by the send_stream_event macro. props is a 0-terminated array of
497 properties and corresponding values, interleaved. This method should not
498 be called from any other place than the send_stream_event macro.
501 Context::internal_send_stream_event (SCM type, Input *origin, SCM props[])
503 Stream_event *e = new Stream_event
504 (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"), type),
506 for (int i = 0; props[i]; i += 2)
508 e->set_property (props[i], props[i + 1]);
510 event_source_->broadcast (e);
515 Context::is_alias (SCM sym) const
517 if (scm_is_eq (sym, ly_symbol2scm ("Bottom")))
518 return is_bottom_context ();
519 if (scm_is_eq (sym, context_name_symbol ()))
522 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
526 Context::add_alias (SCM sym)
528 aliases_ = scm_cons (sym, aliases_);
531 /* we don't (yet) instrument context properties */
533 Context::instrumented_set_property (SCM sym, SCM val, const char *, int, const char *)
535 internal_set_property (sym, val);
539 Context::internal_set_property (SCM sym, SCM val)
541 bool type_check_ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
543 if (do_internal_type_checking_global)
544 assert (type_check_ok);
547 properties_dict ()->set (sym, val);
551 TODO: look up to check whether we have inherited var?
554 Context::unset_property (SCM sym)
556 properties_dict ()->remove (sym);
559 IMPLEMENT_LISTENER (Context, change_parent);
561 Context::change_parent (SCM sev)
563 Stream_event *ev = Stream_event::unsmob (sev);
564 Context *to = Context::unsmob (ev->get_property ("context"));
566 disconnect_from_parent ();
567 to->add_context (this);
571 Die. The next GC sweep should take care of the actual death.
573 IMPLEMENT_LISTENER (Context, remove_context);
575 Context::remove_context (SCM)
577 /* ugh, the translator group should listen to RemoveContext events by itself */
578 Translator_group *impl = implementation ();
580 impl->disconnect_from_context ();
581 disconnect_from_parent ();
585 Context::disconnect_from_parent ()
587 daddy_context_->events_below_->unregister_as_listener (this->events_below_);
588 daddy_context_->context_list_ = scm_delq_x (this->self_scm (), daddy_context_->context_list_);
593 ID == "" means accept any ID.
596 find_context_below (Context *where,
597 SCM type, const string &id)
599 if (where->is_alias (type))
601 if (id == "" || where->id_string () == id)
606 for (SCM s = where->children_contexts ();
607 !found && scm_is_pair (s); s = scm_cdr (s))
609 Context *tr = Context::unsmob (scm_car (s));
611 found = find_context_below (tr, type, id);
618 Context::properties_as_alist () const
620 return properties_dict ()->to_alist ();
624 Context::context_name_symbol () const
626 Context_def *td = Context_def::unsmob (definition_);
627 return td->get_context_name ();
631 Context::context_name () const
633 return ly_symbol2string (context_name_symbol ());
637 Context::get_score_context () const
640 return daddy_context_->get_score_context ();
646 Context::get_output_def () const
648 return daddy_context_ ? daddy_context_->get_output_def () : 0;
656 Context::now_mom () const
658 Context const *p = this;
659 while (p->daddy_context_)
660 p = p->daddy_context_;
662 return p->now_mom ();
666 Context::print_smob (SCM s, SCM port, scm_print_state *)
668 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
670 scm_puts ("#<", port);
671 scm_puts (sc->class_name (), port);
672 if (Context_def *d = Context_def::unsmob (sc->definition_))
674 scm_puts (" ", port);
675 scm_display (d->get_context_name (), port);
678 if (!sc->id_string_.empty ())
680 scm_puts ("=", port);
681 scm_puts (sc->id_string_.c_str (), port);
684 scm_puts (" ", port);
686 scm_display (sc->context_list_, port);
687 scm_puts (" >", port);
693 Context::mark_smob (SCM sm)
695 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
697 scm_gc_mark (me->context_list_);
698 scm_gc_mark (me->aliases_);
699 scm_gc_mark (me->definition_);
700 scm_gc_mark (me->definition_mods_);
701 scm_gc_mark (me->properties_scm_);
702 scm_gc_mark (me->accepts_list_);
703 scm_gc_mark (me->default_child_);
705 if (me->implementation_)
706 scm_gc_mark (me->implementation_->self_scm ());
708 if (me->event_source_)
709 scm_gc_mark (me->event_source_->self_scm ());
711 if (me->events_below_)
712 scm_gc_mark (me->events_below_->self_scm ());
714 return me->properties_scm_;
717 const char Context::type_p_name_[] = "ly:context?";
720 Context::get_global_context () const
722 if (dynamic_cast<Global_context *> ((Context *) this))
723 return dynamic_cast<Global_context *> ((Context *) this);
726 return daddy_context_->get_global_context ();
728 programming_error ("no Global context");
733 Context::get_parent_context () const
735 return daddy_context_;
739 Ugh. Where to put this?
742 measure_length (Context const *context)
744 SCM l = context->get_property ("measureLength");
746 if (Moment::unsmob (l))
747 length = Moment::unsmob (l)->main_part_;
752 measure_position (Context const *context)
754 SCM sm = context->get_property ("measurePosition");
757 if (Moment::unsmob (sm))
759 m = *Moment::unsmob (sm);
761 if (m.main_part_ < Rational (0))
763 Rational length (measure_length (context));
764 while (m.main_part_ < Rational (0))
765 m.main_part_ += length;
772 /* Finds the measure position after a note of length DUR that
773 begins at the current measure position. */
775 measure_position (Context const *context, Duration const *dur)
777 Moment pos = measure_position (context);
778 Rational dur_length = dur ? dur->get_length () : Rational (0);
780 Moment end_pos = pos.grace_part_ < Rational (0)
781 ? Moment (pos.main_part_, pos.grace_part_ + dur_length)
782 : Moment (pos.main_part_ + dur_length, 0);
788 measure_number (Context const *context)
790 SCM barnum = context->get_property ("internalBarNumber");
791 SCM smp = context->get_property ("measurePosition");
793 int bn = robust_scm2int (barnum, 0);
794 Moment mp = robust_scm2moment (smp, Moment (0));
795 if (mp.main_part_ < Rational (0))
802 set_context_property_on_children (Context *trans, SCM sym, SCM val)
804 trans->set_property (sym, ly_deep_copy (val));
805 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
807 Context *trg = Context::unsmob (scm_car (p));
808 set_context_property_on_children (trg, sym, ly_deep_copy (val));
813 melisma_busy (Context *tr)
815 // When there are subcontexts, they are responsible for maintaining
817 SCM ch = tr->children_contexts ();
818 if (scm_is_pair (ch))
820 // all contexts need to have a busy melisma for this to evaluate
824 if (!melisma_busy (Context::unsmob (scm_car (ch))))
827 } while (scm_is_pair (ch));
831 for (SCM melisma_properties = tr->get_property ("melismaBusyProperties");
832 scm_is_pair (melisma_properties);
833 melisma_properties = scm_cdr (melisma_properties))
834 if (to_boolean (tr->get_property (scm_car (melisma_properties))))
841 check_repeat_count_visibility (Context const *context, SCM count)
843 SCM proc = context->get_property ("repeatCountVisibility");
844 return (ly_is_procedure (proc)
845 && to_boolean (scm_call_2 (proc,
847 context->self_scm ())));