]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
* lily/smobs.cc (protect_smob): switch off fancy smob protection
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.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 "context.hh"
15 #include "international.hh"
16 #include "output-def.hh"
17 #include "translator.hh"
18 #include "warn.hh"
19
20 Context_def::Context_def ()
21 {
22   context_aliases_ = SCM_EOL;
23   translator_group_type_ = SCM_EOL;
24   accept_mods_ = SCM_EOL;
25   translator_mods_ = SCM_EOL;
26   property_ops_ = SCM_EOL;
27   context_name_ = SCM_EOL;
28   default_child_ = SCM_EOL;
29   description_ = SCM_EOL;
30
31   smobify_self ();
32
33   context_name_ = ly_symbol2scm ("");
34 }
35
36 Context_def::Context_def (Context_def const &s)
37   : Input (s)
38 {
39   context_aliases_ = SCM_EOL;
40   translator_group_type_ = SCM_EOL;
41   accept_mods_ = SCM_EOL;
42   translator_mods_ = SCM_EOL;
43   property_ops_ = SCM_EOL;
44   context_name_ = SCM_EOL;
45   description_ = SCM_EOL;
46   default_child_ = SCM_EOL;
47
48   smobify_self ();
49   description_ = s.description_;
50
51   default_child_ = s.default_child_;
52   accept_mods_ = s.accept_mods_;
53   property_ops_ = s.property_ops_;
54   translator_mods_ = s.translator_mods_;
55   context_aliases_ = s.context_aliases_;
56   translator_group_type_ = s.translator_group_type_;
57   context_name_ = s.context_name_;
58 }
59
60 Context_def::~Context_def ()
61 {
62 }
63
64 #include "ly-smobs.icc"
65 IMPLEMENT_SMOBS (Context_def);
66 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
67
68 int
69 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
70 {
71   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
72
73   scm_puts ("#<Context_def ", port);
74   scm_display (me->context_name_, port);
75   scm_puts (">", port);
76   return 1;
77 }
78
79 SCM
80 Context_def::mark_smob (SCM smob)
81 {
82   Context_def *me = (Context_def *) SCM_CELL_WORD_1 (smob);
83
84   scm_gc_mark (me->description_);
85   scm_gc_mark (me->context_aliases_);
86   scm_gc_mark (me->accept_mods_);
87   scm_gc_mark (me->translator_mods_);
88   scm_gc_mark (me->property_ops_);
89   scm_gc_mark (me->translator_group_type_);
90   scm_gc_mark (me->default_child_);
91
92   return me->context_name_;
93 }
94
95 void
96 Context_def::add_context_mod (SCM mod)
97 {
98   SCM tag = scm_car (mod);
99   if (ly_symbol2scm ("description") == tag)
100     {
101       description_ = scm_cadr (mod);
102       return;
103     }
104
105   /*
106     other modifiers take symbols as argument.
107   */
108   SCM sym = scm_cadr (mod);
109   if (scm_is_string (sym))
110     sym = scm_string_to_symbol (sym);
111
112   if (ly_symbol2scm ("default-child") == tag)
113     default_child_ = sym;
114   else if (ly_symbol2scm ("consists") == tag
115            || ly_symbol2scm ("consists-end") == tag
116            || ly_symbol2scm ("remove") == tag)
117     {
118       if (!get_translator (sym))
119         error (_f ("program has no such type: `%s'",
120                    ly_symbol2string (sym).c_str ()));
121       else
122         translator_mods_ = scm_cons (scm_list_2 (tag, sym), translator_mods_);
123     }
124   else if (ly_symbol2scm ("accepts") == tag
125            || ly_symbol2scm ("denies") == tag)
126     accept_mods_ = scm_cons (scm_list_2 (tag, sym), accept_mods_);
127   else if (ly_symbol2scm ("pop") == tag
128            || ly_symbol2scm ("push") == tag
129            || ly_symbol2scm ("assign") == tag
130            || ly_symbol2scm ("unset") == tag)
131     property_ops_ = scm_cons (mod, property_ops_);
132   else if (ly_symbol2scm ("alias") == tag)
133     context_aliases_ = scm_cons (sym, context_aliases_);
134   else if (ly_symbol2scm ("translator-type") == tag)
135     translator_group_type_ = sym;
136   else if (ly_symbol2scm ("context-name") == tag)
137     context_name_ = sym;
138   else
139     programming_error ("unknown context mod tag");
140 }
141
142 SCM
143 Context_def::get_accepted (SCM user_mod) const
144 {
145   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_), user_mod);
146   SCM acc = SCM_EOL;
147   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
148     {
149       SCM tag = scm_caar (s);
150       SCM sym = scm_cadar (s);
151       if (tag == ly_symbol2scm ("accepts"))
152         acc = scm_cons (sym, acc);
153       else if (tag == ly_symbol2scm ("denies"))
154         acc = scm_delete_x (sym, acc);
155     }
156
157   SCM def = get_default_child (user_mod);
158   if (scm_is_symbol (def))
159     {
160       if (scm_memq (def, acc))
161         acc = scm_delete_x (def, acc);
162       acc = scm_cons (def, acc);
163     }
164
165   return acc;
166 }
167
168 SCM
169 Context_def::get_default_child (SCM user_mod) const
170 {
171   SCM name = default_child_;
172   for (SCM s = user_mod; scm_is_pair (s); s = scm_cdr (s))
173     {
174       SCM entry = scm_car (s);
175       if (scm_car (entry) == ly_symbol2scm ("default-child"))
176         {
177           name = scm_cadr (entry);
178           break;
179         }
180     }
181
182   return name;
183 }
184
185 vector<Context_def*>
186 Context_def::path_to_acceptable_context (SCM type_sym, Output_def *odef) const
187 {
188   assert (scm_is_symbol (type_sym));
189
190   SCM accepted = get_accepted (SCM_EOL);
191
192   vector<Context_def*> accepteds;
193   for (SCM s = accepted; scm_is_pair (s); s = scm_cdr (s))
194     if (Context_def *t = unsmob_context_def (find_context_def (odef,
195                                                                scm_car (s))))
196       accepteds.push_back (t);
197
198   vector<Context_def*> best_result;
199   for (vsize i = 0; i < accepteds.size (); i++)
200     {
201       /* do not check aliases, because \context Staff should not
202          create RhythmicStaff. */
203       if (ly_is_equal (accepteds[i]->get_context_name (), type_sym))
204         {
205           best_result.push_back (accepteds[i]);
206           return best_result;
207         }
208     }
209
210   vsize best_depth = INT_MAX;
211   for (vsize i = 0; i < accepteds.size (); i++)
212     {
213       Context_def *g = accepteds[i];
214
215       vector<Context_def*> result
216         = g->path_to_acceptable_context (type_sym, odef);
217       if (result.size () && result.size () < best_depth)
218         {
219           best_depth = result.size ();
220           result.insert (result.begin (), g);
221           best_result = result;
222         }
223     }
224
225   return best_result;
226 }
227
228 SCM
229 Context_def::get_translator_names (SCM user_mod) const
230 {
231   SCM l1 = SCM_EOL;
232
233   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_), user_mod);
234
235   for (SCM s = mods; scm_is_pair (s); s = scm_cdr (s))
236     {
237       SCM tag = scm_caar (s);
238       SCM arg = scm_cadar (s);
239
240       if (scm_is_string (arg))
241         arg = scm_string_to_symbol (arg);
242
243       if (ly_symbol2scm ("consists") == tag)
244         l1 = scm_cons (arg, l1);
245       else if (ly_symbol2scm ("remove") == tag)
246         l1 = scm_delete_x (arg, l1);
247     }
248
249   return l1;
250 }
251
252 Context *
253 Context_def::instantiate (SCM ops, Object_key const *key)
254 {
255   Context *context = new Context (key);
256
257   context->definition_ = self_scm ();
258   context->definition_mods_ = ops;
259   context->aliases_ = context_aliases_;
260   context->accepts_list_ = get_accepted (ops);
261
262   return context;
263 }
264
265 SCM
266 Context_def::make_scm ()
267 {
268   Context_def *t = new Context_def;
269   return t->unprotect ();
270 }
271
272 void
273 Context_def::apply_default_property_operations (Context *tg)
274 {
275   apply_property_operations (tg, property_ops_);
276 }
277
278 SCM
279 Context_def::to_alist () const
280 {
281   SCM ell = SCM_EOL;
282
283   ell = scm_cons (scm_cons (ly_symbol2scm ("consists"),
284                             get_translator_names (SCM_EOL)), ell);
285   ell = scm_cons (scm_cons (ly_symbol2scm ("description"), description_), ell);
286   ell = scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_), ell);
287   ell = scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL)),
288                   ell);
289   ell = scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_),
290                   ell);
291   ell = scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_),
292                   ell);
293
294   if (scm_is_symbol (translator_group_type_))
295     ell = scm_cons (scm_cons (ly_symbol2scm ("group-type"),
296                               translator_group_type_), ell);
297   return ell;
298 }
299