2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2000--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 /* TODO: should junk this class an replace by
21 a single list of context modifications? */
23 #include "context-def.hh"
26 #include "international.hh"
27 #include "output-def.hh"
28 #include "translator.hh"
31 Context_def::Context_def ()
33 context_aliases_ = SCM_EOL;
34 translator_group_type_ = SCM_EOL;
35 accept_mods_ = SCM_EOL;
36 translator_mods_ = SCM_EOL;
37 property_ops_ = SCM_EOL;
38 context_name_ = SCM_EOL;
39 default_child_ = SCM_EOL;
40 description_ = SCM_EOL;
41 input_location_ = SCM_EOL;
45 input_location_ = make_input (Input ());
46 context_name_ = ly_symbol2scm ("");
50 Context_def::origin () const
52 return unsmob_input (input_location_);
55 Context_def::Context_def (Context_def const &s)
57 context_aliases_ = SCM_EOL;
58 translator_group_type_ = SCM_EOL;
59 accept_mods_ = SCM_EOL;
60 translator_mods_ = SCM_EOL;
61 property_ops_ = SCM_EOL;
62 context_name_ = SCM_EOL;
63 description_ = SCM_EOL;
64 default_child_ = SCM_EOL;
65 input_location_ = SCM_EOL;
68 description_ = s.description_;
69 input_location_ = make_input (*s.origin ());
70 default_child_ = s.default_child_;
71 accept_mods_ = s.accept_mods_;
72 property_ops_ = s.property_ops_;
73 translator_mods_ = s.translator_mods_;
74 context_aliases_ = s.context_aliases_;
75 translator_group_type_ = s.translator_group_type_;
76 context_name_ = s.context_name_;
79 Context_def::~Context_def ()
83 #include "ly-smobs.icc"
84 IMPLEMENT_SMOBS (Context_def);
85 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
88 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
90 Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
92 scm_puts ("#<Context_def ", port);
93 scm_display (me->context_name_, port);
99 Context_def::mark_smob (SCM smob)
101 ASSERT_LIVE_IS_ALLOWED ();
103 Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
105 scm_gc_mark (me->description_);
106 scm_gc_mark (me->context_aliases_);
107 scm_gc_mark (me->accept_mods_);
108 scm_gc_mark (me->translator_mods_);
109 scm_gc_mark (me->property_ops_);
110 scm_gc_mark (me->translator_group_type_);
111 scm_gc_mark (me->default_child_);
112 scm_gc_mark (me->input_location_);
114 return me->context_name_;
118 Context_def::add_context_mod (SCM mod)
120 SCM tag = scm_car (mod);
121 if (ly_symbol2scm ("description") == tag)
123 description_ = scm_cadr (mod);
128 other modifiers take symbols as argument.
130 SCM sym = scm_cadr (mod);
131 if (scm_is_string (sym))
132 sym = scm_string_to_symbol (sym);
134 if (ly_symbol2scm ("default-child") == tag)
135 default_child_ = sym;
136 else if (ly_symbol2scm ("consists") == tag
137 || ly_symbol2scm ("remove") == tag)
139 if (!get_translator (sym))
140 warning (_f ("program has no such type: `%s'",
141 ly_symbol2string (sym).c_str ()));
143 translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
145 else if (ly_symbol2scm ("accepts") == tag
146 || ly_symbol2scm ("denies") == tag)
147 accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
148 else if (ly_symbol2scm ("pop") == tag
149 || ly_symbol2scm ("push") == tag
150 || ly_symbol2scm ("assign") == tag
151 || ly_symbol2scm ("unset") == tag)
152 property_ops_ = scm_cons (mod, property_ops_);
153 else if (ly_symbol2scm ("alias") == tag)
154 context_aliases_ = scm_cons (sym, context_aliases_);
155 else if (ly_symbol2scm ("translator-type") == tag)
156 translator_group_type_ = sym;
157 else if (ly_symbol2scm ("context-name") == tag)
160 programming_error ("unknown context mod tag");
164 Context_def::get_accepted (SCM user_mod) const
166 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
168 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
170 SCM tag = scm_caar (s);
171 SCM sym = scm_cadar (s);
172 if (tag == ly_symbol2scm ("accepts"))
173 acc = scm_cons (sym, acc);
174 else if (tag == ly_symbol2scm ("denies"))
175 acc = scm_delete_x (sym, acc);
178 SCM def = get_default_child (user_mod);
179 if (scm_is_symbol (def))
181 if (scm_memq (def, acc))
182 acc = scm_delete_x (def, acc);
183 acc = scm_cons (def, acc);
190 Context_def::get_default_child (SCM user_mod) const
192 SCM name = default_child_;
193 for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
195 SCM entry = scm_car (s);
196 if (scm_car (entry) == ly_symbol2scm ("default-child"))
198 name = scm_cadr (entry);
207 Given a name of a context that we want to create, finds a list of context
208 definitions such that:
209 - the first element in the list defines a context that is a valid child of
210 the context defined by this Context_def
211 - each subsequent element in the list defines a context that is a valid child
212 of the the context defined by the preceding element in the list
213 - the last element in the list defines a context with the given name
215 The ADDITIONAL_ACCEPTS parameter is a list of additional contexts that this
216 specific context def (but not any of the child context defs) should accept.
218 vector<Context_def *>
219 Context_def::path_to_acceptable_context (SCM type_sym,
221 SCM additional_accepts) const
223 set<const Context_def *> seen;
224 return internal_path_to_acceptable_context (type_sym, odef, additional_accepts, &seen);
228 The SEEN parameter is a set which keeps track of visited contexts, allowing
229 contexts of the same type to be nested.
232 Context_def::internal_path_to_acceptable_context (SCM type_sym,
234 SCM additional_accepts,
235 set<const Context_def *> *seen) const
237 assert (scm_is_symbol (type_sym));
239 SCM accepted = get_accepted (additional_accepts);
241 vector<Context_def*> accepteds;
242 for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
243 if (Context_def *t = unsmob_context_def (find_context_def (odef,
245 accepteds.push_back (t);
247 vector<Context_def*> best_result;
248 for (vsize i = 0; i < accepteds.size (); i++)
250 /* do not check aliases, because \context Staff should not
251 create RhythmicStaff. */
252 if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
254 best_result.push_back (accepteds[i]);
260 vsize best_depth = INT_MAX;
261 for (vsize i = 0; i < accepteds.size (); i++)
263 Context_def *g = accepteds[i];
265 if (!seen->count (g))
267 vector<Context_def*> result
268 = g->internal_path_to_acceptable_context (type_sym, odef, SCM_EOL, seen);
269 if (result.size () && result.size () < best_depth)
271 best_depth = result.size ();
272 result.insert (result.begin (), g);
273 best_result = result;
283 Context_def::get_translator_names (SCM user_mod) const
287 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
289 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
291 SCM tag = scm_caar (s);
292 SCM arg = scm_cadar (s);
294 if (scm_is_string (arg))
295 arg = scm_string_to_symbol (arg);
297 if (ly_symbol2scm ("consists") == tag)
298 l1 = scm_cons (arg, l1);
299 else if (ly_symbol2scm ("remove") == tag
300 && get_translator (arg))
301 l1 = scm_delete_x (arg, l1);
308 Context_def::instantiate (SCM ops)
310 Context *context = new Context ();
312 context->definition_ = self_scm ();
313 context->definition_mods_ = ops;
314 context->aliases_ = context_aliases_;
315 context->accepts_list_ = get_accepted (ops);
321 Context_def::make_scm ()
323 Context_def *t = new Context_def;
324 return t->unprotect ();
328 Context_def::apply_default_property_operations (Context *tg)
330 apply_property_operations (tg, property_ops_);
334 Context_def::to_alist () const
338 ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
339 get_translator_names (SCM_EOL)), ell);
340 ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
341 ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
342 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
344 ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
346 ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
349 if (scm_is_symbol (translator_group_type_))
350 ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
351 translator_group_type_), ell);