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;
64 Context_def::~Context_def ()
68 Context_def::Context_def (Context_def const & s)
71 context_aliases_ = SCM_EOL;
72 translator_group_type_ = SCM_EOL;
73 accept_mods_ = SCM_EOL;
74 translator_mods_ = SCM_EOL;
75 property_ops_ = SCM_EOL;
76 context_name_ = SCM_EOL;
77 description_ = SCM_EOL;
80 description_ = s.description_;
82 accept_mods_ = s.accept_mods_;
83 property_ops_ = s.property_ops_;
84 translator_mods_ = s.translator_mods_;
85 context_aliases_ = s.context_aliases_;
86 translator_group_type_ = s.translator_group_type_;
87 context_name_ = s.context_name_;
92 Context_def::add_context_mod (SCM mod)
94 SCM tag = gh_car (mod);
95 if (ly_symbol2scm ("description") == tag)
97 description_ = gh_cadr (mod);
101 SCM sym = gh_cadr (mod);
102 if (gh_string_p (sym))
103 sym = scm_string_to_symbol (sym);
105 if (ly_symbol2scm ("consists") == tag
106 || ly_symbol2scm ("consists-end") == tag
107 || ly_symbol2scm ("remove") == tag)
109 if (!get_translator (sym))
110 error (_f ("Program has no such type: `%s'", ly_symbol2string (sym).to_str0 ()));
112 translator_mods_ = gh_cons (scm_list_2 (tag, sym), translator_mods_ );
114 else if (ly_symbol2scm ("accepts") == tag
115 || ly_symbol2scm ("denies") == tag)
117 accept_mods_ = gh_cons (scm_list_2 (tag, sym), accept_mods_);
119 else if (ly_symbol2scm ("poppush") == tag
120 || ly_symbol2scm ("pop") == tag
121 || ly_symbol2scm ("push") == tag
122 || ly_symbol2scm ("assign") == tag
123 || ly_symbol2scm ("unset") == tag)
125 property_ops_ = gh_cons (mod, property_ops_);
127 else if (ly_symbol2scm ("alias") == tag)
129 context_aliases_ = gh_cons (sym, context_aliases_);
131 else if (ly_symbol2scm ("translator-type") == tag)
133 translator_group_type_ = sym;
135 else if (ly_symbol2scm ("context-name") == tag)
141 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_),
159 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
161 SCM tag = gh_caar (s);
162 SCM sym = gh_cadar (s);
163 if (tag == ly_symbol2scm ("accepts"))
164 acc = gh_cons (sym, acc);
165 else if (tag == ly_symbol2scm ("denies"))
166 acc = scm_delete_x (sym, acc);
172 Link_array<Context_def>
173 Context_def::path_to_acceptable_context (SCM type_sym, Music_output_def* odef) const
175 assert (gh_symbol_p (type_sym));
177 SCM accepted = get_accepted (SCM_EOL);
179 Link_array<Context_def> accepteds;
180 for (SCM s = accepted; gh_pair_p (s); s = ly_cdr (s))
182 Context_def *t = unsmob_context_def (odef->find_context_def (ly_car (s)));
188 Link_array<Context_def> best_result;
189 for (int i=0; i < accepteds.size (); i++)
192 don't check aliases, because \context Staff should not create RhythmicStaff.
194 if (gh_equal_p (accepteds[i]->get_context_name (), type_sym))
196 best_result.push (accepteds[i]);
201 int best_depth= INT_MAX;
202 for (int i=0; i < accepteds.size (); i++)
204 Context_def * g = accepteds[i];
206 Link_array<Context_def> result
207 = g->path_to_acceptable_context (type_sym, odef);
208 if (result.size () && result.size () < best_depth)
211 best_result = result;
214 this following line was added in 1.9.3, but hsould've been
215 there all along... Let's hope it doesn't cause nightmares.
217 best_depth = result.size ();
224 IMPLEMENT_SMOBS (Context_def);
225 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
229 Context_def::get_translator_names (SCM user_mod) const
234 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_),
237 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
239 SCM tag = gh_caar (s);
240 SCM arg = gh_cadar (s);
242 if (gh_string_p (arg))
243 arg = scm_string_to_symbol (arg);
245 if (ly_symbol2scm ("consists") == tag)
246 l1 = gh_cons (arg, l1);
247 else if (ly_symbol2scm ("consists-end") == tag)
248 l2 = gh_cons (arg, l2);
249 else if (ly_symbol2scm ("remove") == tag)
251 l1 = scm_delete_x (arg, l1);
252 l2 = scm_delete_x (arg, l2);
256 return scm_append_x (scm_list_2 (l1, l2));
261 Context_def::instantiate (SCM ops)
265 if (context_name_ == ly_symbol2scm ("Score"))
266 tg = new Score_context ();
271 tg->definition_ = self_scm ();
273 SCM trans_names = get_translator_names (ops);
275 Translator * g = get_translator (translator_group_type_);
278 g->simple_trans_list_ = names_to_translators (trans_names, tg);
279 tg->implementation_ = g->self_scm ();
280 g->daddy_context_ = tg;
282 scm_gc_unprotect_object (g->self_scm ());
284 tg->accepts_list_ = get_accepted (ops);
291 Context_def::clone_scm () const
293 Context_def * t = new Context_def (*this);
295 SCM x = t->self_scm ();
296 scm_gc_unprotect_object (x);
301 Context_def::make_scm ()
303 Context_def* t = new Context_def;
305 SCM x =t->self_scm ();
306 scm_gc_unprotect_object (x);
311 Context_def::apply_default_property_operations (Context *tg)
313 apply_property_operations (tg , property_ops_);
317 Context_def::to_alist () const
321 l = gh_cons (gh_cons (ly_symbol2scm ("consists"),
322 get_translator_names (SCM_EOL)), l);
323 l = gh_cons (gh_cons (ly_symbol2scm ("description"), description_), l);
324 l = gh_cons (gh_cons (ly_symbol2scm ("aliases"), context_aliases_), l);
325 l = gh_cons (gh_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)), l);
326 l = gh_cons (gh_cons (ly_symbol2scm ("property-ops"), property_ops_), l);
327 l = gh_cons (gh_cons (ly_symbol2scm ("context-name"), context_name_), l);
329 if (gh_symbol_p (translator_group_type_))
330 l = gh_cons (gh_cons (ly_symbol2scm ("group-type"), translator_group_type_), l);
336 Context_def::is_alias (SCM sym) const
338 bool b = sym == context_name_;
340 for (SCM a = context_aliases_; !b && gh_pair_p (a); a = ly_cdr (a))
341 b = b || sym == ly_car (a);