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