2 scheme-engraver.cc -- implement Scheme_engraver
4 source file of the GNU LilyPond music typesetter
6 Copyright (c) 2009--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
8 LilyPond is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 LilyPond is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "scheme-engraver.hh"
26 #include "translator.icc"
28 Scheme_engraver::Scheme_engraver (SCM definition)
30 stop_translation_timestep_function_ = SCM_EOL;
31 start_translation_timestep_function_ = SCM_EOL;
32 process_music_function_ = SCM_EOL;
33 process_acknowledged_function_ = SCM_EOL;
34 initialize_function_ = SCM_EOL;
35 finalize_function_ = SCM_EOL;
37 interface_acknowledger_hash_ = SCM_EOL;
38 interface_end_acknowledger_hash_ = SCM_EOL;
40 must_be_last_ = false;
41 per_instance_listeners_ = SCM_EOL;
43 init_from_scheme (definition);
46 Scheme_engraver::~Scheme_engraver ()
50 // Extracts the value if callable, if not return #f.
52 callable (SCM symbol, SCM defn)
54 SCM val = ly_assoc_get (symbol, defn, SCM_BOOL_F);
55 return ly_is_procedure (val) ? val : SCM_BOOL_F;
59 Scheme_engraver::must_be_last () const
65 Scheme_engraver::init_from_scheme (SCM definition)
67 start_translation_timestep_function_ = callable (ly_symbol2scm ("start-translation-timestep"),
69 stop_translation_timestep_function_ = callable (ly_symbol2scm ("stop-translation-timestep"),
71 process_music_function_ = callable (ly_symbol2scm ("process-music"), definition);
72 process_acknowledged_function_ = callable (ly_symbol2scm ("process-acknowledged"),
74 initialize_function_ = callable (ly_symbol2scm ("initialize"), definition);
75 finalize_function_ = callable (ly_symbol2scm ("finalize"), definition);
77 SCM listeners = ly_assoc_get (ly_symbol2scm ("listeners"), definition, SCM_EOL);
79 per_instance_listeners_ = SCM_EOL;
81 must_be_last_ = to_boolean (ly_assoc_get (ly_symbol2scm ("must-be-last"),
85 for (SCM p = listeners; scm_is_pair (p); p = scm_cdr (p))
87 SCM event_class = scm_caar (p);
88 SCM proc = scm_cdar (p);
90 if (!(ly_is_procedure (proc) && ly_is_symbol (event_class)))
93 // We should check the arity of the function?
95 // Record for later lookup.
96 per_instance_listeners_ = scm_acons (event_class, proc, per_instance_listeners_);
99 init_acknowledgers (ly_assoc_get (ly_symbol2scm ("acknowledgers"),
100 definition, SCM_EOL),
101 &interface_acknowledger_hash_);
103 init_acknowledgers (ly_assoc_get (ly_symbol2scm ("end-acknowledgers"),
104 definition, SCM_EOL),
105 &interface_end_acknowledger_hash_);
107 // TODO: hook up description, props read/written, grobs created
108 // etc. to provide automatic documentation.
112 Scheme_engraver::init_acknowledgers (SCM alist,
115 *hash = scm_c_make_hash_table (7);
116 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
118 SCM iface = scm_caar (p);
119 SCM proc = scm_cdar (p);
121 if (!(ly_is_procedure (proc) && ly_is_symbol (iface)))
124 scm_hashq_set_x (*hash, iface, proc);
128 // This is the easy way to do it, at the cost of too many invocations
129 // of Scheme_engraver::acknowledge_grob. The clever dispatching of
130 // acknowledgers is hardwired to have 1 method per engraver per
131 // grob-type, which doesn't work for this case.
133 Scheme_engraver::acknowledge_grob (Grob_info info)
135 acknowledge_grob_by_hash (info, interface_acknowledger_hash_);
139 Scheme_engraver::acknowledge_end_grob (Grob_info info)
141 acknowledge_grob_by_hash (info, interface_end_acknowledger_hash_);
145 Scheme_engraver::acknowledge_grob_by_hash (Grob_info info,
146 SCM iface_function_hash)
148 SCM meta = info.grob ()->get_property ("meta");
149 SCM ifaces = scm_cdr (scm_assoc (ly_symbol2scm ("interfaces"), meta));
150 for (SCM s = ifaces; scm_is_pair (s); s = scm_cdr (s))
152 SCM func = scm_hashq_ref (iface_function_hash,
153 scm_car (s), SCM_BOOL_F);
155 if (ly_is_procedure (func))
156 scm_call_3 (func, self_scm (), info.grob ()->self_scm (),
157 info.origin_translator ()->self_scm ());
162 Scheme_engraver::get_listener_list () const
164 return per_instance_listeners_;
167 #define DISPATCH(what) \
169 Scheme_engraver::what () \
171 if (what ## _function_ != SCM_BOOL_F) \
172 scm_call_1 (what ## _function_, self_scm ()); \
175 DISPATCH (start_translation_timestep);
176 DISPATCH (stop_translation_timestep);
177 DISPATCH (initialize);
179 DISPATCH (process_music);
180 DISPATCH (process_acknowledged);
183 Scheme_engraver::derived_mark () const
185 scm_gc_mark (start_translation_timestep_function_);
186 scm_gc_mark (stop_translation_timestep_function_);
187 scm_gc_mark (initialize_function_);
188 scm_gc_mark (finalize_function_);
189 scm_gc_mark (process_music_function_);
190 scm_gc_mark (process_acknowledged_function_);
191 scm_gc_mark (per_instance_listeners_);
192 scm_gc_mark (interface_acknowledger_hash_);
193 scm_gc_mark (interface_end_acknowledger_hash_);
196 ADD_ACKNOWLEDGER (Scheme_engraver, grob);
197 ADD_END_ACKNOWLEDGER (Scheme_engraver, grob);
199 ADD_TRANSLATOR_FAMILY (Scheme_engraver);