]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
Split WWW target in two stages WWW-1 and WWW-2
[lilypond.git] / lily / context.cc
1 /*
2   context.cc -- implement Context
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10
11 #include "context-def.hh"
12 #include "dispatcher.hh"
13 #include "global-context.hh"
14 #include "international.hh"
15 #include "ly-smobs.icc"
16 #include "main.hh"
17 #include "output-def.hh"
18 #include "profile.hh"
19 #include "program-option.hh"
20 #include "scm-hash.hh"
21 #include "translator-group.hh"
22 #include "warn.hh"
23
24 bool
25 Context::is_removable () const
26 {
27   return context_list_ == SCM_EOL && ! iterator_count_
28     && !dynamic_cast<Global_context const *> (daddy_context_);
29 }
30
31 void
32 Context::check_removal ()
33 {
34   for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
35     {
36       Context *ctx = unsmob_context (scm_car (p));
37
38       ctx->check_removal ();
39       if (ctx->is_removable ())
40         {
41           recurse_over_translators (ctx, &Translator::finalize,
42                                     &Translator_group::finalize,
43                                     UP);
44           send_stream_event (ctx, "RemoveContext", 0, 0);
45         }
46     }
47 }
48
49 Context::Context (Context const &src)
50 {
51   (void) src;
52   assert (false);
53 }
54
55 Scheme_hash_table *
56 Context::properties_dict () const
57 {
58   return Scheme_hash_table::unsmob (properties_scm_);
59 }
60
61 void
62 Context::add_context (Context *child)
63 {
64   context_list_ = ly_append2 (context_list_,
65                               scm_cons (child->self_scm (), SCM_EOL));
66
67   child->daddy_context_ = this;
68   this->events_below_->register_as_listener (child->events_below_);
69 }
70
71
72 Context::Context ()
73 {
74   daddy_context_ = 0;
75   aliases_ = SCM_EOL;
76   iterator_count_ = 0;
77   implementation_ = 0;
78   properties_scm_ = SCM_EOL;
79   accepts_list_ = SCM_EOL;
80   context_list_ = SCM_EOL;
81   definition_ = SCM_EOL;
82   definition_mods_ = SCM_EOL;
83   event_source_ = 0;
84   events_below_ = 0;
85
86   smobify_self ();
87
88   Scheme_hash_table *tab = new Scheme_hash_table;
89   properties_scm_ = tab->unprotect ();
90   event_source_ = new Dispatcher ();
91   event_source_->unprotect ();
92   events_below_ = new Dispatcher ();
93   events_below_->unprotect ();
94 }
95
96 /* TODO:  this shares code with find_create_context ().  */
97 Context *
98 Context::create_unique_context (SCM name, string id, SCM operations)
99 {
100   /*
101     Don't create multiple score contexts.
102   */
103   Global_context *gthis = dynamic_cast<Global_context *> (this);
104   if (gthis && gthis->get_score_context ())
105     return gthis->get_score_context ()->create_unique_context (name, id, operations);
106
107   /*
108     TODO: use accepts_list_.
109   */
110   vector<Context_def*> path
111     = unsmob_context_def (definition_)->path_to_acceptable_context (name, get_output_def ());
112
113   if (path.size ())
114     {
115       Context *current = this;
116
117       // start at 1.  The first one (index 0) will be us.
118       for (vsize i = 0; i < path.size (); i++)
119         {
120           SCM ops = SCM_EOL;
121           string id_str = "\\new";
122           if (i == path.size () - 1)
123             {
124               ops = operations;
125               id_str = id;
126             }
127           current = current->create_context (path[i],
128                                              id_str,
129                                              ops);
130         }
131
132       return current;
133     }
134
135   /*
136     Don't go up to Global_context, because global goes down to the
137     Score context
138   */
139   Context *ret = 0;
140   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
141     ret = daddy_context_->create_unique_context (name, id, operations);
142   else
143     {
144       warning (_f ("cannot find or create new `%s'",
145                    ly_symbol2string (name).c_str ()));
146       ret = 0;
147     }
148   return ret;
149 }
150
151 Context *
152 Context::find_create_context (SCM n, string id, SCM operations)
153 {
154   /*
155     Don't create multiple score contexts.
156   */
157   Global_context *gthis = dynamic_cast<Global_context *> (this);
158   if (gthis && gthis->get_score_context ())
159     return gthis->get_score_context ()->find_create_context (n, id, operations);
160
161   if (Context *existing = find_context_below (this, n, id))
162     return existing;
163
164   if (n == ly_symbol2scm ("Bottom"))
165     {
166       Context *tg = get_default_interpreter ();
167       return tg;
168     }
169
170   /*
171     TODO: use accepts_list_.
172   */
173   vector<Context_def*> path
174     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
175
176   if (path.size ())
177     {
178       Context *current = this;
179
180       // start at 1.  The first one (index 0) will be us.
181       for (vsize i = 0; i < path.size (); i++)
182         {
183           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
184
185           string this_id = "";
186           if (i == path.size () -1)
187             this_id = id;
188
189           current = current->create_context (path[i],
190                                              this_id,
191                                              ops);
192         }
193
194       return current;
195     }
196
197   /*
198     Don't go up to Global_context, because global goes down to the
199     Score context
200   */
201   Context *ret = 0;
202   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
203     ret = daddy_context_->find_create_context (n, id, operations);
204   else
205     {
206       warning (_f ("cannot find or create `%s' called `%s'",
207                    ly_symbol2string (n).c_str (), id));
208       ret = 0;
209     }
210   return ret;
211 }
212
213 IMPLEMENT_LISTENER (Context, acknowledge_infant);
214 void
215 Context::acknowledge_infant (SCM sev)
216 {
217   infant_event_ = unsmob_stream_event (sev);
218 }
219
220 IMPLEMENT_LISTENER (Context, set_property_from_event);
221 void
222 Context::set_property_from_event (SCM sev)
223 {
224   Stream_event *ev = unsmob_stream_event (sev);
225   
226   SCM sym = ev->get_property ("symbol");
227   if (scm_is_symbol (sym))
228     {
229       SCM val = ev->get_property ("value");
230       bool ok = true;
231       if (val != SCM_EOL)
232         ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
233       if (ok)
234         set_property (sym, val);
235     }
236 }
237
238 IMPLEMENT_LISTENER (Context, unset_property_from_event);
239 void
240 Context::unset_property_from_event (SCM sev)
241 {
242   Stream_event *ev = unsmob_stream_event (sev);
243   
244   SCM sym = ev->get_property ("symbol");
245   type_check_assignment (sym, SCM_EOL, ly_symbol2scm ("translation-type?"));
246   unset_property (sym);
247 }
248
249 /*
250   Creates a new context from a CreateContext event, and sends an
251   AnnounceNewContext event to this context.
252 */
253 IMPLEMENT_LISTENER (Context, create_context_from_event);
254 void
255 Context::create_context_from_event (SCM sev)
256 {
257   Stream_event *ev = unsmob_stream_event (sev);
258   
259   string id = ly_scm2string (ev->get_property ("id"));
260   SCM ops = ev->get_property ("ops");
261   SCM type_scm = ev->get_property ("type");
262   string type = ly_symbol2string (type_scm);
263   
264   vector<Context_def*> path
265     = unsmob_context_def (definition_)->path_to_acceptable_context (type_scm, get_output_def ());
266   if (path.size () != 1)
267     {
268       programming_error (_f ("Invalid CreateContext event: Cannot create %s context", type.c_str ()));
269       return;
270     }
271   Context_def *cdef = path[0];
272   
273   Context *new_context = cdef->instantiate (ops);
274
275   new_context->id_string_ = id;
276   
277   /* Register various listeners:
278       - Make the new context hear events that universally affect contexts
279       - connect events_below etc. properly */
280   /* We want to be the first ones to hear our own events. Therefore, wait
281      before registering events_below_ */
282   new_context->event_source ()->
283     add_listener (GET_LISTENER (new_context->create_context_from_event),
284                   ly_symbol2scm ("CreateContext"));
285   new_context->event_source ()->
286     add_listener (GET_LISTENER (new_context->remove_context),
287                   ly_symbol2scm ("RemoveContext"));
288   new_context->event_source ()->
289     add_listener (GET_LISTENER (new_context->change_parent),
290                   ly_symbol2scm ("ChangeParent"));
291   new_context->event_source ()->
292     add_listener (GET_LISTENER (new_context->set_property_from_event),
293                   ly_symbol2scm ("SetProperty"));
294   new_context->event_source ()->
295     add_listener (GET_LISTENER (new_context->unset_property_from_event),
296                   ly_symbol2scm ("UnsetProperty"));
297
298   new_context->events_below_->register_as_listener (new_context->event_source_);
299   this->add_context (new_context);
300
301   new_context->unprotect ();
302
303   Context_def *td = unsmob_context_def (new_context->definition_);
304
305   /* This cannot move before add_context (), because \override
306      operations require that we are in the hierarchy.  */
307   td->apply_default_property_operations (new_context);
308   apply_property_operations (new_context, ops);
309
310   send_stream_event (this, "AnnounceNewContext", 0,
311                      ly_symbol2scm ("context"), new_context->self_scm (),
312                      ly_symbol2scm ("creator"), sev);
313 }
314
315 Context *
316 Context::create_context (Context_def *cdef,
317                          string id,
318                          SCM ops)
319 {
320   infant_event_ = 0;
321   /* TODO: This is fairly misplaced. We can fix this when we have taken out all
322      iterator specific stuff from the Context class */
323   event_source_->
324     add_listener (GET_LISTENER (acknowledge_infant),
325                   ly_symbol2scm ("AnnounceNewContext"));
326   /* The CreateContext creates a new context, and sends an announcement of the
327      new context through another event. That event will be stored in
328      infant_event_ to create a return value. */
329   send_stream_event (this, "CreateContext", 0,
330                      ly_symbol2scm ("ops"), ops,
331                      ly_symbol2scm ("type"), cdef->get_context_name (),
332                      ly_symbol2scm ("id"), ly_string2scm (id));
333   event_source_->
334     remove_listener (GET_LISTENER (acknowledge_infant),
335                      ly_symbol2scm ("AnnounceNewContext"));
336
337   assert (infant_event_);
338   SCM infant_scm = infant_event_->get_property ("context");
339   Context *infant = unsmob_context (infant_scm);
340
341   if (!infant || infant->get_parent_context () != this)
342     {
343       programming_error ("create_context: can't locate newly created context");
344       return 0;
345     }
346
347   return infant;
348 }
349
350 /*
351   Default child context as a SCM string, or something else if there is
352   none.
353 */
354 SCM
355 Context::default_child_context_name () const
356 {
357   return scm_is_pair (accepts_list_)
358     ? scm_car (accepts_list_)
359     : SCM_EOL;
360 }
361
362 bool
363 Context::is_bottom_context () const
364 {
365   return !scm_is_symbol (default_child_context_name ());
366 }
367
368 Context *
369 Context::get_default_interpreter ()
370 {
371   if (!is_bottom_context ())
372     {
373       SCM nm = default_child_context_name ();
374       SCM st = find_context_def (get_output_def (), nm);
375
376       string name = ly_symbol2string (nm);
377       Context_def *t = unsmob_context_def (st);
378       if (!t)
379         {
380           warning (_f ("cannot find or create: `%s'", name.c_str ()));
381           t = unsmob_context_def (this->definition_);
382         }
383
384       Context *tg = create_context (t, "", SCM_EOL);
385       return tg->get_default_interpreter ();
386     }
387   return this;
388 }
389
390 /*
391   PROPERTIES
392 */
393 Context *
394 Context::where_defined (SCM sym, SCM *value) const
395 {
396 #ifndef NDEBUG
397   if (profile_property_accesses)
398     note_property_access (&context_property_lookup_table, sym);
399 #endif
400
401   if (properties_dict ()->try_retrieve (sym, value))
402     return (Context *)this;
403
404   return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
405 }
406
407 /*
408   return SCM_EOL when not found.
409 */
410 SCM
411 Context::internal_get_property (SCM sym) const
412 {
413 #ifndef NDEBUG
414   if (profile_property_accesses)
415     note_property_access (&context_property_lookup_table, sym);
416 #endif
417
418   SCM val = SCM_EOL;
419   if (properties_dict ()->try_retrieve (sym, &val))
420     return val;
421
422   if (daddy_context_)
423     return daddy_context_->internal_get_property (sym);
424
425   return val;
426 }
427
428 /*
429 Called by the send_stream_event macro. props is a 0-terminated array of
430 properties and corresponding values, interleaved. This method should not
431 be called from any other place than the send_stream_event macro.
432 */
433 void
434 Context::internal_send_stream_event (SCM type, Input *origin, SCM props[])
435 {
436   Stream_event *e = new Stream_event (type, origin);
437   for (int i = 0; props[i]; i += 2)
438     {
439       e->set_property (props[i], props[i+1]);
440     }
441   event_source_->broadcast (e);
442   e->unprotect ();
443 }
444
445 bool
446 Context::is_alias (SCM sym) const
447 {
448   if (sym == ly_symbol2scm ("Bottom")
449       && !scm_is_pair (accepts_list_))
450     return true;
451   if (sym == unsmob_context_def (definition_)->get_context_name ())
452     return true;
453
454   return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
455 }
456
457 void
458 Context::add_alias (SCM sym)
459 {
460   aliases_ = scm_cons (sym, aliases_);
461 }
462
463 /* we don't (yet) instrument context properties */
464 void
465 Context::instrumented_set_property (SCM sym, SCM val, const char*, int, const char*)
466 {
467   internal_set_property (sym, val);
468 }
469
470 void
471 Context::internal_set_property (SCM sym, SCM val)
472 {
473   if (do_internal_type_checking_global)
474     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
475
476   properties_dict ()->set (sym, val);
477 }
478
479 /*
480   TODO: look up to check whether we have inherited var?
481 */
482 void
483 Context::unset_property (SCM sym)
484 {
485   properties_dict ()->remove (sym);
486 }
487
488 IMPLEMENT_LISTENER (Context, change_parent);
489 void
490 Context::change_parent (SCM sev)
491 {
492   Stream_event *ev = unsmob_stream_event (sev);
493   Context *to = unsmob_context (ev->get_property ("context"));
494
495   disconnect_from_parent ();
496   to->add_context (this);
497 }
498
499 /*
500   Die. The next GC sweep should take care of the actual death.
501  */
502 IMPLEMENT_LISTENER (Context, remove_context);
503 void
504 Context::remove_context (SCM)
505 {
506   /* ugh, the translator group should listen to RemoveContext events by itself */
507   Translator_group *impl = implementation ();
508   if (impl)
509     impl->disconnect_from_context ();
510   disconnect_from_parent ();
511 }
512
513 void
514 Context::disconnect_from_parent ()
515 {
516   daddy_context_->events_below_->unregister_as_listener (this->events_below_);
517   daddy_context_->context_list_ = scm_delq_x (this->self_scm (), daddy_context_->context_list_);
518   daddy_context_ = 0;
519 }
520
521 /*
522   ID == "" means accept any ID.
523 */
524 Context *
525 find_context_below (Context *where,
526                     SCM type, string id)
527 {
528   if (where->is_alias (type))
529     {
530       if (id == "" || where->id_string () == id)
531         return where;
532     }
533
534   Context *found = 0;
535   for (SCM s = where->children_contexts ();
536        !found && scm_is_pair (s); s = scm_cdr (s))
537     {
538       Context *tr = unsmob_context (scm_car (s));
539
540       found = find_context_below (tr, type, id);
541     }
542
543   return found;
544 }
545
546 SCM
547 Context::properties_as_alist () const
548 {
549   return properties_dict ()->to_alist ();
550 }
551
552 SCM
553 Context::context_name_symbol () const
554 {
555   Context_def *td = unsmob_context_def (definition_);
556   return td->get_context_name ();
557 }
558
559 string
560 Context::context_name () const
561 {
562   return ly_symbol2string (context_name_symbol ());
563 }
564
565 Context *
566 Context::get_score_context () const
567 {
568   if (daddy_context_)
569     return daddy_context_->get_score_context ();
570   else
571     return 0;
572 }
573
574 Output_def *
575 Context::get_output_def () const
576 {
577   return daddy_context_ ? daddy_context_->get_output_def () : 0;
578 }
579
580 Context::~Context ()
581 {
582 }
583
584 Moment
585 Context::now_mom () const
586 {
587   Context const *p = this;
588   while (p->daddy_context_)
589     p = p->daddy_context_;
590
591   return p->now_mom ();
592 }
593
594 int
595 Context::print_smob (SCM s, SCM port, scm_print_state *)
596 {
597   Context *sc = (Context *) SCM_CELL_WORD_1 (s);
598
599   scm_puts ("#<", port);
600   scm_puts (sc->class_name (), port);
601   if (Context_def *d = unsmob_context_def (sc->definition_))
602     {
603       scm_puts (" ", port);
604       scm_display (d->get_context_name (), port);
605     }
606
607   if (!sc->id_string_.empty ())
608     {
609       scm_puts ("=", port);
610       scm_puts (sc->id_string_.c_str (), port);
611     }
612
613   scm_puts (" ", port);
614
615   scm_display (sc->context_list_, port);
616   scm_puts (" >", port);
617
618   return 1;
619 }
620
621 SCM
622 Context::mark_smob (SCM sm)
623 {
624   Context *me = (Context *) SCM_CELL_WORD_1 (sm);
625
626   scm_gc_mark (me->context_list_);
627   scm_gc_mark (me->aliases_);
628   scm_gc_mark (me->definition_);
629   scm_gc_mark (me->definition_mods_);
630   scm_gc_mark (me->properties_scm_);
631   scm_gc_mark (me->accepts_list_);
632
633   if (me->implementation_)
634     scm_gc_mark (me->implementation_->self_scm ());
635
636   if (me->event_source_)
637     scm_gc_mark (me->event_source_->self_scm ());
638
639   if (me->events_below_)
640     scm_gc_mark (me->events_below_->self_scm ());
641
642   return me->properties_scm_;
643 }
644
645 IMPLEMENT_SMOBS (Context);
646 IMPLEMENT_DEFAULT_EQUAL_P (Context);
647 IMPLEMENT_TYPE_P (Context, "ly:context?");
648
649 Global_context *
650 Context::get_global_context () const
651 {
652   if (dynamic_cast<Global_context *> ((Context *) this))
653     return dynamic_cast<Global_context *> ((Context *) this);
654
655   if (daddy_context_)
656     return daddy_context_->get_global_context ();
657
658   programming_error ("no Global context");
659   return 0;
660 }
661
662 Context *
663 Context::get_parent_context () const
664 {
665   return daddy_context_;
666 }
667
668 /*
669   Ugh. Where to put this?
670 */
671 Rational
672 measure_length (Context const *context)
673 {
674   SCM l = context->get_property ("measureLength");
675   Rational length (1);
676   if (unsmob_moment (l))
677     length = unsmob_moment (l)->main_part_;
678   return length;
679 }
680
681 Moment
682 measure_position (Context const *context)
683 {
684   SCM sm = context->get_property ("measurePosition");
685
686   Moment m = 0;
687   if (unsmob_moment (sm))
688     {
689       m = *unsmob_moment (sm);
690
691       if (m.main_part_ < Rational (0))
692         {
693           Rational length (measure_length (context));
694           while (m.main_part_ < Rational (0))
695             m.main_part_ += length;
696         }
697     }
698
699   return m;
700 }
701
702
703 void
704 set_context_property_on_children (Context *trans, SCM sym, SCM val)
705 {
706   trans->set_property (sym, ly_deep_copy (val));
707   for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
708     {
709       Context *trg = unsmob_context (scm_car (p));
710       set_context_property_on_children (trg, sym, ly_deep_copy (val));
711     }
712 }
713
714 bool
715 melisma_busy (Context *tr)
716 {
717   SCM melisma_properties = tr->get_property ("melismaBusyProperties");
718   bool busy = false;
719
720   for (; !busy && scm_is_pair (melisma_properties);
721        melisma_properties = scm_cdr (melisma_properties))
722     busy = busy || to_boolean (tr->internal_get_property (scm_car (melisma_properties)));
723
724   return busy;
725 }