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