]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
(DECLARE_BASE_SMOBS): add methods
[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 *context = 0;
297
298   if (context_name_ == ly_symbol2scm ("Score"))
299     context = new Score_context (key);
300   else
301     context = new Context (key);
302
303   context->definition_ = self_scm ();
304
305   SCM trans_names = get_translator_names (ops);
306
307   Translator_group *g = get_translator_group (translator_group_type_);
308   SCM trans_list = SCM_EOL;
309
310   for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
311     {
312       Translator *t = get_translator (scm_car (s));
313       if (!t)
314         warning (_f ("can't find: `%s'", s));
315       else
316         {
317           Translator *tr = t->clone ();
318           SCM str = tr->self_scm ();
319
320           if (tr->must_be_last ())
321             {
322               SCM cons = scm_cons (str, SCM_EOL);
323               if (scm_is_pair (trans_list))
324                 scm_set_cdr_x (scm_last_pair (trans_list), cons);
325               else
326                 trans_list = cons;
327             }
328           else
329             {
330               trans_list = scm_cons (str, trans_list);
331             }
332
333           tr->daddy_context_ = context;
334           tr->unprotect ();
335         }
336     }
337
338
339   /*
340     Ugh,  todo: should just make a private
341     copy of Context_def with the user mods.
342   */
343
344   g->simple_trans_list_ = trans_list;
345
346   context->implementation_ = g;
347   if (dynamic_cast<Engraver *> (g))
348     g->simple_trans_list_ = filter_performers (g->simple_trans_list_);
349   else if (dynamic_cast<Performer *> (g))
350     g->simple_trans_list_ = filter_engravers (g->simple_trans_list_);
351
352   g->context_ = context;
353   context->aliases_ = context_aliases_;
354
355   g->unprotect ();
356
357   context->accepts_list_ = get_accepted (ops);
358     
359   return context;
360 }
361
362 SCM
363 Context_def::clone_scm () const
364 {
365   Context_def *t = new Context_def (*this);
366   return t->unprotect ();
367 }
368
369 SCM
370 Context_def::make_scm ()
371 {
372   Context_def *t = new Context_def;
373   return t->unprotect ();
374 }
375
376 void
377 Context_def::apply_default_property_operations (Context *tg)
378 {
379   apply_property_operations (tg, property_ops_);
380 }
381
382 SCM
383 Context_def::to_alist () const
384 {
385   SCM ell = SCM_EOL;
386
387   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
388                             get_translator_names (SCM_EOL)), ell);
389   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
390   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
391   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
392                   ell);
393   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
394                   ell);
395   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
396                   ell);
397
398   if (scm_is_symbol (translator_group_type_))
399     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
400                               translator_group_type_), ell);
401   return ell;
402 }
403