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