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