]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/context.C
some tabular fixes for the problems reported by Helge
[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 using std::ostream;
19 using std::endl;
20 using std::string;
21
22
23 namespace {
24
25 void begin_layout(ostream & os, LyXLayout_ptr layout)
26 {
27         os << "\n\\begin_layout " << layout->name() << "\n";
28 }
29
30
31 void end_layout(ostream & os)
32 {
33         os << "\n\\end_layout\n";
34 }
35
36
37 void begin_deeper(ostream & os)
38 {
39         os << "\n\\begin_deeper";
40 }
41
42
43 void end_deeper(ostream & os)
44 {
45         os << "\n\\end_deeper";
46 }
47
48 }
49
50 Context::Context(bool need_layout_,
51                  LyXTextClass const & textclass_,
52                  LyXLayout_ptr layout_, LyXLayout_ptr parent_layout_,
53                  Font font_)
54         : need_layout(need_layout_),
55           need_end_layout(false), need_end_deeper(false),
56           has_item(false), deeper_paragraph(false),
57           textclass(textclass_),
58           layout(layout_), parent_layout(parent_layout_),
59           font(font_)
60 {
61         if (!layout.get())
62                 layout = textclass.defaultLayout();
63         if (!parent_layout.get())
64                 parent_layout = textclass.defaultLayout();
65 }
66
67
68 void Context::check_layout(ostream & os)
69 {
70         if (need_layout) {
71                 check_end_layout(os);
72
73                 // are we in a list-like environment?
74                 if (layout->isEnvironment()
75                     && layout->latextype != LATEX_ENVIRONMENT) {
76                         // A list-like environment
77                         if (has_item) {
78                                 // a new item. If we had a standard
79                                 // paragraph before, we have to end it.
80                                 if (deeper_paragraph) {
81                                         end_deeper(os);
82                                         deeper_paragraph = false;
83                                 }
84                                 begin_layout(os, layout);
85                                 has_item = false;
86                                 need_layout=false;
87                                 need_end_layout = true;
88                         } else {
89                                 // a standard paragraph in an
90                                 // enumeration. We have to recognize
91                                 // that this may require a begin_deeper.
92                                 if (!deeper_paragraph)
93                                         begin_deeper(os);
94                                 begin_layout(os, textclass.defaultLayout());
95                                 need_layout=false;
96                                 need_end_layout = true;
97                                 deeper_paragraph = true;
98                         }
99                 } else {
100                         // No list-like environment
101                         begin_layout(os, layout);
102                         need_layout=false;
103                         need_end_layout = true;
104                 }
105                 if (!extra_stuff.empty()) {
106                         os << extra_stuff;
107                         extra_stuff.erase();
108                 }
109                 os << "\n";
110         }
111 }
112
113
114 void Context::check_end_layout(ostream & os)
115 {
116         if (need_end_layout) {
117                 end_layout(os);
118                 need_end_layout = false;
119         }
120 }
121
122
123 void Context::check_deeper(ostream & os)
124 {
125         if (parent_layout->isEnvironment()) {
126                 // We start a nested environment.
127                 // We need to increase the depth.
128                 if (need_end_deeper) {
129                         // no need to have \end_deeper \begin_deeper
130                         need_end_deeper = false;
131                 } else {
132                         begin_deeper(os);
133                         need_end_deeper = true;
134                 }
135         } else
136                 check_end_deeper(os);
137 }
138
139
140 void Context::check_end_deeper(ostream & os)
141 {
142         if (need_end_deeper) {
143                 end_deeper(os);
144                 need_end_deeper = false;
145         }
146         if (deeper_paragraph) {
147                 end_deeper(os);
148                 deeper_paragraph = false;
149         }
150 }
151
152
153 void Context::set_item()
154 {
155         need_layout = true;
156         has_item = true;
157 }
158
159
160 void Context::new_paragraph(ostream & os)
161 {
162         check_end_layout(os);
163         need_layout = true;
164 }
165
166
167 void Context::add_extra_stuff(std::string const & stuff)
168 {
169         if (!lyx::support::contains(extra_stuff, stuff))
170                 extra_stuff += stuff;
171 }
172
173
174 void Context::dump(ostream & os, string const & desc) const
175 {
176         os << "\n" << desc <<" [";
177         if (need_layout)
178                 os << "need_layout ";
179         if (need_end_layout)
180                 os << "need_end_layout ";
181         if (need_end_deeper)
182                 os << "need_end_deeper ";
183         if (has_item)
184                 os << "has_item ";
185         if (deeper_paragraph)
186                 os << "deeper_paragraph ";
187         if (!extra_stuff.empty())
188                 os << "extrastuff=[" << extra_stuff << "] ";
189         os << "layout=" << layout->name();
190         os << " parent_layout=" << parent_layout->name() << "]" << endl;
191 }