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