]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* flower
[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--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10
11 #include "context-def.hh"
12 #include "ly-smobs.icc"
13 #include "main.hh"
14 #include "output-def.hh"
15 #include "scm-hash.hh"
16 #include "score-context.hh"
17 #include "translator-group.hh"
18 #include "warn.hh"
19 #include "lilypond-key.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
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 Object_key const *
81 Context::get_key () const
82 {
83   return key_;
84 }
85
86 Context::Context (Object_key const *key)
87 {
88   key_ = key;
89   daddy_context_ = 0;
90   init_ = false;
91   aliases_ = SCM_EOL;
92   iterator_count_ = 0;
93   implementation_ = SCM_EOL;
94   properties_scm_ = SCM_EOL;
95   accepts_list_ = SCM_EOL;
96   context_list_ = SCM_EOL;
97   definition_ = SCM_EOL;
98
99   smobify_self ();
100   properties_scm_ = (new Scheme_hash_table)->self_scm ();
101   scm_gc_unprotect_object (properties_scm_);
102   scm_gc_unprotect_object (key_->self_scm ());
103 }
104
105 /*
106   TODO:  this shares code with find_create_context().
107 */
108 Context *
109 Context::create_unique_context (SCM n, SCM operations)
110 {
111   /*
112     Don't create multiple score contexts.
113   */
114   if (dynamic_cast<Global_context *> (this)
115       && dynamic_cast<Global_context *> (this)->get_score_context ())
116     return get_score_context ()->create_unique_context (n, operations);
117
118   /*
119     TODO: use accepts_list_.
120   */
121   Link_array<Context_def> path
122     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
123
124   if (path.size ())
125     {
126       Context *current = this;
127
128       // start at 1.  The first one (index 0) will be us.
129       for (int i = 0; i < path.size (); i++)
130         {
131           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
132
133           current = current->create_context (path[i],
134                                              "\\new",
135                                              ops);
136         }
137
138       return current;
139     }
140
141   /*
142     Don't go up to Global_context, because global goes down to
143     Score_context
144   */
145   Context *ret = 0;
146   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
147     ret = daddy_context_->create_unique_context (n, operations);
148   else
149     {
150       warning (_f ("Cannot find or create new `%s'",
151                    ly_symbol2string (n).to_str0 ()));
152       ret = 0;
153     }
154   return ret;
155 }
156
157 Context *
158 Context::find_create_context (SCM n, String id, SCM operations)
159 {
160   /*
161     Don't create multiple score contexts.
162   */
163   if (dynamic_cast<Global_context *> (this)
164       && dynamic_cast<Global_context *> (this)->get_score_context ())
165     return get_score_context ()->find_create_context (n, id, operations);
166
167   if (Context *existing = find_context_below (this, n, id))
168     return existing;
169
170   if (n == ly_symbol2scm ("Bottom"))
171     {
172       Context *tg = get_default_interpreter ();
173       return tg;
174     }
175
176   /*
177     TODO: use accepts_list_.
178   */
179   Link_array<Context_def> path
180     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
181
182   if (path.size ())
183     {
184       Context *current = this;
185
186       // start at 1.  The first one (index 0) will be us.
187       for (int i = 0; i < path.size (); i++)
188         {
189           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
190
191           String this_id = "";
192           if (i == path.size () -1)
193             {
194               this_id = id;
195             }
196
197           current = current->create_context (path[i],
198                                              this_id,
199                                              ops);
200         }
201
202       return current;
203     }
204
205   /*
206     Don't go up to Global_context, because global goes down to
207     Score_context
208   */
209   Context *ret = 0;
210   if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
211     ret = daddy_context_->find_create_context (n, id, operations);
212   else
213     {
214       warning (_f ("Cannot find or create `%s' called `%s'",
215                    ly_symbol2string (n).to_str0 (), id));
216       ret = 0;
217     }
218   return ret;
219 }
220
221 Context *
222 Context::create_context (Context_def *cdef,
223                          String id,
224                          SCM ops)
225 {
226   String type = ly_symbol2string (cdef->get_context_name ());
227   Object_key const *key = get_context_key (type, id);
228   Context *new_group
229     = cdef->instantiate (ops, key);
230
231   new_group->id_string_ = id;
232   add_context (new_group);
233   apply_property_operations (new_group, ops);
234
235   return new_group;
236 }
237
238 Object_key const *
239 Context::get_context_key (String type, String id)
240 {
241   String now_key = type + "@" + id;
242
243   int disambiguation_count = 0;
244   if (context_counts_.find (now_key) != context_counts_.end ())
245     {
246       disambiguation_count = context_counts_[now_key];
247     }
248
249   context_counts_[now_key] = disambiguation_count + 1;
250
251   return new Lilypond_context_key (get_key (),
252                                    now_mom (),
253                                    type, id,
254                                    disambiguation_count);
255 }
256
257 Object_key const *
258 Context::get_grob_key (String name)
259 {
260   int disambiguation_count = 0;
261   if (grob_counts_.find (name) != grob_counts_.end ())
262     {
263       disambiguation_count = grob_counts_[name];
264     }
265   grob_counts_[name] = disambiguation_count + 1;
266
267   Object_key *k = new Lilypond_grob_key (get_key (),
268                                          now_mom (),
269                                          name,
270                                          disambiguation_count);
271
272   return k;
273 }
274
275 /*
276   Default child context as a SCM string, or something else if there is
277   none.
278 */
279 SCM
280 Context::default_child_context_name () const
281 {
282   return scm_is_pair (accepts_list_)
283     ? scm_car (scm_last_pair (accepts_list_))
284     : SCM_EOL;
285 }
286
287 bool
288 Context::is_bottom_context () const
289 {
290   return !scm_is_symbol (default_child_context_name ());
291 }
292
293 Context *
294 Context::get_default_interpreter ()
295 {
296   if (!is_bottom_context ())
297     {
298       SCM nm = default_child_context_name ();
299       SCM st = find_context_def (get_output_def (), nm);
300
301       String name = ly_symbol2string (nm);
302       Context_def *t = unsmob_context_def (st);
303       if (!t)
304         {
305           warning (_f ("can't find or create: `%s'", name.to_str0 ()));
306           t = unsmob_context_def (this->definition_);
307         }
308
309       Context *tg = create_context (t, "", SCM_EOL);
310       if (!tg->is_bottom_context ())
311         return tg->get_default_interpreter ();
312       else
313         return tg;
314     }
315   return this;
316 }
317
318 /*
319   PROPERTIES
320 */
321 Context *
322 Context::where_defined (SCM sym) const
323 {
324   if (properties_dict ()->contains (sym))
325     {
326       return (Context *)this;
327     }
328
329   return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
330 }
331
332 /*
333   return SCM_EOL when not found.
334 */
335 SCM
336 Context::internal_get_property (SCM sym) const
337 {
338   SCM val = SCM_EOL;
339   if (properties_dict ()->try_retrieve (sym, &val))
340     return val;
341
342   if (daddy_context_)
343     return daddy_context_->internal_get_property (sym);
344
345   return val;
346 }
347
348 bool
349 Context::is_alias (SCM sym) const
350 {
351   if (sym == ly_symbol2scm ("Bottom")
352       && !scm_is_pair (accepts_list_))
353     return true;
354   if (sym == unsmob_context_def (definition_)->get_context_name ())
355     return true;
356
357   return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
358 }
359
360 void
361 Context::add_alias (SCM sym)
362 {
363   aliases_ = scm_cons (sym, aliases_);
364 }
365
366 void
367 Context::internal_set_property (SCM sym, SCM val)
368 {
369 #ifndef NDEBUG
370   if (do_internal_type_checking_global)
371     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
372 #endif
373
374   properties_dict ()->set (sym, val);
375 }
376
377 /*
378   TODO: look up to check whether we have inherited var?
379 */
380 void
381 Context::unset_property (SCM sym)
382 {
383   properties_dict ()->remove (sym);
384 }
385
386 /**
387    Remove a context from the hierarchy.
388 */
389 Context *
390 Context::remove_context (Context *trans)
391 {
392   assert (trans);
393
394   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
395   trans->daddy_context_ = 0;
396   return trans;
397 }
398
399 /*
400   ID == "" means accept any ID.
401 */
402 Context *
403 find_context_below (Context *where,
404                     SCM type, String id)
405 {
406   if (where->is_alias (type))
407     {
408       if (id == "" || where->id_string () == id)
409         return where;
410     }
411
412   Context *found = 0;
413   for (SCM s = where->children_contexts ();
414        !found && scm_is_pair (s); s = scm_cdr (s))
415     {
416       Context *tr = unsmob_context (scm_car (s));
417
418       found = find_context_below (tr, type, id);
419     }
420
421   return found;
422 }
423
424 SCM
425 Context::properties_as_alist () const
426 {
427   return properties_dict ()->to_alist ();
428 }
429
430 SCM
431 Context::context_name_symbol () const
432 {
433   Context_def *td = unsmob_context_def (definition_);
434   return td->get_context_name ();
435 }
436
437 String
438 Context::context_name () const
439 {
440   return ly_symbol2string (context_name_symbol ());
441 }
442
443 Score_context *
444 Context::get_score_context () const
445 {
446   if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
447     return sc;
448   else if (daddy_context_)
449     return daddy_context_->get_score_context ();
450   else
451     return 0;
452 }
453
454 Output_def *
455 Context::get_output_def () const
456 {
457   return daddy_context_ ? daddy_context_->get_output_def () : 0;
458 }
459
460 Context::~Context ()
461 {
462
463 }
464
465 Moment
466 Context::now_mom () const
467 {
468   return daddy_context_->now_mom ();
469 }
470
471 int
472 Context::print_smob (SCM s, SCM port, scm_print_state *)
473 {
474   Context *sc = (Context *) SCM_CELL_WORD_1 (s);
475
476   scm_puts ("#<", port);
477   scm_puts (classname (sc), port);
478   if (Context_def *d = unsmob_context_def (sc->definition_))
479     {
480       scm_puts (" ", port);
481       scm_display (d->get_context_name (), port);
482     }
483
484   if (Context *td = dynamic_cast<Context *> (sc))
485     {
486       scm_puts ("=", port);
487       scm_puts (td->id_string_.to_str0 (), port);
488     }
489
490   scm_puts (" ", port);
491
492   scm_display (sc->context_list_, port);
493   scm_puts (" >", port);
494
495   return 1;
496 }
497
498 SCM
499 Context::mark_smob (SCM sm)
500 {
501   Context *me = (Context *) SCM_CELL_WORD_1 (sm);
502   scm_gc_mark (me->key_->self_scm ());
503   scm_gc_mark (me->context_list_);
504   scm_gc_mark (me->aliases_);
505   scm_gc_mark (me->definition_);
506   scm_gc_mark (me->properties_scm_);
507   scm_gc_mark (me->accepts_list_);
508   scm_gc_mark (me->implementation_);
509
510   return me->properties_scm_;
511 }
512
513 IMPLEMENT_SMOBS (Context);
514 IMPLEMENT_DEFAULT_EQUAL_P (Context);
515 IMPLEMENT_TYPE_P (Context, "ly:context?");
516
517 bool
518 Context::try_music (Music *m)
519 {
520   Translator *t = implementation ();
521   if (!t)
522     return false;
523
524   bool b = t->try_music (m);
525   if (!b && daddy_context_)
526     b = daddy_context_->try_music (m);
527
528   return b;
529 }
530
531 Global_context *
532 Context::get_global_context () const
533 {
534   if (dynamic_cast<Global_context *> ((Context *) this))
535     return dynamic_cast<Global_context *> ((Context *) this);
536
537   if (daddy_context_)
538     return daddy_context_->get_global_context ();
539
540   programming_error ("No Global context!");
541   return 0;
542 }
543
544 Context *
545 Context::get_parent_context () const
546 {
547   return daddy_context_;
548 }
549
550 Translator_group *
551 Context::implementation () const
552 {
553   return dynamic_cast<Translator_group *> (unsmob_translator (implementation_));
554 }
555
556 void
557 Context::clear_key_disambiguations ()
558 {
559   grob_counts_.clear ();
560   context_counts_.clear ();
561   for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
562     {
563       unsmob_context (scm_car (s))->clear_key_disambiguations ();
564     }
565 }