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;
26 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;
79 Grob::instrumented_set_property (SCM sym, SCM v,
85 if (ly_is_procedure (modification_callback))
86 scm_apply_0 (modification_callback,
87 scm_list_n (self_scm (),
88 scm_from_locale_string (file),
90 scm_from_locale_string (fun),
91 sym, v, SCM_UNDEFINED));
98 internal_set_property (sym, v);
102 Grob::get_property_alist_chain (SCM def) const
104 return scm_list_n (mutable_property_alist_,
105 immutable_property_alist_,
110 extern void check_interfaces_for_property (Grob const *me, SCM sym);
113 Grob::internal_set_property (SCM sym, SCM v)
115 internal_set_value_on_alist (&mutable_property_alist_,
121 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
123 /* Perhaps we simply do the assq_set, but what the heck. */
127 if (do_internal_type_checking_global)
129 if (!ly_is_procedure (v)
130 && !is_simple_closure (v)
131 && v != ly_symbol2scm ("calculation-in-progress"))
132 type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
134 check_interfaces_for_property (this, sym);
137 *alist = scm_assq_set_x (*alist, sym, v);
141 Grob::internal_get_property_data (SCM sym) const
144 if (profile_property_accesses)
145 note_property_access (&grob_property_lookup_table, sym);
148 SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
149 if (handle != SCM_BOOL_F)
150 return scm_cdr (handle);
152 handle = scm_sloppy_assq (sym, immutable_property_alist_);
154 if (do_internal_type_checking_global && scm_is_pair (handle))
156 SCM val = scm_cdr (handle);
157 if (!ly_is_procedure (val) && !is_simple_closure (val))
158 type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
160 check_interfaces_for_property (this, sym);
163 return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
167 Grob::internal_get_property (SCM sym) const
169 SCM val = get_property_data (sym);
172 if (val == ly_symbol2scm ("calculation-in-progress"))
174 programming_error (_f ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
175 ly_symbol2string (sym).c_str (),
177 if (debug_property_callbacks)
179 message ("backtrace: ");
180 print_property_callback_stack ();
185 if (ly_is_procedure (val)
186 || is_simple_closure (val))
188 Grob *me = ((Grob*)this);
189 val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
196 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
198 SCM marker = ly_symbol2scm ("calculation-in-progress");
200 need to put a value in SYM to ensure that we don't get a
203 *alist = scm_assq_set_x (*alist, sym, marker);
206 if (debug_property_callbacks)
207 grob_property_callback_stack = scm_cons (scm_list_3 (self_scm (), sym, proc), grob_property_callback_stack);
211 if (ly_is_procedure (proc))
212 value = scm_call_1 (proc, self_scm ());
213 else if (is_simple_closure (proc))
215 value = evaluate_with_simple_closure (self_scm (),
216 simple_closure_expression (proc),
221 if (debug_property_callbacks)
222 grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
226 If the function returns SCM_UNSPECIFIED, we assume the
227 property has been set with an explicit set_property ()
230 if (value == SCM_UNSPECIFIED)
232 value = get_property_data (sym);
233 assert (value == SCM_EOL || value == marker);
235 *alist = scm_assq_remove_x (*alist, marker);
240 if (ly_is_procedure (cache_callback))
241 scm_apply_0 (cache_callback,
242 scm_list_n (self_scm (),
248 internal_set_value_on_alist (alist, sym, value);
255 Grob::internal_set_object (SCM s, SCM v)
257 /* Perhaps we simply do the assq_set, but what the heck. */
261 object_alist_ = scm_assq_set_x (object_alist_, s, v);
265 Grob::internal_del_property (SCM sym)
267 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
271 Grob::internal_get_object (SCM sym) const
273 if (profile_property_accesses)
274 note_property_access (&grob_property_lookup_table, sym);
276 SCM s = scm_sloppy_assq (sym, object_alist_);
280 SCM val = scm_cdr (s);
281 if (ly_is_procedure (val)
282 || is_simple_closure (val))
284 Grob *me = ((Grob*)this);
285 val = me->try_callback_on_alist (&me->object_alist_, sym, val);
295 Grob::is_live () const
297 return scm_is_pair (immutable_property_alist_);
301 Grob::internal_has_interface (SCM k)
303 return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
307 call_pure_function (SCM unpure, SCM args, int start, int end)
309 SCM scm_call_pure_function = ly_lily_module_constant ("call-pure-function");
311 return scm_apply_0 (scm_call_pure_function,
312 scm_list_4 (unpure, args, scm_from_int (start), scm_from_int (end)));