]> git.donarmstrong.com Git - lilypond.git/blob - lily/engraver-group.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / engraver-group.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "context.hh"
21 #include "dispatcher.hh"
22 #include "engraver-group.hh"
23 #include "grob.hh"
24 #include "paper-score.hh"
25 #include "translator-dispatch-list.hh"
26 #include "warn.hh"
27
28 IMPLEMENT_LISTENER (Engraver_group, override);
29 void
30 Engraver_group::override (SCM sev)
31 {
32   Stream_event *ev = unsmob_stream_event (sev);
33
34   sloppy_general_pushpop_property (context (),
35                                    ev->get_property ("symbol"),
36                                    ev->get_property ("property-path"),
37                                    ev->get_property ("value"));
38 }
39
40 IMPLEMENT_LISTENER (Engraver_group, revert);
41 void
42 Engraver_group::revert (SCM sev)
43 {
44   Stream_event *ev = unsmob_stream_event (sev);
45
46   sloppy_general_pushpop_property (context (),
47                                    ev->get_property ("symbol"),
48                                    ev->get_property ("property-path"),
49                                    SCM_UNDEFINED);
50 }
51
52 void
53 Engraver_group::connect_to_context (Context *c)
54 {
55   Translator_group::connect_to_context (c);
56   c->event_source ()->add_listener (GET_LISTENER (override), ly_symbol2scm ("Override"));
57   c->event_source ()->add_listener (GET_LISTENER (revert), ly_symbol2scm ("Revert"));
58 }
59
60 void
61 Engraver_group::disconnect_from_context ()
62 {
63   context ()->event_source ()->remove_listener (GET_LISTENER (override), ly_symbol2scm ("Override"));
64   context ()->event_source ()->remove_listener (GET_LISTENER (revert), ly_symbol2scm ("Revert"));
65   Translator_group::disconnect_from_context ();
66 }
67
68 void
69 Engraver_group::announce_grob (Grob_info info)
70 {
71   announce_infos_.push_back (info);
72
73   Context *dad_con = context_->get_parent_context ();
74   if (info.rerouting_daddy_context_)
75     {
76       dad_con = info.rerouting_daddy_context_;
77       info.rerouting_daddy_context_ = 0;
78     }
79
80   Engraver_group *dad_eng
81     = dad_con
82       ? dynamic_cast<Engraver_group *> (dad_con->implementation ())
83       : 0;
84
85   if (dad_eng)
86     dad_eng->announce_grob (info);
87 }
88
89 void
90 Engraver_group::acknowledge_grobs ()
91 {
92   if (!announce_infos_.size ())
93     return;
94
95   SCM name_sym = ly_symbol2scm ("name");
96   SCM meta_sym = ly_symbol2scm ("meta");
97
98   for (vsize j = 0; j < announce_infos_.size (); j++)
99     {
100       Grob_info info = announce_infos_[j];
101
102       SCM meta = info.grob ()->internal_get_property (meta_sym);
103       SCM nm = scm_assoc (name_sym, meta);
104       if (scm_is_pair (nm))
105         nm = scm_cdr (nm);
106       else
107         continue;
108
109       SCM acklist = scm_hashq_ref (acknowledge_hash_table_drul_[info.start_end ()],
110                                    nm, SCM_BOOL_F);
111
112       Engraver_dispatch_list *dispatch
113         = Engraver_dispatch_list::unsmob (acklist);
114
115       if (acklist == SCM_BOOL_F)
116         {
117           SCM ifaces
118             = scm_cdr (scm_assoc (ly_symbol2scm ("interfaces"), meta));
119           acklist = Engraver_dispatch_list::create (get_simple_trans_list (),
120                                                     ifaces, info.start_end ());
121
122           dispatch
123             = Engraver_dispatch_list::unsmob (acklist);
124
125           scm_hashq_set_x (acknowledge_hash_table_drul_[info.start_end ()], nm, acklist);
126         }
127
128       if (dispatch)
129         dispatch->apply (info);
130     }
131 }
132
133 /*
134   Ugh. This is slightly expensive. We could/should cache the value of
135   the group count?
136 */
137 int
138 Engraver_group::pending_grob_count () const
139 {
140   int count = announce_infos_.size ();
141   for (SCM s = context_->children_contexts ();
142        scm_is_pair (s); s = scm_cdr (s))
143     {
144       Context *c = unsmob_context (scm_car (s));
145       Engraver_group *group
146         = dynamic_cast<Engraver_group *> (c->implementation ());
147
148       if (group)
149         count += group->pending_grob_count ();
150     }
151   return count;
152 }
153
154 void
155 Engraver_group::do_announces ()
156 {
157   do
158     {
159       /*
160         DOCME: why is this inside the loop?
161        */
162       for (SCM s = context ()->children_contexts ();
163            scm_is_pair (s); s = scm_cdr (s))
164         {
165           Context *c = unsmob_context (scm_car (s));
166           Engraver_group *group
167             = dynamic_cast<Engraver_group *> (c->implementation ());
168           if (group)
169             group->do_announces ();
170         }
171
172       while (1)
173         {
174           precomputed_translator_foreach (PROCESS_ACKNOWLEDGED);
175           if (announce_infos_.size () == 0)
176             break;
177
178           acknowledge_grobs ();
179           announce_infos_.clear ();
180         }
181     }
182   while (pending_grob_count () > 0);
183 }
184
185 Engraver_group::Engraver_group ()
186 {
187   acknowledge_hash_table_drul_[LEFT]
188     = acknowledge_hash_table_drul_[RIGHT]
189       = SCM_EOL;
190
191   acknowledge_hash_table_drul_[LEFT] = scm_c_make_hash_table (61);
192   acknowledge_hash_table_drul_[RIGHT] = scm_c_make_hash_table (61);
193 }
194
195 #include "translator.icc"
196
197 ADD_TRANSLATOR_GROUP (Engraver_group,
198                       /* doc */
199                       "A group of engravers taken together.",
200
201                       /* create */
202                       "",
203
204                       /* read */
205                       "",
206
207                       /* write */
208                       ""
209                      );
210
211 void
212 Engraver_group::derived_mark () const
213 {
214   scm_gc_mark (acknowledge_hash_table_drul_[LEFT]);
215   scm_gc_mark (acknowledge_hash_table_drul_[RIGHT]);
216 }