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