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