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 #include "scm-hash.hh"
30 Scheme_engraver::Scheme_engraver (SCM definition)
32 stop_translation_timestep_function_ = SCM_EOL;
33 start_translation_timestep_function_ = SCM_EOL;
34 process_music_function_ = SCM_EOL;
35 process_acknowledged_function_ = SCM_EOL;
36 initialize_function_ = SCM_EOL;
37 finalize_function_ = SCM_EOL;
39 interface_acknowledger_hash_ = SCM_EOL;
40 interface_end_acknowledger_hash_ = SCM_EOL;
42 must_be_last_ = false;
43 per_instance_listeners_ = SCM_EOL;
45 init_from_scheme (definition);
48 Scheme_engraver::~Scheme_engraver ()
52 // Extracts the value if callable, if not return #f.
54 callable (SCM symbol, SCM defn)
56 SCM val = ly_assoc_get (symbol, defn, SCM_BOOL_F);
57 return ly_is_procedure (val) ? val : SCM_BOOL_F;
61 Scheme_engraver::must_be_last () const
67 Scheme_engraver::init_from_scheme (SCM definition)
69 start_translation_timestep_function_ = callable (ly_symbol2scm ("start-translation-timestep"),
71 stop_translation_timestep_function_ = callable (ly_symbol2scm ("stop-translation-timestep"),
73 process_music_function_ = callable (ly_symbol2scm ("process-music"), definition);
74 process_acknowledged_function_ = callable (ly_symbol2scm ("process-acknowledged"),
76 initialize_function_ = callable (ly_symbol2scm ("initialize"), definition);
77 finalize_function_ = callable (ly_symbol2scm ("finalize"), definition);
79 SCM listeners = ly_assoc_get (ly_symbol2scm ("listeners"), definition, SCM_EOL);
81 per_instance_listeners_ = SCM_EOL;
83 must_be_last_ = to_boolean (ly_assoc_get (ly_symbol2scm ("must-be-last"),
87 for (SCM p = listeners; scm_is_pair (p); p = scm_cdr (p))
89 SCM event_class = scm_caar (p);
90 SCM proc = scm_cdar (p);
92 if (!(ly_is_procedure (proc) && ly_is_symbol (event_class)))
95 // We should check the arity of the function?
97 // Record for later lookup.
98 per_instance_listeners_ = scm_acons (event_class, proc, per_instance_listeners_);
101 init_acknowledgers (ly_assoc_get (ly_symbol2scm ("acknowledgers"),
102 definition, SCM_EOL),
103 &interface_acknowledger_hash_);
105 init_acknowledgers (ly_assoc_get (ly_symbol2scm ("end-acknowledgers"),
106 definition, SCM_EOL),
107 &interface_end_acknowledger_hash_);
109 // TODO: hook up description, props read/written, grobs created
110 // etc. to provide automatic documentation.
114 Scheme_engraver::init_acknowledgers (SCM alist,
117 *hash = Scheme_hash_table::make_smob ();
118 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
120 SCM iface = scm_caar (p);
121 SCM proc = scm_cdar (p);
123 if (!(ly_is_procedure (proc) && ly_is_symbol (iface)))
126 unsmob<Scheme_hash_table>(*hash)->set (iface, proc);
131 Scheme_engraver::get_listener_list () const
133 return per_instance_listeners_;
136 #define DISPATCH(what) \
138 Scheme_engraver::what () \
140 if (what ## _function_ != SCM_BOOL_F) \
141 scm_call_1 (what ## _function_, self_scm ()); \
144 DISPATCH (start_translation_timestep);
145 DISPATCH (stop_translation_timestep);
146 DISPATCH (initialize);
148 DISPATCH (process_music);
149 DISPATCH (process_acknowledged);
152 Scheme_engraver::derived_mark () const
154 scm_gc_mark (start_translation_timestep_function_);
155 scm_gc_mark (stop_translation_timestep_function_);
156 scm_gc_mark (initialize_function_);
157 scm_gc_mark (finalize_function_);
158 scm_gc_mark (process_music_function_);
159 scm_gc_mark (process_acknowledged_function_);
160 scm_gc_mark (per_instance_listeners_);
161 scm_gc_mark (interface_acknowledger_hash_);
162 scm_gc_mark (interface_end_acknowledger_hash_);
165 ADD_TRANSLATOR_FAMILY (Scheme_engraver);