]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
(get_default_child): new function.
[lilypond.git] / lily / context-def.cc
1 /*
2   translator-def.cc -- implement Context_def
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 /* TODO: should junk this class an replace by
10    a single list of context modifications?  */
11
12 #include "context-def.hh"
13
14 #include "engraver.hh"
15 #include "output-def.hh"
16 #include "performer.hh"
17 #include "score-context.hh"
18 #include "translator-group.hh"
19 #include "warn.hh"
20
21 Context_def::Context_def ()
22 {
23   context_aliases_ = SCM_EOL;
24   translator_group_type_ = SCM_EOL;
25   accept_mods_ = SCM_EOL;
26   translator_mods_ = SCM_EOL;
27   property_ops_ = SCM_EOL;
28   context_name_ = SCM_EOL;
29   default_child_ = SCM_EOL;
30   description_ = SCM_EOL;
31
32   smobify_self ();
33
34   context_name_ = ly_symbol2scm ("");
35 }
36
37 Context_def::Context_def (Context_def const &s)
38   : Input (s)
39 {
40   context_aliases_ = SCM_EOL;
41   translator_group_type_ = SCM_EOL;
42   accept_mods_ = SCM_EOL;
43   translator_mods_ = SCM_EOL;
44   property_ops_ = SCM_EOL;
45   context_name_ = SCM_EOL;
46   description_ = SCM_EOL;
47   default_child_ = SCM_EOL;
48
49   smobify_self ();
50   description_ = s.description_;
51
52   default_child_ = s.default_child_;
53   accept_mods_ = s.accept_mods_;
54   property_ops_ = s.property_ops_;
55   translator_mods_ = s.translator_mods_;
56   context_aliases_ = s.context_aliases_;
57   translator_group_type_ = s.translator_group_type_;
58   context_name_ = s.context_name_;
59 }
60
61 Context_def::~Context_def ()
62 {
63 }
64
65 #include "ly-smobs.icc"
66 IMPLEMENT_SMOBS (Context_def);
67 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
68
69 int
70 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
71 {
72   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
73
74   scm_puts ("#<Context_def ", port);
75   scm_display (me->context_name_, port);
76   scm_puts (">", port);
77   return 1;
78 }
79
80 SCM
81 Context_def::mark_smob (SCM smob)
82 {
83   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
84
85   scm_gc_mark (me->description_);
86   scm_gc_mark (me->context_aliases_);
87   scm_gc_mark (me->accept_mods_);
88   scm_gc_mark (me->translator_mods_);
89   scm_gc_mark (me->property_ops_);
90   scm_gc_mark (me->translator_group_type_);
91   scm_gc_mark (me->default_child_);
92
93   return me->context_name_;
94 }
95
96 void
97 Context_def::add_context_mod (SCM mod)
98 {
99   SCM tag = scm_car (mod);
100   if (ly_symbol2scm ("description") == tag)
101     {
102       description_ = scm_cadr (mod);
103       return;
104     }
105
106   /*
107     other modifiers take symbols as argument. 
108   */
109   SCM sym = scm_cadr (mod);
110   if (scm_is_string (sym))
111     sym = scm_string_to_symbol (sym);
112
113   if (ly_symbol2scm ("default-child") == tag)
114     {
115       default_child_ = sym;
116     }
117   else if (ly_symbol2scm ("consists") == tag
118       || ly_symbol2scm ("consists-end") == tag
119       || ly_symbol2scm ("remove") == tag)
120     {
121       if (!get_translator (sym))
122         error (_f ("program has no such type: `%s'",
123                    ly_symbol2string (sym).to_str0 ()));
124       else
125         translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
126     }
127   else if (ly_symbol2scm ("accepts") == tag
128            || ly_symbol2scm ("denies") == tag)
129     accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
130   else if (ly_symbol2scm ("poppush") == tag
131            || ly_symbol2scm ("pop") == tag
132            || ly_symbol2scm ("push") == tag
133            || ly_symbol2scm ("assign") == tag
134            || ly_symbol2scm ("unset") == tag)
135     property_ops_ = scm_cons (mod, property_ops_);
136   else if (ly_symbol2scm ("alias") == tag)
137     context_aliases_ = scm_cons (sym, context_aliases_);
138   else if (ly_symbol2scm ("translator-type") == tag)
139     translator_group_type_ = sym;
140   else if (ly_symbol2scm ("context-name") == tag)
141     context_name_ = sym;
142   else
143     programming_error ("unknown context mod tag");
144 }
145
146 SCM
147 Context_def::get_context_name () const
148 {
149   return context_name_;
150 }
151
152 SCM
153 Context_def::get_accepted (SCM user_mod) const
154 {
155   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
156   SCM acc = SCM_EOL;
157   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
158     {
159       SCM tag = scm_caar (s);
160       SCM sym = scm_cadar (s);
161       if (tag == ly_symbol2scm ("accepts"))
162         acc = scm_cons (sym, acc);
163       else if (tag == ly_symbol2scm ("denies"))
164         acc = scm_delete_x (sym, acc);
165     }
166
167   SCM def = get_default_child (user_mod);
168   if (scm_is_symbol (def))
169     {
170       if (scm_memq (def, acc))
171         acc = scm_delete_x (def, acc);
172       acc = scm_cons (def, acc);
173     }
174   
175   return acc;
176 }
177
178
179 SCM
180 Context_def::get_default_child (SCM user_mod) const
181 {
182   SCM name = default_child_;
183   for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
184     {
185       SCM entry = scm_car (s);
186       if (scm_car (entry) == ly_symbol2scm ("default-child"))
187         {
188           name = scm_cadr (entry);
189           break;
190         }
191     }
192
193   return name;
194 }
195
196 Link_array<Context_def>
197 Context_def::path_to_acceptable_context (SCM type_sym, Output_def *odef) const
198 {
199   assert (scm_is_symbol (type_sym));
200
201   SCM accepted = get_accepted (SCM_EOL);
202
203   Link_array<Context_def> accepteds;
204   for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
205     if (Context_def *t = unsmob_context_def (find_context_def (odef,
206                                                                scm_car (s))))
207       accepteds.push (t);
208
209   Link_array<Context_def> best_result;
210   for (int i = 0; i < accepteds.size (); i++)
211     {
212       /* do not check aliases, because \context Staff should not
213          create RhythmicStaff. */
214       if (ly_c_equal_p (accepteds[i]->get_context_name (), type_sym))
215         {
216           best_result.push (accepteds[i]);
217           return best_result;
218         }
219     }
220
221   int best_depth = INT_MAX;
222   for (int i = 0; i < accepteds.size (); i++)
223     {
224       Context_def *g = accepteds[i];
225
226       Link_array<Context_def> result
227         = g->path_to_acceptable_context (type_sym, odef);
228       if (result.size () && result.size () < best_depth)
229         {
230           result.insert (g, 0);
231           best_result = result;
232
233           /* this following line was added in 1.9.3, but hsould've been
234              there all along... Let's hope it doesn't cause nightmares.  */
235           best_depth = result.size ();
236         }
237     }
238
239   return best_result;
240 }
241
242 SCM
243 Context_def::get_translator_names (SCM user_mod) const
244 {
245   SCM l1 = SCM_EOL;
246
247   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
248
249   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
250     {
251       SCM tag = scm_caar (s);
252       SCM arg = scm_cadar (s);
253
254       if (scm_is_string (arg))
255         arg = scm_string_to_symbol (arg);
256
257       if (ly_symbol2scm ("consists") == tag)
258         l1 = scm_cons (arg, l1);
259       else if (ly_symbol2scm ("remove") == tag)
260         {
261           l1 = scm_delete_x (arg, l1);
262         }
263     }
264
265   return l1;
266 }
267
268 SCM
269 filter_performers (SCM ell)
270 {
271   for (SCM *tail = &ell; scm_is_pair (*tail); tail = SCM_CDRLOC (*tail))
272     {
273       if (dynamic_cast<Performer *> (unsmob_translator (scm_car (*tail))))
274         {
275           *tail = scm_cdr (*tail);
276           if (!scm_is_pair (*tail))
277             break;
278         }
279     }
280   return ell;
281 }
282
283 SCM
284 filter_engravers (SCM ell)
285 {
286   SCM *tail = &ell;
287   for (; scm_is_pair (*tail); tail = SCM_CDRLOC (*tail))
288     {
289       if (dynamic_cast<Engraver *> (unsmob_translator (scm_car (*tail))))
290         {
291           *tail = scm_cdr (*tail);
292           if (!scm_is_pair (*tail))
293             break;
294         }
295     }
296   return ell;
297 }
298
299 Context *
300 Context_def::instantiate (SCM ops, Object_key const *key)
301 {
302   Context *tg = 0;
303
304   if (context_name_ == ly_symbol2scm ("Score"))
305     tg = new Score_context (key);
306   else
307     tg = new Context (key);
308
309   tg->definition_ = self_scm ();
310
311   SCM trans_names = get_translator_names (ops);
312
313   Translator_group *g = dynamic_cast<Translator_group *>
314     (get_translator (translator_group_type_));
315   g = dynamic_cast<Translator_group *> (g->clone ());
316
317   SCM trans_list = SCM_EOL;
318
319   for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
320     {
321       Translator *t = get_translator (scm_car (s));
322       if (!t)
323         warning (_f ("can't find: `%s'", s));
324       else
325         {
326           Translator *tr = t->clone ();
327           SCM str = tr->self_scm ();
328
329           if (tr->must_be_last ())
330             {
331               SCM cons = scm_cons (str, SCM_EOL);
332               if (scm_is_pair (trans_list))
333                 scm_set_cdr_x (scm_last_pair (trans_list), cons);
334               else
335                 trans_list = cons;
336             }
337           else
338             {
339               trans_list = scm_cons (str, trans_list);
340             }
341
342           tr->daddy_context_ = tg;
343           scm_gc_unprotect_object (str);
344         }
345     }
346
347
348   /*
349     Ugh,  todo: should just make a private
350     copy of Context_def with the user mods.
351   */
352
353   g->simple_trans_list_ = trans_list;
354
355   tg->implementation_ = g->self_scm ();
356   if (dynamic_cast<Engraver *> (g))
357     g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
358   else if (dynamic_cast<Performer *> (g))
359     g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
360
361   g->daddy_context_ = tg;
362   tg->aliases_ = context_aliases_;
363
364   scm_gc_unprotect_object (g->self_scm ());
365
366   tg->accepts_list_ = get_accepted (ops);
367     
368   return tg;
369 }
370
371 SCM
372 Context_def::clone_scm () const
373 {
374   Context_def *t = new Context_def (*this);
375   SCM x = t->self_scm ();
376   scm_gc_unprotect_object (x);
377   return x;
378 }
379
380 SCM
381 Context_def::make_scm ()
382 {
383   Context_def *t = new Context_def;
384   SCM x = t->self_scm ();
385   scm_gc_unprotect_object (x);
386   return x;
387 }
388
389 void
390 Context_def::apply_default_property_operations (Context *tg)
391 {
392   apply_property_operations (tg, property_ops_);
393 }
394
395 SCM
396 Context_def::to_alist () const
397 {
398   SCM ell = SCM_EOL;
399
400   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
401                             get_translator_names (SCM_EOL)), ell);
402   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
403   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
404   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
405                   ell);
406   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
407                   ell);
408   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
409                   ell);
410
411   if (scm_is_symbol (translator_group_type_))
412     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
413                               translator_group_type_), ell);
414   return ell;
415 }
416