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>
10 #include "lily-proto.hh"
11 #include "context-def.hh"
12 #include "translator-group.hh"
14 #include "music-output-def.hh"
15 #include "ly-smobs.icc"
16 #include "score-context.hh"
19 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
21 Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
23 scm_puts ("#<Context_def ", port);
24 scm_display (me->context_name_, port);
31 Context_def::mark_smob (SCM smob)
33 Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
35 scm_gc_mark (me->description_);
36 scm_gc_mark (me->context_aliases_);
37 scm_gc_mark (me->accept_mods_);
38 scm_gc_mark (me->translator_mods_);
39 scm_gc_mark (me->property_ops_);
40 scm_gc_mark (me->translator_group_type_);
41 return me->context_name_;
45 Context_def::Context_def ()
47 context_aliases_ = SCM_EOL;
48 translator_group_type_ = SCM_EOL;
49 accept_mods_ = SCM_EOL;
50 translator_mods_ = SCM_EOL;
51 property_ops_ = SCM_EOL;
52 context_name_ = SCM_EOL;
53 description_ = SCM_EOL;
58 Context_def::~Context_def ()
62 Context_def::Context_def (Context_def const & s)
65 context_aliases_ = SCM_EOL;
66 translator_group_type_ = SCM_EOL;
67 accept_mods_ = SCM_EOL;
68 translator_mods_ = SCM_EOL;
69 property_ops_ = SCM_EOL;
70 context_name_ = SCM_EOL;
71 description_ = SCM_EOL;
74 description_ = s.description_;
76 accept_mods_ = s.accept_mods_;
77 property_ops_ = s.property_ops_;
78 translator_mods_ = s.translator_mods_;
79 context_aliases_ = s.context_aliases_;
80 translator_group_type_ = s.translator_group_type_;
81 context_name_ = s.context_name_;
86 Context_def::add_context_mod (SCM mod)
88 SCM tag = gh_car (mod);
89 if (ly_symbol2scm ("description") == tag)
91 description_ = gh_cadr (mod);
95 SCM sym = gh_cadr (mod);
96 if (gh_string_p (sym))
97 sym = scm_string_to_symbol (sym);
99 if (ly_symbol2scm ("consists") == tag
100 || ly_symbol2scm ("consists-end") == tag
101 || ly_symbol2scm ("remove") == tag)
103 if (!get_translator (sym))
104 error (_f ("Program has no such type: `%s'", ly_symbol2string (sym).to_str0 ()));
106 translator_mods_ = gh_cons (scm_list_2 (tag, sym), translator_mods_ );
108 else if (ly_symbol2scm ("accepts") == tag
109 || ly_symbol2scm ("denies") == tag)
111 accept_mods_ = gh_cons (scm_list_2 (tag, sym), accept_mods_);
113 else if (ly_symbol2scm ("poppush") == tag
114 || ly_symbol2scm ("pop") == tag
115 || ly_symbol2scm ("push") == tag
116 || ly_symbol2scm ("assign") == tag
117 || ly_symbol2scm ("unset") == tag)
119 property_ops_ = gh_cons (mod, property_ops_);
121 else if (ly_symbol2scm ("alias") == tag)
123 context_aliases_ = gh_cons (sym, context_aliases_);
125 else if (ly_symbol2scm ("translator-type") == tag)
127 translator_group_type_ = sym;
129 else if (ly_symbol2scm ("context-name") == tag)
135 programming_error ("Unknown context mod tag.");
142 Context_def::get_context_name () const
144 return context_name_;
148 Context_def::get_accepted (SCM user_mod) const
150 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_),
153 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
155 SCM tag = gh_caar (s);
156 SCM sym = gh_cadar (s);
157 if (tag == ly_symbol2scm ("accepts"))
158 acc = gh_cons (sym, acc);
159 else if (tag == ly_symbol2scm ("denies"))
160 acc = scm_delete_x (sym, acc);
166 Link_array<Context_def>
167 Context_def::path_to_acceptable_context (SCM type_sym, Music_output_def* odef) const
169 assert (gh_symbol_p (type_sym));
171 SCM accepted = get_accepted (SCM_EOL);
173 Link_array<Context_def> accepteds;
174 for (SCM s = accepted; gh_pair_p (s); s = ly_cdr (s))
176 Context_def *t = unsmob_context_def (odef->find_context_def (ly_car (s)));
182 Link_array<Context_def> best_result;
183 for (int i=0; i < accepteds.size (); i++)
186 don't check aliases, because \context Staff should not create RhythmicStaff.
188 if (gh_equal_p (accepteds[i]->get_context_name (), type_sym))
190 best_result.push (accepteds[i]);
195 int best_depth= INT_MAX;
196 for (int i=0; i < accepteds.size (); i++)
198 Context_def * g = accepteds[i];
200 Link_array<Context_def> result
201 = g->path_to_acceptable_context (type_sym, odef);
202 if (result.size () && result.size () < best_depth)
205 best_result = result;
208 this following line was added in 1.9.3, but hsould've been
209 there all along... Let's hope it doesn't cause nightmares.
211 best_depth = result.size();
218 IMPLEMENT_SMOBS (Context_def);
219 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
223 Context_def::get_translator_names (SCM user_mod) const
228 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_),
231 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
233 SCM tag = gh_caar (s);
234 SCM arg = gh_cadar (s);
236 if (gh_string_p (arg))
237 arg = scm_string_to_symbol (arg);
239 if (ly_symbol2scm ("consists") == tag)
240 l1 = gh_cons (arg, l1);
241 else if (ly_symbol2scm ("consists-end") == tag)
242 l2 = gh_cons (arg, l2);
243 else if (ly_symbol2scm ("remove") == tag)
245 l1 = scm_delete_x (arg, l1);
246 l2 = scm_delete_x (arg, l2);
250 return scm_append_x (scm_list_2 (l1, l2));
255 Context_def::instantiate (SCM ops)
259 if (context_name_ == ly_symbol2scm ("Score"))
260 tg = new Score_context ();
265 tg->definition_ = self_scm ();
267 SCM trans_names = get_translator_names (ops);
269 Translator * g = get_translator (translator_group_type_);
272 g->simple_trans_list_ = names_to_translators (trans_names, tg);
273 tg->implementation_ = g->self_scm ();
274 g->daddy_context_ = tg;
276 scm_gc_unprotect_object (g->self_scm ());
278 tg->accepts_list_ = get_accepted (ops);
285 Context_def::clone_scm () const
287 Context_def * t = new Context_def (*this);
288 scm_gc_unprotect_object (t->self_scm());
289 return t->self_scm();
293 Context_def::make_scm ()
295 Context_def* t = new Context_def;
296 scm_gc_unprotect_object (t->self_scm());
297 return t->self_scm();
301 Context_def::apply_default_property_operations (Context *tg)
303 apply_property_operations (tg , property_ops_);
307 Context_def::to_alist () const
311 l = gh_cons (gh_cons (ly_symbol2scm ("consists"),
312 get_translator_names (SCM_EOL)), l);
313 l = gh_cons (gh_cons (ly_symbol2scm ("description"), description_), l);
314 l = gh_cons (gh_cons (ly_symbol2scm ("aliases"), context_aliases_), l);
315 l = gh_cons (gh_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)), l);
316 l = gh_cons (gh_cons (ly_symbol2scm ("property-ops"), property_ops_), l);
317 l = gh_cons (gh_cons (ly_symbol2scm ("context-name"), context_name_), l);
318 l = gh_cons (gh_cons (ly_symbol2scm ("group-type"), translator_group_type_), l);
324 Context_def::is_alias (SCM sym) const
326 bool b = sym == context_name_;
328 for (SCM a = context_aliases_; !b && gh_pair_p (a); a = ly_cdr (a))
329 b = b || sym == ly_car (a);