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_);
42 return me->context_name_;
46 Context_def::Context_def ()
48 context_aliases_ = SCM_EOL;
49 translator_group_type_ = SCM_EOL;
50 accept_mods_ = SCM_EOL;
51 translator_mods_ = SCM_EOL;
52 property_ops_ = SCM_EOL;
53 context_name_ = SCM_EOL;
54 description_ = SCM_EOL;
59 Context_def::~Context_def ()
63 Context_def::Context_def (Context_def const & s)
66 context_aliases_ = SCM_EOL;
67 translator_group_type_ = SCM_EOL;
68 accept_mods_ = SCM_EOL;
69 translator_mods_ = SCM_EOL;
70 property_ops_ = SCM_EOL;
71 context_name_ = SCM_EOL;
72 description_ = SCM_EOL;
75 description_ = s.description_;
77 accept_mods_ = s.accept_mods_;
78 property_ops_ = s.property_ops_;
79 translator_mods_ = s.translator_mods_;
80 context_aliases_ = s.context_aliases_;
81 translator_group_type_ = s.translator_group_type_;
82 context_name_ = s.context_name_;
87 Context_def::add_context_mod (SCM mod)
89 SCM tag = gh_car (mod);
90 if (ly_symbol2scm ("description") == tag)
92 description_ = gh_cadr (mod);
96 SCM sym = gh_cadr (mod);
97 if (gh_string_p (sym))
98 sym = scm_string_to_symbol (sym);
100 if (ly_symbol2scm ("consists") == tag
101 || ly_symbol2scm ("consists-end") == tag
102 || ly_symbol2scm ("remove") == tag)
104 if (!get_translator (sym))
105 error (_f ("Program has no such type: `%s'", ly_symbol2string (sym).to_str0 ()));
107 translator_mods_ = gh_cons (scm_list_2 (tag, sym), translator_mods_ );
109 else if (ly_symbol2scm ("accepts") == tag
110 || ly_symbol2scm ("denies") == tag)
112 accept_mods_ = gh_cons (scm_list_2 (tag, sym), accept_mods_);
114 else if (ly_symbol2scm ("poppush") == tag
115 || ly_symbol2scm ("pop") == tag
116 || ly_symbol2scm ("push") == tag
117 || ly_symbol2scm ("assign") == tag
118 || ly_symbol2scm ("unset") == tag)
120 property_ops_ = gh_cons (mod, property_ops_);
122 else if (ly_symbol2scm ("alias") == tag)
124 context_aliases_ = gh_cons (sym, context_aliases_);
126 else if (ly_symbol2scm ("translator-type") == tag)
128 translator_group_type_ = sym;
130 else if (ly_symbol2scm ("context-name") == tag)
136 programming_error ("Unknown context mod tag.");
143 Context_def::get_context_name () const
145 return context_name_;
149 Context_def::get_accepted (SCM user_mod) const
151 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_),
154 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
156 SCM tag = gh_caar (s);
157 SCM sym = gh_cadar (s);
158 if (tag == ly_symbol2scm ("accepts"))
159 acc = gh_cons (sym, acc);
160 else if (tag == ly_symbol2scm ("denies"))
161 acc = scm_delete_x (sym, acc);
167 Link_array<Context_def>
168 Context_def::path_to_acceptable_context (SCM type_sym, Music_output_def* odef) const
170 assert (gh_symbol_p (type_sym));
172 SCM accepted = get_accepted (SCM_EOL);
174 Link_array<Context_def> accepteds;
175 for (SCM s = accepted; gh_pair_p (s); s = ly_cdr (s))
177 Context_def *t = unsmob_context_def (odef->find_context_def (ly_car (s)));
183 Link_array<Context_def> best_result;
184 for (int i=0; i < accepteds.size (); i++)
187 don't check aliases, because \context Staff should not create RhythmicStaff.
189 if (gh_equal_p (accepteds[i]->get_context_name (), type_sym))
191 best_result.push (accepteds[i]);
196 int best_depth= INT_MAX;
197 for (int i=0; i < accepteds.size (); i++)
199 Context_def * g = accepteds[i];
201 Link_array<Context_def> result
202 = g->path_to_acceptable_context (type_sym, odef);
203 if (result.size () && result.size () < best_depth)
206 best_result = result;
209 this following line was added in 1.9.3, but hsould've been
210 there all along... Let's hope it doesn't cause nightmares.
212 best_depth = result.size();
219 IMPLEMENT_SMOBS (Context_def);
220 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
224 Context_def::get_translator_names (SCM user_mod) const
229 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_),
232 for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
234 SCM tag = gh_caar (s);
235 SCM arg = gh_cadar (s);
237 if (gh_string_p (arg))
238 arg = scm_string_to_symbol (arg);
240 if (ly_symbol2scm ("consists") == tag)
241 l1 = gh_cons (arg, l1);
242 else if (ly_symbol2scm ("consists-end") == tag)
243 l2 = gh_cons (arg, l2);
244 else if (ly_symbol2scm ("remove") == tag)
246 l1 = scm_delete_x (arg, l1);
247 l2 = scm_delete_x (arg, l2);
251 return scm_append_x (scm_list_2 (l1, l2));
256 Context_def::instantiate (SCM ops)
260 if (context_name_ == ly_symbol2scm ("Score"))
261 tg = new Score_context ();
266 tg->definition_ = self_scm ();
268 SCM trans_names = get_translator_names (ops);
270 Translator * g = get_translator (translator_group_type_);
273 g->simple_trans_list_ = names_to_translators (trans_names, tg);
274 tg->implementation_ = g->self_scm ();
275 g->daddy_context_ = tg;
277 scm_gc_unprotect_object (g->self_scm ());
279 tg->accepts_list_ = get_accepted (ops);
286 Context_def::clone_scm () const
288 Context_def * t = new Context_def (*this);
290 SCM x = t->self_scm();
291 scm_gc_unprotect_object (x);
296 Context_def::make_scm ()
298 Context_def* t = new Context_def;
300 SCM x =t->self_scm();
301 scm_gc_unprotect_object (x);
306 Context_def::apply_default_property_operations (Context *tg)
308 apply_property_operations (tg , property_ops_);
312 Context_def::to_alist () const
316 l = gh_cons (gh_cons (ly_symbol2scm ("consists"),
317 get_translator_names (SCM_EOL)), l);
318 l = gh_cons (gh_cons (ly_symbol2scm ("description"), description_), l);
319 l = gh_cons (gh_cons (ly_symbol2scm ("aliases"), context_aliases_), l);
320 l = gh_cons (gh_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)), l);
321 l = gh_cons (gh_cons (ly_symbol2scm ("property-ops"), property_ops_), l);
322 l = gh_cons (gh_cons (ly_symbol2scm ("context-name"), context_name_), l);
324 if (gh_symbol_p (translator_group_type_))
325 l = gh_cons (gh_cons (ly_symbol2scm ("group-type"), translator_group_type_), l);
331 Context_def::is_alias (SCM sym) const
333 bool b = sym == context_name_;
335 for (SCM a = context_aliases_; !b && gh_pair_p (a); a = ly_cdr (a))
336 b = b || sym == ly_car (a);