]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
Issue 5056/2: Don't assume uninstantiable engravers to be symbols
[lilypond.git] / lily / context.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "context.hh"
21
22 #include "context-def.hh"
23 #include "dispatcher.hh"
24 #include "global-context.hh"
25 #include "international.hh"
26 #include "main.hh"
27 #include "output-def.hh"
28 #include "profile.hh"
29 #include "program-option.hh"
30 #include "scm-hash.hh"
31 #include "translator-group.hh"
32 #include "warn.hh"
33 #include "lily-imports.hh"
34
35 bool
36 Context::is_removable () const
37 {
38   return scm_is_null (context_list_) && ! client_count_
39          && !dynamic_cast<Global_context const *> (daddy_context_);
40 }
41
42 void
43 Context::check_removal ()
44 {
45   for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
46     {
47       Context *ctx = unsmob<Context> (scm_car (p));
48
49       ctx->check_removal ();
50       if (ctx->is_removable ())
51         {
52           recurse_over_translators
53             (ctx,
54              Callback0_wrapper::make_smob
55              <Translator, &Translator::finalize> (),
56              Callback0_wrapper::make_smob
57              <Translator_group, &Translator_group::finalize> (),
58              UP);
59           send_stream_event (ctx, "RemoveContext", 0, 0);
60         }
61     }
62 }
63
64 Scheme_hash_table *
65 Context::properties_dict () const
66 {
67   return unsmob<Scheme_hash_table> (properties_scm_);
68 }
69
70 void
71 Context::add_context (Context *child)
72 {
73   context_list_ = ly_append2 (context_list_,
74                               scm_cons (child->self_scm (), SCM_EOL));
75
76   child->daddy_context_ = this;
77   events_below_->register_as_listener (child->events_below_);
78 }
79
80 Context::Context ()
81 {
82   daddy_context_ = 0;
83   aliases_ = SCM_EOL;
84   client_count_ = 0;
85   implementation_ = 0;
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;
92   event_source_ = 0;
93   events_below_ = 0;
94
95   smobify_self ();
96
97   properties_scm_ = Scheme_hash_table::make_smob ();
98   event_source_ = new Dispatcher ();
99   event_source_->unprotect ();
100   events_below_ = new Dispatcher ();
101   events_below_->unprotect ();
102 }
103
104 /* TODO:  this shares code with find_create_context ().  */
105 Context *
106 Context::create_unique_context (SCM name, const string &id, SCM operations)
107 {
108   /*
109     Don't create multiple score contexts.
110   */
111   Global_context *gthis = dynamic_cast<Global_context *> (this);
112   if (gthis && gthis->get_score_context ())
113     return gthis->get_score_context ()->create_unique_context (name, id, operations);
114
115   vector<Context_def *> path = path_to_acceptable_context (name);
116   if (path.size ())
117     {
118       Context *current = this;
119
120       // Iterate through the path and create all of the implicit contexts.
121       for (vsize i = 0; i < path.size (); i++)
122         {
123           SCM ops = SCM_EOL;
124           string id_str = "\\new";
125           if (i == path.size () - 1)
126             {
127               ops = operations;
128               id_str = id;
129             }
130           current = current->create_context (path[i],
131                                              id_str,
132                                              ops);
133         }
134
135       return current;
136     }
137
138   /*
139     Don't go up to Global_context, because global goes down to the
140     Score context
141   */
142   Context *ret = 0;
143   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
144     ret = daddy_context_->create_unique_context (name, id, operations);
145   else
146     {
147       warning (_f ("cannot find or create new `%s'",
148                    ly_symbol2string (name).c_str ()));
149       ret = 0;
150     }
151   return ret;
152 }
153
154 Context *
155 Context::find_create_context (SCM n, const string &id, SCM operations)
156 {
157   /*
158     Don't create multiple score contexts.
159   */
160   Global_context *gthis = dynamic_cast<Global_context *> (this);
161   if (gthis)
162     {
163       if (gthis->get_score_context ())
164         return gthis->get_score_context ()->find_create_context (n, id, operations);
165
166       // Special case: If we use \set Timing.xxx = whatever before
167       // Score is established, the alias of Score to Timing will not
168       // be taken into account.  We check for this particular case
169       // here.  Aliases apart from Score-level ones don't warrant
170       // context creation as they could create unwanted contexts, like
171       // RhythmicVoice instead of Voice.  Creating a Score context,
172       // however, can't really do anything wrong.
173
174       SCM score_name = default_child_context_name ();
175       SCM score_def = find_context_def (get_output_def (), score_name);
176
177       if (Context_def *cd = unsmob<Context_def> (score_def))
178         {
179           if (cd->is_alias (n))
180             return create_context (cd, id, operations);
181         }
182     }
183
184
185   if (Context *existing = find_context_below (this, n, id))
186     return existing;
187
188   if (scm_is_eq (n, ly_symbol2scm ("Bottom")))
189     {
190       Context *tg = get_default_interpreter (id);
191       return tg;
192     }
193
194   vector<Context_def *> path = path_to_acceptable_context (n);
195
196   if (path.size ())
197     {
198       Context *current = this;
199
200       // start at 1.  The first one (index 0) will be us.
201       for (vsize i = 0; i < path.size (); i++)
202         {
203           SCM ops = (i == path.size () - 1) ? operations : SCM_EOL;
204
205           string this_id = "";
206           if (i == path.size () - 1)
207             this_id = id;
208
209           current = current->create_context (path[i],
210                                              this_id,
211                                              ops);
212         }
213
214       return current;
215     }
216
217   /*
218     Don't go up to Global_context, because global goes down to the
219     Score context
220   */
221   Context *ret = 0;
222   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
223     ret = daddy_context_->find_create_context (n, id, operations);
224   else
225     {
226       warning (_f ("cannot find or create `%s' called `%s'",
227                    ly_symbol2string (n).c_str (), id));
228       ret = 0;
229     }
230   return ret;
231 }
232
233 void
234 Context::acknowledge_infant (SCM sev)
235 {
236   infant_event_ = unsmob<Stream_event> (sev);
237 }
238
239 void
240 Context::set_property_from_event (SCM sev)
241 {
242   Stream_event *ev = unsmob<Stream_event> (sev);
243
244   SCM sym = ev->get_property ("symbol");
245   if (scm_is_symbol (sym))
246     {
247       SCM val = ev->get_property ("value");
248
249       if (SCM_UNBNDP (val)) {
250         unset_property (sym);
251         return;
252       }
253
254       bool ok = true;
255       ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
256
257       if (ok)
258         {
259           if (to_boolean (ev->get_property ("once")))
260             {
261               if (Global_context *g = get_global_context ())
262                 {
263                   SCM old_val = SCM_UNDEFINED;
264                   if (here_defined (sym, &old_val))
265                     g->add_finalization (scm_list_4 (ly_context_set_property_x_proc,
266                                                      self_scm (),
267                                                      sym,
268                                                      old_val));
269                   else
270                     g->add_finalization (scm_list_3 (ly_context_unset_property_proc,
271                                                      self_scm (),
272                                                      sym));
273                 }
274             }
275           set_property (sym, val);
276         }
277     }
278 }
279
280 void
281 Context::unset_property_from_event (SCM sev)
282 {
283   Stream_event *ev = unsmob<Stream_event> (sev);
284
285   SCM sym = ev->get_property ("symbol");
286   bool ok = type_check_assignment (sym, SCM_EOL, ly_symbol2scm ("translation-type?"));
287
288   if (ok)
289     {
290       if (to_boolean (ev->get_property ("once")))
291         {
292           if (Global_context *g = get_global_context ())
293             {
294               SCM old_val = SCM_UNDEFINED;
295               if (here_defined (sym, &old_val))
296                 g->add_finalization (scm_list_4 (ly_context_set_property_x_proc,
297                                                  self_scm (),
298                                                  sym,
299                                                  old_val));
300               else
301                 g->add_finalization (scm_list_3 (ly_context_unset_property_proc,
302                                                  self_scm (),
303                                                  sym));
304             }
305         }
306       unset_property (sym);
307     }
308 }
309
310 /*
311   Creates a new context from a CreateContext event, and sends an
312   AnnounceNewContext event to this context.
313 */
314 void
315 Context::create_context_from_event (SCM sev)
316 {
317   Stream_event *ev = unsmob<Stream_event> (sev);
318
319   string id = ly_scm2string (ev->get_property ("id"));
320   SCM ops = ev->get_property ("ops");
321   SCM type_scm = ev->get_property ("type");
322   string type = ly_symbol2string (type_scm);
323
324   vector<Context_def *> path = path_to_acceptable_context (type_scm);
325
326   if (path.size () != 1)
327     {
328       programming_error (to_string ("Invalid CreateContext event: Cannot create %s context", type.c_str ()));
329       return;
330     }
331   Context_def *cdef = path[0];
332
333   Context *new_context = cdef->instantiate (ops);
334
335   new_context->id_string_ = id;
336
337   /* Register various listeners:
338       - Make the new context hear events that universally affect contexts
339       - connect events_below etc. properly */
340   /* We want to be the first ones to hear our own events. Therefore, wait
341      before registering events_below_ */
342   new_context->event_source ()->
343   add_listener (new_context->GET_LISTENER (Context, create_context_from_event),
344                 ly_symbol2scm ("CreateContext"));
345   new_context->event_source ()->
346   add_listener (new_context->GET_LISTENER (Context, remove_context),
347                 ly_symbol2scm ("RemoveContext"));
348   new_context->event_source ()->
349   add_listener (new_context->GET_LISTENER (Context, change_parent),
350                 ly_symbol2scm ("ChangeParent"));
351   new_context->event_source ()->
352   add_listener (new_context->GET_LISTENER (Context, set_property_from_event),
353                 ly_symbol2scm ("SetProperty"));
354   new_context->event_source ()->
355   add_listener (new_context->GET_LISTENER (Context, unset_property_from_event),
356                 ly_symbol2scm ("UnsetProperty"));
357
358   new_context->events_below_->register_as_listener (new_context->event_source_);
359   add_context (new_context);
360
361   new_context->unprotect ();
362
363   Context_def *td = unsmob<Context_def> (new_context->definition_);
364
365   /* This cannot move before add_context (), because \override
366      operations require that we are in the hierarchy.  */
367   td->apply_default_property_operations (new_context);
368   apply_property_operations (new_context, ops);
369
370   send_stream_event (this, "AnnounceNewContext", 0,
371                      ly_symbol2scm ("context"), new_context->self_scm (),
372                      ly_symbol2scm ("creator"), sev);
373 }
374
375 vector<Context_def *>
376 Context::path_to_acceptable_context (SCM name) const
377 {
378   // The 'accepts elements in definition_mods_ is a list of ('accepts string),
379   // but the Context_def expects to see elements of the form ('accepts symbol).
380   SCM accepts = SCM_EOL;
381   for (SCM s = definition_mods_; scm_is_pair (s); s = scm_cdr (s))
382     if (scm_is_eq (scm_caar (s), ly_symbol2scm ("accepts")))
383       {
384         SCM elt = scm_list_2 (scm_caar (s), scm_string_to_symbol (scm_cadar (s)));
385         accepts = scm_cons (elt, accepts);
386       }
387
388   return unsmob<Context_def> (definition_)->path_to_acceptable_context (name,
389          get_output_def (),
390          scm_reverse_x (accepts, SCM_EOL));
391
392 }
393
394 Context *
395 Context::create_context (Context_def *cdef,
396                          const string &id,
397                          SCM ops)
398 {
399   infant_event_ = 0;
400   /* TODO: This is fairly misplaced. We can fix this when we have taken out all
401      iterator specific stuff from the Context class */
402   event_source_->
403   add_listener (GET_LISTENER (Context, acknowledge_infant),
404                 ly_symbol2scm ("AnnounceNewContext"));
405   /* The CreateContext creates a new context, and sends an announcement of the
406      new context through another event. That event will be stored in
407      infant_event_ to create a return value. */
408   send_stream_event (this, "CreateContext", 0,
409                      ly_symbol2scm ("ops"), ops,
410                      ly_symbol2scm ("type"), cdef->get_context_name (),
411                      ly_symbol2scm ("id"), ly_string2scm (id));
412   event_source_->
413   remove_listener (GET_LISTENER (Context, acknowledge_infant),
414                    ly_symbol2scm ("AnnounceNewContext"));
415
416   assert (infant_event_);
417   SCM infant_scm = infant_event_->get_property ("context");
418   Context *infant = unsmob<Context> (infant_scm);
419
420   if (!infant || infant->get_parent_context () != this)
421     {
422       programming_error ("create_context: can't locate newly created context");
423       return 0;
424     }
425
426   return infant;
427 }
428
429 /*
430   Default child context as a SCM string, or something else if there is
431   none.
432 */
433 SCM
434 Context::default_child_context_name () const
435 {
436   return default_child_;
437 }
438
439 bool
440 Context::is_bottom_context () const
441 {
442   return !scm_is_symbol (default_child_context_name ());
443 }
444
445 Context *
446 Context::get_default_interpreter (const string &context_id)
447 {
448   if (!is_bottom_context ())
449     {
450       SCM nm = default_child_context_name ();
451       SCM st = find_context_def (get_output_def (), nm);
452
453       string name = ly_symbol2string (nm);
454       Context_def *t = unsmob<Context_def> (st);
455       if (!t)
456         {
457           warning (_f ("cannot find or create: `%s'", name.c_str ()));
458           t = unsmob<Context_def> (definition_);
459         }
460       if (scm_is_symbol (t->get_default_child (SCM_EOL)))
461         {
462           Context *tg = create_context (t, "\\new", SCM_EOL);
463           return tg->get_default_interpreter (context_id);
464         }
465       return create_context (t, context_id, SCM_EOL);
466     }
467   else if (!context_id.empty () && context_id != id_string ())
468     {
469       if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
470         return daddy_context_->get_default_interpreter (context_id);
471       warning (_f ("cannot find or create new Bottom = \"%s\"",
472                    context_id.c_str ()));
473     }
474   return this;
475 }
476
477 /*
478   PROPERTIES
479 */
480 Context *
481 Context::where_defined (SCM sym, SCM *value) const
482 {
483 #ifdef DEBUG
484   if (profile_property_accesses)
485     note_property_access (&context_property_lookup_table, sym);
486 #endif
487
488   if (properties_dict ()->try_retrieve (sym, value))
489     return (Context *)this;
490
491   return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
492 }
493
494 /* Quick variant of where_defined.  Checks only the context itself. */
495
496 bool
497 Context::here_defined (SCM sym, SCM *value) const
498 {
499 #ifdef DEBUG
500   if (profile_property_accesses)
501     note_property_access (&context_property_lookup_table, sym);
502 #endif
503
504   return properties_dict ()->try_retrieve (sym, value);
505 }
506
507 /*
508   return SCM_EOL when not found.
509 */
510 SCM
511 Context::internal_get_property (SCM sym) const
512 {
513 #ifdef DEBUG
514   if (profile_property_accesses)
515     note_property_access (&context_property_lookup_table, sym);
516 #endif
517
518   SCM val = SCM_EOL;
519   if (properties_dict ()->try_retrieve (sym, &val))
520     return val;
521
522   if (daddy_context_)
523     return daddy_context_->internal_get_property (sym);
524
525   return val;
526 }
527
528 /*
529 Called by the send_stream_event macro. props is a 0-terminated array of
530 properties and corresponding values, interleaved. This method should not
531 be called from any other place than the send_stream_event macro.
532 */
533 void
534 Context::internal_send_stream_event (SCM type, Input *origin, SCM props[])
535 {
536   Stream_event *e = new Stream_event (Lily::ly_make_event_class (type), origin);
537   for (int i = 0; props[i]; i += 2)
538     {
539       e->set_property (props[i], props[i + 1]);
540     }
541   event_source_->broadcast (e);
542   e->unprotect ();
543 }
544
545 bool
546 Context::is_alias (SCM sym) const
547 {
548   if (scm_is_eq (sym, ly_symbol2scm ("Bottom")))
549     return is_bottom_context ();
550   if (scm_is_eq (sym, context_name_symbol ()))
551     return true;
552
553   return scm_is_true (scm_c_memq (sym, aliases_));
554 }
555
556 void
557 Context::add_alias (SCM sym)
558 {
559   aliases_ = scm_cons (sym, aliases_);
560 }
561
562 /* we don't (yet) instrument context properties */
563 void
564 Context::instrumented_set_property (SCM sym, SCM val, const char *, int, const char *)
565 {
566   internal_set_property (sym, val);
567 }
568
569 void
570 Context::internal_set_property (SCM sym, SCM val)
571 {
572   bool type_check_ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
573
574   if (do_internal_type_checking_global)
575     assert (type_check_ok);
576
577   if (type_check_ok)
578     properties_dict ()->set (sym, val);
579 }
580
581 /*
582   TODO: look up to check whether we have inherited var?
583 */
584 void
585 Context::unset_property (SCM sym)
586 {
587   properties_dict ()->remove (sym);
588 }
589
590 void
591 Context::change_parent (SCM sev)
592 {
593   Stream_event *ev = unsmob<Stream_event> (sev);
594   Context *to = unsmob<Context> (ev->get_property ("context"));
595
596   disconnect_from_parent ();
597   to->add_context (this);
598 }
599
600 /*
601   Die. The next GC sweep should take care of the actual death.
602  */
603 void
604 Context::remove_context (SCM)
605 {
606   /* ugh, the translator group should listen to RemoveContext events by itself */
607   Translator_group *impl = implementation ();
608   if (impl)
609     impl->disconnect_from_context ();
610   disconnect_from_parent ();
611 }
612
613 void
614 Context::disconnect_from_parent ()
615 {
616   daddy_context_->events_below_->unregister_as_listener (events_below_);
617   daddy_context_->context_list_ = scm_delq_x (self_scm (), daddy_context_->context_list_);
618   daddy_context_ = 0;
619 }
620
621 Context *
622 find_context_above (Context *where, SCM type)
623 {
624   while (where && !where->is_alias (type))
625     where = where->get_parent_context ();
626
627   return where;
628 }
629
630 Context *
631 find_context_above_by_parent_type (Context *where, SCM parent_type)
632 {
633   for (Context *child = 0; where;
634        child = where, where = where->get_parent_context ())
635     if (where->is_alias (parent_type))
636       return child;
637
638   return 0;
639 }
640
641 Context *
642 find_context_below (Context *where,
643                     SCM type, const string &id)
644 {
645   if (where->is_alias (type))
646     {
647       if (id == "" || where->id_string () == id)
648         return where;
649     }
650
651   Context *found = 0;
652   for (SCM s = where->children_contexts ();
653        !found && scm_is_pair (s); s = scm_cdr (s))
654     {
655       Context *tr = unsmob<Context> (scm_car (s));
656
657       found = find_context_below (tr, type, id);
658     }
659
660   return found;
661 }
662
663 Context *
664 find_context_near (Context *where,
665                    SCM type, const string &id)
666 {
667   for ( ; where; where = where->get_parent_context ())
668     {
669       Context *found = find_context_below (where, type, id);
670       if (found)
671         return found;
672     }
673
674   return 0;
675 }
676
677 Context *
678 find_top_context (Context *where)
679 {
680   Context *top = where;
681   for ( ; where; where = where->get_parent_context())
682     top = where;
683   return top;
684 }
685
686 SCM
687 Context::properties_as_alist () const
688 {
689   return properties_dict ()->to_alist ();
690 }
691
692 SCM
693 Context::context_name_symbol () const
694 {
695   Context_def *td = unsmob<Context_def> (definition_);
696   return td->get_context_name ();
697 }
698
699 string
700 Context::context_name () const
701 {
702   return ly_symbol2string (context_name_symbol ());
703 }
704
705 Context *
706 Context::get_score_context () const
707 {
708   if (daddy_context_)
709     return daddy_context_->get_score_context ();
710   else
711     return 0;
712 }
713
714 Output_def *
715 Context::get_output_def () const
716 {
717   return daddy_context_ ? daddy_context_->get_output_def () : 0;
718 }
719
720 Context::~Context ()
721 {
722 }
723
724 Moment
725 Context::now_mom () const
726 {
727   Context const *p = this;
728   while (p->daddy_context_)
729     p = p->daddy_context_;
730
731   return p->now_mom ();
732 }
733
734 int
735 Context::print_smob (SCM port, scm_print_state *) const
736 {
737   scm_puts ("#<", port);
738   scm_puts (class_name (), port);
739   if (Context_def *d = unsmob<Context_def> (definition_))
740     {
741       scm_puts (" ", port);
742       scm_display (d->get_context_name (), port);
743     }
744
745   if (!id_string_.empty ())
746     {
747       scm_puts ("=", port);
748       scm_puts (id_string_.c_str (), port);
749     }
750
751   scm_puts (" ", port);
752
753   scm_display (context_list_, port);
754   scm_puts (" >", port);
755
756   return 1;
757 }
758
759 void
760 Context::derived_mark () const
761 {
762 }
763
764 SCM
765 Context::mark_smob () const
766 {
767   scm_gc_mark (context_list_);
768   scm_gc_mark (aliases_);
769   scm_gc_mark (definition_);
770   scm_gc_mark (definition_mods_);
771   scm_gc_mark (properties_scm_);
772   scm_gc_mark (accepts_list_);
773   scm_gc_mark (default_child_);
774
775   if (implementation_)
776     scm_gc_mark (implementation_->self_scm ());
777
778   if (event_source_)
779     scm_gc_mark (event_source_->self_scm ());
780
781   if (events_below_)
782     scm_gc_mark (events_below_->self_scm ());
783
784   derived_mark ();
785
786   return properties_scm_;
787 }
788
789 const char * const Context::type_p_name_ = "ly:context?";
790
791 Global_context *
792 Context::get_global_context () const
793 {
794   if (dynamic_cast<Global_context *> ((Context *) this))
795     return dynamic_cast<Global_context *> ((Context *) this);
796
797   if (daddy_context_)
798     return daddy_context_->get_global_context ();
799
800   programming_error ("no Global context");
801   return 0;
802 }
803
804 Context *
805 Context::get_parent_context () const
806 {
807   return daddy_context_;
808 }
809
810 /*
811   Ugh. Where to put this?
812 */
813 Rational
814 measure_length (Context const *context)
815 {
816   SCM l = context->get_property ("measureLength");
817   Rational length (1);
818   if (unsmob<Moment> (l))
819     length = unsmob<Moment> (l)->main_part_;
820   return length;
821 }
822
823 Moment
824 measure_position (Context const *context)
825 {
826   SCM sm = context->get_property ("measurePosition");
827
828   Moment m = 0;
829   if (unsmob<Moment> (sm))
830     {
831       m = *unsmob<Moment> (sm);
832
833       if (m.main_part_ < Rational (0))
834         {
835           Rational length (measure_length (context));
836           while (m.main_part_ < Rational (0))
837             m.main_part_ += length;
838         }
839     }
840
841   return m;
842 }
843
844 /* Finds the measure position after a note of length DUR that
845    begins at the current measure position. */
846 Moment
847 measure_position (Context const *context, Duration const *dur)
848 {
849   Moment pos = measure_position (context);
850   Rational dur_length = dur ? dur->get_length () : Rational (0);
851
852   Moment end_pos = pos.grace_part_ < Rational (0)
853                    ? Moment (pos.main_part_, pos.grace_part_ + dur_length)
854                    : Moment (pos.main_part_ + dur_length, 0);
855
856   return end_pos;
857 }
858
859 int
860 measure_number (Context const *context)
861 {
862   SCM barnum = context->get_property ("internalBarNumber");
863   SCM smp = context->get_property ("measurePosition");
864
865   int bn = robust_scm2int (barnum, 0);
866   Moment mp = robust_scm2moment (smp, Moment (0));
867   if (mp.main_part_ < Rational (0))
868     bn--;
869
870   return bn;
871 }
872
873 void
874 set_context_property_on_children (Context *trans, SCM sym, SCM val)
875 {
876   trans->set_property (sym, ly_deep_copy (val));
877   for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
878     {
879       Context *trg = unsmob<Context> (scm_car (p));
880       set_context_property_on_children (trg, sym, ly_deep_copy (val));
881     }
882 }
883
884 bool
885 melisma_busy (Context *tr)
886 {
887   // When there are subcontexts, they are responsible for maintaining
888   // melismata.
889   SCM ch = tr->children_contexts ();
890   if (scm_is_pair (ch))
891     {
892       // all contexts need to have a busy melisma for this to evaluate
893       // to true.
894
895       do {
896         if (!melisma_busy (unsmob<Context> (scm_car (ch))))
897           return false;
898         ch = scm_cdr (ch);
899       } while (scm_is_pair (ch));
900       return true;
901     }
902
903   for (SCM melisma_properties = tr->get_property ("melismaBusyProperties");
904        scm_is_pair (melisma_properties);
905        melisma_properties = scm_cdr (melisma_properties))
906     if (to_boolean (tr->get_property (scm_car (melisma_properties))))
907       return true;
908
909   return false;
910 }
911
912 bool
913 check_repeat_count_visibility (Context const *context, SCM count)
914 {
915   SCM proc = context->get_property ("repeatCountVisibility");
916   return (ly_is_procedure (proc)
917           && to_boolean (scm_call_2 (proc,
918                                      count,
919                                      context->self_scm ())));
920 }