]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/context.C
fix bug 1750
[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\n";
40 }
41
42
43 void end_deeper(ostream & os)
44 {
45         os << "\n\\end_deeper\n";
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                         if (has_item) {
77                                 if (deeper_paragraph) {
78                                         end_deeper(os);
79                                         deeper_paragraph = false;
80                                 }
81                                 begin_layout(os, layout);
82                                 has_item = false;
83                                 need_layout=false;
84                                 need_end_layout = true;
85                         } else {
86                                 // a standard paragraph in an
87                                 // enumeration. We have to recognize
88                                 // that this may require a begin_deeper.
89                                 if (!deeper_paragraph)
90                                         begin_deeper(os);
91                                 begin_layout(os, textclass.defaultLayout());
92                                 need_layout=false;
93                                 need_end_layout = true;
94                                 deeper_paragraph = true;
95                         }
96                 } else {
97                         begin_layout(os, layout);
98                         need_layout=false;
99                         need_end_layout = true;
100                 }
101                 if (!extra_stuff.empty()) {
102                         os << extra_stuff;
103                         extra_stuff.erase();
104                 }
105                 os << "\n";
106         }
107 }
108
109
110 void Context::check_end_layout(ostream & os)
111 {
112         if (need_end_layout) {
113                 end_layout(os);
114                 need_end_layout = false;
115         }
116 }
117
118
119 void Context::check_deeper(ostream & os)
120 {
121         if (parent_layout->isEnvironment()) {
122                 if (need_end_deeper) {
123                         // no need to have \end_deeper \begin_deeper
124                         need_end_deeper = false;
125                 } else {
126                         begin_deeper(os);
127                         need_end_deeper = true;
128                 }
129         } else
130                 check_end_deeper(os);
131 }
132
133
134 void Context::check_end_deeper(ostream & os)
135 {
136         if (need_end_deeper) {
137                 end_deeper(os);
138                 need_end_deeper = false;
139         }
140         if (deeper_paragraph) {
141                 end_deeper(os);
142                 deeper_paragraph = false;
143         }
144 }
145
146
147 void Context::set_item()
148 {
149         need_layout = true;
150         has_item = true;
151 }
152
153
154 void Context::new_paragraph(ostream & os)
155 {
156         check_end_layout(os);
157         need_layout = true;
158 }
159
160
161 void Context::add_extra_stuff(std::string const & stuff)
162 {
163         if (!lyx::support::contains(extra_stuff, stuff))
164                 extra_stuff += stuff;
165 }
166
167
168 void Context::dump(ostream & os, string const & desc) const
169 {
170         os << "\n" << desc <<" [";
171         if (need_layout)
172                 os << "need_layout ";
173         if (need_end_layout)
174                 os << "need_end_layout ";
175         if (need_end_deeper)
176                 os << "need_end_deeper ";
177         if (has_item)
178                 os << "has_item ";
179         if (deeper_paragraph)
180                 os << "deeper_paragraph ";
181         if (!extra_stuff.empty())
182                 os << "extrastuff=[" << extra_stuff << "] ";
183         os << "layout=" << layout->name();
184         os << " parent_layout=" << parent_layout->name() << "]" << endl;
185 }