]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* scm/layout-page-layout.scm (write-page-breaks): record tweaks
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10
11 #include "program-option.hh"
12 #include "context-def.hh"
13 #include "ly-smobs.icc"
14 #include "main.hh"
15 #include "output-def.hh"
16 #include "scm-hash.hh"
17 #include "score-context.hh"
18 #include "translator-group.hh"
19 #include "warn.hh"
20 #include "lilypond-key.hh"
21 #include "profile.hh"
22
23 bool
24 Context::is_removable () const
25 {
26   return context_list_ == SCM_EOL && ! iterator_count_
27     && !dynamic_cast<Score_context const *> (this);
28 }
29
30 void
31 Context::check_removal ()
32 {
33   for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
34     {
35       Context *trg = unsmob_context (scm_car (p));
36
37       trg->check_removal ();
38       if (trg->is_removable ())
39         {
40           recurse_over_translators (trg, &Translator::finalize,
41                                     &Translator_group::finalize,
42                                     UP);
43           remove_context (trg);
44         }
45     }
46 }
47
48 Context::Context (Context const &)
49 {
50   assert (false);
51 }
52
53 Scheme_hash_table *
54 Context::properties_dict () const
55 {
56   return Scheme_hash_table::unsmob (properties_scm_);
57 }
58
59 void
60 Context::add_context (Context *t)
61 {
62   SCM ts = t->self_scm ();
63   context_list_ = ly_append2 (context_list_,
64                               scm_cons (ts, SCM_EOL));
65
66   t->daddy_context_ = this;
67   if (!t->init_)
68     {
69       t->init_ = true;
70
71       t->unprotect ();
72       Context_def *td = unsmob_context_def (t->definition_);
73
74       /* This cannot move before add_context (), because \override
75          operations require that we are in the hierarchy.  */
76       td->apply_default_property_operations (t);
77
78       recurse_over_translators (t,
79                                 &Translator::initialize,
80                                 &Translator_group::initialize,
81                                 DOWN);
82     }
83 }
84
85
86 Context::Context (Object_key const *key)
87 {
88   key_ = key;
89   daddy_context_ = 0;
90   init_ = false;
91   aliases_ = SCM_EOL;
92   iterator_count_ = 0;
93   implementation_ = 0;
94   properties_scm_ = SCM_EOL;
95   accepts_list_ = SCM_EOL;
96   context_list_ = SCM_EOL;
97   definition_ = SCM_EOL;
98
99   smobify_self ();
100
101   Scheme_hash_table *tab = new Scheme_hash_table;
102   properties_scm_ = tab->unprotect ();
103
104   /*
105     UGH UGH
106     const correctness.
107   */
108   if (key_)
109     ((Object_key *)key)->unprotect ();
110 }
111
112 /* TODO:  this shares code with find_create_context ().  */
113 Context *
114 Context::create_unique_context (SCM n, SCM operations)
115 {
116   /*
117     Don't create multiple score contexts.
118   */
119   if (dynamic_cast<Global_context *> (this)
120       && dynamic_cast<Global_context *> (this)->get_score_context ())
121     return get_score_context ()->create_unique_context (n, operations);
122
123   /*
124     TODO: use accepts_list_.
125   */
126   Link_array<Context_def> path
127     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
128
129   if (path.size ())
130     {
131       Context *current = this;
132
133       // start at 1.  The first one (index 0) will be us.
134       for (int i = 0; i < path.size (); i++)
135         {
136           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
137
138           current = current->create_context (path[i],
139                                              "\\new",
140                                              ops);
141         }
142
143       return current;
144     }
145
146   /*
147     Don't go up to Global_context, because global goes down to
148     Score_context
149   */
150   Context *ret = 0;
151   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
152     ret = daddy_context_->create_unique_context (n, operations);
153   else
154     {
155       warning (_f ("can't find or create new `%s'",
156                    ly_symbol2string (n).c_str ()));
157       ret = 0;
158     }
159   return ret;
160 }
161
162 Context *
163 Context::find_create_context (SCM n, String id, SCM operations)
164 {
165   /*
166     Don't create multiple score contexts.
167   */
168   if (dynamic_cast<Global_context *> (this)
169       && dynamic_cast<Global_context *> (this)->get_score_context ())
170     return get_score_context ()->find_create_context (n, id, operations);
171
172   if (Context *existing = find_context_below (this, n, id))
173     return existing;
174
175   if (n == ly_symbol2scm ("Bottom"))
176     {
177       Context *tg = get_default_interpreter ();
178       return tg;
179     }
180
181   /*
182     TODO: use accepts_list_.
183   */
184   Link_array<Context_def> path
185     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
186
187   if (path.size ())
188     {
189       Context *current = this;
190
191       // start at 1.  The first one (index 0) will be us.
192       for (int i = 0; i < path.size (); i++)
193         {
194           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
195
196           String this_id = "";
197           if (i == path.size () -1)
198             this_id = id;
199
200           current = current->create_context (path[i],
201                                              this_id,
202                                              ops);
203         }
204
205       return current;
206     }
207
208   /*
209     Don't go up to Global_context, because global goes down to
210     Score_context
211   */
212   Context *ret = 0;
213   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
214     ret = daddy_context_->find_create_context (n, id, operations);
215   else
216     {
217       warning (_f ("can't find or create `%s' called `%s'",
218                    ly_symbol2string (n).c_str (), id));
219       ret = 0;
220     }
221   return ret;
222 }
223
224 Context *
225 Context::create_context (Context_def *cdef,
226                          String id,
227                          SCM ops)
228 {
229   String type = ly_symbol2string (cdef->get_context_name ());
230   Object_key const *key = get_context_key (type, id);
231   Context *new_context
232     = cdef->instantiate (ops, key);
233
234   new_context->id_string_ = id;
235   add_context (new_context);
236   apply_property_operations (new_context, ops);
237
238   return new_context;
239 }
240
241 Object_key const *
242 Context::get_context_key (String type, String id)
243 {
244   if (!use_object_keys)
245     return 0;
246
247   String now_key = type + "@" + id;
248
249   int disambiguation_count = 0;
250   if (context_counts_.find (now_key) != context_counts_.end ())
251     disambiguation_count = context_counts_[now_key];
252
253   context_counts_[now_key] = disambiguation_count + 1;
254
255   return new Lilypond_context_key (key (),
256                                    now_mom (),
257                                    type, id,
258                                    disambiguation_count);
259 }
260
261 Object_key const *
262 Context::get_grob_key (String name)
263 {
264   if (!use_object_keys)
265     return 0;
266
267   return create_grob_key (name);
268 }
269
270 /*
271   We want to have a key for some objects anyway, so we can invent a
272   unique identifier for each (book,score) tuple.
273 */
274 Object_key const *
275 Context::create_grob_key (String name)
276 {
277   int disambiguation_count = 0;
278   if (grob_counts_.find (name) != grob_counts_.end ())
279     disambiguation_count = grob_counts_[name];
280   grob_counts_[name] = disambiguation_count + 1;
281
282   Object_key *k = new Lilypond_grob_key (key (),
283                                          now_mom (),
284                                          name,
285                                          disambiguation_count);
286
287   return k;
288 }
289
290 /*
291   Default child context as a SCM string, or something else if there is
292   none.
293 */
294 SCM
295 Context::default_child_context_name () const
296 {
297   return scm_is_pair (accepts_list_)
298     ? scm_car (accepts_list_)
299     : SCM_EOL;
300 }
301
302 bool
303 Context::is_bottom_context () const
304 {
305   return !scm_is_symbol (default_child_context_name ());
306 }
307
308 Context *
309 Context::get_default_interpreter ()
310 {
311   if (!is_bottom_context ())
312     {
313       SCM nm = default_child_context_name ();
314       SCM st = find_context_def (get_output_def (), nm);
315
316       String name = ly_symbol2string (nm);
317       Context_def *t = unsmob_context_def (st);
318       if (!t)
319         {
320           warning (_f ("can't find or create: `%s'", name.c_str ()));
321           t = unsmob_context_def (this->definition_);
322         }
323
324       Context *tg = create_context (t, "", SCM_EOL);
325       if (!tg->is_bottom_context ())
326         return tg->get_default_interpreter ();
327       else
328         return tg;
329     }
330   return this;
331 }
332
333 /*
334   PROPERTIES
335 */
336 Context *
337 Context::where_defined (SCM sym, SCM *value) const
338 {
339 #ifndef NDEBUG
340   if (profile_property_accesses)
341     note_property_access (&context_property_lookup_table, sym);
342 #endif
343
344   if (properties_dict ()->try_retrieve (sym, value))
345     return (Context *)this;
346
347   return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
348 }
349
350 /*
351   return SCM_EOL when not found.
352 */
353 SCM
354 Context::internal_get_property (SCM sym) const
355 {
356 #ifndef NDEBUG
357   if (profile_property_accesses)
358     note_property_access (&context_property_lookup_table, sym);
359 #endif
360
361   SCM val = SCM_EOL;
362   if (properties_dict ()->try_retrieve (sym, &val))
363     return val;
364
365   if (daddy_context_)
366     return daddy_context_->internal_get_property (sym);
367
368   return val;
369 }
370
371 bool
372 Context::is_alias (SCM sym) const
373 {
374   if (sym == ly_symbol2scm ("Bottom")
375       && !scm_is_pair (accepts_list_))
376     return true;
377   if (sym == unsmob_context_def (definition_)->get_context_name ())
378     return true;
379
380   return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
381 }
382
383 void
384 Context::add_alias (SCM sym)
385 {
386   aliases_ = scm_cons (sym, aliases_);
387 }
388
389 void
390 Context::internal_set_property (SCM sym, SCM val)
391 {
392 #ifndef NDEBUG
393   if (do_internal_type_checking_global)
394     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
395 #endif
396
397   properties_dict ()->set (sym, val);
398 }
399
400 /*
401   TODO: look up to check whether we have inherited var?
402 */
403 void
404 Context::unset_property (SCM sym)
405 {
406   properties_dict ()->remove (sym);
407 }
408
409 /**
410    Remove a context from the hierarchy.
411 */
412 Context *
413 Context::remove_context (Context *trans)
414 {
415   assert (trans);
416
417   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
418   trans->daddy_context_ = 0;
419   return trans;
420 }
421
422 /*
423   ID == "" means accept any ID.
424 */
425 Context *
426 find_context_below (Context *where,
427                     SCM type, String id)
428 {
429   if (where->is_alias (type))
430     {
431       if (id == "" || where->id_string () == id)
432         return where;
433     }
434
435   Context *found = 0;
436   for (SCM s = where->children_contexts ();
437        !found && scm_is_pair (s); s = scm_cdr (s))
438     {
439       Context *tr = unsmob_context (scm_car (s));
440
441       found = find_context_below (tr, type, id);
442     }
443
444   return found;
445 }
446
447 SCM
448 Context::properties_as_alist () const
449 {
450   return properties_dict ()->to_alist ();
451 }
452
453 SCM
454 Context::context_name_symbol () const
455 {
456   Context_def *td = unsmob_context_def (definition_);
457   return td->get_context_name ();
458 }
459
460 String
461 Context::context_name () const
462 {
463   return ly_symbol2string (context_name_symbol ());
464 }
465
466 Score_context *
467 Context::get_score_context () const
468 {
469   if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
470     return sc;
471   else if (daddy_context_)
472     return daddy_context_->get_score_context ();
473   else
474     return 0;
475 }
476
477 Output_def *
478 Context::get_output_def () const
479 {
480   return daddy_context_ ? daddy_context_->get_output_def () : 0;
481 }
482
483 Context::~Context ()
484 {
485 }
486
487 Moment
488 Context::now_mom () const
489 {
490   Context const *p = this;
491   while (p->daddy_context_)
492     p = p->daddy_context_;
493
494   return p->now_mom ();
495 }
496
497 int
498 Context::print_smob (SCM s, SCM port, scm_print_state *)
499 {
500   Context *sc = (Context *) SCM_CELL_WORD_1 (s);
501
502   scm_puts ("#<", port);
503   scm_puts (sc->class_name (), port);
504   if (Context_def *d = unsmob_context_def (sc->definition_))
505     {
506       scm_puts (" ", port);
507       scm_display (d->get_context_name (), port);
508     }
509
510   if (!sc->id_string_.empty ())
511     {
512       scm_puts ("=", port);
513       scm_puts (sc->id_string_.c_str (), port);
514     }
515
516   scm_puts (" ", port);
517
518   scm_display (sc->context_list_, port);
519   scm_puts (" >", port);
520
521   return 1;
522 }
523
524 SCM
525 Context::mark_smob (SCM sm)
526 {
527   Context *me = (Context *) SCM_CELL_WORD_1 (sm);
528   if (me->key_)
529     scm_gc_mark (me->key_->self_scm ());
530
531   scm_gc_mark (me->context_list_);
532   scm_gc_mark (me->aliases_);
533   scm_gc_mark (me->definition_);
534   scm_gc_mark (me->properties_scm_);
535   scm_gc_mark (me->accepts_list_);
536   if (me->implementation_)
537     scm_gc_mark (me->implementation_->self_scm ());
538
539   return me->properties_scm_;
540 }
541
542 IMPLEMENT_SMOBS (Context);
543 IMPLEMENT_DEFAULT_EQUAL_P (Context);
544 IMPLEMENT_TYPE_P (Context, "ly:context?");
545
546 bool
547 Context::try_music (Music *m)
548 {
549   Translator_group *t = implementation ();
550   if (!t)
551     return false;
552
553   bool b = t->try_music (m);
554   if (!b && daddy_context_)
555     b = daddy_context_->try_music (m);
556
557   return b;
558 }
559
560 Global_context *
561 Context::get_global_context () const
562 {
563   if (dynamic_cast<Global_context *> ((Context *) this))
564     return dynamic_cast<Global_context *> ((Context *) this);
565
566   if (daddy_context_)
567     return daddy_context_->get_global_context ();
568
569   programming_error ("no Global context");
570   return 0;
571 }
572
573 Context *
574 Context::get_parent_context () const
575 {
576   return daddy_context_;
577 }
578
579 void
580 Context::clear_key_disambiguations ()
581 {
582   if (!use_object_keys)
583     return;
584
585   grob_counts_.clear ();
586   context_counts_.clear ();
587   for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
588     unsmob_context (scm_car (s))->clear_key_disambiguations ();
589 }
590
591 /*
592   Ugh. Where to put this?
593 */
594 Rational
595 measure_length (Context const *context)
596 {
597   SCM l = context->get_property ("measureLength");
598   Rational length (1);
599   if (unsmob_moment (l))
600     length = unsmob_moment (l)->main_part_;
601   return length;
602 }
603
604 Moment
605 measure_position (Context const *context)
606 {
607   SCM sm = context->get_property ("measurePosition");
608
609   Moment m = 0;
610   if (unsmob_moment (sm))
611     {
612       m = *unsmob_moment (sm);
613
614       if (m.main_part_ < Rational (0))
615         {
616           Rational length (measure_length (context));
617           while (m.main_part_ < Rational (0))
618             m.main_part_ += length;
619         }
620     }
621
622   return m;
623 }
624
625
626 void
627 set_context_property_on_children (Context *trans, SCM sym, SCM val)
628 {
629   trans->internal_set_property (sym, ly_deep_copy (val));
630   for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
631     {
632       Context *trg = unsmob_context (scm_car (p));
633       set_context_property_on_children (trg, sym, ly_deep_copy (val));
634     }
635 }