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