]> git.donarmstrong.com Git - lilypond.git/blob - lily/property-iterator.cc
Run grand replace for 2015.
[lilypond.git] / lily / property-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 "property-iterator.hh"
21
22 #include "context-def.hh"
23 #include "global-context.hh"
24 #include "international.hh"
25 #include "music.hh"
26
27 bool check_grob (Music *mus, SCM sym);
28
29 /**
30    There is no real processing to a property: just lookup the
31    translation unit, and set the property.
32 */
33 void
34 Property_iterator::process (Moment mom)
35 {
36   Context *o = get_outlet ();
37   Music *m = get_music ();
38   bool once = to_boolean (m->get_property ("once"));
39   SCM symbol = m->get_property ("symbol");
40   SCM previous_value = SCM_UNDEFINED;
41   if (once)
42     o->here_defined (symbol, &previous_value);
43
44   send_stream_event (o, "SetProperty", m->origin (),
45                      ly_symbol2scm ("symbol"), symbol,
46                      ly_symbol2scm ("value"), m->get_property ("value"));
47
48   /* For \once \set install a finalization hook to reset the property to the
49    * previous value after the timestep */
50   if (once)
51     {
52       Global_context *tg = get_outlet ()->get_global_context ();
53       tg->add_finalization (scm_list_4 (once_finalization_proc,
54                                         o->self_scm (), m->self_scm (),
55                                         previous_value));
56     }
57
58   Simple_music_iterator::process (mom);
59 }
60
61 void
62 Property_unset_iterator::process (Moment mom)
63 {
64   Context *o = get_outlet ();
65   Music *m = get_music ();
66   bool once = to_boolean (m->get_property ("once"));
67   SCM symbol = m->get_property ("symbol");
68   SCM previous_value = SCM_UNDEFINED;
69   if (once)
70     o->here_defined (symbol, &previous_value);
71
72   send_stream_event (o, "UnsetProperty", m->origin (),
73                      ly_symbol2scm ("symbol"), symbol);
74
75   /* For \once \unset install a finalization hook to reset the property to the
76    * previous value after the timestep */
77   if (once && !SCM_UNBNDP (previous_value))
78     {
79       Global_context *tg = get_outlet ()->get_global_context ();
80       tg->add_finalization (scm_list_4 (Property_iterator::once_finalization_proc,
81                                         o->self_scm (), m->self_scm (),
82                                         previous_value));
83     }
84
85   Simple_music_iterator::process (mom);
86 }
87
88 MAKE_SCHEME_CALLBACK (Property_iterator, once_finalization, 3);
89 SCM
90 Property_iterator::once_finalization (SCM ctx, SCM music, SCM previous_value)
91 {
92   Music *m = Music::unsmob (music);
93   Context *c = Context::unsmob (ctx);
94
95   // Do not use UnsetProperty, which sets the default, but rather
96   // cache the value before the \once \set command and restore it now
97   send_stream_event (c, "SetProperty", m->origin (),
98                      ly_symbol2scm ("symbol"), m->get_property ("symbol"),
99                      ly_symbol2scm ("value"), previous_value);
100
101   return SCM_UNSPECIFIED;
102 }
103
104 void
105 Property_iterator::do_quit ()
106 {
107 }
108
109 bool
110 check_grob (Music *mus, SCM sym)
111 {
112   bool g = to_boolean (scm_object_property (sym, ly_symbol2scm ("is-grob?")));
113
114   if (!g)
115     mus->origin ()->warning (_f ("not a grob name, `%s'",
116                                  ly_symbol2string (sym)));
117
118   return g;
119 }
120
121 SCM
122 get_property_path (Music *m)
123 {
124   SCM grob_property_path = m->get_property ("grob-property-path");
125
126   SCM eprop = m->get_property ("grob-property");
127   if (scm_is_symbol (eprop))
128     {
129       grob_property_path = scm_list_1 (eprop);
130     }
131
132   return grob_property_path;
133 }
134
135 void
136 Push_property_iterator::process (Moment m)
137 {
138   SCM sym = get_music ()->get_property ("symbol");
139   if (check_grob (get_music (), sym))
140     {
141       SCM grob_property_path = get_property_path (get_music ());
142       SCM val = get_music ()->get_property ("grob-value");
143
144       if (to_boolean (get_music ()->get_property ("pop-first"))
145           && !to_boolean (get_music ()->get_property ("once")))
146         send_stream_event (get_outlet (), "Revert", get_music ()->origin (),
147                            ly_symbol2scm ("symbol"), sym,
148                            ly_symbol2scm ("property-path"), grob_property_path);
149
150       send_stream_event (get_outlet (), "Override", get_music ()->origin (),
151                          ly_symbol2scm ("symbol"), sym,
152                          ly_symbol2scm ("property-path"), grob_property_path,
153                          ly_symbol2scm ("value"), val);
154     }
155   Simple_music_iterator::process (m);
156 }
157
158 MAKE_SCHEME_CALLBACK (Push_property_iterator, once_finalization, 2);
159 SCM
160 Push_property_iterator::once_finalization (SCM ctx, SCM music)
161 {
162   Music *mus = Music::unsmob (music);
163   Context *c = Context::unsmob (ctx);
164
165   SCM sym = mus->get_property ("symbol");
166   if (check_grob (mus, sym))
167     {
168       SCM grob_property_path = get_property_path (mus);
169
170       send_stream_event (c, "Revert", mus->origin (),
171                          ly_symbol2scm ("symbol"), sym,
172                          ly_symbol2scm ("property-path"), grob_property_path);
173     }
174   return SCM_UNSPECIFIED;
175 }
176
177 void
178 Push_property_iterator::do_quit ()
179 {
180   if (to_boolean (get_music ()->get_property ("once")))
181     {
182       SCM trans = get_outlet ()->self_scm ();
183       SCM music = get_music ()->self_scm ();
184
185       Global_context *tg = get_outlet ()->get_global_context ();
186       tg->add_finalization (scm_list_3 (once_finalization_proc,
187                                         trans, music));
188     }
189 }
190
191 void
192 Pop_property_iterator::process (Moment m)
193 {
194   SCM sym = get_music ()->get_property ("symbol");
195
196   if (check_grob (get_music (), sym))
197     {
198       SCM grob_property_path = get_property_path (get_music ());
199
200       send_stream_event (get_outlet (), "Revert", get_music ()->origin (),
201                          ly_symbol2scm ("symbol"), sym,
202                          ly_symbol2scm ("property-path"), grob_property_path);
203     }
204   Simple_music_iterator::process (m);
205 }
206
207 IMPLEMENT_CTOR_CALLBACK (Pop_property_iterator);
208 IMPLEMENT_CTOR_CALLBACK (Push_property_iterator);
209 IMPLEMENT_CTOR_CALLBACK (Property_iterator);
210 IMPLEMENT_CTOR_CALLBACK (Property_unset_iterator);