]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
the up/down stuff reworked
[lyx.git] / src / mathed / math_data.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_data.h"
8 #include "math_inset.h"
9 #include "math_deliminset.h"
10 #include "math_charinset.h"
11 #include "math_scriptinset.h"
12 #include "math_stringinset.h"
13 #include "math_matrixinset.h"
14 #include "math_mathmlstream.h"
15 #include "math_support.h"
16 #include "math_replace.h"
17 #include "debug.h"
18 #include "support/LAssert.h"
19
20
21 MathArray::MathArray(const_iterator from, const_iterator to)
22         : base_type(from, 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 MathAtom & MathArray::operator[](size_type pos)
34 {
35         lyx::Assert(pos < size());
36         return base_type::operator[](pos);
37 }
38
39
40 MathAtom const & MathArray::operator[](size_type pos) const
41 {
42         lyx::Assert(pos < size());
43         return base_type::operator[](pos);
44 }
45
46
47 void MathArray::insert(size_type pos, MathAtom const & t)
48 {
49         base_type::insert(begin() + pos, t);
50 }
51
52
53 void MathArray::insert(size_type pos, MathArray const & ar)
54 {
55         base_type::insert(begin() + pos, ar.begin(), ar.end());
56 }
57
58
59 void MathArray::append(MathArray const & ar)
60 {
61         insert(size(), ar);
62 }
63
64
65 void MathArray::erase(size_type pos)
66 {
67         if (pos < size())
68                 erase(pos, pos + 1);
69 }
70
71
72 void MathArray::erase(iterator pos1, iterator pos2)
73 {
74         base_type::erase(pos1, pos2);
75 }
76
77
78 void MathArray::erase(iterator pos)
79 {
80         base_type::erase(pos);
81 }
82
83
84 void MathArray::erase(size_type pos1, size_type pos2)
85 {
86         base_type::erase(begin() + pos1, begin() + pos2);
87 }
88
89
90 void MathArray::dump2() const
91 {
92         NormalStream ns(lyxerr);
93         for (const_iterator it = begin(); it != end(); ++it)
94                 ns << it->nucleus() << ' ';
95 }
96
97
98 void MathArray::dump() const
99 {
100         NormalStream ns(lyxerr);
101         for (const_iterator it = begin(); it != end(); ++it)
102                 ns << "<" << it->nucleus() << ">";
103 }
104
105
106 void MathArray::validate(LaTeXFeatures & features) const
107 {
108         for (const_iterator it = begin(); it != end(); ++it)
109                 if (it->nucleus())
110                         it->nucleus()->validate(features);
111 }
112
113
114 bool MathArray::match(MathArray const & ar) const
115 {
116         return size() == ar.size() && matchpart(ar, 0);
117 }
118
119
120 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
121 {
122         if (size() < ar.size() + pos)
123                 return false;
124         const_iterator it = begin() + pos;
125         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
126                 if (!jt->nucleus()->match(it->nucleus()))
127                         return false;
128         return true;
129 }
130
131
132 void MathArray::replace(ReplaceData & rep)
133 {
134         for (size_type i = 0; i < size(); ++i) {
135                 if (find1(rep.from, i)) {
136                         // match found
137                         lyxerr << "match found!\n";
138                         erase(i, i + rep.from.size());
139                         insert(i, rep.to);
140                 }
141         }
142
143         for (const_iterator it = begin(); it != end(); ++it)
144                 it->nucleus()->replace(rep);
145 }
146
147
148 bool MathArray::find1(MathArray const & ar, size_type pos) const
149 {
150         //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
151         for (size_type i = 0, n = ar.size(); i < n; ++i)
152                 if (!operator[](pos + i)->match(ar[i].nucleus()))
153                         return false;
154         return true;
155 }
156
157
158 MathArray::size_type MathArray::find(MathArray const & ar) const
159 {
160         for (int i = 0, last = size() - ar.size(); i < last; ++i)
161                 if (find1(ar, i))
162                         return i;
163         return size();
164 }
165
166
167 MathArray::size_type MathArray::find_last(MathArray const & ar) const
168 {
169         for (int i = size() - ar.size(); i >= 0; --i) 
170                 if (find1(ar, i))
171                         return i;
172         return size();
173 }
174
175
176 bool MathArray::contains(MathArray const & ar) const
177 {
178         if (find(ar) != size())
179                 return true;
180         for (const_iterator it = begin(); it != end(); ++it)
181                 if (it->nucleus()->contains(ar))
182                         return true;
183         return false;
184 }