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