]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* lily/include/grob-info.hh: origin_contexts() now does not
[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   Context * existing = find_existing_context (n,id);
122   if (existing)
123     return existing;
124
125   if (n == ly_symbol2scm ("Bottom"))
126     {
127       Context* tg = get_default_interpreter ();
128       tg->id_string_ = id;
129       return tg;
130     }
131
132   /*
133     TODO: use accepts_list_.
134    */
135   Link_array<Context_def> path
136     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
137
138   if (path.size ())
139     {
140       Context * current = this;
141
142       // start at 1.  The first one (index 0) will be us.
143       for (int i=0; i < path.size (); i++)
144         {
145           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
146
147           Context * new_group
148             = path[i]->instantiate (ops);
149
150           if (i == path.size () -1)
151             {
152               new_group->id_string_ = id;
153             }
154
155           current->add_context (new_group);
156           apply_property_operations (new_group, ops);
157           
158           current = new_group;
159         }
160
161       return current;
162     }
163
164   Context *ret = 0;
165   if (daddy_context_)
166     ret = daddy_context_->find_create_context (n, id, operations);
167   else
168     {
169       warning (_f ("Cannot find or create `%s' called `%s'",
170                    ly_symbol2string (n).to_str0 (), id));
171       ret =0;
172     }
173   return ret;
174 }
175
176 /*
177   Default child context as a SCM string, or something else if there is
178   none.
179 */
180 SCM
181 default_child_context_name (Context const *tg)
182 {
183   return gh_pair_p (tg->accepts_list_)
184     ? ly_car (scm_last_pair (tg->accepts_list_))
185     : SCM_EOL;
186 }
187
188
189 bool
190 Context::is_bottom_context () const
191 {
192   return !gh_symbol_p (default_child_context_name (this));
193 }
194
195 Context*
196 Context::get_default_interpreter ()
197 {
198   if (!is_bottom_context ())
199     {
200       SCM nm = default_child_context_name (this);
201       SCM st = get_output_def ()->find_context_def (nm);
202
203       Context_def *t = unsmob_context_def (st);
204       if (!t)
205         {
206           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
207           t = unsmob_context_def (this->definition_);
208         }
209       Context *tg = t->instantiate (SCM_EOL);
210       add_context (tg);
211       if (!tg->is_bottom_context ())
212         return tg->get_default_interpreter ();
213       else
214         return tg;
215     }
216   return this;
217 }
218
219 /*
220   PROPERTIES
221  */
222 Context*
223 Context::where_defined (SCM sym) const
224 {
225   if (properties_dict ()->contains (sym))
226     {
227       return (Context*)this;
228     }
229
230   return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
231 }
232
233 /*
234   return SCM_EOL when not found.
235 */
236 SCM
237 Context::internal_get_property (SCM sym) const
238 {
239   SCM val =SCM_EOL;
240   if (properties_dict ()->try_retrieve (sym, &val))
241     return val;
242
243   if (daddy_context_)
244     return daddy_context_->internal_get_property (sym);
245   
246   return val;
247 }
248
249 bool
250 Context::is_alias (SCM sym) const
251 {
252   if (sym == ly_symbol2scm ("Bottom")
253       && !gh_pair_p (accepts_list_))
254     return true;
255   return unsmob_context_def (definition_)->is_alias (sym);
256 }
257
258 void
259 Context::internal_set_property (SCM sym, SCM val)
260 {
261 #ifndef NDEBUG
262   if (internal_type_checking_global_b)
263     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
264 #endif
265   
266   properties_dict ()->set (sym, val);
267 }
268
269 /*
270   TODO: look up to check whether we have inherited var? 
271  */
272 void
273 Context::unset_property (SCM sym)
274 {
275   properties_dict ()->remove (sym);
276 }
277
278 /**
279    Remove a context from the hierarchy.
280  */
281 Context *
282 Context::remove_context (Context*trans)
283 {
284   assert (trans);
285
286   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
287   trans->daddy_context_ = 0;
288   return trans;
289 }
290
291 /*
292   ID == "" means accept any ID.
293  */
294 Context *
295 find_context_below (Context * where,
296                     String type, String id)
297 {
298   if (where->is_alias (ly_symbol2scm (type.to_str0 ())))
299     {
300       if (id == "" || where->id_string_ == id)
301         return where;
302     }
303   
304   Context * found = 0;
305   for (SCM s = where->context_list_;
306        !found && gh_pair_p (s); s = gh_cdr (s))
307     {
308       Context * tr = unsmob_context (gh_car (s));
309
310       found = find_context_below (tr, type, id);
311     }
312
313   return found; 
314 }
315
316 SCM
317 Context::properties_as_alist () const
318 {
319   return properties_dict()->to_alist();
320 }
321
322 String
323 Context::context_name () const
324 {
325   Context_def * td = unsmob_context_def (definition_ );
326   return ly_symbol2string (td->get_context_name ());
327 }
328
329
330 Score_context*
331 Context::get_score_context () const
332 {
333   if (Score_context *sc =dynamic_cast<Score_context*> ((Context*)this))
334     return sc;
335   else if (daddy_context_)
336     return daddy_context_->get_score_context ();
337   else
338     return 0;
339 }
340
341 Music_output_def *
342 Context::get_output_def () const
343 {
344   return  (daddy_context_)
345     ? daddy_context_->get_output_def () : 0;
346 }
347
348 Context::~Context()
349 {
350   
351 }
352
353 Moment
354 Context::now_mom () const
355 {
356   return daddy_context_->now_mom();
357 }
358
359 int
360 Context::print_smob (SCM s, SCM port, scm_print_state *)
361 {
362   Context *sc = (Context *) ly_cdr (s);
363      
364   scm_puts ("#<", port);
365   scm_puts (classname (sc), port);
366   if (Context_def *d=unsmob_context_def (sc->definition_))
367     {
368       scm_puts (" ", port);
369       scm_display (d->get_context_name (), port);
370     }
371
372   if (Context *td=dynamic_cast<Context *> (sc))
373     {
374       scm_puts ("=", port);
375       scm_puts (td->id_string_.to_str0 (), port);
376     }
377   
378
379   scm_puts (" ", port);
380   scm_display (sc->implementation_, port);
381   
382 #if 0
383   Translator * tr = unsmob_translator (sc->implementation_);
384   if  (Translator_group *tg = dynamic_cast<Translator_group*> (tr))
385     scm_display (tg->get_simple_trans_list (), port);
386 #endif
387   
388   /*
389     don't try to print properties, that is too much hassle.
390    */
391   scm_puts (" >", port);
392   
393   return 1;
394 }
395
396 SCM
397 Context::mark_smob (SCM sm)
398 {
399   Context * me = (Context*) SCM_CELL_WORD_1 (sm);
400   
401   scm_gc_mark (me->context_list_);
402   scm_gc_mark (me->definition_);  
403   scm_gc_mark (me->properties_scm_);  
404   scm_gc_mark (me->accepts_list_);
405   scm_gc_mark (me->implementation_);
406
407   return me->properties_scm_;
408 }
409
410 IMPLEMENT_SMOBS (Context);
411 IMPLEMENT_DEFAULT_EQUAL_P (Context);
412 IMPLEMENT_TYPE_P(Context,"ly:context?");
413
414 bool
415 Context::try_music (Music* m)
416 {
417   Translator*  t = unsmob_translator (implementation_);
418   if (!t)
419     return false;
420   
421   bool b = t->try_music (m);
422   if (!b && daddy_context_)
423     b = daddy_context_->try_music (m);
424
425   return b;
426 }
427
428
429 Global_context*
430 Context::get_global_context () const
431 {
432   if (dynamic_cast<Global_context *>((Context*) this))
433     return dynamic_cast<Global_context *> ((Context*) this);
434
435   if (daddy_context_)
436     return daddy_context_->get_global_context ();
437
438   programming_error ("No Global context!");
439   return 0;
440 }