2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2000--2015 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"
36 Context_def::Context_def ()
38 context_aliases_ = SCM_EOL;
39 translator_group_type_ = SCM_EOL;
40 accept_mods_ = SCM_EOL;
41 translator_mods_ = SCM_EOL;
42 property_ops_ = SCM_EOL;
43 context_name_ = SCM_EOL;
44 default_child_ = SCM_EOL;
45 description_ = SCM_EOL;
46 input_location_ = SCM_EOL;
50 input_location_ = Input ().smobbed_copy ();
51 context_name_ = ly_symbol2scm ("");
55 Context_def::origin () const
57 return unsmob<Input> (input_location_);
60 Context_def::Context_def (Context_def const &s)
61 : Smob<Context_def> ()
63 context_aliases_ = SCM_EOL;
64 translator_group_type_ = SCM_EOL;
65 accept_mods_ = SCM_EOL;
66 translator_mods_ = SCM_EOL;
67 property_ops_ = SCM_EOL;
68 context_name_ = SCM_EOL;
69 description_ = SCM_EOL;
70 default_child_ = SCM_EOL;
71 input_location_ = SCM_EOL;
74 description_ = s.description_;
75 input_location_ = s.origin ()->smobbed_copy ();
76 default_child_ = s.default_child_;
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_;
85 Context_def::~Context_def ()
89 const char Context_def::type_p_name_[] = "ly:context-def?";
92 Context_def::print_smob (SCM port, scm_print_state *) const
94 scm_puts ("#<Context_def ", port);
95 scm_display (context_name_, port);
97 string loc = origin ()->location_string ();
98 scm_puts (loc.c_str (), port);
104 Context_def::mark_smob () const
106 ASSERT_LIVE_IS_ALLOWED (self_scm ());
108 scm_gc_mark (description_);
109 scm_gc_mark (context_aliases_);
110 scm_gc_mark (accept_mods_);
111 scm_gc_mark (translator_mods_);
112 scm_gc_mark (property_ops_);
113 scm_gc_mark (translator_group_type_);
114 scm_gc_mark (default_child_);
115 scm_gc_mark (input_location_);
117 return context_name_;
121 Context_def::add_context_mod (SCM mod)
123 SCM tag = scm_car (mod);
124 if (scm_is_eq (tag, ly_symbol2scm ("description")))
126 description_ = scm_cadr (mod);
131 other modifiers take symbols as argument.
133 SCM sym = scm_cadr (mod);
134 if (scm_is_string (sym))
135 sym = scm_string_to_symbol (sym);
137 if (scm_is_eq (tag, ly_symbol2scm ("default-child")))
138 default_child_ = sym;
139 else if (scm_is_eq (tag, ly_symbol2scm ("consists"))
140 || scm_is_eq (tag, ly_symbol2scm ("remove")))
142 translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
144 else if (scm_is_eq (tag, ly_symbol2scm ("accepts"))
145 || scm_is_eq (tag, ly_symbol2scm ("denies")))
146 accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
147 else if (scm_is_eq (tag, ly_symbol2scm ("pop"))
148 || scm_is_eq (tag, ly_symbol2scm ("push"))
149 || scm_is_eq (tag, ly_symbol2scm ("assign"))
150 || scm_is_eq (tag, ly_symbol2scm ("unset"))
151 || scm_is_eq (tag, ly_symbol2scm ("apply")))
152 property_ops_ = scm_cons (mod, property_ops_);
153 else if (scm_is_eq (tag, ly_symbol2scm ("alias")))
154 context_aliases_ = scm_cons (sym, context_aliases_);
155 else if (scm_is_eq (tag, ly_symbol2scm ("translator-type")))
156 translator_group_type_ = sym;
157 else if (scm_is_eq (tag, ly_symbol2scm ("context-name")))
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 (scm_is_eq (tag, ly_symbol2scm ("accepts")))
173 acc = scm_cons (sym, acc);
174 else if (scm_is_eq (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 acc = scm_delete_x (def, acc);
182 acc = scm_cons (def, acc);
189 Context_def::get_default_child (SCM user_mod) const
191 SCM name = default_child_;
192 for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
194 SCM entry = scm_car (s);
195 if (scm_is_eq (scm_car (entry), ly_symbol2scm ("default-child")))
197 name = scm_cadr (entry);
206 Given a name of a context that we want to create, finds a list of context
207 definitions such that:
208 - the first element in the list defines a context that is a valid child of
209 the context defined by this Context_def
210 - each subsequent element in the list defines a context that is a valid child
211 of the context defined by the preceding element in the list
212 - the last element in the list defines a context with the given name
214 The ADDITIONAL_ACCEPTS parameter is a list of additional contexts that this
215 specific context def (but not any of the child context defs) should accept.
217 vector<Context_def *>
218 Context_def::path_to_acceptable_context (SCM type_sym,
220 SCM additional_accepts) const
222 set<const Context_def *> seen;
223 return internal_path_to_acceptable_context (type_sym, odef, additional_accepts, &seen);
227 The SEEN parameter is a set which keeps track of visited contexts, allowing
228 contexts of the same type to be nested.
230 vector<Context_def *>
231 Context_def::internal_path_to_acceptable_context (SCM type_sym,
233 SCM additional_accepts,
234 set<const Context_def *> *seen) const
236 assert (scm_is_symbol (type_sym));
238 SCM accepted = get_accepted (additional_accepts);
240 vector<Context_def *> accepteds;
241 for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
242 if (Context_def *t = unsmob<Context_def> (find_context_def (odef,
244 accepteds.push_back (t);
246 vector<Context_def *> best_result;
247 for (vsize i = 0; i < accepteds.size (); i++)
249 /* do not check aliases, because \context Staff should not
250 create RhythmicStaff. */
251 if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
253 best_result.push_back (accepteds[i]);
259 vsize best_depth = INT_MAX;
260 for (vsize i = 0; i < accepteds.size (); i++)
262 Context_def *g = accepteds[i];
264 if (!seen->count (g))
266 vector<Context_def *> result
267 = g->internal_path_to_acceptable_context (type_sym, odef, SCM_EOL, seen);
268 if (result.size () && result.size () < best_depth)
270 best_depth = result.size ();
271 result.insert (result.begin (), g);
272 best_result = result;
282 Context_def::get_translator_names (SCM user_mod) const
286 SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
288 for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
290 SCM tag = scm_caar (s);
291 SCM arg = scm_cadar (s);
293 if (scm_is_string (arg))
294 arg = scm_string_to_symbol (arg);
296 if (scm_is_eq (tag, ly_symbol2scm ("consists")))
297 l1 = scm_cons (arg, l1);
298 else if (scm_is_eq (tag, ly_symbol2scm ("remove"))
299 && (scm_is_pair (arg)
300 || ly_is_procedure (arg)
301 || get_translator (arg)))
302 l1 = scm_delete_x (arg, l1);
309 Context_def::instantiate (SCM ops)
311 Context *context = new Context ();
313 context->definition_ = self_scm ();
314 context->definition_mods_ = ops;
315 context->aliases_ = context_aliases_;
316 context->accepts_list_ = get_accepted (ops);
317 context->default_child_ = get_default_child (ops);
323 Context_def::make_scm ()
325 Context_def *t = new Context_def;
326 return t->unprotect ();
330 Context_def::apply_default_property_operations (Context *tg)
332 apply_property_operations (tg, scm_reverse (property_ops_));
336 Context_def::to_alist () const
340 ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
341 get_translator_names (SCM_EOL)), ell);
342 ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
343 ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
344 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
346 if (scm_is_symbol (default_child_))
347 ell = scm_acons (ly_symbol2scm ("default-child"), default_child_, ell);
348 ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
350 ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
352 ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
355 if (scm_is_symbol (translator_group_type_))
356 ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
357 translator_group_type_), ell);
362 Context_def::lookup (SCM sym) const
364 if (scm_is_eq (ly_symbol2scm ("default-child"), sym))
365 return default_child_;
366 else if (scm_is_eq (ly_symbol2scm ("consists"), sym))
367 return get_translator_names (SCM_EOL);
368 else if (scm_is_eq (ly_symbol2scm ("description"), sym))
370 else if (scm_is_eq (ly_symbol2scm ("aliases"), sym))
371 return context_aliases_;
372 else if (scm_is_eq (ly_symbol2scm ("accepts"), sym))
373 return get_accepted (SCM_EOL);
374 else if (scm_is_eq (ly_symbol2scm ("property-ops"), sym))
375 return property_ops_;
376 else if (scm_is_eq (ly_symbol2scm ("context-name"), sym))
377 return context_name_;
378 else if (scm_is_eq (ly_symbol2scm ("group-type"), sym))
379 return translator_group_type_;
380 return SCM_UNDEFINED;
384 Context_def::is_alias (SCM sym) const
386 if (scm_is_eq (sym, ly_symbol2scm ("Bottom")))
387 return !scm_is_symbol (get_default_child (SCM_EOL));
389 if (scm_is_eq (sym, get_context_name ()))
392 return scm_is_true (scm_c_memq (sym, context_aliases_));
395 LY_DEFINE (ly_context_def_lookup, "ly:context-def-lookup",
396 2, 1, 0, (SCM def, SCM sym, SCM val),
397 "Return the value of @var{sym} in context definition @var{def}"
398 " (e.g., @code{\\Voice}). If no value is found, return"
399 " @var{val} or @code{'()} if @var{val} is undefined."
400 " @var{sym} can be any of @samp{default-child}, @samp{consists},"
401 " @samp{description}, @samp{aliases}, @samp{accepts},"
402 " @samp{property-ops}, @samp{context-name}, @samp{group-type}.")
404 LY_ASSERT_SMOB (Context_def, def, 1);
405 Context_def *cd = unsmob<Context_def> (def);
406 LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
408 SCM res = cd->lookup (sym);
410 scm_remember_upto_here_1 (def);
412 if (SCM_UNBNDP (res))
415 if (scm_is_null (res) && !SCM_UNBNDP (val))
421 LY_DEFINE (ly_context_def_modify, "ly:context-def-modify",
422 2, 0, 0, (SCM def, SCM mod),
423 "Return the result of applying the context-mod @var{mod} to"
424 " the context definition @var{def}. Does not change @var{def}.")
426 LY_ASSERT_SMOB (Context_def, def, 1);
427 LY_ASSERT_SMOB (Context_mod, mod, 2);
429 Context_def *cd = unsmob<Context_def> (def)->clone ();
431 for (SCM s = unsmob<Context_mod> (mod)->get_mods ();
434 cd->add_context_mod (scm_car (s));
436 return cd->unprotect ();