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"
24 static SCM modification_callback = SCM_EOL;
26 LY_DEFINE (ly_set_grob_modification_callback, "ly:set-grob-modification-callback",
28 "Specify a procedure that will be called every time lilypond modifies "
29 "a grob property. The callback will receive as arguments "
30 "the grob that is being modified, the name of the C++ file in which "
31 "the modification was requested, the line number in the C++ file in "
32 "which the modification was requested, the property to be changed and "
33 "the new value for the property.")
36 SCM_ASSERT_TYPE(ly_is_procedure (cb), cb, SCM_ARG1, __FUNCTION__,
39 modification_callback = cb;
40 return SCM_UNSPECIFIED;
45 Grob::get_property_alist_chain (SCM def) const
47 return scm_list_n (mutable_property_alist_,
48 immutable_property_alist_,
54 extern void check_interfaces_for_property (Grob const *me, SCM sym);
58 Grob::internal_set_property (SCM sym, SCM v, char const *file, int line, char const *fun)
60 SCM grob_p = ly_lily_module_constant ("ly:grob?");
61 SCM grob_list_p = ly_lily_module_constant ("grob-list?");
62 SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
65 || type == grob_list_p
66 || (unsmob_grob (v) && ly_symbol2scm ("cause") != sym))
68 scm_display (scm_list_2 (sym, type), scm_current_output_port ());
72 internal_set_value_on_alist (&mutable_property_alist_,
76 if (ly_is_procedure (modification_callback))
77 scm_apply_0 (modification_callback,
78 scm_list_n (self_scm (),
79 scm_from_locale_string (file),
81 scm_from_locale_string (fun),
82 sym, v, SCM_UNDEFINED));
86 Grob::internal_set_property (SCM sym, SCM v)
88 internal_set_value_on_alist (&mutable_property_alist_,
95 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
97 /* Perhaps we simply do the assq_set, but what the heck. */
101 if (do_internal_type_checking_global)
103 if (!ly_is_procedure (v)
104 && !is_simple_closure (v)
105 && v != ly_symbol2scm ("calculation-in-progress")
106 && !type_check_assignment (sym, v, ly_symbol2scm ("backend-type?")))
108 check_interfaces_for_property (this, sym);
111 *alist = scm_assq_set_x (*alist, sym, v);
115 Grob::internal_get_property_data (SCM sym) const
118 if (profile_property_accesses)
119 note_property_access (&grob_property_lookup_table, sym);
122 SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
123 if (handle != SCM_BOOL_F)
124 return scm_cdr (handle);
126 handle = scm_sloppy_assq (sym, immutable_property_alist_);
128 if (do_internal_type_checking_global && scm_is_pair (handle))
130 SCM val = scm_cdr (handle);
131 if (!ly_is_procedure (val)
132 && !is_simple_closure (val)
133 && !type_check_assignment (sym, val,
134 ly_symbol2scm ("backend-type?")))
137 check_interfaces_for_property (this, sym);
140 return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
144 Grob::internal_get_property (SCM sym) const
146 SCM val = get_property_data (sym);
147 if (ly_is_procedure (val)
148 || is_simple_closure (val))
150 Grob *me = ((Grob*)this);
151 val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
158 #include "protected-scm.hh"
160 Protected_scm grob_property_callback_stack = SCM_EOL;
161 bool debug_property_callbacks = 0;
165 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
167 SCM marker = ly_symbol2scm ("calculation-in-progress");
169 need to put a value in SYM to ensure that we don't get a
172 *alist = scm_assq_set_x (*alist, sym, marker);
175 if (debug_property_callbacks)
176 grob_property_callback_stack = scm_acons (sym, proc, grob_property_callback_stack);
180 if (ly_is_procedure (proc))
181 value = scm_call_1 (proc, self_scm ());
182 else if (is_simple_closure (proc))
184 value = evaluate_with_simple_closure (self_scm (),
185 simple_closure_expression (proc),
190 if (debug_property_callbacks)
191 grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
195 If the function returns SCM_UNSPECIFIED, we assume the
196 property has been set with an explicit set_property()
199 if (value == SCM_UNSPECIFIED)
201 value = internal_get_property (sym);
202 assert (value == SCM_EOL || value == marker);
204 *alist = scm_assq_remove_x (*alist, marker);
207 internal_set_value_on_alist (alist, sym, value);
213 Grob::internal_set_object (SCM s, SCM v)
215 /* Perhaps we simply do the assq_set, but what the heck. */
219 object_alist_ = scm_assq_set_x (object_alist_, s, v);
223 Grob::internal_del_property (SCM sym)
225 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
229 Grob::internal_get_object (SCM sym) const
231 if (profile_property_accesses)
232 note_property_access (&grob_property_lookup_table, sym);
234 SCM s = scm_sloppy_assq (sym, object_alist_);
238 SCM val = scm_cdr (s);
239 if (ly_is_procedure (val)
240 || is_simple_closure (val))
242 Grob *me = ((Grob*)this);
243 val = me->try_callback_on_alist (&me->object_alist_, sym, val);
253 Grob::is_live () const
255 return scm_is_pair (immutable_property_alist_);
259 Grob::internal_has_interface (SCM k)
261 return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
265 call_pure_function (SCM unpure, SCM args, int start, int end)
267 SCM scm_call_pure_function = ly_lily_module_constant ("call-pure-function");
269 return scm_apply_0 (scm_call_pure_function,
270 scm_list_4 (unpure, args, scm_from_int (start), scm_from_int (end)));