]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/context.C
Add a Buffer::fully_loaded member function, returning true only when
[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\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                         if (!extra_stuff.empty()) {
98                                 os << extra_stuff;
99                                 extra_stuff.erase();
100                         }
101                 }
102         }
103 }
104
105
106 void Context::check_end_layout(ostream & os)
107 {
108         if (need_end_layout) {
109                 end_layout(os);
110                 need_end_layout = false;
111         }
112 }
113
114
115 void Context::check_deeper(ostream & os)
116 {
117         if (parent_layout->isEnvironment()) {
118                 if (need_end_deeper) {
119                         // no need to have \end_deeper \begin_deeper
120                         need_end_deeper = false;
121                 } else {
122                         begin_deeper(os);
123                         need_end_deeper = true;
124                 }
125         } else
126                 check_end_deeper(os);
127 }
128
129
130 void Context::check_end_deeper(ostream & os)
131 {
132         if (need_end_deeper) {
133                 end_deeper(os);
134                 need_end_deeper = false;
135         }
136         if (deeper_paragraph) {
137                 end_deeper(os);
138                 deeper_paragraph = false;
139         }
140 }
141
142
143 void Context::dump(ostream & os, string const & desc) const
144 {
145         os << "\n" << desc <<" [";
146         if (need_layout)
147                 os << "need_layout ";
148         if (need_end_layout)
149                 os << "need_end_layout ";
150         if (!extra_stuff.empty())
151                 os << "extrastuff=[" << extra_stuff << "] ";
152         os << "layout=" << layout->name();
153         os << " parent_layout=" << parent_layout->name() << "]" << endl;
154 }