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