]> git.donarmstrong.com Git - lilypond.git/blob - lily/property-iterator.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / property-iterator.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 "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     Context *w = o->where_defined (symbol, &previous_value);
43     if (o != w)
44       previous_value = SCM_UNDEFINED;
45   }
46   send_stream_event (o, "SetProperty", m->origin (),
47                      ly_symbol2scm ("symbol"), symbol,
48                      ly_symbol2scm ("value"), m->get_property ("value"));
49
50   /* For \once \set install a finalization hook to reset the property to the
51    * previous value after the timestep */
52   if (once)
53     {
54       Global_context *tg = get_outlet ()->get_global_context ();
55       tg->add_finalization (scm_list_n (once_finalization_proc,
56                                         o->self_scm (), m->self_scm (),
57                                         ly_quote_scm (previous_value), SCM_UNDEFINED));
58     }
59
60   Simple_music_iterator::process (mom);
61 }
62
63 void
64 Property_unset_iterator::process (Moment m)
65 {
66   SCM sym = get_music ()->get_property ("symbol");
67   send_stream_event (get_outlet (), "UnsetProperty", get_music ()->origin (),
68                      ly_symbol2scm ("symbol"), sym);
69
70   Simple_music_iterator::process (m);
71 }
72
73 MAKE_SCHEME_CALLBACK (Property_iterator, once_finalization, 3);
74 SCM
75 Property_iterator::once_finalization (SCM ctx, SCM music, SCM previous_value)
76 {
77   Music *m = unsmob_music (music);
78   Context *c = unsmob_context (ctx);
79
80   // Do not use UnsetProperty, which sets the default, but rather
81   // cache the value before the \once \set command and restore it now
82   send_stream_event (c, "SetProperty", m->origin (),
83                      ly_symbol2scm ("symbol"), m->get_property ("symbol"),
84                      ly_symbol2scm ("value"), previous_value);
85
86   return SCM_UNSPECIFIED;
87 }
88
89 void
90 Property_iterator::do_quit ()
91 {
92 }
93
94 bool
95 check_grob (Music *mus, SCM sym)
96 {
97   bool g = to_boolean (scm_object_property (sym, ly_symbol2scm ("is-grob?")));
98
99   if (!g)
100     mus->origin ()->warning (_f ("not a grob name, `%s'",
101                                  ly_symbol2string (sym)));
102
103   return g;
104 }
105
106 SCM
107 get_property_path (Music *m)
108 {
109   SCM grob_property_path = m->get_property ("grob-property-path");
110
111   SCM eprop = m->get_property ("grob-property");
112   if (scm_is_symbol (eprop))
113     {
114       grob_property_path = scm_list_1 (eprop);
115     }
116
117   return grob_property_path;
118 }
119
120 void
121 Push_property_iterator::process (Moment m)
122 {
123   SCM sym = get_music ()->get_property ("symbol");
124   if (check_grob (get_music (), sym))
125     {
126       SCM grob_property_path = get_property_path (get_music ());
127       SCM val = get_music ()->get_property ("grob-value");
128
129       if (to_boolean (get_music ()->get_property ("pop-first"))
130           && !to_boolean (get_music ()->get_property ("once")))
131         send_stream_event (get_outlet (), "Revert", get_music ()->origin (),
132                            ly_symbol2scm ("symbol"), sym,
133                            ly_symbol2scm ("property-path"), grob_property_path);
134
135       send_stream_event (get_outlet (), "Override", get_music ()->origin (),
136                          ly_symbol2scm ("symbol"), sym,
137                          ly_symbol2scm ("property-path"), grob_property_path,
138                          ly_symbol2scm ("value"), val);
139     }
140   Simple_music_iterator::process (m);
141 }
142
143 MAKE_SCHEME_CALLBACK (Push_property_iterator, once_finalization, 2);
144 SCM
145 Push_property_iterator::once_finalization (SCM ctx, SCM music)
146 {
147   Music *mus = unsmob_music (music);
148   Context *c = unsmob_context (ctx);
149
150   SCM sym = mus->get_property ("symbol");
151   if (check_grob (mus, sym))
152     {
153       SCM grob_property_path = get_property_path (mus);
154
155       send_stream_event (c, "Revert", mus->origin (),
156                          ly_symbol2scm ("symbol"), sym,
157                          ly_symbol2scm ("property-path"), grob_property_path);
158     }
159   return SCM_UNSPECIFIED;
160 }
161
162 void
163 Push_property_iterator::do_quit ()
164 {
165   if (to_boolean (get_music ()->get_property ("once")))
166     {
167       SCM trans = get_outlet ()->self_scm ();
168       SCM music = get_music ()->self_scm ();
169
170       Global_context *tg = get_outlet ()->get_global_context ();
171       tg->add_finalization (scm_list_n (once_finalization_proc,
172                                         trans, music, SCM_UNDEFINED));
173     }
174 }
175
176 void
177 Pop_property_iterator::process (Moment m)
178 {
179   SCM sym = get_music ()->get_property ("symbol");
180
181   if (check_grob (get_music (), sym))
182     {
183       SCM grob_property_path = get_property_path (get_music ());
184
185       send_stream_event (get_outlet (), "Revert", get_music ()->origin (),
186                          ly_symbol2scm ("symbol"), sym,
187                          ly_symbol2scm ("property-path"), grob_property_path);
188     }
189   Simple_music_iterator::process (m);
190 }
191
192 IMPLEMENT_CTOR_CALLBACK (Pop_property_iterator);
193 IMPLEMENT_CTOR_CALLBACK (Push_property_iterator);
194 IMPLEMENT_CTOR_CALLBACK (Property_iterator);
195 IMPLEMENT_CTOR_CALLBACK (Property_unset_iterator);