2 context.cc -- implement Context
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "context-def.hh"
12 #include "dispatcher.hh"
13 #include "international.hh"
14 #include "ly-smobs.icc"
16 #include "output-def.hh"
18 #include "program-option.hh"
19 #include "scm-hash.hh"
20 #include "score-context.hh"
21 #include "translator-group.hh"
25 Context::is_removable () const
27 return context_list_ == SCM_EOL && ! iterator_count_
28 && !dynamic_cast<Global_context const *> (daddy_context_);
32 Context::check_removal ()
34 for (SCM p = context_list_; scm_is_pair (p); p = scm_cdr (p))
36 Context *trg = unsmob_context (scm_car (p));
38 trg->check_removal ();
39 if (trg->is_removable ())
41 recurse_over_translators (trg, &Translator::finalize,
42 &Translator_group::finalize,
49 Context::Context (Context const &src)
50 : key_manager_ (src.key_manager_)
56 Context::properties_dict () const
58 return Scheme_hash_table::unsmob (properties_scm_);
62 Context::add_context (Context *t)
64 SCM ts = t->self_scm ();
65 context_list_ = ly_append2 (context_list_,
66 scm_cons (ts, SCM_EOL));
68 t->daddy_context_ = this;
74 Context_def *td = unsmob_context_def (t->definition_);
76 /* This cannot move before add_context (), because \override
77 operations require that we are in the hierarchy. */
78 td->apply_default_property_operations (t);
80 recurse_over_translators (t,
81 &Translator::initialize,
82 &Translator_group::initialize,
88 Context::Context (Object_key const *key)
96 properties_scm_ = SCM_EOL;
97 accepts_list_ = SCM_EOL;
98 context_list_ = SCM_EOL;
99 definition_ = SCM_EOL;
100 definition_mods_ = SCM_EOL;
107 Scheme_hash_table *tab = new Scheme_hash_table;
108 properties_scm_ = tab->unprotect ();
109 event_source_ = new Dispatcher ();
110 event_source_->unprotect ();
111 events_below_ = new Dispatcher ();
112 events_below_->unprotect ();
118 key_manager_.unprotect();
121 /* TODO: this shares code with find_create_context (). */
123 Context::create_unique_context (SCM name, string id, SCM operations)
126 Don't create multiple score contexts.
128 Global_context *gthis = dynamic_cast<Global_context *> (this);
129 if (gthis && gthis->get_score_context ())
130 return gthis->get_score_context ()->create_unique_context (name, id, operations);
133 TODO: use accepts_list_.
135 vector<Context_def*> path
136 = unsmob_context_def (definition_)->path_to_acceptable_context (name, get_output_def ());
140 Context *current = this;
142 // start at 1. The first one (index 0) will be us.
143 for (vsize i = 0; i < path.size (); i++)
146 string id_str = "\\new";
147 if (i == path.size () - 1)
152 current = current->create_context (path[i],
161 Don't go up to Global_context, because global goes down to
165 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
166 ret = daddy_context_->create_unique_context (name, id, operations);
169 warning (_f ("can't find or create new `%s'",
170 ly_symbol2string (name).c_str ()));
177 Context::find_create_context (SCM n, string id, SCM operations)
180 Don't create multiple score contexts.
182 Global_context *gthis = dynamic_cast<Global_context *> (this);
183 if (gthis && gthis->get_score_context ())
184 return gthis->get_score_context ()->find_create_context (n, id, operations);
186 if (Context *existing = find_context_below (this, n, id))
189 if (n == ly_symbol2scm ("Bottom"))
191 Context *tg = get_default_interpreter ();
196 TODO: use accepts_list_.
198 vector<Context_def*> path
199 = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
203 Context *current = this;
205 // start at 1. The first one (index 0) will be us.
206 for (vsize i = 0; i < path.size (); i++)
208 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
211 if (i == path.size () -1)
214 current = current->create_context (path[i],
223 Don't go up to Global_context, because global goes down to
227 if (daddy_context_ && !dynamic_cast<Global_context *> (daddy_context_))
228 ret = daddy_context_->find_create_context (n, id, operations);
231 warning (_f ("can't find or create `%s' called `%s'",
232 ly_symbol2string (n).c_str (), id));
239 Context::create_context (Context_def *cdef,
243 int unique = get_global_context()->new_unique();
245 // TODO: The following should be carried out by a listener.
246 string type = ly_symbol2string (cdef->get_context_name ());
247 Object_key const *key = key_manager_.get_context_key (now_mom(), type, id);
249 = cdef->instantiate (ops, key);
251 new_context->id_string_ = id;
252 new_context->unique_ = unique;
254 new_context->events_below_->register_as_listener (new_context->event_source_);
256 add_context (new_context);
257 apply_property_operations (new_context, ops);
258 events_below_->register_as_listener (new_context->events_below_);
260 // TODO: The above operations should be performed by a listener to the following event.
261 send_stream_event (this, "CreateContext",
262 ly_symbol2scm ("unique"), scm_int2num (unique),
263 ly_symbol2scm ("ops"), ops,
264 ly_symbol2scm ("type"), cdef->get_context_name (),
265 ly_symbol2scm ("id"), scm_makfrom0str (id.c_str ()));
271 Default child context as a SCM string, or something else if there is
275 Context::default_child_context_name () const
277 return scm_is_pair (accepts_list_)
278 ? scm_car (accepts_list_)
283 Context::is_bottom_context () const
285 return !scm_is_symbol (default_child_context_name ());
289 Context::get_default_interpreter ()
291 if (!is_bottom_context ())
293 SCM nm = default_child_context_name ();
294 SCM st = find_context_def (get_output_def (), nm);
296 string name = ly_symbol2string (nm);
297 Context_def *t = unsmob_context_def (st);
300 warning (_f ("can't find or create: `%s'", name.c_str ()));
301 t = unsmob_context_def (this->definition_);
304 Context *tg = create_context (t, "", SCM_EOL);
305 return tg->get_default_interpreter ();
314 Context::where_defined (SCM sym, SCM *value) const
317 if (profile_property_accesses)
318 note_property_access (&context_property_lookup_table, sym);
321 if (properties_dict ()->try_retrieve (sym, value))
322 return (Context *)this;
324 return (daddy_context_) ? daddy_context_->where_defined (sym, value) : 0;
328 return SCM_EOL when not found.
331 Context::internal_get_property (SCM sym) const
334 if (profile_property_accesses)
335 note_property_access (&context_property_lookup_table, sym);
339 if (properties_dict ()->try_retrieve (sym, &val))
343 return daddy_context_->internal_get_property (sym);
349 Context::internal_send_stream_event (SCM type, SCM props[])
351 Stream_event *e = new Stream_event (this, type);
352 for (int i = 0; props[i]; i++)
355 e->internal_set_property (props[i], props[i+1]);
357 event_source_->broadcast (e);
362 Context::is_alias (SCM sym) const
364 if (sym == ly_symbol2scm ("Bottom")
365 && !scm_is_pair (accepts_list_))
367 if (sym == unsmob_context_def (definition_)->get_context_name ())
370 return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
374 Context::add_alias (SCM sym)
376 aliases_ = scm_cons (sym, aliases_);
380 Context::internal_set_property (SCM sym, SCM val)
383 if (do_internal_type_checking_global)
384 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
387 properties_dict ()->set (sym, val);
391 TODO: look up to check whether we have inherited var?
394 Context::unset_property (SCM sym)
396 properties_dict ()->remove (sym);
400 Remove a context from the hierarchy.
403 Context::remove_context (Context *trans)
407 context_list_ = scm_delq_x (trans->self_scm (), context_list_);
408 trans->daddy_context_ = 0;
414 ID == "" means accept any ID.
417 find_context_below (Context *where,
420 if (where->is_alias (type))
422 if (id == "" || where->id_string () == id)
427 for (SCM s = where->children_contexts ();
428 !found && scm_is_pair (s); s = scm_cdr (s))
430 Context *tr = unsmob_context (scm_car (s));
432 found = find_context_below (tr, type, id);
439 find_context_below (Context *where,
442 if (where->get_unique () == unique)
446 for (SCM s = where->children_contexts ();
447 !found && scm_is_pair (s); s = scm_cdr (s))
449 Context *tr = unsmob_context (scm_car (s));
451 found = find_context_below (tr, unique);
458 Context::properties_as_alist () const
460 return properties_dict ()->to_alist ();
464 Context::context_name_symbol () const
466 Context_def *td = unsmob_context_def (definition_);
467 return td->get_context_name ();
471 Context::context_name () const
473 return ly_symbol2string (context_name_symbol ());
477 Context::get_score_context () const
479 if (Score_context *sc = dynamic_cast<Score_context *> ((Context *) this))
481 else if (daddy_context_)
482 return daddy_context_->get_score_context ();
488 Context::get_output_def () const
490 return daddy_context_ ? daddy_context_->get_output_def () : 0;
498 Context::now_mom () const
500 Context const *p = this;
501 while (p->daddy_context_)
502 p = p->daddy_context_;
504 return p->now_mom ();
508 Context::print_smob (SCM s, SCM port, scm_print_state *)
510 Context *sc = (Context *) SCM_CELL_WORD_1 (s);
512 scm_puts ("#<", port);
513 scm_puts (sc->class_name (), port);
514 if (Context_def *d = unsmob_context_def (sc->definition_))
516 scm_puts (" ", port);
517 scm_display (d->get_context_name (), port);
520 if (!sc->id_string_.empty ())
522 scm_puts ("=", port);
523 scm_puts (sc->id_string_.c_str (), port);
526 scm_puts (" ", port);
528 scm_display (sc->context_list_, port);
529 scm_puts (" >", port);
535 Context::get_grob_key (string name)
537 return key_manager_.get_grob_key (now_mom (), name);
541 Context::get_context_key (string name, string id)
543 return key_manager_.get_context_key (now_mom (), name, id);
547 Context::mark_smob (SCM sm)
549 Context *me = (Context *) SCM_CELL_WORD_1 (sm);
550 me->key_manager_.gc_mark();
552 scm_gc_mark (me->context_list_);
553 scm_gc_mark (me->aliases_);
554 scm_gc_mark (me->definition_);
555 scm_gc_mark (me->definition_mods_);
556 scm_gc_mark (me->properties_scm_);
557 scm_gc_mark (me->accepts_list_);
558 if (me->implementation_)
559 scm_gc_mark (me->implementation_->self_scm ());
560 if (me->event_source_) scm_gc_mark (me->event_source_->self_scm ());
561 if (me->events_below_) scm_gc_mark (me->events_below_->self_scm ());
563 return me->properties_scm_;
566 IMPLEMENT_SMOBS (Context);
567 IMPLEMENT_DEFAULT_EQUAL_P (Context);
568 IMPLEMENT_TYPE_P (Context, "ly:context?");
571 Context::try_music (Music *m)
573 Translator_group *t = implementation ();
577 bool b = t->try_music (m);
578 if (!b && daddy_context_)
579 b = daddy_context_->try_music (m);
585 Context::get_global_context () const
587 if (dynamic_cast<Global_context *> ((Context *) this))
588 return dynamic_cast<Global_context *> ((Context *) this);
591 return daddy_context_->get_global_context ();
593 programming_error ("no Global context");
598 Context::get_parent_context () const
600 return daddy_context_;
604 Context::clear_key_disambiguations ()
606 if (!use_object_keys)
609 key_manager_.clear ();
610 for (SCM s = context_list_; scm_is_pair (s); s = scm_cdr (s))
611 unsmob_context (scm_car (s))->clear_key_disambiguations ();
615 Ugh. Where to put this?
618 measure_length (Context const *context)
620 SCM l = context->get_property ("measureLength");
622 if (unsmob_moment (l))
623 length = unsmob_moment (l)->main_part_;
628 measure_position (Context const *context)
630 SCM sm = context->get_property ("measurePosition");
633 if (unsmob_moment (sm))
635 m = *unsmob_moment (sm);
637 if (m.main_part_ < Rational (0))
639 Rational length (measure_length (context));
640 while (m.main_part_ < Rational (0))
641 m.main_part_ += length;
650 set_context_property_on_children (Context *trans, SCM sym, SCM val)
652 trans->internal_set_property (sym, ly_deep_copy (val));
653 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
655 Context *trg = unsmob_context (scm_car (p));
656 set_context_property_on_children (trg, sym, ly_deep_copy (val));