2 translator-def.cc -- implement Context_def
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 /* TODO: should junk this class an replace by
10 a single list of context modifications? */
12 #include "context-def.hh"
14 #include "engraver-group.hh"
15 #include "engraver.hh"
16 #include "international.hh"
17 #include "output-def.hh"
18 #include "performer-group.hh"
19 #include "performer.hh"
20 #include "score-context.hh"
21 #include "translator-group.hh"
24 Context_def::Context_def ()
26 context_aliases_ = SCM_EOL;
27 translator_group_type_ = SCM_EOL;
28 accept_mods_ = SCM_EOL;
29 translator_mods_ = SCM_EOL;
30 property_ops_ = SCM_EOL;
31 context_name_ = SCM_EOL;
32 default_child_ = SCM_EOL;
33 description_ = SCM_EOL;
37 context_name_ = ly_symbol2scm ("");
40 Context_def::Context_def (Context_def const &s)
43 context_aliases_ = SCM_EOL;
44 translator_group_type_ = SCM_EOL;
45 accept_mods_ = SCM_EOL;
46 translator_mods_ = SCM_EOL;
47 property_ops_ = SCM_EOL;
48 context_name_ = SCM_EOL;
49 description_ = SCM_EOL;
50 default_child_ = SCM_EOL;
53 description_ = s.description_;
55 default_child_ = s.default_child_;
56 accept_mods_ = s.accept_mods_;
57 property_ops_ = s.property_ops_;
58 translator_mods_ = s.translator_mods_;
59 context_aliases_ = s.context_aliases_;
60 translator_group_type_ = s.translator_group_type_;
61 context_name_ = s.context_name_;
64 Context_def::~Context_def ()
68 #include "ly-smobs.icc"
69 IMPLEMENT_SMOBS (Context_def);
70 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
73 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
75 Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
77 scm_puts ("#<Context_def ", port);
78 scm_display (me->context_name_, port);
84 Context_def::mark_smob (SCM smob)
86 Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
88 scm_gc_mark (me->description_);
89 scm_gc_mark (me->context_aliases_);
90 scm_gc_mark (me->accept_mods_);
91 scm_gc_mark (me->translator_mods_);
92 scm_gc_mark (me->property_ops_);
93 scm_gc_mark (me->translator_group_type_);
94 scm_gc_mark (me->default_child_);
96 return me->context_name_;
100 Context_def::add_context_mod (SCM mod)
102 SCM tag = scm_car (mod);
103 if (ly_symbol2scm ("description") == tag)
105 description_ = scm_cadr (mod);
110 other modifiers take symbols as argument.
112 SCM sym = scm_cadr (mod);
113 if (scm_is_string (sym))
114 sym = scm_string_to_symbol (sym);
116 if (ly_symbol2scm ("default-child") == tag)
117 default_child_ = sym;
118 else if (ly_symbol2scm ("consists") == tag
119 || ly_symbol2scm ("consists-end") == tag
120 || ly_symbol2scm ("remove") == tag)
122 if (!get_translator (sym))
123 error (_f ("program has no such type: `%s'",
124 ly_symbol2string (sym).c_str ()));
126 translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
128 else if (ly_symbol2scm ("accepts") == tag
129 || ly_symbol2scm ("denies") == tag)
130 accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
131 else if (ly_symbol2scm ("pop") == tag
132 || ly_symbol2scm ("push") == tag
133 || ly_symbol2scm ("assign") == tag
134 || ly_symbol2scm ("unset") == tag)
135 property_ops_ = scm_cons (mod, property_ops_);
136 else if (ly_symbol2scm ("alias") == tag)
137 context_aliases_ = scm_cons (sym, context_aliases_);
138 else if (ly_symbol2scm ("translator-type") == tag)
139 translator_group_type_ = sym;
140 else if (ly_symbol2scm ("context-name") == tag)
143 programming_error ("unknown context mod tag");
147 Context_def::get_context_name () const
149 return context_name_;
153 Context_def::get_accepted (SCM user_mod) const
155 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
157 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
159 SCM tag = scm_caar (s);
160 SCM sym = scm_cadar (s);
161 if (tag == ly_symbol2scm ("accepts"))
162 acc = scm_cons (sym, acc);
163 else if (tag == ly_symbol2scm ("denies"))
164 acc = scm_delete_x (sym, acc);
167 SCM def = get_default_child (user_mod);
168 if (scm_is_symbol (def))
170 if (scm_memq (def, acc))
171 acc = scm_delete_x (def, acc);
172 acc = scm_cons (def, acc);
179 Context_def::get_default_child (SCM user_mod) const
181 SCM name = default_child_;
182 for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
184 SCM entry = scm_car (s);
185 if (scm_car (entry) == ly_symbol2scm ("default-child"))
187 name = scm_cadr (entry);
196 Context_def::path_to_acceptable_context (SCM type_sym, Output_def *odef) const
198 assert (scm_is_symbol (type_sym));
200 SCM accepted = get_accepted (SCM_EOL);
202 vector<Context_def*> accepteds;
203 for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
204 if (Context_def *t = unsmob_context_def (find_context_def (odef,
206 accepteds.push_back (t);
208 vector<Context_def*> best_result;
209 for (vsize i = 0; i < accepteds.size (); i++)
211 /* do not check aliases, because \context Staff should not
212 create RhythmicStaff. */
213 if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
215 best_result.push_back (accepteds[i]);
220 vsize best_depth = INT_MAX;
221 for (vsize i = 0; i < accepteds.size (); i++)
223 Context_def *g = accepteds[i];
225 vector<Context_def*> result
226 = g->path_to_acceptable_context (type_sym, odef);
227 if (result.size () && result.size () < best_depth)
229 best_depth = result.size ();
230 result.insert (result.begin (), g);
231 best_result = result;
239 Context_def::get_translator_names (SCM user_mod) const
243 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
245 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
247 SCM tag = scm_caar (s);
248 SCM arg = scm_cadar (s);
250 if (scm_is_string (arg))
251 arg = scm_string_to_symbol (arg);
253 if (ly_symbol2scm ("consists") == tag)
254 l1 = scm_cons (arg, l1);
255 else if (ly_symbol2scm ("remove") == tag)
256 l1 = scm_delete_x (arg, l1);
263 filter_performers (SCM ell)
266 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
268 if (dynamic_cast<Performer *> (unsmob_translator (scm_car (*tail))))
269 *tail = scm_cdr (*tail);
271 tail = SCM_CDRLOC (*tail);
277 filter_engravers (SCM ell)
280 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
282 if (dynamic_cast<Engraver *> (unsmob_translator (scm_car (*tail))))
283 *tail = scm_cdr (*tail);
285 tail = SCM_CDRLOC (*tail);
291 Context_def::instantiate (SCM ops, Object_key const *key)
293 Context *context = 0;
295 if (context_name_ == ly_symbol2scm ("Score"))
296 context = new Score_context (key);
298 context = new Context (key);
300 context->definition_ = self_scm ();
302 SCM trans_names = get_translator_names (ops);
304 Translator_group *g = get_translator_group (translator_group_type_);
305 SCM trans_list = SCM_EOL;
307 for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
309 Translator *t = get_translator (scm_car (s));
311 warning (_f ("can't find: `%s'", ly_symbol2string (scm_car (s)).c_str ()));
314 Translator *tr = t->clone ();
315 SCM str = tr->self_scm ();
317 if (tr->must_be_last ())
319 SCM cons = scm_cons (str, SCM_EOL);
320 if (scm_is_pair (trans_list))
321 scm_set_cdr_x (scm_last_pair (trans_list), cons);
326 trans_list = scm_cons (str, trans_list);
328 tr->daddy_context_ = context;
334 Ugh, todo: should just make a private
335 copy of Context_def with the user mods.
338 g->simple_trans_list_ = trans_list;
340 context->implementation_ = g;
341 if (dynamic_cast<Engraver_group *> (g))
342 g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
343 else if (dynamic_cast<Performer_group *> (g))
344 g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
346 g->context_ = context;
347 context->aliases_ = context_aliases_;
351 context->accepts_list_ = get_accepted (ops);
357 Context_def::clone_scm () const
359 Context_def *t = new Context_def (*this);
360 return t->unprotect ();
364 Context_def::make_scm ()
366 Context_def *t = new Context_def;
367 return t->unprotect ();
371 Context_def::apply_default_property_operations (Context *tg)
373 apply_property_operations (tg, property_ops_);
377 Context_def::to_alist () const
381 ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
382 get_translator_names (SCM_EOL)), ell);
383 ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
384 ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
385 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
387 ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
389 ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
392 if (scm_is_symbol (translator_group_type_))
393 ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
394 translator_group_type_), ell);