2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2000--2012 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 "context-mod.hh"
27 #include "international.hh"
28 #include "output-def.hh"
29 #include "translator.hh"
32 Context_def::Context_def ()
34 context_aliases_ = SCM_EOL;
35 translator_group_type_ = SCM_EOL;
36 accept_mods_ = SCM_EOL;
37 translator_mods_ = SCM_EOL;
38 property_ops_ = SCM_EOL;
39 context_name_ = SCM_EOL;
40 default_child_ = SCM_EOL;
41 description_ = SCM_EOL;
42 input_location_ = SCM_EOL;
46 input_location_ = make_input (Input ());
47 context_name_ = ly_symbol2scm ("");
51 Context_def::origin () const
53 return unsmob_input (input_location_);
56 Context_def::Context_def (Context_def const &s)
58 context_aliases_ = SCM_EOL;
59 translator_group_type_ = SCM_EOL;
60 accept_mods_ = SCM_EOL;
61 translator_mods_ = SCM_EOL;
62 property_ops_ = SCM_EOL;
63 context_name_ = SCM_EOL;
64 description_ = SCM_EOL;
65 default_child_ = SCM_EOL;
66 input_location_ = SCM_EOL;
69 description_ = s.description_;
70 input_location_ = make_input (*s.origin ());
71 default_child_ = s.default_child_;
72 accept_mods_ = s.accept_mods_;
73 property_ops_ = s.property_ops_;
74 translator_mods_ = s.translator_mods_;
75 context_aliases_ = s.context_aliases_;
76 translator_group_type_ = s.translator_group_type_;
77 context_name_ = s.context_name_;
80 Context_def::~Context_def ()
84 #include "ly-smobs.icc"
85 IMPLEMENT_SMOBS (Context_def);
86 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
87 IMPLEMENT_TYPE_P (Context_def, "ly:context-def?");
90 Context_def::print_smob (SCM smob, SCM port, scm_print_state *)
92 Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
94 scm_puts ("#<Context_def ", port);
95 scm_display (me->context_name_, port);
97 string loc = me->origin ()->location_string ();
98 scm_puts (loc.c_str (), port);
104 Context_def::mark_smob (SCM smob)
106 ASSERT_LIVE_IS_ALLOWED (smob);
108 Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
110 scm_gc_mark (me->description_);
111 scm_gc_mark (me->context_aliases_);
112 scm_gc_mark (me->accept_mods_);
113 scm_gc_mark (me->translator_mods_);
114 scm_gc_mark (me->property_ops_);
115 scm_gc_mark (me->translator_group_type_);
116 scm_gc_mark (me->default_child_);
117 scm_gc_mark (me->input_location_);
119 return me->context_name_;
123 Context_def::add_context_mod (SCM mod)
125 SCM tag = scm_car (mod);
126 if (ly_symbol2scm ("description") == tag)
128 description_ = scm_cadr (mod);
133 other modifiers take symbols as argument.
135 SCM sym = scm_cadr (mod);
136 if (scm_is_string (sym))
137 sym = scm_string_to_symbol (sym);
139 if (ly_symbol2scm ("default-child") == tag)
140 default_child_ = sym;
141 else if (ly_symbol2scm ("consists") == tag
142 || ly_symbol2scm ("remove") == tag)
144 translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
146 else if (ly_symbol2scm ("accepts") == tag
147 || ly_symbol2scm ("denies") == tag)
148 accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
149 else if (ly_symbol2scm ("pop") == tag
150 || ly_symbol2scm ("push") == tag
151 || ly_symbol2scm ("assign") == tag
152 || ly_symbol2scm ("unset") == tag
153 || ly_symbol2scm ("apply") == tag)
154 property_ops_ = scm_cons (mod, property_ops_);
155 else if (ly_symbol2scm ("alias") == tag)
156 context_aliases_ = scm_cons (sym, context_aliases_);
157 else if (ly_symbol2scm ("translator-type") == tag)
158 translator_group_type_ = sym;
159 else if (ly_symbol2scm ("context-name") == tag)
162 programming_error ("unknown context mod tag");
166 Context_def::get_accepted (SCM user_mod) const
168 SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
170 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
172 SCM tag = scm_caar (s);
173 SCM sym = scm_cadar (s);
174 if (tag == ly_symbol2scm ("accepts"))
175 acc = scm_cons (sym, acc);
176 else if (tag == ly_symbol2scm ("denies"))
177 acc = scm_delete_x (sym, acc);
180 SCM def = get_default_child (user_mod);
181 if (scm_is_symbol (def))
183 acc = scm_delete_x (def, acc);
184 acc = scm_cons (def, acc);
191 Context_def::get_default_child (SCM user_mod) const
193 SCM name = default_child_;
194 for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
196 SCM entry = scm_car (s);
197 if (scm_car (entry) == ly_symbol2scm ("default-child"))
199 name = scm_cadr (entry);
208 Given a name of a context that we want to create, finds a list of context
209 definitions such that:
210 - the first element in the list defines a context that is a valid child of
211 the context defined by this Context_def
212 - each subsequent element in the list defines a context that is a valid child
213 of the context defined by the preceding element in the list
214 - the last element in the list defines a context with the given name
216 The ADDITIONAL_ACCEPTS parameter is a list of additional contexts that this
217 specific context def (but not any of the child context defs) should accept.
219 vector<Context_def *>
220 Context_def::path_to_acceptable_context (SCM type_sym,
222 SCM additional_accepts) const
224 set<const Context_def *> seen;
225 return internal_path_to_acceptable_context (type_sym, odef, additional_accepts, &seen);
229 The SEEN parameter is a set which keeps track of visited contexts, allowing
230 contexts of the same type to be nested.
232 vector<Context_def *>
233 Context_def::internal_path_to_acceptable_context (SCM type_sym,
235 SCM additional_accepts,
236 set<const Context_def *> *seen) const
238 assert (scm_is_symbol (type_sym));
240 SCM accepted = get_accepted (additional_accepts);
242 vector<Context_def *> accepteds;
243 for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
244 if (Context_def *t = unsmob_context_def (find_context_def (odef,
246 accepteds.push_back (t);
248 vector<Context_def *> best_result;
249 for (vsize i = 0; i < accepteds.size (); i++)
251 /* do not check aliases, because \context Staff should not
252 create RhythmicStaff. */
253 if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
255 best_result.push_back (accepteds[i]);
261 vsize best_depth = INT_MAX;
262 for (vsize i = 0; i < accepteds.size (); i++)
264 Context_def *g = accepteds[i];
266 if (!seen->count (g))
268 vector<Context_def *> result
269 = g->internal_path_to_acceptable_context (type_sym, odef, SCM_EOL, seen);
270 if (result.size () && result.size () < best_depth)
272 best_depth = result.size ();
273 result.insert (result.begin (), g);
274 best_result = result;
284 Context_def::get_translator_names (SCM user_mod) const
288 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
290 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
292 SCM tag = scm_caar (s);
293 SCM arg = scm_cadar (s);
295 if (scm_is_string (arg))
296 arg = scm_string_to_symbol (arg);
298 if (ly_symbol2scm ("consists") == tag)
299 l1 = scm_cons (arg, l1);
300 else if (ly_symbol2scm ("remove") == tag
301 && (scm_is_pair (arg)
302 || ly_is_procedure (arg)
303 || get_translator (arg)))
304 l1 = scm_delete_x (arg, l1);
311 Context_def::instantiate (SCM ops)
313 Context *context = new Context ();
315 context->definition_ = self_scm ();
316 context->definition_mods_ = ops;
317 context->aliases_ = context_aliases_;
318 context->accepts_list_ = get_accepted (ops);
319 context->default_child_ = get_default_child (ops);
325 Context_def::make_scm ()
327 Context_def *t = new Context_def;
328 return t->unprotect ();
332 Context_def::apply_default_property_operations (Context *tg)
334 apply_property_operations (tg, scm_reverse (property_ops_));
338 Context_def::to_alist () const
342 ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
343 get_translator_names (SCM_EOL)), ell);
344 ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
345 ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
346 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
348 if (scm_is_symbol (default_child_))
349 ell = scm_acons (ly_symbol2scm ("default-child"), default_child_, ell);
350 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
352 ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
354 ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
357 if (scm_is_symbol (translator_group_type_))
358 ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
359 translator_group_type_), ell);
364 Context_def::lookup (SCM sym) const
366 if (scm_is_eq (ly_symbol2scm ("default-child"), sym))
367 return default_child_;
368 else if (scm_is_eq (ly_symbol2scm ("consists"), sym))
369 return get_translator_names (SCM_EOL);
370 else if (scm_is_eq (ly_symbol2scm ("description"), sym))
372 else if (scm_is_eq (ly_symbol2scm ("aliases"), sym))
373 return context_aliases_;
374 else if (scm_is_eq (ly_symbol2scm ("accepts"), sym))
375 return get_accepted (SCM_EOL);
376 else if (scm_is_eq (ly_symbol2scm ("property-ops"), sym))
377 return property_ops_;
378 else if (scm_is_eq (ly_symbol2scm ("context-name"), sym))
379 return context_name_;
380 else if (scm_is_eq (ly_symbol2scm ("group-type"), sym))
381 return translator_group_type_;
382 return SCM_UNDEFINED;
386 Context_def::is_alias (SCM sym) const
388 if (scm_is_eq (sym, ly_symbol2scm ("Bottom")))
389 return !scm_is_symbol (get_default_child (SCM_EOL));
391 if (scm_is_eq (sym, get_context_name ()))
394 return scm_is_true (scm_c_memq (sym, context_aliases_));
397 LY_DEFINE (ly_context_def_lookup, "ly:context-def-lookup",
398 2, 1, 0, (SCM def, SCM sym, SCM val),
399 "Return the value of @var{sym} in output definition @var{def}"
400 " (e.g., @code{\\paper}). If no value is found, return"
401 " @var{val} or @code{'()} if @var{val} is undefined.")
403 LY_ASSERT_SMOB (Context_def, def, 1);
404 Context_def *cd = unsmob_context_def (def);
405 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
407 SCM res = cd->lookup (sym);
409 scm_remember_upto_here_1 (def);
411 if (SCM_UNBNDP (res))
414 if (scm_is_null (res) && !SCM_UNBNDP (val))
420 LY_DEFINE (ly_context_def_modify, "ly:context-def-modify",
421 2, 0, 0, (SCM def, SCM mod),
422 "Return the result of applying the context-mod @var{mod} to"
423 " the context definition @var{def}. Does not change @var{def}.")
425 LY_ASSERT_SMOB (Context_def, def, 1);
426 LY_ASSERT_SMOB (Context_mod, mod, 2);
428 Context_def *cd = unsmob_context_def (def)->clone ();
430 for (SCM s = unsmob_context_mod (mod)->get_mods ();
433 cd->add_context_mod (scm_car (s));
435 return cd->unprotect ();