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