]> git.lyx.org Git - features.git/blobdiff - src/mathed/array.C
write \mathrm{x}\mathrm{y} as \mathrm{xy} again
[features.git] / src / mathed / array.C
index 593b6fe8d4a87b058c709891c4f190fbd6757d8e..48deaf73ab7066e3b125c9b91b8cb0d7bb11a3bb 100644 (file)
@@ -4,95 +4,73 @@
 
 #include "math_inset.h"
 #include "math_charinset.h"
+#include "math_scriptinset.h"
+#include "math_stringinset.h"
 #include "debug.h"
 #include "array.h"
 #include "mathed/support.h"
+#include "support/LAssert.h"
 
 using std::ostream;
 using std::endl;
 
+
 MathArray::MathArray()
 {}
 
 
-MathArray::~MathArray()
-{
-       erase();
-}
-
-
-MathArray::MathArray(MathArray const & array)
-       : bf_(array.bf_)
-{
-       deep_copy(begin(), end());
-}
-
-
-MathArray::MathArray(MathArray const & array, int from, int to)
+MathArray::MathArray(MathArray const & array, size_type from, size_type to)
        : bf_(array.begin() + from, array.begin() + to)
-{
-       deep_copy(begin(), end());
-}
-
-
-void MathArray::deep_copy(iterator from, iterator to)
-{
-       for (iterator it = from; it != to; ++it)
-               *it = (*it)->clone();
-}
-
-
-int MathArray::last() const
-{
-       return size() - 1;
-}
+{}
 
 
 void MathArray::substitute(MathMacro const & m)
 {
-       MathArray tmp;
        for (iterator it = begin(); it != end(); ++it)
-               (*it)->substitute(tmp, m);
-       swap(tmp);
+               it->nucleus()->substitute(m);
 }
 
 
-MathArray & MathArray::operator=(MathArray const & array)
+MathScriptInset const * MathArray::asScript(const_iterator it) const
 {
-       MathArray tmp(array);
-       swap(tmp);
-       return *this;
+       if (it->nucleus()->asScriptInset())
+               return 0;
+       const_iterator jt = it + 1;
+       if (jt == end())
+               return 0;
+       return jt->nucleus()->asScriptInset();
 }
 
 
-MathInset * MathArray::nextInset(int pos)
+MathAtom & MathArray::at(size_type pos)
 {
-       return (pos == size()) ? 0 : bf_[pos];
+       lyx::Assert(pos < size());
+       return bf_[pos];
 }
 
 
-MathInset const * MathArray::nextInset(int pos) const
+MathAtom const & MathArray::at(size_type pos) const
 {
-       return (pos == size()) ? 0 : bf_[pos];
+       lyx::Assert(pos < size());
+       return bf_[pos];
 }
 
 
-void MathArray::insert(int pos, MathInset * p)
+void MathArray::insert(size_type pos, MathAtom const & t)
 {
-       bf_.insert(begin() + pos, p);
+       bf_.insert(begin() + pos, t);
 }
 
 
-void MathArray::insert(int pos, MathArray const & array)
+void MathArray::insert(size_type pos, MathArray const & array)
 {
        bf_.insert(begin() + pos, array.begin(), array.end());
-       deep_copy(begin() + pos, begin() + pos + array.size());
 }
 
 
-void MathArray::push_back(MathInset * p)
+void MathArray::push_back(MathAtom const & t)
 {      
-       insert(size(), p);
+       bf_.push_back(t);
 }
 
 
@@ -119,9 +97,9 @@ bool MathArray::empty() const
 {
        return bf_.empty();
 }
-   
 
-int MathArray::size() const
+
+MathArray::size_type MathArray::size() const
 {
        return bf_.size();
 }
@@ -133,38 +111,36 @@ void MathArray::erase()
 }
 
 
-void MathArray::erase(int pos)
+void MathArray::erase(size_type pos)
 {
        if (pos < size())
                erase(pos, pos + 1);
 }
 
 
