]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
d4f3dc3433d8952c84b7d359007db3f1e161a58d
[lilypond.git] / lily / context-def.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 /* TODO: should junk this class an replace by
21    a single list of context modifications?  */
22
23 #include "context-def.hh"
24
25 #include "context.hh"
26 #include "context-mod.hh"
27 #include "international.hh"
28 #include "output-def.hh"
29 #include "translator.hh"
30 #include "warn.hh"
31
32 Context_def::Context_def ()
33 {
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;
43
44   smobify_self ();
45
46   input_location_ = make_input (Input ());
47   context_name_ = ly_symbol2scm ("");
48 }
49
50 Input *
51 Context_def::origin () const
52 {
53   return unsmob_input (input_location_);
54 }
55
56 Context_def::Context_def (Context_def const &s)
57 {
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;
67   smobify_self ();
68
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_;
78 }
79
80 Context_def::~Context_def ()
81 {
82 }
83
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?");
88
89 int
90 Context_def::print_smob (SCM smob, SCM port, scm_print_state *)
91 {
92   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
93
94   scm_puts ("#<Context_def ", port);
95   scm_display (me->context_name_, port);
96   scm_puts (">", port);
97   return 1;
98 }
99
100 SCM
101 Context_def::mark_smob (SCM smob)
102 {
103   ASSERT_LIVE_IS_ALLOWED ();
104
105   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
106
107   scm_gc_mark (me->description_);
108   scm_gc_mark (me->context_aliases_);
109   scm_gc_mark (me->accept_mods_);
110   scm_gc_mark (me->translator_mods_);
111   scm_gc_mark (me->property_ops_);
112   scm_gc_mark (me->translator_group_type_);
113   scm_gc_mark (me->default_child_);
114   scm_gc_mark (me->input_location_);
115
116   return me->context_name_;
117 }
118
119 void
120 Context_def::add_context_mod (SCM mod)
121 {
122   SCM tag = scm_car (mod);
123   if (ly_symbol2scm ("description") == tag)
124     {
125       description_ = scm_cadr (mod);
126       return;
127     }
128
129   /*
130     other modifiers take symbols as argument.
131   */
132   SCM sym = scm_cadr (mod);
133   if (scm_is_string (sym))
134     sym = scm_string_to_symbol (sym);
135
136   if (ly_symbol2scm ("default-child") == tag)
137     default_child_ = sym;
138   else if (ly_symbol2scm ("consists") == tag
139            || ly_symbol2scm ("remove") == tag)
140     {
141       translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
142     }
143   else if (ly_symbol2scm ("accepts") == tag
144            || ly_symbol2scm ("denies") == tag)
145     accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
146   else if (ly_symbol2scm ("pop") == tag
147            || ly_symbol2scm ("push") == tag
148            || ly_symbol2scm ("assign") == tag
149            || ly_symbol2scm ("unset") == tag
150            || ly_symbol2scm ("apply") == tag)
151     property_ops_ = scm_cons (mod, property_ops_);
152   else if (ly_symbol2scm ("alias") == tag)
153     context_aliases_ = scm_cons (sym, context_aliases_);
154   else if (ly_symbol2scm ("translator-type") == tag)
155     translator_group_type_ = sym;
156   else if (ly_symbol2scm ("context-name") == tag)
157     context_name_ = sym;
158   else
159     programming_error ("unknown context mod tag");
160 }
161
162 SCM
163 Context_def::get_accepted (SCM user_mod) const
164 {
165   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
166   SCM acc = SCM_EOL;
167   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
168     {
169       SCM tag = scm_caar (s);
170       SCM sym = scm_cadar (s);
171       if (tag == ly_symbol2scm ("accepts"))
172         acc = scm_cons (sym, acc);
173       else if (tag == ly_symbol2scm ("denies"))
174         acc = scm_delete_x (sym, acc);
175     }
176
177   SCM def = get_default_child (user_mod);
178   if (scm_is_symbol (def))
179     {
180       acc = scm_delete_x (def, acc);
181       acc = scm_cons (def, acc);
182     }
183
184   return acc;
185 }
186
187 SCM
188 Context_def::get_default_child (SCM user_mod) const
189 {
190   SCM name = default_child_;
191   for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
192     {
193       SCM entry = scm_car (s);
194       if (scm_car (entry) == ly_symbol2scm ("default-child"))
195         {
196           name = scm_cadr (entry);
197           break;
198         }
199     }
200
201   return name;
202 }
203
204 /*
205   Given a name of a context that we want to create, finds a list of context
206   definitions such that:
207    - the first element in the list defines a context that is a valid child of
208      the context defined by this Context_def
209    - each subsequent element in the list defines a context that is a valid child
210      of the context defined by the preceding element in the list
211    - the last element in the list defines a context with the given name
212
213   The ADDITIONAL_ACCEPTS parameter is a list of additional contexts that this
214   specific context def (but not any of the child context defs) should accept.
215 */
216 vector<Context_def *>
217 Context_def::path_to_acceptable_context (SCM type_sym,
218                                          Output_def *odef,
219                                          SCM additional_accepts) const
220 {
221   set<const Context_def *> seen;
222   return internal_path_to_acceptable_context (type_sym, odef, additional_accepts, &seen);
223 }
224
225 /*
226 The SEEN parameter is a set which keeps track of visited contexts, allowing
227 contexts of the same type to be nested.
228 */
229 vector<Context_def *>
230 Context_def::internal_path_to_acceptable_context (SCM type_sym,
231                                                   Output_def *odef,
232                                                   SCM additional_accepts,
233                                                   set<const Context_def *> *seen) const
234 {
235   assert (scm_is_symbol (type_sym));
236
237   SCM accepted = get_accepted (additional_accepts);
238
239   vector<Context_def *> accepteds;
240   for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
241     if (Context_def *t = unsmob_context_def (find_context_def (odef,
242                                                                scm_car (s))))
243       accepteds.push_back (t);
244
245   vector<Context_def *> best_result;
246   for (vsize i = 0; i < accepteds.size (); i++)
247     {
248       /* do not check aliases, because \context Staff should not
249          create RhythmicStaff. */
250       if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
251         {
252           best_result.push_back (accepteds[i]);
253           return best_result;
254         }
255     }
256
257   seen->insert (this);
258   vsize best_depth = INT_MAX;
259   for (vsize i = 0; i < accepteds.size (); i++)
260     {
261       Context_def *g = accepteds[i];
262
263       if (!seen->count (g))
264         {
265           vector<Context_def *> result
266             = g->internal_path_to_acceptable_context (type_sym, odef, SCM_EOL, seen);
267           if (result.size () && result.size () < best_depth)
268             {
269               best_depth = result.size ();
270               result.insert (result.begin (), g);
271               best_result = result;
272             }
273         }
274     }
275   seen->erase (this);
276
277   return best_result;
278 }
279
280 SCM
281 Context_def::get_translator_names (SCM user_mod) const
282 {
283   SCM l1 = SCM_EOL;
284
285   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
286
287   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
288     {
289       SCM tag = scm_caar (s);
290       SCM arg = scm_cadar (s);
291
292       if (scm_is_string (arg))
293         arg = scm_string_to_symbol (arg);
294
295       if (ly_symbol2scm ("consists") == tag)
296         l1 = scm_cons (arg, l1);
297       else if (ly_symbol2scm ("remove") == tag
298                && (scm_is_pair (arg)
299                    || ly_is_procedure (arg)
300                    || get_translator (arg)))
301         l1 = scm_delete_x (arg, l1);
302     }
303
304   return l1;
305 }
306
307 Context *
308 Context_def::instantiate (SCM ops)
309 {
310   Context *context = new Context ();
311
312   context->definition_ = self_scm ();
313   context->definition_mods_ = ops;
314   context->aliases_ = context_aliases_;
315   context->accepts_list_ = get_accepted (ops);
316
317   return context;
318 }
319
320 SCM
321 Context_def::make_scm ()
322 {
323   Context_def *t = new Context_def;
324   return t->unprotect ();
325 }
326
327 void
328 Context_def::apply_default_property_operations (Context *tg)
329 {
330   apply_property_operations (tg, scm_reverse (property_ops_));
331 }
332
333 SCM
334 Context_def::to_alist () const
335 {
336   SCM ell = SCM_EOL;
337
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)),
343                   ell);
344   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
345                   ell);
346   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
347                   ell);
348
349   if (scm_is_symbol (translator_group_type_))
350     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
351                               translator_group_type_), ell);
352   return ell;
353 }
354
355 SCM
356 Context_def::lookup (SCM sym) const
357 {
358   if (scm_is_eq (ly_symbol2scm ("default-child"), sym))
359     return default_child_;
360   else if (scm_is_eq (ly_symbol2scm ("consists"), sym))
361     return get_translator_names (SCM_EOL);
362   else if (scm_is_eq (ly_symbol2scm ("description"), sym))
363     return description_;
364   else if (scm_is_eq (ly_symbol2scm ("aliases"), sym))
365     return context_aliases_;
366   else if (scm_is_eq (ly_symbol2scm ("accepts"), sym))
367     return get_accepted (SCM_EOL);
368   else if (scm_is_eq (ly_symbol2scm ("property-ops"), sym))
369     return property_ops_;
370   else if (scm_is_eq (ly_symbol2scm ("context-name"), sym))
371     return context_name_;
372   else if (scm_is_eq (ly_symbol2scm ("group-type"), sym))
373     return translator_group_type_;
374   return SCM_UNDEFINED;
375 }
376
377 bool
378 Context_def::is_alias (SCM sym) const
379 {
380   if (scm_is_eq (sym, ly_symbol2scm ("Bottom")))
381     return !scm_is_pair (get_accepted (SCM_EOL));
382
383   if (scm_is_eq (sym, get_context_name ()))
384     return true;
385
386   return scm_is_true (scm_c_memq (sym, context_aliases_));
387 }
388
389 LY_DEFINE (ly_context_def_lookup, "ly:context-def-lookup",
390            2, 1, 0, (SCM def, SCM sym, SCM val),
391            "Return the value of @var{sym} in output definition @var{def}"
392            " (e.g., @code{\\paper}).  If no value is found, return"
393            " @var{val} or @code{'()} if @var{val} is undefined.")
394 {
395   LY_ASSERT_SMOB (Context_def, def, 1);
396   Context_def *cd = unsmob_context_def (def);
397   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
398
399   SCM res = cd->lookup (sym);
400
401   scm_remember_upto_here_1 (def);
402
403   if (SCM_UNBNDP (res))
404     res = SCM_EOL;
405
406   if (scm_is_null (res) && !SCM_UNBNDP (val))
407     return val;
408
409   return res;
410 }
411
412 LY_DEFINE (ly_context_def_modify, "ly:context-def-modify",
413            2, 0, 0, (SCM def, SCM mod),
414            "Return the result of applying the context-mod @var{mod} to"
415            " the context definition @var{def}.  Does not change @var{def}.")
416 {
417   LY_ASSERT_SMOB (Context_def, def, 1);
418   LY_ASSERT_SMOB (Context_mod, mod, 2);
419
420   Context_def *cd = unsmob_context_def (def)->clone ();
421
422   for (SCM s = unsmob_context_mod (mod)->get_mods ();
423        scm_is_pair (s);
424        s = scm_cdr (s))
425     cd->add_context_mod (scm_car (s));
426
427   return cd->unprotect ();
428 }