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"
24 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
26 Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
28 scm_puts ("#<Context_def ", port);
29 scm_display (me->context_name_, port);
36 Context_def::mark_smob (SCM smob)
38 Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
40 scm_gc_mark (me->description_);
41 scm_gc_mark (me->context_aliases_);
42 scm_gc_mark (me->accept_mods_);
43 scm_gc_mark (me->translator_mods_);
44 scm_gc_mark (me->property_ops_);
45 scm_gc_mark (me->translator_group_type_);
47 return me->context_name_;
51 Context_def::Context_def ()
53 context_aliases_ = SCM_EOL;
54 translator_group_type_ = SCM_EOL;
55 accept_mods_ = SCM_EOL;
56 translator_mods_ = SCM_EOL;
57 property_ops_ = SCM_EOL;
58 context_name_ = SCM_EOL;
59 description_ = SCM_EOL;
63 context_name_ = ly_symbol2scm ("");
66 Context_def::~Context_def ()
70 Context_def::Context_def (Context_def const & s)
73 context_aliases_ = SCM_EOL;
74 translator_group_type_ = SCM_EOL;
75 accept_mods_ = SCM_EOL;
76 translator_mods_ = SCM_EOL;
77 property_ops_ = SCM_EOL;
78 context_name_ = SCM_EOL;
79 description_ = SCM_EOL;
82 description_ = s.description_;
84 accept_mods_ = s.accept_mods_;
85 property_ops_ = s.property_ops_;
86 translator_mods_ = s.translator_mods_;
87 context_aliases_ = s.context_aliases_;
88 translator_group_type_ = s.translator_group_type_;
89 context_name_ = s.context_name_;
94 Context_def::add_context_mod (SCM mod)
96 SCM tag = gh_car (mod);
97 if (ly_symbol2scm ("description") == tag)
99 description_ = gh_cadr (mod);
103 SCM sym = gh_cadr (mod);
104 if (gh_string_p (sym))
105 sym = scm_string_to_symbol (sym);
107 if (ly_symbol2scm ("consists") == tag
108 || ly_symbol2scm ("consists-end") == tag
109 || ly_symbol2scm ("remove") == tag)
111 if (!get_translator (sym))
112 error (_f ("Program has no such type: `%s'", ly_symbol2string (sym).to_str0 ()));
114 translator_mods_ = gh_cons (scm_list_2 (tag, sym), translator_mods_ );
116 else if (ly_symbol2scm ("accepts") == tag
117 || ly_symbol2scm ("denies") == tag)
119 accept_mods_ = gh_cons (scm_list_2 (tag, sym), accept_mods_);
121 else if (ly_symbol2scm ("poppush") == tag
122 || ly_symbol2scm ("pop") == tag
123 || ly_symbol2scm ("push") == tag
124 || ly_symbol2scm ("assign") == tag
125 || ly_symbol2scm ("unset") == tag)
127 property_ops_ = gh_cons (mod, property_ops_);
129 else if (ly_symbol2scm ("alias") == tag)
131 context_aliases_ = gh_cons (sym, context_aliases_);
133 else if (ly_symbol2scm ("translator-type") == tag)
135 translator_group_type_ = sym;
137 else if (ly_symbol2scm ("context-name") == tag)
143 programming_error ("Unknown context mod tag.");
150 Context_def::get_context_name () const
152 return context_name_;
156 Context_def::get_accepted (SCM user_mod) const
158 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_),
161 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
163 SCM tag = gh_caar (s);
164 SCM sym = gh_cadar (s);
165 if (tag == ly_symbol2scm ("accepts"))
166 acc = gh_cons (sym, acc);
167 else if (tag == ly_symbol2scm ("denies"))
168 acc = scm_delete_x (sym, acc);
174 Link_array<Context_def>
175 Context_def::path_to_acceptable_context (SCM type_sym, Music_output_def* odef) const
177 assert (gh_symbol_p (type_sym));
179 SCM accepted = get_accepted (SCM_EOL);
181 Link_array<Context_def> accepteds;
182 for (SCM s = accepted; gh_pair_p (s); s = ly_cdr (s))
184 Context_def *t = unsmob_context_def (odef->find_context_def (ly_car (s)));
190 Link_array<Context_def> best_result;
191 for (int i=0; i < accepteds.size (); i++)
194 don't check aliases, because \context Staff should not create RhythmicStaff.
196 if (gh_equal_p (accepteds[i]->get_context_name (), type_sym))
198 best_result.push (accepteds[i]);
203 int best_depth= INT_MAX;
204 for (int i=0; i < accepteds.size (); i++)
206 Context_def * g = accepteds[i];
208 Link_array<Context_def> result
209 = g->path_to_acceptable_context (type_sym, odef);
210 if (result.size () && result.size () < best_depth)
213 best_result = result;
216 this following line was added in 1.9.3, but hsould've been
217 there all along... Let's hope it doesn't cause nightmares.
219 best_depth = result.size ();
226 IMPLEMENT_SMOBS (Context_def);
227 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
231 Context_def::get_translator_names (SCM user_mod) const
236 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_),
239 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
241 SCM tag = gh_caar (s);
242 SCM arg = gh_cadar (s);
244 if (gh_string_p (arg))
245 arg = scm_string_to_symbol (arg);
247 if (ly_symbol2scm ("consists") == tag)
248 l1 = gh_cons (arg, l1);
249 else if (ly_symbol2scm ("consists-end") == tag)
250 l2 = gh_cons (arg, l2);
251 else if (ly_symbol2scm ("remove") == tag)
253 l1 = scm_delete_x (arg, l1);
254 l2 = scm_delete_x (arg, l2);
258 return scm_append_x (scm_list_2 (l1, l2));
263 Context_def::instantiate (SCM ops)
267 if (context_name_ == ly_symbol2scm ("Score"))
268 tg = new Score_context ();
272 tg->definition_ = self_scm ();
274 SCM trans_names = get_translator_names (ops);
276 Translator * g = get_translator (translator_group_type_);
279 g->simple_trans_list_ = names_to_translators (trans_names, tg);
280 tg->implementation_ = g->self_scm ();
281 g->daddy_context_ = tg;
282 tg->aliases_ = context_aliases_ ;
284 scm_gc_unprotect_object (g->self_scm ());
286 tg->accepts_list_ = get_accepted (ops);
293 Context_def::clone_scm () const
295 Context_def * t = new Context_def (*this);
297 SCM x = t->self_scm ();
298 scm_gc_unprotect_object (x);
303 Context_def::make_scm ()
305 Context_def* t = new Context_def;
307 SCM x =t->self_scm ();
308 scm_gc_unprotect_object (x);
313 Context_def::apply_default_property_operations (Context *tg)
315 apply_property_operations (tg , property_ops_);
319 Context_def::to_alist () const
323 l = gh_cons (gh_cons (ly_symbol2scm ("consists"),
324 get_translator_names (SCM_EOL)), l);
325 l = gh_cons (gh_cons (ly_symbol2scm ("description"), description_), l);
326 l = gh_cons (gh_cons (ly_symbol2scm ("aliases"), context_aliases_), l);
327 l = gh_cons (gh_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)), l);
328 l = gh_cons (gh_cons (ly_symbol2scm ("property-ops"), property_ops_), l);
329 l = gh_cons (gh_cons (ly_symbol2scm ("context-name"), context_name_), l);
331 if (gh_symbol_p (translator_group_type_))
332 l = gh_cons (gh_cons (ly_symbol2scm ("group-type"), translator_group_type_), l);