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