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