]> 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 4197abf417813238c631902e55af4f5645ad3b33..48deaf73ab7066e3b125c9b91b8cb0d7bb11a3bb 100644 (file)
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
 #include "math_inset.h"
+#include "math_charinset.h"
+#include "math_scriptinset.h"
+#include "math_stringinset.h"
 #include "debug.h"
 #include "array.h"
-#include "math_scriptinset.h"
-#include "math_parser.h"
 #include "mathed/support.h"
+#include "support/LAssert.h"
 
 using std::ostream;
 using std::endl;
 
+
 MathArray::MathArray()
 {}
 
 
-MathArray::~MathArray()
-{
-       for (int pos = 0; pos < size(); next(pos)) 
-               if (MathIsInset(pos)) 
-                       delete nextInset(pos);
-}
+MathArray::MathArray(MathArray const & array, size_type from, size_type to)
+       : bf_(array.begin() + from, array.begin() + to)
+{}
 
 
-MathArray::MathArray(MathArray const & array)
-       : bf_(array.bf_)
+void MathArray::substitute(MathMacro const & m)
 {
-       for (int pos = 0; pos < size(); next(pos)) 
-               if (isInset(pos)) 
-                       replace(pos, nextInset(pos)->clone());
+       for (iterator it = begin(); it != end(); ++it)
+               it->nucleus()->substitute(m);
 }
 
 
-bool MathArray::next(int & pos) const
+MathScriptInset const * MathArray::asScript(const_iterator it) const
 {
-       if (pos >= size() - 1)
-               return false;
-
-       pos += item_size(pos);
-       return true;
+       if (it->nucleus()->asScriptInset())
+               return 0;
+       const_iterator jt = it + 1;
+       if (jt == end())
+               return 0;
+       return jt->nucleus()->asScriptInset();
 }
 
 
-bool MathArray::prev(int & pos) const
+MathAtom & MathArray::at(size_type pos)
 {
-       if (pos == 0)
-               return false;
-
-       pos -= item_size(pos - 1);
-       return true;
+       lyx::Assert(pos < size());
+       return bf_[pos];
 }
 
 
-bool MathArray::last(int & pos) const
+MathAtom const & MathArray::at(size_type pos) const
 {
-       pos = bf_.size();
-       return prev(pos);
+       lyx::Assert(pos < size());
+       return bf_[pos];
 }
 
 
-int MathArray::item_size(int pos) const
+void MathArray::insert(size_type pos, MathAtom const & t)
 {
-       return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
+       bf_.insert(begin() + pos, t);
 }
-               
 
 
-void MathArray::substitute(MathMacro const & m)
+void MathArray::insert(size_type pos, MathArray const & array)
 {
-       MathArray tmp;
-       for (int pos = 0; pos < size(); next(pos)) {
-               if (isInset(pos)) 
-                       nextInset(pos)->substitute(tmp, m);
-               else 
-                       tmp.push_back(GetChar(pos), GetCode(pos));
-       }
-       swap(tmp);
+       bf_.insert(begin() + pos, array.begin(), array.end());
 }
 
 
