]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/context.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[lyx.git] / src / tex2lyx / context.C
1 /**
2  * \file context.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include <iostream>
14
15 #include "support/lstrings.h"
16 #include "context.h"
17
18
19 namespace lyx {
20
21 using std::ostream;
22 using std::endl;
23 using std::string;
24
25
26 namespace {
27
28 void begin_layout(ostream & os, LyXLayout_ptr layout, Font const & font,
29                   Font const & normalfont)
30 {
31         os << "\n\\begin_layout " << layout->name() << "\n";
32         // FIXME: This is not enough for things like
33         // \\Huge par1 \\par par2
34         output_font_change(os, normalfont, font);
35 }
36
37
38 void end_layout(ostream & os)
39 {
40         os << "\n\\end_layout\n";
41 }
42
43
44 void begin_deeper(ostream & os)
45 {
46         os << "\n\\begin_deeper";
47 }
48
49
50 void end_deeper(ostream & os)
51 {
52         os << "\n\\end_deeper";
53 }
54
55 }
56
57
58 bool operator==(Font const & f1, Font const & f2)
59 {
60         return
61                 f1.size == f2.size &&
62                 f1.family == f2.family &&
63                 f1.series == f2.series &&
64                 f1.shape == f2.shape;
65 }
66
67
68 void output_font_change(ostream & os, Font const & oldfont,
69                         Font const & newfont)
70 {
71         if (oldfont.family != newfont.family)
72                 os << "\n\\family " << newfont.family << '\n';
73         if (oldfont.series != newfont.series)
74                 os << "\n\\series " << newfont.series << '\n';
75         if (oldfont.shape != newfont.shape)
76                 os << "\n\\shape " << newfont.shape << '\n';
77         if (oldfont.size != newfont.size)
78                 os << "\n\\size " << newfont.size << '\n';
79 }
80
81
82 Font Context::normalfont;
83 bool Context::empty = true;
84
85
86 Context::Context(bool need_layout_,
87                  LyXTextClass const & textclass_,
88                  LyXLayout_ptr layout_, LyXLayout_ptr parent_layout_,
89                  Font font_)
90         : need_layout(need_layout_),
91           need_end_layout(false), need_end_deeper(false),
92           has_item(false), deeper_paragraph(false),
93           new_layout_allowed(true), textclass(textclass_),
94           layout(layout_), parent_layout(parent_layout_),
95           font(font_)
96 {
97         if (!layout.get())
98                 layout = textclass.defaultLayout();
99         if (!parent_layout.get())
100                 parent_layout = textclass.defaultLayout();
101 }
102
103
104 Context::~Context()
105 {
106         if (!extra_stuff.empty())
107                 std::cerr << "Bug: Ignoring extra stuff '" << extra_stuff
108                           << '\'' << std::endl;
109 }
110
111
112 void Context::check_layout(ostream & os)
113 {
114         if (need_layout) {
115                 check_end_layout(os);
116
117                 // are we in a list-like environment?
118                 if (layout->isEnvironment()
119                     && layout->latextype != LATEX_ENVIRONMENT) {
120                         // A list-like environment
121                         if (has_item) {
122                                 // a new item. If we had a standard
123                                 // paragraph before, we have to end it.
124                                 if (deeper_paragraph) {
125                                         end_deeper(os);
126                                         deeper_paragraph = false;
127                                 }
128                                 begin_layout(os, layout, font, normalfont);
129                                 has_item = false;
130                         } else {
131                                 // a standard paragraph in an
132                                 // enumeration. We have to recognize
133                                 // that this may require a begin_deeper.
134                                 if (!deeper_paragraph)
135                                         begin_deeper(os);
136                                 begin_layout(os, textclass.defaultLayout(),
137                                              font, normalfont);
138                                 deeper_paragraph = true;
139                         }
140                 } else {
141                         // No list-like environment
142                         begin_layout(os, layout, font, normalfont);
143                 }
144                 need_layout = false;
145                 need_end_layout = true;
146                 if (!extra_stuff.empty()) {
147                         os << extra_stuff;
148                         extra_stuff.erase();
149                 }
150                 os << "\n";
151                 empty = false;
152         }
153 }
154
155
156 void Context::check_end_layout(ostream & os)
157 {
158         if (need_end_layout) {
159                 end_layout(os);
160                 need_end_layout = false;
161         }
162 }
163
164
165 void Context::check_deeper(ostream & os)
166 {
167         if (parent_layout->isEnvironment()) {
168                 // We start a nested environment.
169                 // We need to increase the depth.
170                 if (need_end_deeper) {
171                         // no need to have \end_deeper \begin_deeper
172                         need_end_deeper = false;
173                 } else {
174                         begin_deeper(os);
175                         need_end_deeper = true;
176                 }
177         } else
178                 check_end_deeper(os);
179 }
180
181
182 void Context::check_end_deeper(ostream & os)
183 {
184         if (need_end_deeper) {
185                 end_deeper(os);
186                 need_end_deeper = false;
187         }
188         if (deeper_paragraph) {
189                 end_deeper(os);
190                 deeper_paragraph = false;
191         }
192 }
193
194
195 void Context::set_item()
196 {
197         need_layout = true;
198         has_item = true;
199 }
200
201
202 void Context::new_paragraph(ostream & os)
203 {
204         check_end_layout(os);
205         need_layout = true;
206 }
207
208
209 void Context::add_extra_stuff(std::string const & stuff)
210 {
211         if (!lyx::support::contains(extra_stuff, stuff))
212                 extra_stuff += stuff;
213 }
214
215
216 void Context::dump(ostream & os, string const & desc) const
217 {
218         os << "\n" << desc <<" [";
219         if (need_layout)
220                 os << "need_layout ";
221         if (need_end_layout)
222                 os << "need_end_layout ";
223         if (need_end_deeper)
224                 os << "need_end_deeper ";
225         if (has_item)
226                 os << "has_item ";
227         if (deeper_paragraph)
228                 os << "deeper_paragraph ";
229         if (new_layout_allowed)
230                 os << "new_layout_allowed ";
231         if (!extra_stuff.empty())
232                 os << "extrastuff=[" << extra_stuff << "] ";
233         os << "textclass=" << textclass.name()
234            << " layout=" << layout->name()
235            << " parent_layout=" << parent_layout->name() << "] font=["
236            << font.size << ' ' << font.family << ' ' << font.series << ' '
237            << font.shape << ']' << endl;
238 }
239
240
241 } // namespace lyx