]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
(find_create_context): go to Score if it exists.
[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 "translator-group.hh"
11 #include "context-def.hh"
12 #include "context.hh"
13 #include "warn.hh"
14 #include "music-output-def.hh"
15 #include "scm-hash.hh"
16 #include "main.hh"
17 #include "ly-smobs.icc"
18 #include "score-context.hh"
19
20 bool
21 Context::is_removable () const
22 {
23   return context_list_ == SCM_EOL && ! iterator_count_ &&
24           !dynamic_cast<Score_context const*> (this)
25     ;
26 }
27
28
29 void
30 Context::check_removal ()
31 {
32   for (SCM p = context_list_; gh_pair_p (p); p = gh_cdr (p))
33     {
34       Context *trg =  unsmob_context (ly_car (p));
35
36       trg->check_removal ();
37       if (trg->is_removable ())
38         {
39           recurse_down_translators (trg, &Translator::finalize, false);
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_ = gh_append2 (context_list_,
61                               gh_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_down_translators (t, &Translator::initialize, true);
78     }
79 }
80
81 Context::Context ()
82 {
83   daddy_context_ = 0;
84   init_ = false;
85   iterator_count_  = 0;
86   implementation_ = SCM_EOL;
87   properties_scm_ = SCM_EOL;
88   accepts_list_ = SCM_EOL;
89   context_list_ = SCM_EOL;
90   definition_ = SCM_EOL;
91   
92   smobify_self ();
93
94   Scheme_hash_table *tab = new Scheme_hash_table ;
95   properties_scm_ = tab->self_scm ();
96   scm_gc_unprotect_object (tab->self_scm ());
97 }
98
99 Context *
100 Context::find_existing_context (SCM n, String id)
101 {
102   if ((is_alias (n) && (id_string_ == id || id.is_empty ())) || n == ly_symbol2scm ("Current"))
103     return this;
104
105   Context* r = 0;
106   for (SCM p = context_list_; !r && gh_pair_p (p); p = ly_cdr (p))
107     {
108       Context *  t = unsmob_context (ly_car (p));
109       
110       r = dynamic_cast<Context*> (t)->find_existing_context (n, id);
111     }
112
113   return r;
114 }
115
116
117 Context*
118 Context::find_create_context (SCM n, String id,
119                               SCM operations)
120 {
121   /*
122     Don't create multiple score contexts.
123    */
124   if (dynamic_cast<Global_context*> (this)
125       && dynamic_cast<Global_context*> (this)->get_score_context ())
126     return get_score_context ()->find_create_context (n, id, operations);
127     
128   
129   Context * existing = find_existing_context (n,id);
130   if (existing)
131     return existing;
132
133   if (n == ly_symbol2scm ("Bottom"))
134     {
135       Context* tg = get_default_interpreter ();
136       tg->id_string_ = id;
137       return tg;
138     }
139
140   /*
141     TODO: use accepts_list_.
142    */
143   Link_array<Context_def> path
144     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
145
146   if (path.size ())
147     {
148       Context * current = this;
149
150       // start at 1.  The first one (index 0) will be us.
151       for (int i=0; i < path.size (); i++)
152         {
153           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
154
155           Context * new_group
156             = path[i]->instantiate (ops);
157
158           if (i == path.size () -1)
159             {
160               new_group->id_string_ = id;
161             }
162
163           current->add_context (new_group);
164           apply_property_operations (new_group, ops);
165           
166           current = new_group;
167         }
168
169       return current;
170     }
171
172   Context *ret = 0;
173   if (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 /*
185   Default child context as a SCM string, or something else if there is
186   none.
187 */
188 SCM
189 default_child_context_name (Context const *tg)
190 {
191   return gh_pair_p (tg->accepts_list_)
192     ? ly_car (scm_last_pair (tg->accepts_list_))
193     : SCM_EOL;
194 }
195
196
197 bool
198 Context::is_bottom_context () const
199 {
200   return !gh_symbol_p (default_child_context_name (this));
201 }
202
203 Context*
204 Context::get_default_interpreter ()
205 {
206   if (!is_bottom_context ())
207     {
208       SCM nm = default_child_context_name (this);
209       SCM st = get_output_def ()->find_context_def (nm);
210
211       Context_def *t = unsmob_context_def (st);
212       if (!t)
213         {
214           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
215           t = unsmob_context_def (this->definition_);
216         }
217       Context *tg = t->instantiate (SCM_EOL);
218       add_context (tg);
219       if (!tg->is_bottom_context ())
220         return tg->get_default_interpreter ();
221       else
222         return tg;
223     }
224   return this;
225 }
226
227 /*
228   PROPERTIES
229  */
230 Context*
231 Context::where_defined (SCM sym) const
232 {
233   if (properties_dict ()->contains (sym))
234     {
235       return (Context*)this;
236     }
237
238   return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
239 }
240
241 /*
242   return SCM_EOL when not found.
243 */
244 SCM
245 Context::internal_get_property (SCM sym) const
246 {
247   SCM val =SCM_EOL;
248   if (properties_dict ()->try_retrieve (sym, &val))
249     return val;
250
251   if (daddy_context_)
252     return daddy_context_->internal_get_property (sym);
253   
254   return val;
255 }
256
257 bool
258 Context::is_alias (SCM sym) const
259 {
260   if (sym == ly_symbol2scm ("Bottom")
261       && !gh_pair_p (accepts_list_))
262     return true;
263   return unsmob_context_def (definition_)->is_alias (sym);
264 }
265
266 void
267 Context::internal_set_property (SCM sym, SCM val)
268 {
269 #ifndef NDEBUG
270   if (internal_type_checking_global_b)
271     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
272 #endif
273   
274   properties_dict ()->set (sym, val);
275 }
276
277 /*
278   TODO: look up to check whether we have inherited var? 
279  */
280 void
281 Context::unset_property (SCM sym)
282 {
283   properties_dict ()->remove (sym);
284 }
285
286 /**
287    Remove a context from the hierarchy.
288  */
289 Context *
290 Context::remove_context (Context*trans)
291 {
292   assert (trans);
293
294   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
295   trans->daddy_context_ = 0;
296   return trans;
297 }
298
299 /*
300   ID == "" means accept any ID.
301  */
302 Context *
303 find_context_below (Context * where,
304                     String type, String id)
305 {
306   if (where->is_alias (ly_symbol2scm (type.to_str0 ())))
307     {
308       if (id == "" || where->id_string_ == id)
309         return where;
310     }
311   
312   Context * found = 0;
313   for (SCM s = where->context_list_;
314        !found && gh_pair_p (s); s = gh_cdr (s))
315     {
316       Context * tr = unsmob_context (gh_car (s));
317
318       found = find_context_below (tr, type, id);
319     }
320
321   return found; 
322 }
323
324 SCM
325 Context::properties_as_alist () const
326 {
327   return properties_dict()->to_alist();
328 }
329
330 String
331 Context::context_name () const
332 {
333   Context_def * td = unsmob_context_def (definition_ );
334   return ly_symbol2string (td->get_context_name ());
335 }
336
337
338 Score_context*
339 Context::get_score_context () const
340 {
341   if (Score_context *sc =dynamic_cast<Score_context*> ((Context*)this))
342     return sc;
343   else if (daddy_context_)
344     return daddy_context_->get_score_context ();
345   else
346     return 0;
347 }
348
349 Music_output_def *
350 Context::get_output_def () const
351 {
352   return  (daddy_context_)
353     ? daddy_context_->get_output_def () : 0;
354 }
355
356 Context::~Context()
357 {
358   
359 }
360
361 Moment
362 Context::now_mom () const
363 {
364   return daddy_context_->now_mom();
365 }
366
367 int
368 Context::print_smob (SCM s, SCM port, scm_print_state *)
369 {
370   Context *sc = (Context *) ly_cdr (s);
371      
372   scm_puts ("#<", port);
373   scm_puts (classname (sc), port);
374   if (Context_def *d=unsmob_context_def (sc->definition_))
375     {
376       scm_puts (" ", port);
377       scm_display (d->get_context_name (), port);
378     }
379
380   if (Context *td=dynamic_cast<Context *> (sc))
381     {
382       scm_puts ("=", port);
383       scm_puts (td->id_string_.to_str0 (), port);
384     }
385   
386
387   scm_puts (" ", port);
388
389   scm_display (sc->context_list_, port);
390   scm_puts (" >", port);
391   
392   return 1;
393 }
394
395 SCM
396 Context::mark_smob (SCM sm)
397 {
398   Context * me = (Context*) SCM_CELL_WORD_1 (sm);
399   
400   scm_gc_mark (me->context_list_);
401   scm_gc_mark (me->definition_);  
402   scm_gc_mark (me->properties_scm_);  
403   scm_gc_mark (me->accepts_list_);
404   scm_gc_mark (me->implementation_);
405
406   return me->properties_scm_;
407 }
408
409 IMPLEMENT_SMOBS (Context);
410 IMPLEMENT_DEFAULT_EQUAL_P (Context);
411 IMPLEMENT_TYPE_P(Context,"ly:context?");
412
413 bool
414 Context::try_music (Music* m)
415 {
416   Translator*  t = unsmob_translator (implementation_);
417   if (!t)
418     return false;
419   
420   bool b = t->try_music (m);
421   if (!b && daddy_context_)
422     b = daddy_context_->try_music (m);
423
424   return b;
425 }
426
427
428 Global_context*
429 Context::get_global_context () const
430 {
431   if (dynamic_cast<Global_context *>((Context*) this))
432     return dynamic_cast<Global_context *> ((Context*) this);
433
434   if (daddy_context_)
435     return daddy_context_->get_global_context ();
436
437   programming_error ("No Global context!");
438   return 0;
439 }