]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-def.cc
89b5536a50c0ed57b0644c8161a9da55afbd2c37
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 /*
11   TODO: should junk this class an replace by
12   a single list of context modifications?
13  */
14
15 #include "lily-proto.hh"
16 #include "context-def.hh"
17 #include "translator-group.hh"
18 #include "warn.hh"
19 #include "music-output-def.hh"
20 #include "ly-smobs.icc"
21 #include "score-context.hh"
22
23 int
24 Context_def::print_smob (SCM smob, SCM port, scm_print_state*)
25 {
26   Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
27
28   scm_puts ("#<Context_def ", port);
29   scm_display (me->context_name_, port);
30   scm_puts (">", port);
31   return 1;
32 }
33
34
35 SCM
36 Context_def::mark_smob (SCM smob)
37 {
38   Context_def* me = (Context_def*) SCM_CELL_WORD_1 (smob);
39
40   scm_gc_mark (me->description_);
41   scm_gc_mark (me->context_aliases_);
42   scm_gc_mark (me->accept_mods_);
43   scm_gc_mark (me->translator_mods_);
44   scm_gc_mark (me->property_ops_);  
45   scm_gc_mark (me->translator_group_type_);
46
47   return me->context_name_;
48 }
49
50
51 Context_def::Context_def ()
52 {
53   context_aliases_ = SCM_EOL;
54   translator_group_type_ = SCM_EOL;
55   accept_mods_ = SCM_EOL;
56   translator_mods_ = SCM_EOL;
57   property_ops_ = SCM_EOL;
58   context_name_ = SCM_EOL;
59   description_ = SCM_EOL;
60
61   smobify_self ();
62 }
63
64 Context_def::~Context_def ()
65 {
66 }
67
68 Context_def::Context_def (Context_def const & s)
69   : Input (s)
70 {
71   context_aliases_ = SCM_EOL;
72   translator_group_type_ = SCM_EOL;
73   accept_mods_ = SCM_EOL;   
74   translator_mods_ = SCM_EOL;
75   property_ops_ = SCM_EOL;
76   context_name_ = SCM_EOL;
77   description_ = SCM_EOL;
78   
79   smobify_self ();
80   description_ = s.description_;
81
82   accept_mods_ = s.accept_mods_;
83   property_ops_ = s.property_ops_;
84   translator_mods_ = s.translator_mods_;
85   context_aliases_ = s.context_aliases_;
86   translator_group_type_ = s.translator_group_type_;
87   context_name_ = s.context_name_;
88 }
89
90
91 void
92 Context_def::add_context_mod (SCM mod)
93 {
94   SCM tag  = gh_car (mod);
95   if (ly_symbol2scm ("description")  == tag)
96     {
97       description_ = gh_cadr (mod);
98       return ;
99     }
100
101   SCM sym = gh_cadr (mod);
102   if (gh_string_p (sym))
103     sym = scm_string_to_symbol (sym);
104   
105   if (ly_symbol2scm ("consists") == tag
106       || ly_symbol2scm ("consists-end") == tag
107       || ly_symbol2scm ("remove") == tag)
108     {
109       if (!get_translator (sym))
110         error (_f ("Program has no such type: `%s'", ly_symbol2string (sym).to_str0 ()));
111       else
112         translator_mods_ = gh_cons (scm_list_2 (tag, sym), translator_mods_ );
113     }
114   else if (ly_symbol2scm ("accepts") == tag
115            || ly_symbol2scm ("denies") == tag)
116     {
117       accept_mods_ = gh_cons (scm_list_2 (tag, sym), accept_mods_); 
118     }
119   else if (ly_symbol2scm ("poppush") == tag
120            || ly_symbol2scm ("pop") == tag
121            || ly_symbol2scm ("push") == tag
122            || ly_symbol2scm ("assign") == tag
123            || ly_symbol2scm ("unset") == tag)
124     {
125       property_ops_ = gh_cons (mod, property_ops_);
126     }
127   else if (ly_symbol2scm ("alias") == tag)
128     {
129       context_aliases_ = gh_cons (sym, context_aliases_);
130     }
131   else if (ly_symbol2scm ("translator-type")  == tag)
132     {
133       translator_group_type_ = sym;
134     }
135   else if (ly_symbol2scm ("context-name")  == tag)
136     {
137       context_name_ = sym;
138     }
139   else
140     {
141       programming_error ("Unknown context mod tag.");
142     }
143 }
144
145
146
147 SCM
148 Context_def::get_context_name () const
149 {
150   return context_name_;
151 }
152
153 SCM
154 Context_def::get_accepted (SCM user_mod) const
155 {
156   SCM mods = scm_reverse_x (scm_list_copy (accept_mods_),
157                             user_mod);
158   SCM acc = SCM_EOL;
159   for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
160     {
161       SCM tag = gh_caar (s);
162       SCM sym = gh_cadar (s);
163       if (tag == ly_symbol2scm ("accepts"))
164         acc = gh_cons (sym, acc);
165       else if (tag == ly_symbol2scm ("denies"))
166         acc = scm_delete_x (sym, acc);
167     }
168   return acc;
169 }
170
171            
172 Link_array<Context_def>
173 Context_def::path_to_acceptable_context (SCM type_sym, Music_output_def* odef) const
174 {
175   assert (gh_symbol_p (type_sym));
176   
177   SCM accepted = get_accepted (SCM_EOL);
178
179   Link_array<Context_def> accepteds;
180   for (SCM s = accepted; gh_pair_p (s); s = ly_cdr (s))
181     {
182       Context_def *t = unsmob_context_def (odef->find_context_def (ly_car (s)));
183       if (!t)
184         continue;
185       accepteds.push (t);
186     }
187
188   Link_array<Context_def> best_result;
189   for (int i=0; i < accepteds.size (); i++)
190     {
191       /*
192         don't check aliases, because \context Staff should not create RhythmicStaff.
193       */
194       if (gh_equal_p (accepteds[i]->get_context_name (), type_sym))
195         {
196           best_result.push (accepteds[i]);
197           return best_result;
198         }
199     }
200       
201   int best_depth= INT_MAX;
202   for (int i=0; i < accepteds.size (); i++)
203     {
204       Context_def * g = accepteds[i];
205
206       Link_array<Context_def> result
207         = g->path_to_acceptable_context (type_sym, odef);
208       if (result.size () && result.size () < best_depth)
209         {
210           result.insert (g,0);
211           best_result = result;
212
213           /*
214             this following line was added in 1.9.3, but hsould've been
215             there all along... Let's hope it doesn't cause nightmares.
216            */
217           best_depth = result.size ();
218         }
219     }
220
221   return best_result;
222 }
223
224 IMPLEMENT_SMOBS (Context_def);
225 IMPLEMENT_DEFAULT_EQUAL_P (Context_def);
226
227
228 SCM
229 Context_def::get_translator_names (SCM user_mod) const
230 {
231   SCM l1 = SCM_EOL;
232   SCM l2 = SCM_EOL;
233
234   SCM mods = scm_reverse_x (scm_list_copy (translator_mods_),
235                             user_mod);
236   
237   for (SCM s = mods; gh_pair_p (s); s = gh_cdr (s))
238     {
239       SCM tag = gh_caar (s);
240       SCM arg = gh_cadar (s);
241
242       if (gh_string_p (arg))
243         arg = scm_string_to_symbol (arg);
244       
245       if (ly_symbol2scm ("consists") == tag)
246         l1 = gh_cons (arg, l1);
247       else if (ly_symbol2scm ("consists-end") == tag)
248         l2 = gh_cons (arg, l2);
249       else if (ly_symbol2scm ("remove") == tag)
250         {
251           l1 = scm_delete_x (arg, l1);
252           l2 = scm_delete_x (arg, l2);
253         }
254     }
255
256   return scm_append_x (scm_list_2 (l1, l2));
257 }
258
259
260 Context *
261 Context_def::instantiate (SCM ops)
262 {
263   Context * tg =  0;
264
265   if (context_name_ == ly_symbol2scm ("Score"))
266     tg = new Score_context ();
267   else
268     tg = new Context ();
269
270   
271   tg->definition_ = self_scm ();
272
273   SCM trans_names = get_translator_names (ops); 
274
275   Translator * g = get_translator (translator_group_type_);
276   g = g->clone ();
277   
278   g->simple_trans_list_ = names_to_translators (trans_names, tg);
279   tg->implementation_ = g->self_scm ();
280   g->daddy_context_ = tg;
281   
282   scm_gc_unprotect_object (g->self_scm ());
283   
284   tg->accepts_list_ = get_accepted  (ops);
285   
286   return tg;
287 }
288
289
290 SCM
291 Context_def::clone_scm () const
292 {
293   Context_def * t = new Context_def (*this);
294
295   SCM x = t->self_scm ();
296   scm_gc_unprotect_object (x);
297   return x;
298 }
299
300 SCM
301 Context_def::make_scm ()
302 {
303   Context_def* t = new Context_def;
304
305   SCM x  =t->self_scm ();
306   scm_gc_unprotect_object (x);
307   return x;
308 }
309
310 void
311 Context_def::apply_default_property_operations (Context *tg)
312 {
313   apply_property_operations (tg , property_ops_);
314 }
315
316 SCM
317 Context_def::to_alist () const
318 {
319   SCM l = SCM_EOL;
320
321   l = gh_cons (gh_cons (ly_symbol2scm ("consists"),
322                         get_translator_names (SCM_EOL)), l);
323   l = gh_cons (gh_cons (ly_symbol2scm ("description"),  description_), l);
324   l = gh_cons (gh_cons (ly_symbol2scm ("aliases"),  context_aliases_), l);
325   l = gh_cons (gh_cons (ly_symbol2scm ("accepts"),  get_accepted (SCM_EOL)), l);
326   l = gh_cons (gh_cons (ly_symbol2scm ("property-ops"),  property_ops_), l);
327   l = gh_cons (gh_cons (ly_symbol2scm ("context-name"),  context_name_), l);
328
329   if (gh_symbol_p (translator_group_type_))
330     l = gh_cons (gh_cons (ly_symbol2scm ("group-type"),  translator_group_type_), l);    
331
332   return l;  
333 }
334
335 bool
336 Context_def::is_alias (SCM sym) const
337 {
338   bool b  = sym == context_name_;
339
340   for (SCM a = context_aliases_; !b && gh_pair_p (a); a = ly_cdr (a))
341     b = b || sym == ly_car (a);
342
343   return b;
344 }