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