]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
* lily/include/engraver-group.hh: rename.
[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 #include "engraver-group.hh"
22 #include "performer-group.hh"
23
24 Context_def::Context_def ()
25 {
26   context_aliases_ = SCM_EOL;
27   translator_group_type_ = SCM_EOL;
28   accept_mods_ = SCM_EOL;
29   translator_mods_ = SCM_EOL;
30   property_ops_ = SCM_EOL;
31   context_name_ = SCM_EOL;
32   default_child_ = SCM_EOL;
33   description_ = SCM_EOL;
34
35   smobify_self ();
36
37   context_name_ = ly_symbol2scm ("");
38 }
39
40 Context_def::Context_def (Context_def const &s)
41   : Input (s)
42 {
43   context_aliases_ = SCM_EOL;
44   translator_group_type_ = SCM_EOL;
45   accept_mods_ = SCM_EOL;
46   translator_mods_ = SCM_EOL;
47   property_ops_ = SCM_EOL;
48   context_name_ = SCM_EOL;
49   description_ = SCM_EOL;
50   default_child_ = SCM_EOL;
51
52   smobify_self ();
53   description_ = s.description_;
54
55   default_child_ = s.default_child_;
56   accept_mods_ = s.accept_mods_;
57   property_ops_ = s.property_ops_;
58   translator_mods_ = s.translator_mods_;
59   context_aliases_ = s.context_aliases_;
60   translator_group_type_ = s.translator_group_type_;
61   context_name_ = s.context_name_;
62 }
63
64 Context_def::~Context_def ()
65 {
66 }
67
68 #include "ly-smobs.icc"
69 IMPLEMENT_SMOBS (Context_def);
70 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
71
72 int
73 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
74 {
75   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
76
77   scm_puts ("#<Context_def ", port);
78   scm_display (me->context_name_, port);
79   scm_puts (">", port);
80   return 1;
81 }
82
83 SCM
84 Context_def::mark_smob (SCM smob)
85 {
86   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
87
88   scm_gc_mark (me->description_);
89   scm_gc_mark (me->context_aliases_);
90   scm_gc_mark (me->accept_mods_);
91   scm_gc_mark (me->translator_mods_);
92   scm_gc_mark (me->property_ops_);
93   scm_gc_mark (me->translator_group_type_);
94   scm_gc_mark (me->default_child_);
95
96   return me->context_name_;
97 }
98
99 void
100 Context_def::add_context_mod (SCM mod)
101 {
102   SCM tag = scm_car (mod);
103   if (ly_symbol2scm ("description") == tag)
104     {
105       description_ = scm_cadr (mod);
106       return;
107     }
108
109   /*
110     other modifiers take symbols as argument.
111   */
112   SCM sym = scm_cadr (mod);
113   if (scm_is_string (sym))
114     sym = scm_string_to_symbol (sym);
115
116   if (ly_symbol2scm ("default-child") == tag)
117     default_child_ = sym;
118   else if (ly_symbol2scm ("consists") == tag
119            || ly_symbol2scm ("consists-end") == tag
120            || ly_symbol2scm ("remove") == tag)
121     {
122       if (!get_translator (sym))
123         error (_f ("program has no such type: `%s'",
124                    ly_symbol2string (sym).to_str0 ()));
125       else
126         translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
127     }
128   else if (ly_symbol2scm ("accepts") == tag
129            || ly_symbol2scm ("denies") == tag)
130     accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
131   else if (ly_symbol2scm ("poppush") == tag
132            || ly_symbol2scm ("pop") == tag
133            || ly_symbol2scm ("push") == tag
134            || ly_symbol2scm ("assign") == tag
135            || ly_symbol2scm ("unset") == tag)
136     property_ops_ = scm_cons (mod, property_ops_);
137   else if (ly_symbol2scm ("alias") == tag)
138     context_aliases_ = scm_cons (sym, context_aliases_);
139   else if (ly_symbol2scm ("translator-type") == tag)
140     translator_group_type_ = sym;
141   else if (ly_symbol2scm ("context-name") == tag)
142     context_name_ = sym;
143   else
144     programming_error ("unknown context mod tag");
145 }
146
147 SCM
148 Context_def::get_context_name () const
149 {
150   return context_name_;
151 }
152
153 SCM
154 Context_def::get_accepted (SCM user_mod) const
155 {
156   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
157   SCM acc = SCM_EOL;
158   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
159     {
160       SCM tag = scm_caar (s);
161       SCM sym = scm_cadar (s);
162       if (tag == ly_symbol2scm ("accepts"))
163         acc = scm_cons (sym, acc);
164       else if (tag == ly_symbol2scm ("denies"))
165         acc = scm_delete_x (sym, acc);
166     }
167
168   SCM def = get_default_child (user_mod);
169   if (scm_is_symbol (def))
170     {
171       if (scm_memq (def, acc))
172         acc = scm_delete_x (def, acc);
173       acc = scm_cons (def, acc);
174     }
175
176   return acc;
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_is_equal (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         l1 = scm_delete_x (arg, l1);
258     }
259
260   return l1;
261 }
262
263 SCM
264 filter_performers (SCM ell)
265 {
266   SCM *tail = &ell;
267   for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
268     {
269       if (dynamic_cast<Performer *> (unsmob_translator (scm_car (*tail))))
270         *tail = scm_cdr (*tail);
271       else
272         tail = SCM_CDRLOC (*tail);
273     }
274   return ell;
275 }
276
277 SCM
278 filter_engravers (SCM ell)
279 {
280   SCM *tail = &ell;
281   for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
282     {
283       if (dynamic_cast<Engraver *> (unsmob_translator (scm_car (*tail))))
284         *tail = scm_cdr (*tail);
285       else
286         tail = SCM_CDRLOC (*tail);
287     }
288   return ell;
289 }
290
291 Context *
292 Context_def::instantiate (SCM ops, Object_key const *key)
293 {
294   Context *context = 0;
295
296   if (context_name_ == ly_symbol2scm ("Score"))
297     context = new Score_context (key);
298   else
299     context = new Context (key);
300
301   context->definition_ = self_scm ();
302
303   SCM trans_names = get_translator_names (ops);
304
305   Translator_group *g = get_translator_group (translator_group_type_);
306   SCM trans_list = SCM_EOL;
307
308   for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
309     {
310       Translator *t = get_translator (scm_car (s));
311       if (!t)
312         warning (_f ("can't find: `%s'", s));
313       else
314         {
315           Translator *tr = t->clone ();
316           SCM str = tr->self_scm ();
317
318           if (tr->must_be_last ())
319             {
320               SCM cons = scm_cons (str, SCM_EOL);
321               if (scm_is_pair (trans_list))
322                 scm_set_cdr_x (scm_last_pair (trans_list), cons);
323               else
324                 trans_list = cons;
325             }
326           else
327             trans_list = scm_cons (str, trans_list);
328
329           tr->daddy_context_ = context;
330           tr->unprotect ();
331         }
332     }
333
334   /*
335     Ugh,  todo: should just make a private
336     copy of Context_def with the user mods.
337   */
338
339   g->simple_trans_list_ = trans_list;
340
341   context->implementation_ = g;
342   if (dynamic_cast<Engraver_group *> (g))
343     g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
344   else if (dynamic_cast<Performer_group *> (g))
345     g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
346
347   g->context_ = context;
348   context->aliases_ = context_aliases_;
349
350   g->unprotect ();
351
352   context->accepts_list_ = get_accepted (ops);
353
354   return context;
355 }
356
357 SCM
358 Context_def::clone_scm () const
359 {
360   Context_def *t = new Context_def (*this);
361   return t->unprotect ();
362 }
363
364 SCM
365 Context_def::make_scm ()
366 {
367   Context_def *t = new Context_def;
368   return t->unprotect ();
369 }
370
371 void
372 Context_def::apply_default_property_operations (Context *tg)
373 {
374   apply_property_operations (tg, property_ops_);
375 }
376
377 SCM
378 Context_def::to_alist () const
379 {
380   SCM ell = SCM_EOL;
381
382   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
383                             get_translator_names (SCM_EOL)), ell);
384   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
385   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
386   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
387                   ell);
388   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
389                   ell);
390   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
391                   ell);
392
393   if (scm_is_symbol (translator_group_type_))
394     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
395                               translator_group_type_), ell);
396   return ell;
397 }
398