2 context-property.cc -- implement manipulation of immutable Grob
5 source file of the GNU LilyPond music typesetter
7 (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "engraver.hh"
12 #include "international.hh"
15 #include "simple-closure.hh"
20 copy ALIST leaving out SYMBOL. Copying stops at ALIST_END
23 evict_from_alist (SCM symbol,
27 SCM new_alist = SCM_EOL;
28 SCM *tail = &new_alist;
30 while (alist != alist_end)
32 if (ly_is_equal (scm_caar (alist), symbol))
34 alist = scm_cdr (alist);
38 *tail = scm_cons (scm_car (alist), SCM_EOL);
39 tail = SCM_CDRLOC (*tail);
40 alist = scm_cdr (alist);
48 general_pushpop_property (Context *context,
50 SCM grob_property_path,
54 if (!scm_is_symbol (context_property)
55 || !scm_is_symbol (scm_car (grob_property_path)))
57 warning (_ ("need symbol arguments for \\override and \\revert"));
58 if (do_internal_type_checking_global)
62 execute_general_pushpop_property (context, context_property,
63 grob_property_path, new_value);
69 Grob descriptions (ie. alists with layout properties) are
70 represented as a (ALIST . BASED-ON) pair, where BASED-ON is the
71 alist defined in a parent context. BASED-ON should always be a tail
74 Push or pop (depending on value of VAL) a single entry entry from a
75 translator property list by name of PROP. GROB_PROPERTY_PATH
76 indicates nested alists, eg. '(beamed-stem-lengths details)
82 execute_override_property (Context *context,
84 SCM grob_property_path,
87 SCM current_context_val = SCM_EOL;
89 Context *where = context->where_defined (context_property,
90 ¤t_context_val);
100 SCM base = updated_grob_properties (context, context_property);
101 current_context_val = scm_cons (base, base);
102 context->set_property (context_property, current_context_val);
105 if (!scm_is_pair (current_context_val))
107 programming_error ("Grob definition should be cons");
111 SCM symbol = scm_car (grob_property_path);
112 SCM target_alist = scm_car (current_context_val);
113 if (scm_is_pair (scm_cdr (grob_property_path)))
115 new_value = nested_property_alist (ly_assoc_get (symbol, target_alist,
117 scm_cdr (grob_property_path),
121 if (scm_is_pair (target_alist)
122 && scm_caar (target_alist) == symbol)
123 target_alist = scm_cdr (target_alist);
125 target_alist = scm_acons (symbol, new_value, target_alist);
128 if (!ly_is_procedure (new_value)
129 && !is_simple_closure (new_value))
130 ok = type_check_assignment (symbol, new_value,
131 ly_symbol2scm ("backend-type?"));
134 tack onto alist. We can use set_car, since
135 updated_grob_properties () in child contexts will check
136 for changes in the car.
140 scm_set_car_x (current_context_val, target_alist);
146 execute_general_pushpop_property (Context *context,
147 SCM context_property,
148 SCM grob_property_path,
152 if (new_value != SCM_UNDEFINED)
154 execute_override_property (context, context_property,
164 SCM current_context_val = SCM_EOL;
165 if (context->where_defined (context_property, ¤t_context_val) == context)
167 SCM current_value = scm_car (current_context_val);
168 SCM daddy = scm_cdr (current_context_val);
170 if (!scm_is_pair (grob_property_path)
171 || !scm_is_symbol (scm_car (grob_property_path)))
173 programming_error ("Grob property path should be list of symbols.");
177 SCM symbol = scm_car (grob_property_path);
178 SCM new_alist = evict_from_alist (symbol, current_value, daddy);
180 if (new_alist == daddy)
181 context->unset_property (context_property);
183 context->set_property (context_property, scm_cons (new_alist, daddy));
188 execute_pushpop_property (Context *context,
189 SCM context_property,
194 general_pushpop_property (context, context_property,
195 scm_list_1 (grob_property),
200 PRE_INIT_OPS is in the order specified, and hence must be reversed.
203 apply_property_operations (Context *tg, SCM pre_init_ops)
205 SCM correct_order = scm_reverse (pre_init_ops);
206 for (SCM s = correct_order; scm_is_pair (s); s = scm_cdr (s))
208 SCM entry = scm_car (s);
209 SCM type = scm_car (entry);
210 entry = scm_cdr (entry);
212 if (type == ly_symbol2scm ("push"))
214 SCM context_prop = scm_car (entry);
215 SCM val = scm_cadr (entry);
216 SCM grob_prop_path = scm_cddr (entry);
217 execute_general_pushpop_property (tg, context_prop, grob_prop_path, val);
219 else if (type == ly_symbol2scm ("pop"))
221 SCM context_prop = scm_car (entry);
222 SCM val = SCM_UNDEFINED;
223 SCM grob_prop_path = scm_cdr (entry);
224 execute_general_pushpop_property (tg, context_prop, grob_prop_path, val);
226 else if (type == ly_symbol2scm ("assign"))
227 tg->set_property (scm_car (entry), scm_cadr (entry));
232 Return the object alist for SYM, checking if its base in enclosing
233 contexts has changed. The alist is updated if necessary.
236 updated_grob_properties (Context *tg, SCM sym)
238 assert (scm_is_symbol (sym));
241 tg = tg->where_defined (sym, &props);
246 = (tg->get_parent_context ())
247 ? updated_grob_properties (tg->get_parent_context (), sym)
250 if (!scm_is_pair (props))
252 programming_error ("grob props not a pair?");
256 SCM based_on = scm_cdr (props);
257 if (based_on == daddy_props)
258 return scm_car (props);
261 SCM copy = daddy_props;
263 SCM p = scm_car (props);
264 while (p != based_on)
266 *tail = scm_cons (scm_car (p), daddy_props);
267 tail = SCM_CDRLOC (*tail);
271 scm_set_car_x (props, copy);
272 scm_set_cdr_x (props, daddy_props);