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