]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
* lily/context-def.cc (path_to_acceptable_context): bugfix, depth
[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           best_depth = result.size ();
231           result.insert (g, 0);
232           best_result = result;
233         }
234     }
235
236   return best_result;
237 }
238
239 SCM
240 Context_def::get_translator_names (SCM user_mod) const
241 {
242   SCM l1 = SCM_EOL;
243
244   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
245
246   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
247     {
248       SCM tag = scm_caar (s);
249       SCM arg = scm_cadar (s);
250
251       if (scm_is_string (arg))
252         arg = scm_string_to_symbol (arg);
253
254       if (ly_symbol2scm ("consists") == tag)
255         l1 = scm_cons (arg, l1);
256       else if (ly_symbol2scm ("remove") == tag)
257         {
258           l1 = scm_delete_x (arg, l1);
259         }
260     }
261
262   return l1;
263 }
264
265 SCM
266 filter_performers (SCM ell)
267 {
268   for (SCM *tail = &ell; scm_is_pair (*tail); tail = SCM_CDRLOC (*tail))
269     {
270       if (dynamic_cast<Performer *> (unsmob_translator (scm_car (*tail))))
271         {
272           *tail = scm_cdr (*tail);
273           if (!scm_is_pair (*tail))
274             break;
275         }
276     }
277   return ell;
278 }
279
280 SCM
281 filter_engravers (SCM ell)
282 {
283   SCM *tail = &ell;
284   for (; scm_is_pair (*tail); tail = SCM_CDRLOC (*tail))
285     {
286       if (dynamic_cast<Engraver *> (unsmob_translator (scm_car (*tail))))
287         {
288           *tail = scm_cdr (*tail);
289           if (!scm_is_pair (*tail))
290             break;
291         }
292     }
293   return ell;
294 }
295
296 Context *
297 Context_def::instantiate (SCM ops, Object_key const *key)
298 {
299   Context *tg = 0;
300
301   if (context_name_ == ly_symbol2scm ("Score"))
302     tg = new Score_context (key);
303   else
304     tg = new Context (key);
305
306   tg->definition_ = self_scm ();
307
308   SCM trans_names = get_translator_names (ops);
309
310   Translator_group *g = dynamic_cast<Translator_group *>
311     (get_translator (translator_group_type_));
312   g = dynamic_cast<Translator_group *> (g->clone ());
313
314   SCM trans_list = SCM_EOL;
315
316   for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
317     {
318       Translator *t = get_translator (scm_car (s));
319       if (!t)
320         warning (_f ("can't find: `%s'", s));
321       else
322         {
323           Translator *tr = t->clone ();
324           SCM str = tr->self_scm ();
325
326           if (tr->must_be_last ())
327             {
328               SCM cons = scm_cons (str, SCM_EOL);
329               if (scm_is_pair (trans_list))
330                 scm_set_cdr_x (scm_last_pair (trans_list), cons);
331               else
332                 trans_list = cons;
333             }
334           else
335             {
336               trans_list = scm_cons (str, trans_list);
337             }
338
339           tr->daddy_context_ = tg;
340           scm_gc_unprotect_object (str);
341         }
342     }
343
344
345   /*
346     Ugh,  todo: should just make a private
347     copy of Context_def with the user mods.
348   */
349
350   g->simple_trans_list_ = trans_list;
351
352   tg->implementation_ = g->self_scm ();
353   if (dynamic_cast<Engraver *> (g))
354     g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
355   else if (dynamic_cast<Performer *> (g))
356     g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
357
358   g->daddy_context_ = tg;
359   tg->aliases_ = context_aliases_;
360
361   scm_gc_unprotect_object (g->self_scm ());
362
363   tg->accepts_list_ = get_accepted (ops);
364     
365   return tg;
366 }
367
368 SCM
369 Context_def::clone_scm () const
370 {
371   Context_def *t = new Context_def (*this);
372   SCM x = t->self_scm ();
373   scm_gc_unprotect_object (x);
374   return x;
375 }
376
377 SCM
378 Context_def::make_scm ()
379 {
380   Context_def *t = new Context_def;
381   SCM x = t->self_scm ();
382   scm_gc_unprotect_object (x);
383   return x;
384 }
385
386 void
387 Context_def::apply_default_property_operations (Context *tg)
388 {
389   apply_property_operations (tg, property_ops_);
390 }
391
392 SCM
393 Context_def::to_alist () const
394 {
395   SCM ell = SCM_EOL;
396
397   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
398                             get_translator_names (SCM_EOL)), ell);
399   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
400   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
401   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
402                   ell);
403   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
404                   ell);
405   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
406                   ell);
407
408   if (scm_is_symbol (translator_group_type_))
409     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
410                               translator_group_type_), ell);
411   return ell;
412 }
413