]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeat-acknowledge-engraver.cc
Issue 4997/3: Use Preinit in Music
[lilypond.git] / lily / repeat-acknowledge-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--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 "engraver.hh"
21 #include "translator-group.hh"
22 #include "context.hh"
23 #include "repeated-music.hh"
24
25 #include "translator.icc"
26
27 /*
28   Objective:
29
30   -- set and reset repeatCommands, so Unfolded_repeat_iterator knows
31   where to set variables.
32
33   -- collect information passed by Unfolded_repeat_iterator for
34   Bar_engraver: writes whichBar property. (TODO: check for
35   interactions with timing engraver.)
36 */
37 class Repeat_acknowledge_engraver : public Engraver
38 {
39 public:
40
41   TRANSLATOR_DECLARATIONS (Repeat_acknowledge_engraver);
42 protected:
43   void start_translation_timestep ();
44   void process_music ();
45   virtual void initialize ();
46 };
47
48 void
49 Repeat_acknowledge_engraver::initialize ()
50 {
51   context ()->set_property ("repeatCommands", SCM_EOL);
52 }
53
54 Repeat_acknowledge_engraver::Repeat_acknowledge_engraver ()
55 {
56 }
57
58 void
59 Repeat_acknowledge_engraver::start_translation_timestep ()
60 {
61   SCM rc;
62   Context *tr = context ()->where_defined (ly_symbol2scm ("repeatCommands"), &rc);
63   if (!tr)
64     tr = context ();
65
66   tr->set_property ("repeatCommands", SCM_EOL);
67 }
68
69 void
70 Repeat_acknowledge_engraver::process_music ()
71 {
72   /*
73     At the start of a piece, we don't print any repeat bars.
74   */
75   if (!now_mom ().main_part_)
76     return;
77
78   SCM cs = get_property ("repeatCommands");
79
80   string s = "";
81   bool start = false;
82   bool end = false;
83   bool segno = false;
84   bool volta_found = false;
85   while (scm_is_pair (cs))
86     {
87       SCM command = scm_car (cs);
88       if (scm_is_eq (command, ly_symbol2scm ("start-repeat")))
89         start = true;
90       else if (scm_is_eq (command, ly_symbol2scm ("end-repeat")))
91         end = true;
92       else if (scm_is_eq (command, ly_symbol2scm ("segno-display")))
93         segno = true;
94       else if (scm_is_pair (command)
95                && scm_is_eq (scm_car (command), ly_symbol2scm ("volta")))
96         volta_found = true;
97       cs = scm_cdr (cs);
98     }
99
100   /*
101     Select which bar type to set
102   */
103   if (segno)
104     if (start)
105       if (end) // { segno, start, end }
106         s = robust_scm2string (get_property ("doubleRepeatSegnoType"), ":|.S.|:");
107       else // { segno, start }
108         s = robust_scm2string (get_property ("startRepeatSegnoType"), "S.|:");
109     else if (end) // { segno, end }
110       s = robust_scm2string (get_property ("endRepeatSegnoType"), ":|.S");
111     else // { segno }
112       s = robust_scm2string (get_property ("segnoType"), "S");
113   else if (start)
114     if (end) // { start, end }
115       s = robust_scm2string (get_property ("doubleRepeatType"), ":|.|:");
116     else // { start }
117       s = robust_scm2string (get_property ("startRepeatType"), ".|:");
118   else if (end) // { end }
119     s = robust_scm2string (get_property ("endRepeatType"), ":|.");
120
121   /*
122     TODO: line breaks might be allowed if we set whichBar to "".
123   */
124
125   /*
126     We only set the barline if we wouldn't overwrite a previously set
127     barline.
128   */
129   SCM wb = get_property ("whichBar");
130   SCM db = get_property ("defaultBarType");
131   if (!scm_is_string (wb) || ly_is_equal (db, wb))
132     {
133       if (s != "" || (volta_found && !scm_is_string (wb)))
134         context ()->set_property ("whichBar", ly_string2scm (s));
135     }
136 }
137
138 void
139 Repeat_acknowledge_engraver::boot ()
140 {
141
142 }
143
144 ADD_TRANSLATOR (Repeat_acknowledge_engraver,
145                 /* doc */
146                 "Acknowledge repeated music, and convert the contents of"
147                 " @code{repeatCommands} into an appropriate setting for"
148                 " @code{whichBar}.",
149
150                 /* create */
151                 "",
152
153                 /* read */
154                 "doubleRepeatType "
155                 "startRepeatType "
156                 "endRepeatType "
157                 "doubleRepeatSegnoType "
158                 "startRepeatSegnoType "
159                 "endRepeatSegnoType "
160                 "segnoType "
161                 "repeatCommands "
162                 "whichBar ",
163
164                 /* write */
165                 ""
166                );