2 translator-def.cc -- implement Context_def
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "lily-proto.hh"
11 #include "context-def.hh"
12 #include "translator-group.hh"
14 #include "music-output-def.hh"
15 #include "ly-smobs.icc"
18 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
20 Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
22 scm_puts ("#<Context_def ", port);
23 scm_display (me->context_name_, port);
30 Context_def::mark_smob (SCM smob)
32 Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
34 scm_gc_mark (me->description_);
35 scm_gc_mark (me->context_aliases_);
36 scm_gc_mark (me->accept_mods_);
37 scm_gc_mark (me->translator_mods_);
38 scm_gc_mark (me->property_ops_);
39 scm_gc_mark (me->translator_group_type_);
40 return me->context_name_;
44 Context_def::Context_def ()
46 context_aliases_ = SCM_EOL;
47 translator_group_type_ = SCM_EOL;
48 accept_mods_ = SCM_EOL;
49 translator_mods_ = SCM_EOL;
50 property_ops_ = SCM_EOL;
51 context_name_ = SCM_EOL;
52 description_ = SCM_EOL;
57 Context_def::~Context_def ()
61 Context_def::Context_def (Context_def const & s)
64 context_aliases_ = SCM_EOL;
65 translator_group_type_ = SCM_EOL;
66 accept_mods_ = SCM_EOL;
67 translator_mods_ = SCM_EOL;
68 property_ops_ = SCM_EOL;
69 context_name_ = SCM_EOL;
70 description_ = SCM_EOL;
73 description_ = s.description_;
75 accept_mods_ = s.accept_mods_;
76 property_ops_ = s.property_ops_;
77 translator_mods_ = s.translator_mods_;
78 context_aliases_ = s.context_aliases_;
79 translator_group_type_ = s.translator_group_type_;
80 context_name_ = s.context_name_;
85 Context_def::add_context_mod (SCM mod)
87 SCM tag = gh_car (mod);
88 if (ly_symbol2scm ("description") == tag)
90 description_ = gh_cadr (mod);
94 SCM sym = gh_cadr (mod);
95 if (gh_string_p (sym))
96 sym = scm_string_to_symbol (sym);
98 if (ly_symbol2scm ("consists") == tag
99 || ly_symbol2scm ("consists-end") == tag
100 || ly_symbol2scm ("remove") == tag)
102 if (!get_translator (sym))
103 error (_f ("Program has no such type: `%s'", ly_symbol2string (sym).to_str0 ()));
105 translator_mods_ = gh_cons (scm_list_2 (tag, sym), translator_mods_ );
107 else if (ly_symbol2scm ("accepts") == tag
108 || ly_symbol2scm ("denies") == tag)
110 accept_mods_ = gh_cons (scm_list_2 (tag, sym), accept_mods_);
112 else if (ly_symbol2scm ("poppush") == tag
113 || ly_symbol2scm ("pop") == tag
114 || ly_symbol2scm ("push") == tag
115 || ly_symbol2scm ("assign") == tag
116 || ly_symbol2scm ("unset") == tag)
118 property_ops_ = gh_cons (mod, property_ops_);
120 else if (ly_symbol2scm ("alias") == tag)
122 context_aliases_ = gh_cons (sym, context_aliases_);
124 else if (ly_symbol2scm ("translator-type") == tag)
126 translator_group_type_ = sym;
128 else if (ly_symbol2scm ("context-name") == tag)
134 programming_error ("Unknown context mod tag.");
141 Context_def::get_context_name () const
143 return context_name_;
147 Context_def::get_accepted (SCM user_mod) const
149 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_),
152 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
154 SCM tag = gh_caar (s);
155 SCM sym = gh_cadar (s);
156 if (tag == ly_symbol2scm ("accepts"))
157 acc = gh_cons (sym, acc);
158 else if (tag == ly_symbol2scm ("denies"))
159 acc = scm_delete_x (sym, acc);
165 Link_array<Context_def>
166 Context_def::path_to_acceptable_translator (SCM type_sym, Music_output_def* odef) const
168 assert (gh_symbol_p (type_sym));
170 SCM accepted = get_accepted (SCM_EOL);
172 Link_array<Context_def> accepteds;
173 for (SCM s = accepted; gh_pair_p (s); s = ly_cdr (s))
175 Context_def *t = unsmob_context_def (odef->find_translator (ly_car (s)));
181 Link_array<Context_def> best_result;
182 for (int i=0; i < accepteds.size (); i++)
185 don't check aliases, because \context Staff should not create RhythmicStaff.
187 if (gh_equal_p (accepteds[i]->get_context_name (), type_sym))
189 best_result.push (accepteds[i]);
194 int best_depth= INT_MAX;
195 for (int i=0; i < accepteds.size (); i++)
197 Context_def * g = accepteds[i];
199 Link_array<Context_def> result
200 = g->path_to_acceptable_translator (type_sym, odef);
201 if (result.size () && result.size () < best_depth)
204 best_result = result;
207 this following line was added in 1.9.3, but hsould've been
208 there all along... Let's hope it doesn't cause nightmares.
210 best_depth = result.size();
217 IMPLEMENT_SMOBS (Context_def);
218 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
224 Context_def::get_translator_names (SCM user_mod) const
229 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_),
232 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
234 SCM tag = gh_caar (s);
235 SCM arg = gh_cadar (s);
237 if (gh_string_p (arg))
238 arg = scm_string_to_symbol (arg);
240 if (ly_symbol2scm ("consists") == tag)
241 l1 = gh_cons (arg, l1);
242 else if (ly_symbol2scm ("consists-end") == tag)
243 l2 = gh_cons (arg, l2);
244 else if (ly_symbol2scm ("remove") == tag)
246 l1 = scm_delete_x (arg, l1);
247 l2 = scm_delete_x (arg, l2);
251 return scm_append_x (scm_list_2 (l1, l2));
256 Context_def::instantiate (Music_output_def* md, SCM ops)
258 Translator * g = get_translator (translator_group_type_);
261 Translator_group *tg = dynamic_cast<Translator_group*> (g);
262 tg->output_def_ = md;
263 tg->definition_ = self_scm ();
265 SCM trans_names = get_translator_names (ops);
266 tg->simple_trans_list_ = names_to_translators (trans_names, tg);
267 tg->accepts_list_ = get_accepted (ops);
273 Context_def::clone_scm () const
275 Context_def * t = new Context_def (*this);
276 scm_gc_unprotect_object (t->self_scm());
277 return t->self_scm();
281 Context_def::make_scm ()
283 Context_def* t = new Context_def;
284 scm_gc_unprotect_object (t->self_scm());
285 return t->self_scm();
289 Context_def::apply_default_property_operations (Translator_group *tg)
291 apply_property_operations (tg , property_ops_);
295 Context_def::to_alist () const
299 l = gh_cons (gh_cons (ly_symbol2scm ("consists"),
300 get_translator_names (SCM_EOL)), l);
301 l = gh_cons (gh_cons (ly_symbol2scm ("description"), description_), l);
302 l = gh_cons (gh_cons (ly_symbol2scm ("aliases"), context_aliases_), l);
303 l = gh_cons (gh_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)), l);
304 l = gh_cons (gh_cons (ly_symbol2scm ("property-ops"), property_ops_), l);
305 l = gh_cons (gh_cons (ly_symbol2scm ("context-name"), context_name_), l);
306 l = gh_cons (gh_cons (ly_symbol2scm ("group-type"), translator_group_type_), l);
312 Context_def::is_alias (SCM sym) const
314 bool b = sym == context_name_;
316 for (SCM a = context_aliases_; !b && gh_pair_p (a); a = ly_cdr (a))
317 b = b || sym == ly_car (a);