-MathArray & MathArray::operator=(MathArray const & array)
-{
-       MathArray tmp(array);
-       swap(tmp);
-       return *this;
+void MathArray::push_back(MathAtom const & t)
+{      
+       bf_.push_back(t);
 }
 
 
-MathInset * MathArray::nextInset(int pos) const
+void MathArray::push_back(MathArray const & array)
 {
-       if (!isInset(pos))
-               return 0;
-       MathInset * p;
-       memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
-       return p;
+       insert(size(), array);
 }
 
-MathInset * MathArray::prevInset(int pos) const
-{
-       if (!pos)
-               return 0;
-       prev(pos);
-       return nextInset(pos);
-}
 
-unsigned char MathArray::GetChar(int pos) const
+void MathArray::clear()
 {
-       return pos < size() ? bf_[pos + 1] : '\0';
+       erase();
 }
 
-string MathArray::GetString(int & pos) const
-{
-       string s;
-       if (isInset(pos))
-               return s;
-
-       MathTextCodes const fcode = GetCode(pos);
-       do {
-               s += GetChar(pos);
-               next(pos);
-       } while (pos < size() && !isInset(pos) && GetCode(pos) == fcode);
-
-       return s;
-}
 
-MathTextCodes MathArray::GetCode(int pos) const
+void MathArray::swap(MathArray & array)
 {
-       return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
+       if (this != &array) 
+               bf_.swap(array.bf_);
 }
 
-void MathArray::setCode(int pos, MathTextCodes t)
-{
-       if (pos > size() || isInset(pos))
-               return;
-       bf_[pos] = t;
-       bf_[pos + 2] = t;
-}
 
-void MathArray::insert(int pos, MathInset * p)
+bool MathArray::empty() const
 {
-       bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
-       memcpy(&bf_[pos + 1], &p, sizeof(p));
+       return bf_.empty();
 }
 
 
-void MathArray::replace(int pos, MathInset * p)
+MathArray::size_type MathArray::size() const
 {
-       memcpy(&bf_[pos + 1], &p, sizeof(p));
+       return bf_.size();
 }
 
-void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
+
+void MathArray::erase()
 {
-       bf_.insert(bf_.begin() + pos, 3, t);
-       bf_[pos + 1] = b;
+       erase(0, size());
 }
 
 
-void MathArray::insert(int pos, MathArray const & array)
+void MathArray::erase(size_type pos)
 {
-       bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
-       for (int p = pos; p < pos + array.size(); next(p)) 
-               if (isInset(p)) 
-                       replace(p, nextInset(p)->clone());
+       if (pos < size())
+               erase(pos, pos + 1);
 }
 
 
-void MathArray::push_back(MathInset * p)
-{      
-       insert(size(), p);
-}
-
-void MathArray::push_back(unsigned char b, MathTextCodes c)
+void MathArray::erase(size_type pos1, size_type pos2)
 {
-       insert(size(), b, c);
+       bf_.erase(begin() + pos1, begin() + pos2);
 }
 
-void MathArray::push_back(MathArray const & array)
+
+MathAtom & MathArray::back()
 {
-       insert(size(), array);
+       return bf_.back();
 }
 
 
-
-void MathArray::clear()
+void MathArray::dump2(ostream & os) const
 {
-       bf_.clear();
+       for (const_iterator it = begin(); it != end(); ++it)
+               os << it->nucleus() << ' ';
 }
 
 
-void MathArray::swap(MathArray & array)
+void MathArray::dump(ostream & os) const
 {
-       if (this != &array) 
-               bf_.swap(array.bf_);
+       for (const_iterator it = begin(); it != end(); ++it)
+               os << "<" << it->nucleus() << ">";
 }
 
 
-bool MathArray::empty() const
+std::ostream & operator<<(std::ostream & os, MathArray const & ar)
 {
-       return bf_.empty();
+       ar.dump2(os);
+       return os;
 }
-   
 
-int MathArray::size() const
+
+// 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)
 {
-       return bf_.size();
+       string s;
+       MathCharInset const * p = it->nucleus()->asCharInset();
+       if (!p)
+               return s;
+
+       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();
+       }
+       return s;
 }
 
 
-void MathArray::erase(int pos)
+MathArray MathArray::glueChars() const
 {
-       if (pos < static_cast<int>(bf_.size()))
-               erase(pos, pos + item_size(pos));
+       MathArray ar;
+       const_iterator it = begin();
+       while (it != end()) {
+               if (it->nucleus() && it->nucleus()->asCharInset()) {
+                       string s = charSequence(it, end());
+                       MathTextCodes c = it->nucleus()->asCharInset()->code();
+                       ar.push_back(MathAtom(new MathStringInset(s, c)));
+                       it += s.size();
+               } else {
+                       ar.push_back(*it);
+                       ++it;
+               }
+       }
+       return ar;
 }
 
 
