]> git.lyx.org Git - lyx.git/blob - src/mathed/array.C
use stream-like syntax for LaTeX output
[lyx.git] / src / mathed / array.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_inset.h"
6 #include "math_charinset.h"
7 #include "math_scriptinset.h"
8 #include "debug.h"
9 #include "array.h"
10 #include "mathed/support.h"
11 #include "support/LAssert.h"
12
13 using std::ostream;
14 using std::endl;
15
16
17 MathArray::MathArray()
18 {}
19
20
21 MathArray::MathArray(MathArray const & array, size_type from, size_type to)
22         : bf_(array.begin() + from, array.begin() + to)
23 {}
24
25
26 void MathArray::substitute(MathMacro const & m)
27 {
28         for (iterator it = begin(); it != end(); ++it)
29                 it->nucleus()->substitute(m);
30 }
31
32
33 MathScriptInset const * MathArray::asScript(const_iterator it) const
34 {
35         if (it->nucleus()->asScriptInset())
36                 return 0;
37         const_iterator jt = it + 1;
38         if (jt == end())
39                 return 0;
40         return jt->nucleus()->asScriptInset();
41 }
42
43
44 MathAtom & MathArray::at(size_type pos)
45 {
46         lyx::Assert(pos < size());
47         return bf_[pos];
48 }
49
50
51 MathAtom const & MathArray::at(size_type pos) const
52 {
53         lyx::Assert(pos < size());
54         return bf_[pos];
55 }
56
57
58 void MathArray::insert(size_type pos, MathAtom const & t)
59 {
60         bf_.insert(begin() + pos, t);
61 }
62
63
64 void MathArray::insert(size_type pos, MathArray const & array)
65 {
66         bf_.insert(begin() + pos, array.begin(), array.end());
67 }
68
69
70 void MathArray::push_back(MathAtom const & t)
71 {       
72         bf_.push_back(t);
73 }
74
75
76 void MathArray::push_back(MathArray const & array)
77 {
78         insert(size(), array);
79 }
80
81
82 void MathArray::clear()
83 {
84         erase();
85 }
86
87
88 void MathArray::swap(MathArray & array)
89 {
90         if (this != &array) 
91                 bf_.swap(array.bf_);
92 }
93
94
95 bool MathArray::empty() const
96 {
97         return bf_.empty();
98 }
99    
100
101 MathArray::size_type MathArray::size() const
102 {
103         return bf_.size();
104 }
105
106
107 void MathArray::erase()
108 {
109         erase(0, size());
110 }
111
112
113 void MathArray::erase(size_type pos)
114 {
115         if (pos < size())
116                 erase(pos, pos + 1);
117 }
118
119
120 void MathArray::erase(size_type pos1, size_type pos2)
121 {
122         bf_.erase(begin() + pos1, begin() + pos2);
123 }
124
125
126 MathAtom & MathArray::back()
127 {
128         return bf_.back();
129 }
130
131
132 void MathArray::dump2(ostream & os) const
133 {
134         for (const_iterator it = begin(); it != end(); ++it)
135                 os << it->nucleus() << ' ';
136 }
137
138
139 void MathArray::dump(ostream & os) const
140 {
141         for (const_iterator it = begin(); it != end(); ++it)
142                 os << "<" << it->nucleus() << ">";
143 }
144
145
146 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
147 {
148         ar.dump2(os);
149         return os;
150 }
151
152
153 // returns sequence of char with same code starting at it up to end
154 // it might be less, though...
155 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
156 {
157         string s;
158         MathCharInset const * p = it->nucleus()->asCharInset();
159         if (!p)
160                 return s;
161
162         for (MathTextCodes c = p->code(); it != end; ++it) {
163                 if (!it->nucleus())
164                         break;
165                 p = it->nucleus()->asCharInset();
166                 if (!p || p->code() != c)
167                         break;
168                 s += p->getChar();
169         }
170         return s;
171 }
172
173
174 void MathArray::write(MathWriteInfo & wi) const
175 {
176         for (const_iterator it = begin(); it != end(); ++it) {  
177                 MathInset * p = it->nucleus();
178                 if (!p)
179                         continue;
180
181 /*
182                 if (p->asCharInset()) {
183                         MathCharInset const * c = p->asCharInset();
184                         // special handling for character sequences with the same code
185                         string s = charSequence(it, end());
186                         c->writeHeader(os);
187                         os << s;
188                         c->writeTrailer(os);
189                         it += s.size() - 1;
190                 } else
191 */
192                 if (MathScriptInset const * q = asScript(it)) {
193                         q->write(p, wi);
194                         ++it;
195                 } else {
196                         p->write(wi);
197                 }
198         }
199 }
200
201
202 void MathArray::writeNormal(ostream & os) const
203 {
204         os << "[par ";
205         for (const_iterator it = begin(); it != end(); ++it)
206                 (*it)->writeNormal(os);
207         os << "]";
208 }
209
210
211 void MathArray::validate(LaTeXFeatures & features) const
212 {
213         for (const_iterator it = begin(); it != end(); ++it)
214                 it->nucleus()->validate(features);
215 }
216
217
218 void MathArray::pop_back()
219 {       
220         if (!size()) {
221                 lyxerr << "pop_back from empty array!\n";
222                 return;
223         }
224         bf_.pop_back();
225 }
226
227
228 MathArray::const_iterator MathArray::begin() const
229 {
230         return bf_.begin();
231 }
232
233
234 MathArray::const_iterator MathArray::end() const
235 {
236         return bf_.end();
237 }
238
239
240 MathArray::iterator MathArray::begin()
241 {
242         return bf_.begin();
243 }
244
245
246 MathArray::iterator MathArray::end()
247 {
248         return bf_.end();
249 }