2 context-property.cc -- implement manipulation of immutable Grob
5 source file of the GNU LilyPond music typesetter
7 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "engraver.hh"
18 Grob descriptions (ie. alists with layout properties) are
19 represented as a (ALIST . BASED-ON) pair, where BASED-ON is the
20 alist defined in a parent context. BASED-ON should always be a tail
26 Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
27 entry from a translator property list by name of PROP
32 execute_pushpop_property (Context * trg,
33 SCM prop, SCM eltprop, SCM val)
35 if (scm_is_symbol (prop) && scm_is_symbol (eltprop))
37 if (val != SCM_UNDEFINED)
40 Context * where = trg->where_defined (prop);
50 SCM base = updated_grob_properties (trg, prop);
51 prev = scm_cons (base, base);
52 trg->internal_set_property (prop, prev);
55 prev = trg->internal_get_property (prop);
57 if (!scm_is_pair (prev))
59 programming_error ("Grob definition should be cons.");
63 SCM prev_alist = scm_car (prev);
65 if (scm_is_pair (prev_alist) || prev_alist == SCM_EOL)
67 bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?"));
73 scm_set_car_x (prev, scm_acons (eltprop, val, prev_alist));
80 else if (trg->where_defined (prop) == trg)
82 SCM prev = trg->internal_get_property (prop);
83 SCM prev_alist = scm_car (prev);
84 SCM daddy = scm_cdr (prev);
86 SCM new_alist = SCM_EOL;
87 SCM *tail = &new_alist;
89 while (prev_alist != daddy)
91 if (ly_c_equal_p (scm_caar (prev_alist), eltprop))
93 prev_alist = scm_cdr (prev_alist);
98 *tail = scm_cons (scm_car (prev_alist), SCM_EOL);
99 tail = SCM_CDRLOC (*tail);
100 prev_alist = scm_cdr (prev_alist);
103 if (new_alist == SCM_EOL && prev_alist == daddy)
104 trg->unset_property (prop);
108 trg->internal_set_property (prop, scm_cons (new_alist, daddy));
114 warning ("Need symbol arguments for \\override and \\revert");
115 if (internal_type_checking_global_b)
121 PRE_INIT_OPS is in the order specified, and hence must be reversed.
124 apply_property_operations (Context *tg, SCM pre_init_ops)
126 SCM correct_order = scm_reverse (pre_init_ops);
127 for (SCM s = correct_order; scm_is_pair (s); s = scm_cdr (s))
129 SCM entry = scm_car (s);
130 SCM type = scm_car (entry);
131 entry = scm_cdr (entry);
133 if (type == ly_symbol2scm ("push") || type == ly_symbol2scm ("poppush"))
135 SCM val = scm_cddr (entry);
136 val = scm_is_pair (val) ? scm_car (val) : SCM_UNDEFINED;
138 execute_pushpop_property (tg, scm_car (entry), scm_cadr (entry), val);
140 else if (type == ly_symbol2scm ("assign"))
142 tg->internal_set_property (scm_car (entry), scm_cadr (entry));
148 Return the object alist for SYM, checking if its base in enclosing
149 contexts has changed. The alist is updated if necessary.
152 updated_grob_properties (Context * tg, SCM sym)
154 assert (scm_is_symbol (sym));
156 tg = tg->where_defined (sym);
161 = (tg->get_parent_context ())
162 ? updated_grob_properties (tg->get_parent_context (), sym)
165 SCM props = tg->internal_get_property (sym);
167 if (!scm_is_pair (props))
169 programming_error ("grob props not a pair?");
173 SCM based_on = scm_cdr (props);
174 if (based_on == daddy_props)
176 return scm_car (props);
180 SCM copy = daddy_props;
182 SCM p = scm_car (props);
183 while (p != based_on)
185 *tail = scm_cons (scm_car (p), daddy_props);
186 tail = SCM_CDRLOC (*tail);
190 scm_set_car_x (props, copy);
191 scm_set_cdr_x (props, daddy_props);
198 make_item_from_properties (Engraver *tr, SCM x, SCM cause, const char * name)
200 Context *context = tr->context ();
202 SCM props = updated_grob_properties (context, x);
204 Object_key const*key = context->get_grob_key (name);
205 Item *it = new Item (props, key);
207 dynamic_cast<Engraver*>(tr)->announce_grob (it, cause);
213 make_spanner_from_properties (Engraver *tr, SCM x, SCM cause, const char *name)
215 Context *context = tr->context ();
217 SCM props = updated_grob_properties (context, x);
218 Spanner *it = new Spanner (props, context->get_grob_key (name));
220 dynamic_cast<Engraver*>(tr)->announce_grob (it, cause);