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;
41 LY_DEFINE (ly_set_grob_modification_callback, "ly:set-grob-modification-callback",
43 "Specify a procedure that will be called every time lilypond modifies "
44 "a grob property. The callback will receive as arguments "
45 "the grob that is being modified, "
46 "the name of the C++ file in which the modification was requested, "
47 "the line number in the C++ file in which the modification was requested, "
48 "the name of the function in which the modification was requested, "
49 "the property to be changed and "
50 "the new value for the property.")
52 LY_ASSERT_TYPE (ly_is_procedure, cb, 1);
54 modification_callback = cb;
55 return SCM_UNSPECIFIED;
58 LY_DEFINE (ly_set_property_cache_callback, "ly:set-property-cache-callback",
60 "Specify a procedure that will be called whenever lilypond calculates "
61 "a callback function and caches the result. The callback will "
62 "receive as arguments "
63 "the grob whose property it is, "
64 "the name of the property, "
65 "the name of the callback that calculated the property and "
66 "the new (cached) value of the property.")
68 LY_ASSERT_TYPE (ly_is_procedure, cb, 1);
71 return SCM_UNSPECIFIED;
76 Grob::instrumented_set_property (SCM sym, SCM v,
82 if (ly_is_procedure (modification_callback))
83 scm_apply_0 (modification_callback,
84 scm_list_n (self_scm (),
85 scm_from_locale_string (file),
87 scm_from_locale_string (fun),
88 sym, v, SCM_UNDEFINED));
91 internal_set_property (sym, v);
95 Grob::get_property_alist_chain (SCM def) const
97 return scm_list_n (mutable_property_alist_,
98 immutable_property_alist_,
103 extern void check_interfaces_for_property (Grob const *me, SCM sym);
106 Grob::internal_set_property (SCM sym, SCM v)
108 internal_set_value_on_alist (&mutable_property_alist_,
114 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
116 /* Perhaps we simply do the assq_set, but what the heck. */
120 if (do_internal_type_checking_global)
122 if (!ly_is_procedure (v)
123 && !is_simple_closure (v)
124 && v != ly_symbol2scm ("calculation-in-progress"))
125 type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
127 check_interfaces_for_property (this, sym);
130 *alist = scm_assq_set_x (*alist, sym, v);
134 Grob::internal_get_property_data (SCM sym) const
137 if (profile_property_accesses)
138 note_property_access (&grob_property_lookup_table, sym);
141 SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
142 if (handle != SCM_BOOL_F)
143 return scm_cdr (handle);
145 handle = scm_sloppy_assq (sym, immutable_property_alist_);
147 if (do_internal_type_checking_global && scm_is_pair (handle))
149 SCM val = scm_cdr (handle);
150 if (!ly_is_procedure (val) && !is_simple_closure (val))
151 type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
153 check_interfaces_for_property (this, sym);
156 return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
160 Grob::internal_get_property (SCM sym) const
162 SCM val = get_property_data (sym);
165 if (val == ly_symbol2scm ("calculation-in-progress"))
167 programming_error (_f ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
168 ly_symbol2string (sym).c_str (),
170 if (debug_property_callbacks)
172 message ("backtrace: ");
173 print_property_callback_stack ();
178 if (ly_is_procedure (val)
179 || is_simple_closure (val))
181 Grob *me = ((Grob*)this);
182 val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
189 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
191 SCM marker = ly_symbol2scm ("calculation-in-progress");
193 need to put a value in SYM to ensure that we don't get a
196 *alist = scm_assq_set_x (*alist, sym, marker);
199 if (debug_property_callbacks)
200 grob_property_callback_stack = scm_cons (scm_list_3 (self_scm (), sym, proc), grob_property_callback_stack);
204 if (ly_is_procedure (proc))
205 value = scm_call_1 (proc, self_scm ());
206 else if (is_simple_closure (proc))
208 value = evaluate_with_simple_closure (self_scm (),
209 simple_closure_expression (proc),
214 if (debug_property_callbacks)
215 grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
219 If the function returns SCM_UNSPECIFIED, we assume the
220 property has been set with an explicit set_property ()
223 if (value == SCM_UNSPECIFIED)
225 value = get_property_data (sym);
226 assert (value == SCM_EOL || value == marker);
228 *alist = scm_assq_remove_x (*alist, marker);
233 if (ly_is_procedure (cache_callback))
234 scm_apply_0 (cache_callback,
235 scm_list_n (self_scm (),
241 internal_set_value_on_alist (alist, sym, value);
248 Grob::internal_set_object (SCM s, SCM v)
250 /* Perhaps we simply do the assq_set, but what the heck. */
254 object_alist_ = scm_assq_set_x (object_alist_, s, v);
258 Grob::internal_del_property (SCM sym)
260 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
264 Grob::internal_get_object (SCM sym) const
266 if (profile_property_accesses)
267 note_property_access (&grob_property_lookup_table, sym);
269 SCM s = scm_sloppy_assq (sym, object_alist_);
273 SCM val = scm_cdr (s);
274 if (ly_is_procedure (val)
275 || is_simple_closure (val))
277 Grob *me = ((Grob*)this);
278 val = me->try_callback_on_alist (&me->object_alist_, sym, val);
288 Grob::is_live () const
290 return scm_is_pair (immutable_property_alist_);
294 Grob::internal_has_interface (SCM k)
296 return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
300 call_pure_function (SCM unpure, SCM args, int start, int end)
302 SCM scm_call_pure_function = ly_lily_module_constant ("call-pure-function");
304 return scm_apply_0 (scm_call_pure_function,
305 scm_list_4 (unpure, args, scm_from_int (start), scm_from_int (end)));