2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "engraver.hh"
22 #include "international.hh"
25 #include "simple-closure.hh"
27 #include "unpure-pure-container.hh"
31 like execute_general_pushpop_property(), but typecheck
32 grob_property_path and context_property.
35 general_pushpop_property (Context *context,
37 SCM grob_property_path,
40 if (!scm_is_symbol (context_property)
41 || !scm_is_symbol (scm_car (grob_property_path)))
43 warning (_ ("need symbol arguments for \\override and \\revert"));
44 if (do_internal_type_checking_global)
48 sloppy_general_pushpop_property (context, context_property,
49 grob_property_path, new_value);
53 Grob descriptions (ie. alists with layout properties) are
54 represented as a (ALIST . BASED-ON) pair, where BASED-ON is the
55 alist defined in a parent context. BASED-ON should always be a tail
58 Push or pop (depending on value of VAL) a single entry from a
59 translator property list by name of PROP. GROB_PROPERTY_PATH
60 indicates nested alists, eg. '(beamed-stem-lengths details)
63 execute_override_property (Context *context,
65 SCM grob_property_path,
68 SCM current_context_val = SCM_EOL;
70 Context *where = context->where_defined (context_property,
71 ¤t_context_val);
81 SCM base = updated_grob_properties (context, context_property);
82 current_context_val = scm_cons (base, base);
83 context->set_property (context_property, current_context_val);
86 if (!scm_is_pair (current_context_val))
88 programming_error ("Grob definition should be cons");
92 SCM target_alist = scm_car (current_context_val);
94 SCM symbol = scm_car (grob_property_path);
95 if (scm_is_pair (scm_cdr (grob_property_path)))
97 new_value = nested_property_alist (ly_assoc_get (symbol, target_alist,
99 scm_cdr (grob_property_path),
103 /* it's tempting to replace the head of the list if it's the same
104 property. However, we have to keep this info around, in case we have to
107 target_alist = scm_acons (symbol, new_value, target_alist);
110 bool pc = is_unpure_pure_container (new_value);
111 SCM vals[] = {pc ? unpure_pure_container_unpure_part (new_value) : new_value,
112 pc ? unpure_pure_container_pure_part (new_value) : SCM_BOOL_F
115 for (int i = 0; i < 2; i++)
116 if (!ly_is_procedure (vals[i])
117 && !is_simple_closure (vals[i]))
118 ok = ok && type_check_assignment (symbol, vals[i],
119 ly_symbol2scm ("backend-type?"));
122 tack onto alist. We can use set_car, since
123 updated_grob_properties () in child contexts will check
124 for changes in the car.
128 scm_set_car_x (current_context_val, target_alist);
133 do a pop (indicated by new_value==SCM_UNDEFINED) or push
136 sloppy_general_pushpop_property (Context *context,
137 SCM context_property,
138 SCM grob_property_path,
141 if (new_value == SCM_UNDEFINED)
142 execute_revert_property (context, context_property,
145 execute_override_property (context, context_property,
151 Revert the property given by property_path.
154 execute_revert_property (Context *context,
155 SCM context_property,
156 SCM grob_property_path)
158 SCM current_context_val = SCM_EOL;
159 if (context->where_defined (context_property, ¤t_context_val)
162 SCM current_alist = scm_car (current_context_val);
163 SCM daddy = scm_cdr (current_context_val);
165 if (!scm_is_pair (grob_property_path)
166 || !scm_is_symbol (scm_car (grob_property_path)))
168 programming_error ("Grob property path should be list of symbols.");
172 SCM symbol = scm_car (grob_property_path);
173 if (scm_is_pair (scm_cdr (grob_property_path)))
175 SCM current_sub_alist = ly_assoc_get (symbol, current_alist, SCM_EOL);
177 = nested_property_revert_alist (current_sub_alist,
178 scm_cdr (grob_property_path));
180 if (scm_is_pair (current_alist)
181 && scm_caar (current_alist) == symbol
182 && current_alist != daddy)
183 current_alist = scm_cdr (current_alist);
185 current_alist = scm_acons (symbol, new_val, current_alist);
186 scm_set_car_x (current_context_val, current_alist);
190 SCM new_alist = evict_from_alist (symbol, current_alist, daddy);
192 if (new_alist == daddy)
193 context->unset_property (context_property);
195 context->set_property (context_property,
196 scm_cons (new_alist, daddy));
201 Convenience: a push/pop grob property using a single grob_property
205 execute_pushpop_property (Context *context,
206 SCM context_property,
210 general_pushpop_property (context, context_property,
211 scm_list_1 (grob_property),
216 PRE_INIT_OPS is in the order specified, and hence must be reversed.
219 apply_property_operations (Context *tg, SCM pre_init_ops)
221 for (SCM s = pre_init_ops; scm_is_pair (s); s = scm_cdr (s))
223 SCM entry = scm_car (s);
224 SCM type = scm_car (entry);
225 entry = scm_cdr (entry);
227 if (type == ly_symbol2scm ("push"))
229 SCM context_prop = scm_car (entry);
230 SCM val = scm_cadr (entry);
231 SCM grob_prop_path = scm_cddr (entry);
232 sloppy_general_pushpop_property (tg, context_prop, grob_prop_path, val);
234 else if (type == ly_symbol2scm ("pop"))
236 SCM context_prop = scm_car (entry);
237 SCM val = SCM_UNDEFINED;
238 SCM grob_prop_path = scm_cdr (entry);
239 sloppy_general_pushpop_property (tg, context_prop, grob_prop_path, val);
241 else if (type == ly_symbol2scm ("assign"))
242 tg->set_property (scm_car (entry), scm_cadr (entry));
243 else if (type == ly_symbol2scm ("apply"))
244 scm_apply_1 (scm_car (entry), tg->self_scm (), scm_cdr (entry));
245 else if (type == ly_symbol2scm ("unset"))
246 tg->unset_property (scm_car (entry));
251 Return the object alist for SYM, checking if its base in enclosing
252 contexts has changed. The alist is updated if necessary.
255 updated_grob_properties (Context *tg, SCM sym)
257 assert (scm_is_symbol (sym));
260 tg = tg->where_defined (sym, &props);
265 = (tg->get_parent_context ())
266 ? updated_grob_properties (tg->get_parent_context (), sym)
269 if (!scm_is_pair (props))
271 programming_error ("grob props not a pair?");
275 SCM based_on = scm_cdr (props);
276 if (based_on == daddy_props)
277 return scm_car (props);
280 SCM copy = daddy_props;
282 SCM p = scm_car (props);
283 while (p != based_on)
285 *tail = scm_cons (scm_car (p), daddy_props);
286 tail = SCM_CDRLOC (*tail);
290 scm_set_car_x (props, copy);
291 scm_set_cdr_x (props, daddy_props);