]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[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 void
29 Context::check_removal ()
30 {
31   for (SCM p = context_list_; ly_pair_p (p); p = ly_cdr (p))
32     {
33       Context *trg =  unsmob_context (ly_car (p));
34
35       trg->check_removal ();
36       if (trg->is_removable ())
37         {
38           recurse_over_translators (trg, &Translator::finalize, UP);
39           remove_context (trg);
40         }
41     }
42 }
43
44 Context::Context (Context const&)
45 {
46   assert (false);
47 }
48
49 Scheme_hash_table*
50 Context::properties_dict () const
51 {
52   return Scheme_hash_table::unsmob (properties_scm_);
53 }
54
55 void
56 Context::add_context (Context*t)
57 {
58   SCM ts = t->self_scm ();
59   context_list_ = ly_append2 (context_list_,
60                               scm_cons (ts, SCM_EOL));
61   
62   t->daddy_context_ = this;
63   if (!t->init_)
64     {
65       t->init_ = true;
66       
67       scm_gc_unprotect_object (ts);
68       Context_def * td = unsmob_context_def (t->definition_);
69
70       /*
71         this can not move before add_context (), because \override
72         operations require that we are in the hierarchy.
73       */
74       td->apply_default_property_operations (t);
75
76       recurse_over_translators (t, &Translator::initialize, DOWN);
77     }
78 }
79
80 Context::Context ()
81 {
82   daddy_context_ = 0;
83   init_ = false;
84   aliases_ = SCM_EOL;
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   properties_scm_ = (new Scheme_hash_table)->self_scm ();
95   scm_gc_unprotect_object (properties_scm_);
96 }
97
98 Context *
99 Context::find_context_below (SCM n, String id)
100 {
101   if ((is_alias (n) && (id_string_ == id || id.is_empty ())))
102     return this;
103
104   Context* r = 0;
105   for (SCM p = context_list_; !r && ly_pair_p (p); p = ly_cdr (p))
106     {
107       Context *  t = unsmob_context (ly_car (p));
108       
109       r = dynamic_cast<Context*> (t)->find_context_below (n, id);
110     }
111
112   return r;
113 }
114
115
116 Context*
117 Context::find_create_context (SCM n, String id,
118                               SCM operations)
119 {
120   /*
121     Don't create multiple score contexts.
122    */
123   if (dynamic_cast<Global_context*> (this)
124       && dynamic_cast<Global_context*> (this)->get_score_context ())
125     return get_score_context ()->find_create_context (n, id, operations);
126     
127   
128   Context * existing = find_context_below (n,id);
129   if (existing)
130     return existing;
131
132   if (n == ly_symbol2scm ("Bottom"))
133     {
134       Context* tg = get_default_interpreter ();
135       tg->id_string_ = id;
136       return tg;
137     }
138
139   /*
140     TODO: use accepts_list_.
141    */
142   Link_array<Context_def> path
143     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
144
145   if (path.size ())
146     {
147       Context * current = this;
148
149       // start at 1.  The first one (index 0) will be us.
150       for (int i=0; i < path.size (); i++)
151         {
152           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
153
154           Context * new_group
155             = path[i]->instantiate (ops);
156
157           if (i == path.size () -1)
158             {
159               new_group->id_string_ = id;
160             }
161
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 default_child_context_name (Context const *tg)
193 {
194   return ly_pair_p (tg->accepts_list_)
195     ? ly_car (scm_last_pair (tg->accepts_list_))
196     : SCM_EOL;
197 }
198
199
200 bool
201 Context::is_bottom_context () const
202 {
203   return !ly_symbol_p (default_child_context_name (this));
204 }
205
206 Context*
207 Context::get_default_interpreter ()
208 {
209   if (!is_bottom_context ())
210     {
211       SCM nm = default_child_context_name (this);
212       SCM st = get_output_def ()->find_context_def (nm);
213
214       Context_def *t = unsmob_context_def (st);
215       if (!t)
216         {
217           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
218           t = unsmob_context_def (this->definition_);
219         }
220       Context *tg = t->instantiate (SCM_EOL);
221       add_context (tg);
222       if (!tg->is_bottom_context ())
223         return tg->get_default_interpreter ();
224       else
225         return tg;
226     }
227   return this;
228 }
229
230 /*
231   PROPERTIES
232  */
233 Context*
234 Context::where_defined (SCM sym) const
235 {
236   if (properties_dict ()->contains (sym))
237     {
238       return (Context*)this;
239     }
240
241   return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
242 }
243
244 /*
245   return SCM_EOL when not found.
246 */
247 SCM
248 Context::internal_get_property (SCM sym) const
249 {
250   SCM val =SCM_EOL;
251   if (properties_dict ()->try_retrieve (sym, &val))
252     return val;
253
254   if (daddy_context_)
255     return daddy_context_->internal_get_property (sym);
256   
257   return val;
258 }
259
260 bool
261 Context::is_alias (SCM sym) const
262 {
263   if (sym == ly_symbol2scm ("Bottom")
264       && !ly_pair_p (accepts_list_))
265     return true;
266   if (sym == unsmob_context_def (definition_)->get_context_name ())
267     return true;
268   
269   return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
270 }
271
272 void
273 Context::add_alias (SCM sym)
274 {
275   aliases_ = scm_cons (sym, aliases_);
276 }
277
278
279
280 void
281 Context::internal_set_property (SCM sym, SCM val)
282 {
283 #ifndef NDEBUG
284   if (internal_type_checking_global_b)
285     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
286 #endif
287   
288   properties_dict ()->set (sym, val);
289 }
290
291 /*
292   TODO: look up to check whether we have inherited var? 
293  */
294 void
295 Context::unset_property (SCM sym)
296 {
297   properties_dict ()->remove (sym);
298 }
299
300 /**
301    Remove a context from the hierarchy.
302  */
303 Context *
304 Context::remove_context (Context*trans)
305 {
306   assert (trans);
307
308   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
309   trans->daddy_context_ = 0;
310   return trans;
311 }
312
313 /*
314   ID == "" means accept any ID.
315  */
316 Context *
317 find_context_below (Context * where,
318                     String type, String id)
319 {
320   if (where->is_alias (ly_symbol2scm (type.to_str0 ())))
321     {
322       if (id == "" || where->id_string_ == id)
323         return where;
324     }
325   
326   Context * found = 0;
327   for (SCM s = where->context_list_;
328        !found && ly_pair_p (s); s = ly_cdr (s))
329     {
330       Context * tr = unsmob_context (ly_car (s));
331
332       found = find_context_below (tr, type, id);
333     }
334
335   return found; 
336 }
337
338 SCM
339 Context::properties_as_alist () const
340 {
341   return properties_dict ()->to_alist ();
342 }
343
344 String
345 Context::context_name () const
346 {
347   Context_def * td = unsmob_context_def (definition_ );
348   return ly_symbol2string (td->get_context_name ());
349 }
350
351
352 Score_context*
353 Context::get_score_context () const
354 {
355   if (Score_context *sc =dynamic_cast<Score_context*> ((Context*)this))
356     return sc;
357   else if (daddy_context_)
358     return daddy_context_->get_score_context ();
359   else
360     return 0;
361 }
362
363 Music_output_def *
364 Context::get_output_def () const
365 {
366   return  (daddy_context_)
367     ? daddy_context_->get_output_def () : 0;
368 }
369
370 Context::~Context ()
371 {
372   
373 }
374
375 Moment
376 Context::now_mom () const
377 {
378   return daddy_context_->now_mom ();
379 }
380
381 int
382 Context::print_smob (SCM s, SCM port, scm_print_state *)
383 {
384   Context *sc = (Context *) ly_cdr (s);
385      
386   scm_puts ("#<", port);
387   scm_puts (classname (sc), port);
388   if (Context_def *d=unsmob_context_def (sc->definition_))
389     {
390       scm_puts (" ", port);
391       scm_display (d->get_context_name (), port);
392     }
393
394   if (Context *td=dynamic_cast<Context *> (sc))
395     {
396       scm_puts ("=", port);
397       scm_puts (td->id_string_.to_str0 (), port);
398     }
399   
400
401   scm_puts (" ", port);
402
403   scm_display (sc->context_list_, port);
404   scm_puts (" >", port);
405   
406   return 1;
407 }
408
409 SCM
410 Context::mark_smob (SCM sm)
411 {
412   Context * me = (Context*) SCM_CELL_WORD_1 (sm);
413   
414   scm_gc_mark (me->context_list_);
415   scm_gc_mark (me->aliases_);
416   scm_gc_mark (me->definition_);  
417   scm_gc_mark (me->properties_scm_);  
418   scm_gc_mark (me->accepts_list_);
419   scm_gc_mark (me->implementation_);
420
421   return me->properties_scm_;
422 }
423
424 IMPLEMENT_SMOBS (Context);
425 IMPLEMENT_DEFAULT_EQUAL_P (Context);
426 IMPLEMENT_TYPE_P (Context,"ly:context?");
427
428 bool
429 Context::try_music (Music* m)
430 {
431   Translator*  t = unsmob_translator (implementation_);
432   if (!t)
433     return false;
434   
435   bool b = t->try_music (m);
436   if (!b && daddy_context_)
437     b = daddy_context_->try_music (m);
438
439   return b;
440 }
441
442
443 Global_context*
444 Context::get_global_context () const
445 {
446   if (dynamic_cast<Global_context *>((Context*) this))
447     return dynamic_cast<Global_context *> ((Context*) this);
448
449   if (daddy_context_)
450     return daddy_context_->get_global_context ();
451
452   programming_error ("No Global context!");
453   return 0;
454 }