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