2 translator-def.cc -- implement Context_def
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.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.hh"
15 #include "output-def.hh"
16 #include "performer.hh"
17 #include "score-context.hh"
18 #include "translator-group.hh"
21 #include "engraver-group-engraver.hh"
22 #include "performer-group-performer.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).to_str0 ()));
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 ("poppush") == tag
132 || ly_symbol2scm ("pop") == tag
133 || ly_symbol2scm ("push") == tag
134 || ly_symbol2scm ("assign") == tag
135 || ly_symbol2scm ("unset") == tag)
136 property_ops_ = scm_cons (mod, property_ops_);
137 else if (ly_symbol2scm ("alias") == tag)
138 context_aliases_ = scm_cons (sym, context_aliases_);
139 else if (ly_symbol2scm ("translator-type") == tag)
140 translator_group_type_ = sym;
141 else if (ly_symbol2scm ("context-name") == tag)
144 programming_error ("unknown context mod tag");
148 Context_def::get_context_name () const
150 return context_name_;
154 Context_def::get_accepted (SCM user_mod) const
156 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
158 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
160 SCM tag = scm_caar (s);
161 SCM sym = scm_cadar (s);
162 if (tag == ly_symbol2scm ("accepts"))
163 acc = scm_cons (sym, acc);
164 else if (tag == ly_symbol2scm ("denies"))
165 acc = scm_delete_x (sym, acc);
168 SCM def = get_default_child (user_mod);
169 if (scm_is_symbol (def))
171 if (scm_memq (def, acc))
172 acc = scm_delete_x (def, acc);
173 acc = scm_cons (def, acc);
180 Context_def::get_default_child (SCM user_mod) const
182 SCM name = default_child_;
183 for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
185 SCM entry = scm_car (s);
186 if (scm_car (entry) == ly_symbol2scm ("default-child"))
188 name = scm_cadr (entry);
196 Link_array<Context_def>
197 Context_def::path_to_acceptable_context (SCM type_sym, Output_def *odef) const
199 assert (scm_is_symbol (type_sym));
201 SCM accepted = get_accepted (SCM_EOL);
203 Link_array<Context_def> accepteds;
204 for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
205 if (Context_def *t = unsmob_context_def (find_context_def (odef,
209 Link_array<Context_def> best_result;
210 for (int i = 0; i < accepteds.size (); i++)
212 /* do not check aliases, because \context Staff should not
213 create RhythmicStaff. */
214 if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
216 best_result.push (accepteds[i]);
221 int best_depth = INT_MAX;
222 for (int i = 0; i < accepteds.size (); i++)
224 Context_def *g = accepteds[i];
226 Link_array<Context_def> result
227 = g->path_to_acceptable_context (type_sym, odef);
228 if (result.size () && result.size () < best_depth)
230 best_depth = result.size ();
231 result.insert (g, 0);
232 best_result = result;
240 Context_def::get_translator_names (SCM user_mod) const
244 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
246 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
248 SCM tag = scm_caar (s);
249 SCM arg = scm_cadar (s);
251 if (scm_is_string (arg))
252 arg = scm_string_to_symbol (arg);
254 if (ly_symbol2scm ("consists") == tag)
255 l1 = scm_cons (arg, l1);
256 else if (ly_symbol2scm ("remove") == tag)
257 l1 = scm_delete_x (arg, l1);
264 filter_performers (SCM ell)
267 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
269 if (dynamic_cast<Performer *> (unsmob_translator (scm_car (*tail))))
270 *tail = scm_cdr (*tail);
272 tail = SCM_CDRLOC (*tail);
278 filter_engravers (SCM ell)
281 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
283 if (dynamic_cast<Engraver *> (unsmob_translator (scm_car (*tail))))
284 *tail = scm_cdr (*tail);
286 tail = SCM_CDRLOC (*tail);
292 Context_def::instantiate (SCM ops, Object_key const *key)
294 Context *context = 0;
296 if (context_name_ == ly_symbol2scm ("Score"))
297 context = new Score_context (key);
299 context = new Context (key);
301 context->definition_ = self_scm ();
303 SCM trans_names = get_translator_names (ops);
305 Translator_group *g = get_translator_group (translator_group_type_);
306 SCM trans_list = SCM_EOL;
308 for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
310 Translator *t = get_translator (scm_car (s));
312 warning (_f ("can't find: `%s'", s));
315 Translator *tr = t->clone ();
316 SCM str = tr->self_scm ();
318 if (tr->must_be_last ())
320 SCM cons = scm_cons (str, SCM_EOL);
321 if (scm_is_pair (trans_list))
322 scm_set_cdr_x (scm_last_pair (trans_list), cons);
327 trans_list = scm_cons (str, trans_list);
329 tr->daddy_context_ = context;
335 Ugh, todo: should just make a private
336 copy of Context_def with the user mods.
339 g->simple_trans_list_ = trans_list;
341 context->implementation_ = g;
342 if (dynamic_cast<Engraver_group *> (g))
343 g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
344 else if (dynamic_cast<Performer_group *> (g))
345 g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
347 g->context_ = context;
348 context->aliases_ = context_aliases_;
352 context->accepts_list_ = get_accepted (ops);
358 Context_def::clone_scm () const
360 Context_def *t = new Context_def (*this);
361 return t->unprotect ();
365 Context_def::make_scm ()
367 Context_def *t = new Context_def;
368 return t->unprotect ();
372 Context_def::apply_default_property_operations (Context *tg)
374 apply_property_operations (tg, property_ops_);
378 Context_def::to_alist () const
382 ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
383 get_translator_names (SCM_EOL)), ell);
384 ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
385 ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
386 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
388 ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
390 ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
393 if (scm_is_symbol (translator_group_type_))
394 ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
395 translator_group_type_), ell);