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