2 context-def.cc -- implement Context_def
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 /* TODO: should junk this class an replace by
10 a single list of context modifications? */
12 #include "context-def.hh"
15 #include "international.hh"
16 #include "output-def.hh"
17 #include "translator.hh"
20 Context_def::Context_def ()
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
;
34 input_location_
= make_input (Input ());
35 context_name_
= ly_symbol2scm ("");
39 Context_def::origin () const
41 return unsmob_input (input_location_
);
44 Context_def::Context_def (Context_def
const &s
)
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
;
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_
;
68 Context_def::~Context_def ()
72 #include "ly-smobs.icc"
73 IMPLEMENT_SMOBS (Context_def
);
74 IMPLEMENT_DEFAULT_EQUAL_P (Context_def
);
77 Context_def::print_smob (SCM smob
, SCM port
, scm_print_state
*)
79 Context_def
*me
= (Context_def
*) SCM_CELL_WORD_1 (smob
);
81 scm_puts ("#<Context_def ", port
);
82 scm_display (me
->context_name_
, port
);
88 Context_def::mark_smob (SCM smob
)
90 ASSERT_LIVE_IS_ALLOWED ();
92 Context_def
*me
= (Context_def
*) SCM_CELL_WORD_1 (smob
);
94 scm_gc_mark (me
->description_
);
95 scm_gc_mark (me
->context_aliases_
);
96 scm_gc_mark (me
->accept_mods_
);
97 scm_gc_mark (me
->translator_mods_
);
98 scm_gc_mark (me
->property_ops_
);
99 scm_gc_mark (me
->translator_group_type_
);
100 scm_gc_mark (me
->default_child_
);
101 scm_gc_mark (me
->input_location_
);
103 return me
->context_name_
;
107 Context_def::add_context_mod (SCM mod
)
109 SCM tag
= scm_car (mod
);
110 if (ly_symbol2scm ("description") == tag
)
112 description_
= scm_cadr (mod
);
117 other modifiers take symbols as argument.
119 SCM sym
= scm_cadr (mod
);
120 if (scm_is_string (sym
))
121 sym
= scm_string_to_symbol (sym
);
123 if (ly_symbol2scm ("default-child") == tag
)
124 default_child_
= sym
;
125 else if (ly_symbol2scm ("consists") == tag
126 || ly_symbol2scm ("remove") == tag
)
128 if (!get_translator (sym
))
129 warning (_f ("program has no such type: `%s'",
130 ly_symbol2string (sym
).c_str ()));
132 translator_mods_
= scm_cons (scm_list_2 (tag
, sym
), translator_mods_
);
134 else if (ly_symbol2scm ("accepts") == tag
135 || ly_symbol2scm ("denies") == tag
)
136 accept_mods_
= scm_cons (scm_list_2 (tag
, sym
), accept_mods_
);
137 else if (ly_symbol2scm ("pop") == tag
138 || ly_symbol2scm ("push") == tag
139 || ly_symbol2scm ("assign") == tag
140 || ly_symbol2scm ("unset") == tag
)
141 property_ops_
= scm_cons (mod
, property_ops_
);
142 else if (ly_symbol2scm ("alias") == tag
)
143 context_aliases_
= scm_cons (sym
, context_aliases_
);
144 else if (ly_symbol2scm ("translator-type") == tag
)
145 translator_group_type_
= sym
;
146 else if (ly_symbol2scm ("context-name") == tag
)
149 programming_error ("unknown context mod tag");
153 Context_def::get_accepted (SCM user_mod
) const
155 SCM mods
= scm_reverse_x (scm_list_copy (accept_mods_
), user_mod
);
157 for (SCM s
= mods
; scm_is_pair (s
); s
= scm_cdr (s
))
159 SCM tag
= scm_caar (s
);
160 SCM sym
= scm_cadar (s
);
161 if (tag
== ly_symbol2scm ("accepts"))
162 acc
= scm_cons (sym
, acc
);
163 else if (tag
== ly_symbol2scm ("denies"))
164 acc
= scm_delete_x (sym
, acc
);
167 SCM def
= get_default_child (user_mod
);
168 if (scm_is_symbol (def
))
170 if (scm_memq (def
, acc
))
171 acc
= scm_delete_x (def
, acc
);
172 acc
= scm_cons (def
, acc
);
179 Context_def::get_default_child (SCM user_mod
) const
181 SCM name
= default_child_
;
182 for (SCM s
= user_mod
; scm_is_pair (s
); s
= scm_cdr (s
))
184 SCM entry
= scm_car (s
);
185 if (scm_car (entry
) == ly_symbol2scm ("default-child"))
187 name
= scm_cadr (entry
);
196 Given a name of a context that we want to create, finds a list of context
197 definitions such that:
198 - the first element in the list defines a context that is a valid child of
199 the context defined by this Context_def
200 - each subsequent element in the list defines a context that is a valid child
201 of the the context defined by the preceding element in the list
202 - the last element in the list defines a context with the given name
204 The ADDITIONAL_ACCEPTS parameter is a list of additional contexts that this
205 specific context def (but not any of the child context defs) should accept.
207 vector
<Context_def
*>
208 Context_def::path_to_acceptable_context (SCM type_sym
,
210 SCM additional_accepts
) const
212 set
<const Context_def
*> seen
;
213 return internal_path_to_acceptable_context (type_sym
, odef
, additional_accepts
, &seen
);
217 The SEEN parameter is a set which keeps track of visited contexts, allowing
218 contexts of the same type to be nested.
221 Context_def::internal_path_to_acceptable_context (SCM type_sym
,
223 SCM additional_accepts
,
224 set
<const Context_def
*> *seen
) const
226 assert (scm_is_symbol (type_sym
));
228 SCM accepted
= get_accepted (additional_accepts
);
230 vector
<Context_def
*> accepteds
;
231 for (SCM s
= accepted
; scm_is_pair (s
); s
= scm_cdr (s
))
232 if (Context_def
*t
= unsmob_context_def (find_context_def (odef
,
234 accepteds
.push_back (t
);
236 vector
<Context_def
*> best_result
;
237 for (vsize i
= 0; i
< accepteds
.size (); i
++)
239 /* do not check aliases, because \context Staff should not
240 create RhythmicStaff. */
241 if (ly_is_equal (accepteds
[i
]->get_context_name (), type_sym
))
243 best_result
.push_back (accepteds
[i
]);
249 vsize best_depth
= INT_MAX
;
250 for (vsize i
= 0; i
< accepteds
.size (); i
++)
252 Context_def
*g
= accepteds
[i
];
254 if (!seen
->count (g
))
256 vector
<Context_def
*> result
257 = g
->internal_path_to_acceptable_context (type_sym
, odef
, SCM_EOL
, seen
);
258 if (result
.size () && result
.size () < best_depth
)
260 best_depth
= result
.size ();
261 result
.insert (result
.begin (), g
);
262 best_result
= result
;
272 Context_def::get_translator_names (SCM user_mod
) const
276 SCM mods
= scm_reverse_x (scm_list_copy (translator_mods_
), user_mod
);
278 for (SCM s
= mods
; scm_is_pair (s
); s
= scm_cdr (s
))
280 SCM tag
= scm_caar (s
);
281 SCM arg
= scm_cadar (s
);
283 if (scm_is_string (arg
))
284 arg
= scm_string_to_symbol (arg
);
286 if (ly_symbol2scm ("consists") == tag
)
287 l1
= scm_cons (arg
, l1
);
288 else if (ly_symbol2scm ("remove") == tag
289 && get_translator (arg
))
290 l1
= scm_delete_x (arg
, l1
);
297 Context_def::instantiate (SCM ops
)
299 Context
*context
= new Context ();
301 context
->definition_
= self_scm ();
302 context
->definition_mods_
= ops
;
303 context
->aliases_
= context_aliases_
;
304 context
->accepts_list_
= get_accepted (ops
);
310 Context_def::make_scm ()
312 Context_def
*t
= new Context_def
;
313 return t
->unprotect ();
317 Context_def::apply_default_property_operations (Context
*tg
)
319 apply_property_operations (tg
, property_ops_
);
323 Context_def::to_alist () const
327 ell
= scm_cons (scm_cons (ly_symbol2scm ("consists"),
328 get_translator_names (SCM_EOL
)), ell
);
329 ell
= scm_cons (scm_cons (ly_symbol2scm ("description"), description_
), ell
);
330 ell
= scm_cons (scm_cons (ly_symbol2scm ("aliases"), context_aliases_
), ell
);
331 ell
= scm_cons (scm_cons (ly_symbol2scm ("accepts"), get_accepted (SCM_EOL
)),
333 ell
= scm_cons (scm_cons (ly_symbol2scm ("property-ops"), property_ops_
),
335 ell
= scm_cons (scm_cons (ly_symbol2scm ("context-name"), context_name_
),
338 if (scm_is_symbol (translator_group_type_
))
339 ell
= scm_cons (scm_cons (ly_symbol2scm ("group-type"),
340 translator_group_type_
), ell
);