-void MathArray::erase(int pos1, int pos2)
+void MathArray::write(MathWriteInfo & wi) const
 {
-       bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
+       glueChars().write1(wi);
 }
 
 
-bool MathArray::isInset(int pos) const
+void MathArray::write1(MathWriteInfo & wi) const
 {
-       if (pos >= size())
-               return false;
-       return MathIsInset(bf_[pos]);
+       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);
+               }
+       }
 }
 
 
-MathInset * MathArray::back_inset() const
+void MathArray::writeNormal(ostream & os) const
 {
-       if (!empty()) {
-               int pos = size();
-               prev(pos);
-               if (isInset(pos))
-                       return nextInset(pos);
+       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);
+               }
        }
-       return 0;
 }
 
 
-void MathArray::dump2(ostream & os) const
+void MathArray::validate(LaTeXFeatures & features) const
 {
-       for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
-               os << int(*it) << ' ';
-       os << endl;
+       for (const_iterator it = begin(); it != end(); ++it)
+               it->nucleus()->validate(features);
 }
 
 
-
-void MathArray::dump(ostream & os) const
-{
-       for (int pos = 0; pos < size(); next(pos)) {
-               if (isInset(pos)) 
-                       os << "<inset: " << nextInset(pos) << ">";
-               else 
-                       os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
+void MathArray::pop_back()
+{      
+       if (!size()) {
+               lyxerr << "pop_back from empty array!\n";
+               return;
        }
+       bf_.pop_back();
 }
 
 
-std::ostream & operator<<(std::ostream & os, MathArray const & ar)
+MathArray::const_iterator MathArray::begin() const
 {
-       ar.dump2(os);
-       return os;
+       return bf_.begin();
 }
 
 
-void MathArray::Write(ostream & os, bool fragile) const
+MathArray::const_iterator MathArray::end() const
 {
-       if (empty())
-               return;
-
-       int brace = 0;
-       
-       for (int pos = 0; pos < size(); next(pos)) {
-               if (isInset(pos)) {
-
-                       nextInset(pos)->Write(os, fragile);
-
-               } else {
+       return bf_.end();
+}
 
-                       MathTextCodes fcode = GetCode(pos);
-                       unsigned char c = GetChar(pos);
-
-                       if (MathIsSymbol(fcode)) {
-                               latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
-
-                               if (l == 0) {
-                                       l = lm_get_key_by_id(c, LM_TK_BIGSYM);
-                               }
-
-                               if (l) {
-                                       os << '\\' << l->name << ' ';
-                               } else {
-                                       lyxerr << "Could not find the LaTeX name for  " << c << " and fcode " << fcode << "!" << std::endl;
-                               }
-                       } else {
-                               if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM) 
-                                       os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
-
-                               // Is there a standard logical XOR?
-                               if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
-                                               (fcode == LM_TC_SPECIAL))
-                                       os << '\\';
-                               else {
-                                       if (c == '{')
-                                               ++brace;
-                                       if (c == '}')
-                                               --brace;
-                               }
-                               if (c == '}' && fcode == LM_TC_TEX && brace < 0) 
-                                       lyxerr <<"Math warning: Unexpected closing brace.\n";
-                               else           
-                                       os << c;
-                       }
-
-                       if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
-                               os << '}';
-                       
-               }
-       }
 
-       if (brace > 0)
-               os << string(brace, '}');
+MathArray::iterator MathArray::begin()
+{
+       return bf_.begin();
 }
 
 
-void MathArray::WriteNormal(ostream & os) const
+MathArray::iterator MathArray::end()
 {
-       if (empty()) {
-               os << "[par] ";
-               return;
-       }
-
-       Write(os, true);
+       return bf_.end();
 }
-