]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
($(outdir)/%.pdf): add DVIPS_FLAGS. This will
[lilypond.git] / lily / context.cc
1 #include "translator-group.hh"
2 #include "context-def.hh"
3 #include "warn.hh"
4 #include "music-output-def.hh"
5 #include "scm-hash.hh"
6 #include "main.hh"
7
8 bool
9 Translator_group::is_removable () const
10 {
11   return trans_group_list_ == SCM_EOL && ! iterator_count_;
12 }
13
14 Translator_group *
15 Translator_group::find_existing_translator (SCM n, String id)
16 {
17   if ((is_alias (n) && (id_string_ == id || id.is_empty ())) || n == ly_symbol2scm ("Current"))
18     return this;
19
20   Translator_group* r = 0;
21   for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = ly_cdr (p))
22     {
23       Translator *  t = unsmob_translator (ly_car (p));
24       
25       r = dynamic_cast<Translator_group*> (t)->find_existing_translator (n, id);
26     }
27
28   return r;
29 }
30
31
32 Translator_group*
33 Translator_group::find_create_translator (SCM n, String id, SCM operations)
34 {
35   Translator_group * existing = find_existing_translator (n,id);
36   if (existing)
37     return existing;
38
39   if (n == ly_symbol2scm ("Bottom"))
40     {
41       Translator_group* tg = get_default_interpreter ();
42       tg->id_string_ = id;
43       return tg;
44     }
45
46   /*
47     TODO: use accepts_list_.
48    */
49   Link_array<Context_def> path
50     = unsmob_context_def (definition_)->path_to_acceptable_translator (n, get_output_def ());
51
52   if (path.size ())
53     {
54       Translator_group * current = this;
55
56       // start at 1.  The first one (index 0) will be us.
57       for (int i=0; i < path.size (); i++)
58         {
59           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
60
61           Translator_group * new_group
62             = path[i]->instantiate (ops);
63
64           if (i == path.size () -1)
65             {
66               new_group->id_string_ = id;
67             }
68
69           current->add_fresh_group_translator (new_group);
70           apply_property_operations (new_group, ops);
71           
72           current = new_group;
73         }
74
75       return current;
76     }
77
78   Translator_group *ret = 0;
79   if (daddy_trans_)
80     ret = daddy_trans_->find_create_translator (n, id, operations);
81   else
82     {
83       warning (_f ("Cannot find or create `%s' called `%s'",
84                    ly_symbol2string (n).to_str0 (), id));
85       ret =0;
86     }
87   return ret;
88 }
89
90 /*
91   Default child context as a SCM string, or something else if there is
92   none.
93 */
94 SCM
95 default_child_context_name (Translator_group const *tg)
96 {
97   return gh_pair_p (tg->accepts_list_)
98     ? ly_car (scm_last_pair (tg->accepts_list_))
99     : SCM_EOL;
100 }
101
102
103 bool
104 Translator_group::is_bottom_context () const
105 {
106   return !gh_symbol_p (default_child_context_name (this));
107 }
108
109 Translator_group*
110 Translator_group::get_default_interpreter ()
111 {
112   if (!is_bottom_context ())
113     {
114       SCM nm = default_child_context_name (this);
115       SCM st = get_output_def ()->find_translator (nm);
116
117       Context_def *t = unsmob_context_def (st);
118       if (!t)
119         {
120           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
121           t = unsmob_context_def (this->definition_);
122         }
123       Translator_group *tg = t->instantiate (SCM_EOL);
124       add_fresh_group_translator (tg);
125       if (!tg->is_bottom_context ())
126         return tg->get_default_interpreter ();
127       else
128         return tg;
129     }
130   return this;
131 }
132
133 /*
134   PROPERTIES
135  */
136 Translator_group*
137 Translator_group::where_defined (SCM sym) const
138 {
139   if (properties_dict ()->contains (sym))
140     {
141       return (Translator_group*)this;
142     }
143
144   return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0;
145 }
146
147 /*
148   return SCM_EOL when not found.
149 */
150 SCM
151 Translator_group::internal_get_property (SCM sym) const
152 {
153   SCM val =SCM_EOL;
154   if (properties_dict ()->try_retrieve (sym, &val))
155     return val;
156
157   if (daddy_trans_)
158     return daddy_trans_->internal_get_property (sym);
159   
160   return val;
161 }
162
163 bool
164 Translator_group::is_alias (SCM sym) const
165 {
166   if (sym == ly_symbol2scm ("Bottom")
167       && !gh_pair_p (accepts_list_))
168     return true;
169   return unsmob_context_def (definition_)->is_alias (sym);
170 }
171
172 void
173 Translator_group::internal_set_property (SCM sym, SCM val)
174 {
175 #ifndef NDEBUG
176   if (internal_type_checking_global_b)
177     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
178 #endif
179   
180   properties_dict ()->set (sym, val);
181 }
182
183 /*
184   TODO: look up to check whether we have inherited var? 
185  */
186 void
187 Translator_group::unset_property (SCM sym)
188 {
189   properties_dict ()->remove (sym);
190 }
191