-void MathArray::erase(int pos1, int pos2)
+void MathArray::erase(size_type pos1, size_type pos2)
 {
-       for (iterator it = begin() + pos1; it != begin() + pos2; ++it)
-               delete *it;
        bf_.erase(begin() + pos1, begin() + pos2);
 }
 
 
-MathInset * MathArray::back() const
+MathAtom & MathArray::back()
 {
-       return size() ? bf_.back() : 0;
+       return bf_.back();
 }
 
 
 void MathArray::dump2(ostream & os) const
 {
        for (const_iterator it = begin(); it != end(); ++it)
-               os << *it << ' ';
+               os << it->nucleus() << ' ';
 }
 
 
 void MathArray::dump(ostream & os) const
 {
        for (const_iterator it = begin(); it != end(); ++it)
-               os << "<" << *it << ">";
+               os << "<" << it->nucleus() << ">";
 }
 
 
@@ -174,58 +150,89 @@ std::ostream & operator<<(std::ostream & os, MathArray const & ar)
        return os;
 }
 
+
 // returns sequence of char with same code starting at it up to end
 // it might be less, though...
 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
 {
        string s;
-       MathCharInset const * p = (*it)->asCharInset();
+       MathCharInset const * p = it->nucleus()->asCharInset();
        if (!p)
                return s;
 
-       MathTextCodes c = p->code();
-       while (it != end && (p = (*it)->asCharInset()) && p->code() == c) { 
+       for (MathTextCodes c = p->code(); it != end; ++it) {
+               if (!it->nucleus())
+                       break;
+               p = it->nucleus()->asCharInset();
+               if (!p || p->code() != c)
+                       break;
                s += p->getChar();
-               ++it;
        }
        return s;
 }
 
 
-void MathArray::write(ostream & os, bool fragile) const
+MathArray MathArray::glueChars() const
 {
-       for (const_iterator it = begin(); it != end(); ) {
-               MathCharInset const * p = (*it)->asCharInset();
-               if (p) {
-                       // special handling for character sequences with the same code
+       MathArray ar;
+       const_iterator it = begin();
+       while (it != end()) {
+               if (it->nucleus() && it->nucleus()->asCharInset()) {
                        string s = charSequence(it, end());
-                       p->writeHeader(os);
-                       os << s;
-                       p->writeTrailer(os);
+                       MathTextCodes c = it->nucleus()->asCharInset()->code();
+                       ar.push_back(MathAtom(new MathStringInset(s, c)));
                        it += s.size();
                } else {
-                       (*it)->write(os, fragile);
+                       ar.push_back(*it);
                        ++it;
                }
        }
+       return ar;
 }
 
 
-void MathArray::writeNormal(ostream & os) const
+void MathArray::write(MathWriteInfo & wi) const
 {
-       if (empty()) {
-               os << "[par] ";
-               return;
+       glueChars().write1(wi);
+}
+
+
+void MathArray::write1(MathWriteInfo & wi) const
+{
+       for (const_iterator it = begin(); it != end(); ++it) {  
+               MathInset * p = it->nucleus();
+               if (!p)
+                       continue;
+               if (MathScriptInset const * q = asScript(it)) {
+                       q->write(p, wi);
+                       ++it;
+               } else {
+                       p->write(wi);
+               }
        }
+}
 
-       write(os, true);
+
+void MathArray::writeNormal(ostream & os) const
+{
+       for (const_iterator it = begin(); it != end(); ++it) {  
+               MathInset * p = it->nucleus();
+               if (!p)
+                       continue;
+               if (MathScriptInset const * q = asScript(it)) {
+                       q->writeNormal(p, os);
+                       ++it;
+               } else {
+                       p->writeNormal(os);
+               }
+       }
 }
 
 
 void MathArray::validate(LaTeXFeatures & features) const
 {
        for (const_iterator it = begin(); it != end(); ++it)
-               (*it)->validate(features);
+               it->nucleus()->validate(features);
 }
 
 
@@ -235,7 +242,6 @@ void MathArray::pop_back()
                lyxerr << "pop_back from empty array!\n";
                return;
        }
-       delete back();
        bf_.pop_back();
 }