2 Implement storage and manipulation of grob properties.
9 #include "pointer-group-interface.hh"
11 #include "paper-score.hh"
12 #include "output-def.hh"
14 #include "international.hh"
18 #include "program-option.hh"
20 #include "simple-closure.hh"
22 #include "protected-scm.hh"
24 Protected_scm grob_property_callback_stack = SCM_EOL;
27 extern bool debug_property_callbacks;
30 print_property_callback_stack ()
33 for (SCM s = grob_property_callback_stack; scm_is_pair (s); s = scm_cdr (s))
34 message (_f ("%d: %s", frame++, ly_scm_write_string (scm_car (s)).c_str ()));
38 static SCM modification_callback = SCM_EOL;
39 static SCM cache_callback = SCM_EOL;
44 FIXME: this should use ly:set-option interface instead.
48 LY_DEFINE (ly_set_grob_modification_callback, "ly:set-grob-modification-callback",
50 "Specify a procedure that will be called every time lilypond modifies "
51 "a grob property. The callback will receive as arguments "
52 "the grob that is being modified, "
53 "the name of the C++ file in which the modification was requested, "
54 "the line number in the C++ file in which the modification was requested, "
55 "the name of the function in which the modification was requested, "
56 "the property to be changed and "
57 "the new value for the property.")
59 modification_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
60 return SCM_UNSPECIFIED;
63 LY_DEFINE (ly_set_property_cache_callback, "ly:set-property-cache-callback",
65 "Specify a procedure that will be called whenever lilypond calculates "
66 "a callback function and caches the result. The callback will "
67 "receive as arguments "
68 "the grob whose property it is, "
69 "the name of the property, "
70 "the name of the callback that calculated the property and "
71 "the new (cached) value of the property.")
73 cache_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
74 return SCM_UNSPECIFIED;
80 Grob::instrumented_set_property (SCM sym, SCM v,
86 if (ly_is_procedure (modification_callback))
87 scm_apply_0 (modification_callback,
88 scm_list_n (self_scm (),
89 scm_from_locale_string (file),
91 scm_from_locale_string (fun),
92 sym, v, SCM_UNDEFINED));
99 internal_set_property (sym, v);
103 Grob::get_property_alist_chain (SCM def) const
105 return scm_list_n (mutable_property_alist_,
106 immutable_property_alist_,
111 extern void check_interfaces_for_property (Grob const *me, SCM sym);
114 Grob::internal_set_property (SCM sym, SCM v)
116 internal_set_value_on_alist (&mutable_property_alist_,
122 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
124 /* Perhaps we simply do the assq_set, but what the heck. */
128 if (do_internal_type_checking_global)
130 if (!ly_is_procedure (v)
131 && !is_simple_closure (v)
132 && v != ly_symbol2scm ("calculation-in-progress"))
133 type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
135 check_interfaces_for_property (this, sym);
138 *alist = scm_assq_set_x (*alist, sym, v);
142 Grob::internal_get_property_data (SCM sym) const
145 if (profile_property_accesses)
146 note_property_access (&grob_property_lookup_table, sym);
149 SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
150 if (handle != SCM_BOOL_F)
151 return scm_cdr (handle);
153 handle = scm_sloppy_assq (sym, immutable_property_alist_);
155 if (do_internal_type_checking_global && scm_is_pair (handle))
157 SCM val = scm_cdr (handle);
158 if (!ly_is_procedure (val) && !is_simple_closure (val))
159 type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
161 check_interfaces_for_property (this, sym);
164 return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
168 Grob::internal_get_property (SCM sym) const
170 SCM val = get_property_data (sym);
173 if (val == ly_symbol2scm ("calculation-in-progress"))
175 programming_error (_f ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
176 ly_symbol2string (sym).c_str (),
178 if (debug_property_callbacks)
180 message ("backtrace: ");
181 print_property_callback_stack ();
186 if (ly_is_procedure (val)
187 || is_simple_closure (val))
189 Grob *me = ((Grob*)this);
190 val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
197 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
199 SCM marker = ly_symbol2scm ("calculation-in-progress");
201 need to put a value in SYM to ensure that we don't get a
204 *alist = scm_assq_set_x (*alist, sym, marker);
207 if (debug_property_callbacks)
208 grob_property_callback_stack = scm_cons (scm_list_3 (self_scm (), sym, proc), grob_property_callback_stack);
212 if (ly_is_procedure (proc))
213 value = scm_call_1 (proc, self_scm ());
214 else if (is_simple_closure (proc))
216 value = evaluate_with_simple_closure (self_scm (),
217 simple_closure_expression (proc),
222 if (debug_property_callbacks)
223 grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
227 If the function returns SCM_UNSPECIFIED, we assume the
228 property has been set with an explicit set_property ()
231 if (value == SCM_UNSPECIFIED)
233 value = get_property_data (sym);
234 assert (value == SCM_EOL || value == marker);
236 *alist = scm_assq_remove_x (*alist, marker);
241 if (ly_is_procedure (cache_callback))
242 scm_apply_0 (cache_callback,
243 scm_list_n (self_scm (),
249 internal_set_value_on_alist (alist, sym, value);
256 Grob::internal_set_object (SCM s, SCM v)
258 /* Perhaps we simply do the assq_set, but what the heck. */
262 object_alist_ = scm_assq_set_x (object_alist_, s, v);
266 Grob::internal_del_property (SCM sym)
268 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
272 Grob::internal_get_object (SCM sym) const
274 if (profile_property_accesses)
275 note_property_access (&grob_property_lookup_table, sym);
277 SCM s = scm_sloppy_assq (sym, object_alist_);
281 SCM val = scm_cdr (s);
282 if (ly_is_procedure (val)
283 || is_simple_closure (val))
285 Grob *me = ((Grob*)this);
286 val = me->try_callback_on_alist (&me->object_alist_, sym, val);
296 Grob::is_live () const
298 return scm_is_pair (immutable_property_alist_);
302 Grob::internal_has_interface (SCM k)
304 return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
308 call_pure_function (SCM unpure, SCM args, int start, int end)
310 SCM scm_call_pure_function = ly_lily_module_constant ("call-pure-function");
312 return scm_apply_0 (scm_call_pure_function,
313 scm_list_4 (unpure, args, scm_from_int (start), scm_from_int (end)));