]> git.donarmstrong.com Git - lilypond.git/blob - lily/scheme-engraver.cc
Doc: NR - changing-defaults.itely - various improvements
[lilypond.git] / lily / scheme-engraver.cc
1 /*
2   scheme-engraver.cc -- implement Scheme_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   Copyright (c) 2009--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
7
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.
12
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.
17
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/>.
20 */
21
22 #include "scheme-engraver.hh"
23
24 #include "grob.hh"
25
26 #include "translator.icc"
27
28 Scheme_engraver::Scheme_engraver (SCM definition)
29 {
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;
36
37   interface_acknowledger_hash_ = SCM_EOL;
38   interface_end_acknowledger_hash_ = SCM_EOL;
39
40   must_be_last_ = false;
41   per_instance_listeners_ = SCM_EOL;
42
43   init_from_scheme (definition);
44 }
45
46 Scheme_engraver::~Scheme_engraver ()
47 {
48 }
49
50 // Extracts the value if callable, if not return #f.
51 static SCM
52 callable (SCM symbol, SCM defn)
53 {
54   SCM val = ly_assoc_get (symbol, defn, SCM_BOOL_F);
55   return ly_is_procedure (val) ? val : SCM_BOOL_F;
56 }
57
58 bool
59 Scheme_engraver::must_be_last () const
60 {
61   return must_be_last_;
62 }
63
64 void
65 Scheme_engraver::init_from_scheme (SCM definition)
66 {
67   start_translation_timestep_function_ = callable (ly_symbol2scm ("start-translation-timestep"),
68                                                    definition);
69   stop_translation_timestep_function_ = callable (ly_symbol2scm ("stop-translation-timestep"),
70                                                   definition);
71   process_music_function_ = callable (ly_symbol2scm ("process-music"), definition);
72   process_acknowledged_function_ = callable (ly_symbol2scm ("process-acknowledged"),
73                                              definition);
74   initialize_function_ = callable (ly_symbol2scm ("initialize"), definition);
75   finalize_function_ = callable (ly_symbol2scm ("finalize"), definition);
76
77   SCM listeners = ly_assoc_get (ly_symbol2scm ("listeners"), definition, SCM_EOL);
78
79   per_instance_listeners_ = SCM_EOL;
80
81   must_be_last_ = to_boolean (ly_assoc_get (ly_symbol2scm ("must-be-last"),
82                                             definition,
83                                             SCM_BOOL_F));
84
85   for (SCM p = listeners; scm_is_pair (p); p = scm_cdr (p))
86     {
87       SCM event_class = scm_caar (p);
88       SCM proc = scm_cdar (p);
89
90       if (!(ly_is_procedure (proc) && ly_is_symbol (event_class)))
91         continue;
92
93       // We should check the arity of the function?
94
95       // Record for later lookup.
96       per_instance_listeners_ = scm_acons (event_class, proc, per_instance_listeners_);
97     }
98
99   init_acknowledgers (ly_assoc_get (ly_symbol2scm ("acknowledgers"),
100                                     definition, SCM_EOL),
101                       &interface_acknowledger_hash_);
102
103   init_acknowledgers (ly_assoc_get (ly_symbol2scm ("end-acknowledgers"),
104                                     definition, SCM_EOL),
105                       &interface_end_acknowledger_hash_);
106
107   // TODO: hook up description, props read/written, grobs created
108   // etc. to provide automatic documentation.
109 }
110
111 void
112 Scheme_engraver::init_acknowledgers (SCM alist,
113                                      SCM *hash)
114 {
115   *hash = scm_c_make_hash_table (7);
116   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
117     {
118       SCM iface = scm_caar (p);
119       SCM proc = scm_cdar (p);
120
121       if (!(ly_is_procedure (proc) && ly_is_symbol (iface)))
122         continue;
123
124       scm_hashq_set_x (*hash, iface, proc);
125     }
126 }
127
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.
132 void
133 Scheme_engraver::acknowledge_grob (Grob_info info)
134 {
135   acknowledge_grob_by_hash (info, interface_acknowledger_hash_);
136 }
137
138 void
139 Scheme_engraver::acknowledge_end_grob (Grob_info info)
140 {
141   acknowledge_grob_by_hash (info, interface_end_acknowledger_hash_);
142 }
143
144 void
145 Scheme_engraver::acknowledge_grob_by_hash (Grob_info info,
146                                            SCM iface_function_hash)
147 {
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))
151     {
152       SCM func = scm_hashq_ref (iface_function_hash,
153                                 scm_car (s), SCM_BOOL_F);
154
155       if (ly_is_procedure (func))
156         scm_call_3 (func, self_scm (), info.grob ()->self_scm (),
157                     info.origin_translator ()->self_scm ());
158     }
159 }
160
161 SCM
162 Scheme_engraver::get_listener_list () const
163 {
164   return per_instance_listeners_;
165 }
166
167 #define DISPATCH(what)                                  \
168   void                                                  \
169   Scheme_engraver::what ()                              \
170   {                                                     \
171     if (what ## _function_ != SCM_BOOL_F)               \
172       scm_call_1 (what ## _function_, self_scm ());     \
173   }
174
175 DISPATCH (start_translation_timestep);
176 DISPATCH (stop_translation_timestep);
177 DISPATCH (initialize);
178 DISPATCH (finalize);
179 DISPATCH (process_music);
180 DISPATCH (process_acknowledged);
181
182 void
183 Scheme_engraver::derived_mark () const
184 {
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_);
194 }
195
196 ADD_ACKNOWLEDGER (Scheme_engraver, grob);
197 ADD_END_ACKNOWLEDGER (Scheme_engraver, grob);
198
199 ADD_TRANSLATOR_FAMILY (Scheme_engraver);