2 translator-def.cc -- implement Context_def
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 TODO: should junk this class an replace by
12 a single list of context modifications?
15 #include "lily-proto.hh"
16 #include "context-def.hh"
17 #include "translator-group.hh"
19 #include "music-output-def.hh"
20 #include "ly-smobs.icc"
21 #include "score-context.hh"
23 #include "performer.hh"
24 #include "engraver.hh"
27 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
29 Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
31 scm_puts ("#<Context_def ", port);
32 scm_display (me->context_name_, port);
39 Context_def::mark_smob (SCM smob)
41 Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
43 scm_gc_mark (me->description_);
44 scm_gc_mark (me->context_aliases_);
45 scm_gc_mark (me->accept_mods_);
46 scm_gc_mark (me->translator_mods_);
47 scm_gc_mark (me->property_ops_);
48 scm_gc_mark (me->translator_group_type_);
50 return me->context_name_;
54 Context_def::Context_def ()
56 context_aliases_ = SCM_EOL;
57 translator_group_type_ = SCM_EOL;
58 accept_mods_ = SCM_EOL;
59 translator_mods_ = SCM_EOL;
60 property_ops_ = SCM_EOL;
61 context_name_ = SCM_EOL;
62 description_ = SCM_EOL;
66 context_name_ = ly_symbol2scm ("");
69 Context_def::~Context_def ()
73 Context_def::Context_def (Context_def const & s)
76 context_aliases_ = SCM_EOL;
77 translator_group_type_ = SCM_EOL;
78 accept_mods_ = SCM_EOL;
79 translator_mods_ = SCM_EOL;
80 property_ops_ = SCM_EOL;
81 context_name_ = SCM_EOL;
82 description_ = SCM_EOL;
85 description_ = s.description_;
87 accept_mods_ = s.accept_mods_;
88 property_ops_ = s.property_ops_;
89 translator_mods_ = s.translator_mods_;
90 context_aliases_ = s.context_aliases_;
91 translator_group_type_ = s.translator_group_type_;
92 context_name_ = s.context_name_;
97 Context_def::add_context_mod (SCM mod)
99 SCM tag = ly_car (mod);
100 if (ly_symbol2scm ("description") == tag)
102 description_ = ly_cadr (mod);
106 SCM sym = ly_cadr (mod);
107 if (ly_c_string_p (sym))
108 sym = scm_string_to_symbol (sym);
110 if (ly_symbol2scm ("consists") == tag
111 || ly_symbol2scm ("consists-end") == tag
112 || ly_symbol2scm ("remove") == tag)
114 if (!get_translator (sym))
115 error (_f ("Program has no such type: `%s'", ly_symbol2string (sym).to_str0 ()));
117 translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_ );
119 else if (ly_symbol2scm ("accepts") == tag
120 || ly_symbol2scm ("denies") == tag)
122 accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
124 else if (ly_symbol2scm ("poppush") == tag
125 || ly_symbol2scm ("pop") == tag
126 || ly_symbol2scm ("push") == tag
127 || ly_symbol2scm ("assign") == tag
128 || ly_symbol2scm ("unset") == tag)
130 property_ops_ = scm_cons (mod, property_ops_);
132 else if (ly_symbol2scm ("alias") == tag)
134 context_aliases_ = scm_cons (sym, context_aliases_);
136 else if (ly_symbol2scm ("translator-type") == tag)
138 translator_group_type_ = sym;
140 else if (ly_symbol2scm ("context-name") == tag)
146 programming_error ("Unknown context mod tag.");
153 Context_def::get_context_name () const
155 return context_name_;
159 Context_def::get_accepted (SCM user_mod) const
161 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_),
164 for (SCM s = mods; ly_c_pair_p (s); s = ly_cdr (s))
166 SCM tag = ly_caar (s);
167 SCM sym = ly_cadar (s);
168 if (tag == ly_symbol2scm ("accepts"))
169 acc = scm_cons (sym, acc);
170 else if (tag == ly_symbol2scm ("denies"))
171 acc = scm_delete_x (sym, acc);
177 Link_array<Context_def>
178 Context_def::path_to_acceptable_context (SCM type_sym, Music_output_def* odef) const
180 assert (ly_c_symbol_p (type_sym));
182 SCM accepted = get_accepted (SCM_EOL);
184 Link_array<Context_def> accepteds;
185 for (SCM s = accepted; ly_c_pair_p (s); s = ly_cdr (s))
187 Context_def *t = unsmob_context_def (odef->find_context_def (ly_car (s)));
193 Link_array<Context_def> best_result;
194 for (int i=0; i < accepteds.size (); i++)
197 don't check aliases, because \context Staff should not create RhythmicStaff.
199 if (ly_c_equal_p (accepteds[i]->get_context_name (), type_sym))
201 best_result.push (accepteds[i]);
206 int best_depth= INT_MAX;
207 for (int i=0; i < accepteds.size (); i++)
209 Context_def * g = accepteds[i];
211 Link_array<Context_def> result
212 = g->path_to_acceptable_context (type_sym, odef);
213 if (result.size () && result.size () < best_depth)
216 best_result = result;
219 this following line was added in 1.9.3, but hsould've been
220 there all along... Let's hope it doesn't cause nightmares.
222 best_depth = result.size ();
229 IMPLEMENT_SMOBS (Context_def);
230 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
234 Context_def::get_translator_names (SCM user_mod) const
239 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_),
242 for (SCM s = mods; ly_c_pair_p (s); s = ly_cdr (s))
244 SCM tag = ly_caar (s);
245 SCM arg = ly_cadar (s);
247 if (ly_c_string_p (arg))
248 arg = scm_string_to_symbol (arg);
250 if (ly_symbol2scm ("consists") == tag)
251 l1 = scm_cons (arg, l1);
252 else if (ly_symbol2scm ("consists-end") == tag)
253 l2 = scm_cons (arg, l2);
254 else if (ly_symbol2scm ("remove") == tag)
256 l1 = scm_delete_x (arg, l1);
257 l2 = scm_delete_x (arg, l2);
261 return scm_append_x (scm_list_2 (l1, l2));
266 filter_performers (SCM l)
268 for (SCM *tail = &l; ly_c_pair_p (*tail); tail = SCM_CDRLOC (*tail))
270 if (dynamic_cast<Performer*> (unsmob_translator (ly_car (*tail))))
272 *tail = ly_cdr (*tail);
273 if (!ly_c_pair_p (*tail))
282 filter_engravers (SCM l)
285 for (; ly_c_pair_p (*tail) ; tail = SCM_CDRLOC (*tail))
287 if (dynamic_cast<Engraver*> (unsmob_translator (ly_car (*tail))))
289 *tail = ly_cdr (*tail);
290 if (!ly_c_pair_p (*tail))
299 Context_def::instantiate (SCM ops)
303 if (context_name_ == ly_symbol2scm ("Score"))
304 tg = new Score_context ();
308 tg->definition_ = self_scm ();
310 SCM trans_names = get_translator_names (ops);
312 Translator * g = get_translator (translator_group_type_);
315 g->simple_trans_list_ = SCM_EOL;
317 for (SCM s = trans_names; ly_c_pair_p (s) ; s = ly_cdr (s))
319 Translator * t = get_translator (ly_car (s));
321 warning (_f ("can't find: `%s'", s));
324 Translator * tr = t->clone ();
325 SCM str = tr->self_scm ();
326 g->simple_trans_list_ = scm_cons (str, g->simple_trans_list_);
327 tr->daddy_context_ = tg;
328 scm_gc_unprotect_object (str);
334 tg->implementation_ = g->self_scm ();
335 if (dynamic_cast<Engraver*> (g))
336 g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
337 else if (dynamic_cast<Performer*> (g))
338 g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
340 g->daddy_context_ = tg;
341 tg->aliases_ = context_aliases_ ;
343 scm_gc_unprotect_object (g->self_scm ());
345 tg->accepts_list_ = get_accepted (ops);
352 Context_def::clone_scm () const
354 Context_def * t = new Context_def (*this);
356 SCM x = t->self_scm ();
357 scm_gc_unprotect_object (x);
362 Context_def::make_scm ()
364 Context_def* t = new Context_def;
366 SCM x =t->self_scm ();
367 scm_gc_unprotect_object (x);
372 Context_def::apply_default_property_operations (Context *tg)
374 apply_property_operations (tg , property_ops_);
378 Context_def::to_alist () const
382 l = scm_cons (scm_cons (ly_symbol2scm ("consists"),
383 get_translator_names (SCM_EOL)), l);
384 l = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), l);
385 l = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), l);
386 l = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)), l);
387 l = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_), l);
388 l = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_), l);
390 if (ly_c_symbol_p (translator_group_type_))
391 l = scm_cons (scm_cons (ly_symbol2scm ("group-type"), translator_group_type_), l);