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