]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
Nitpick run.
[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     default_child_ = sym;
115   else if (ly_symbol2scm ("consists") == tag
116            || ly_symbol2scm ("consists-end") == tag
117            || ly_symbol2scm ("remove") == tag)
118     {
119       if (!get_translator (sym))
120         error (_f ("program has no such type: `%s'",
121                    ly_symbol2string (sym).to_str0 ()));
122       else
123         translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
124     }
125   else if (ly_symbol2scm ("accepts") == tag
126            || ly_symbol2scm ("denies") == tag)
127     accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
128   else if (ly_symbol2scm ("poppush") == tag
129            || ly_symbol2scm ("pop") == tag
130            || ly_symbol2scm ("push") == tag
131            || ly_symbol2scm ("assign") == tag
132            || ly_symbol2scm ("unset") == tag)
133     property_ops_ = scm_cons (mod, property_ops_);
134   else if (ly_symbol2scm ("alias") == tag)
135     context_aliases_ = scm_cons (sym, context_aliases_);
136   else if (ly_symbol2scm ("translator-type") == tag)
137     translator_group_type_ = sym;
138   else if (ly_symbol2scm ("context-name") == tag)
139     context_name_ = sym;
140   else
141     programming_error ("unknown context mod tag");
142 }
143
144 SCM
145 Context_def::get_context_name () const
146 {
147   return context_name_;
148 }
149
150 SCM
151 Context_def::get_accepted (SCM user_mod) const
152 {
153   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
154   SCM acc = SCM_EOL;
155   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
156     {
157       SCM tag = scm_caar (s);
158       SCM sym = scm_cadar (s);
159       if (tag == ly_symbol2scm ("accepts"))
160         acc = scm_cons (sym, acc);
161       else if (tag == ly_symbol2scm ("denies"))
162         acc = scm_delete_x (sym, acc);
163     }
164
165   SCM def = get_default_child (user_mod);
166   if (scm_is_symbol (def))
167     {
168       if (scm_memq (def, acc))
169         acc = scm_delete_x (def, acc);
170       acc = scm_cons (def, acc);
171     }
172
173   return acc;
174 }
175
176 SCM
177 Context_def::get_default_child (SCM user_mod) const
178 {
179   SCM name = default_child_;
180   for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
181     {
182       SCM entry = scm_car (s);
183       if (scm_car (entry) == ly_symbol2scm ("default-child"))
184         {
185           name = scm_cadr (entry);
186           break;
187         }
188     }
189
190   return name;
191 }
192
193 Link_array<Context_def>
194 Context_def::path_to_acceptable_context (SCM type_sym, Output_def *odef) const
195 {
196   assert (scm_is_symbol (type_sym));
197
198   SCM accepted = get_accepted (SCM_EOL);
199
200   Link_array<Context_def> accepteds;
201   for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
202     if (Context_def *t = unsmob_context_def (find_context_def (odef,
203                                                                scm_car (s))))
204       accepteds.push (t);
205
206   Link_array<Context_def> best_result;
207   for (int i = 0; i < accepteds.size (); i++)
208     {
209       /* do not check aliases, because \context Staff should not
210          create RhythmicStaff. */
211       if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
212         {
213           best_result.push (accepteds[i]);
214           return best_result;
215         }
216     }
217
218   int best_depth = INT_MAX;
219   for (int i = 0; i < accepteds.size (); i++)
220     {
221       Context_def *g = accepteds[i];
222
223       Link_array<Context_def> result
224         = g->path_to_acceptable_context (type_sym, odef);
225       if (result.size () && result.size () < best_depth)
226         {
227           best_depth = result.size ();
228           result.insert (g, 0);
229           best_result = result;
230         }
231     }
232
233   return best_result;
234 }
235
236 SCM
237 Context_def::get_translator_names (SCM user_mod) const
238 {
239   SCM l1 = SCM_EOL;
240
241   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
242
243   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
244     {
245       SCM tag = scm_caar (s);
246       SCM arg = scm_cadar (s);
247
248       if (scm_is_string (arg))
249         arg = scm_string_to_symbol (arg);
250
251       if (ly_symbol2scm ("consists") == tag)
252         l1 = scm_cons (arg, l1);
253       else if (ly_symbol2scm ("remove") == tag)
254         l1 = scm_delete_x (arg, l1);
255     }
256
257   return l1;
258 }
259
260 SCM
261 filter_performers (SCM ell)
262 {
263   SCM *tail = &ell;
264   for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
265     {
266       if (dynamic_cast<Performer *> (unsmob_translator (scm_car (*tail))))
267         *tail = scm_cdr (*tail);
268       else
269         tail = SCM_CDRLOC (*tail);
270     }
271   return ell;
272 }
273
274 SCM
275 filter_engravers (SCM ell)
276 {
277   SCM *tail = &ell;
278   for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
279     {
280       if (dynamic_cast<Engraver *> (unsmob_translator (scm_car (*tail))))
281         *tail = scm_cdr (*tail);
282       else
283         tail = SCM_CDRLOC (*tail);
284     }
285   return ell;
286 }
287
288 Context *
289 Context_def::instantiate (SCM ops, Object_key const *key)
290 {
291   Context *context = 0;
292
293   if (context_name_ == ly_symbol2scm ("Score"))
294     context = new Score_context (key);
295   else
296     context = new Context (key);
297
298   context->definition_ = self_scm ();
299
300   SCM trans_names = get_translator_names (ops);
301
302   Translator_group *g = get_translator_group (translator_group_type_);
303   SCM trans_list = SCM_EOL;
304
305   for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
306     {
307       Translator *t = get_translator (scm_car (s));
308       if (!t)
309         warning (_f ("can't find: `%s'", s));
310       else
311         {
312           Translator *tr = t->clone ();
313           SCM str = tr->self_scm ();
314
315           if (tr->must_be_last ())
316             {
317               SCM cons = scm_cons (str, SCM_EOL);
318               if (scm_is_pair (trans_list))
319                 scm_set_cdr_x (scm_last_pair (trans_list), cons);
320               else
321                 trans_list = cons;
322             }
323           else
324             trans_list = scm_cons (str, trans_list);
325
326           tr->daddy_context_ = context;
327           tr->unprotect ();
328         }
329     }
330
331   /*
332     Ugh,  todo: should just make a private
333     copy of Context_def with the user mods.
334   */
335
336   g->simple_trans_list_ = trans_list;
337
338   context->implementation_ = g;
339   if (dynamic_cast<Engraver *> (g))
340     g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
341   else if (dynamic_cast<Performer *> (g))
342     g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
343
344   g->context_ = context;
345   context->aliases_ = context_aliases_;
346
347   g->unprotect ();
348
349   context->accepts_list_ = get_accepted (ops);
350
351   return context;
352 }
353
354 SCM
355 Context_def::clone_scm () const
356 {
357   Context_def *t = new Context_def (*this);
358   return t->unprotect ();
359 }
360
361 SCM
362 Context_def::make_scm ()
363 {
364   Context_def *t = new Context_def;
365   return t->unprotect ();
366 }
367
368 void
369 Context_def::apply_default_property_operations (Context *tg)
370 {
371   apply_property_operations (tg, property_ops_);
372 }
373
374 SCM
375 Context_def::to_alist () const
376 {
377   SCM ell = SCM_EOL;
378
379   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
380                             get_translator_names (SCM_EOL)), ell);
381   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
382   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
383   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
384                   ell);
385   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
386                   ell);
387   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
388                   ell);
389
390   if (scm_is_symbol (translator_group_type_))
391     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
392                               translator_group_type_), ell);
393   return ell;
394 }
395