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