]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/array.C
fix pullArg when pressing <Delete> at the end of an cell
[lyx.git] / src / mathed / array.C
index acbecb1230a32d76e02017ea4e62c868df3a90bc..a15ec82dedc3a38db7b14c4cb9c65aeb5c1070e8 100644 (file)
@@ -1,19 +1,17 @@
 
-#include <config.h>
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
+#include "math_inset.h"
 #include "debug.h"
 #include "array.h"
-#include "math_inset.h"
 #include "math_scriptinset.h"
 #include "math_parser.h"
 #include "mathed/support.h"
 
-using namespace std;
-
+using std::ostream;
+using std::endl;
 
 MathArray::MathArray()
 {}
@@ -22,8 +20,8 @@ MathArray::MathArray()
 MathArray::~MathArray()
 {
        for (int pos = 0; pos < size(); next(pos)) 
-               if (MathIsInset(pos)) 
-                       delete GetInset(pos);
+               if (isInset(pos)) 
+                       delete nextInset(pos);
 }
 
 
@@ -32,7 +30,15 @@ MathArray::MathArray(MathArray const & array)
 {
        for (int pos = 0; pos < size(); next(pos)) 
                if (isInset(pos)) 
-                       replace(pos, GetInset(pos)->Clone());
+                       replace(pos, nextInset(pos)->clone());
+}
+
+MathArray::MathArray(MathArray const & array, int from, int to)
+       : bf_(array.bf_.begin() + from, array.bf_.begin() + to)
+{
+       for (int pos = 0; pos < size(); next(pos)) 
+               if (isInset(pos)) 
+                       replace(pos, nextInset(pos)->clone());
 }
 
 
@@ -75,7 +81,7 @@ void MathArray::substitute(MathMacro const & m)
        MathArray tmp;
        for (int pos = 0; pos < size(); next(pos)) {
                if (isInset(pos)) 
-                       GetInset(pos)->substitute(tmp, m);
+                       nextInset(pos)->substitute(tmp, m);
                else 
                        tmp.push_back(GetChar(pos), GetCode(pos));
        }
@@ -91,25 +97,56 @@ MathArray & MathArray::operator=(MathArray const & array)
 }
 
 
-MathInset * MathArray::GetInset(int pos) const
+MathInset * MathArray::nextInset(int pos) const
 {
        if (!isInset(pos))
                return 0;
        MathInset * p;
-       memcpy(&p, bf_.begin() + pos + 1, sizeof(p));
+       memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
        return p;
 }
 
-char MathArray::GetChar(int pos) const
+MathInset * MathArray::prevInset(int pos) const
+{
+       if (!pos)
+               return 0;
+       prev(pos);
+       return nextInset(pos);
+}
+
+unsigned char MathArray::GetChar(int pos) const
 {
        return pos < size() ? bf_[pos + 1] : '\0';
 }
 
+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
 {
        return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
 }
 
+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)
 {
        bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
@@ -122,7 +159,7 @@ void MathArray::replace(int pos, MathInset * p)
        memcpy(&bf_[pos + 1], &p, sizeof(p));
 }
 
-void MathArray::insert(int pos, char b, MathTextCodes t)
+void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
 {
        bf_.insert(bf_.begin() + pos, 3, t);
        bf_[pos + 1] = b;
@@ -131,11 +168,10 @@ void MathArray::insert(int pos, char b, MathTextCodes t)
 
 void MathArray::insert(int pos, MathArray const & array)
 {
-#ifdef WITH_WARNINGS
-#warning quick and really dirty: make sure that we really own our insets
-#endif
-       MathArray a = array;
-       bf_.insert(bf_.begin() + pos, a.bf_.begin(), a.bf_.end());
+       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());
 }
 
 
@@ -144,7 +180,7 @@ void MathArray::push_back(MathInset * p)
        insert(size(), p);
 }
 
-void MathArray::push_back(char b, MathTextCodes c)
+void MathArray::push_back(unsigned char b, MathTextCodes c)
 {
        insert(size(), b, c);
 }
@@ -181,9 +217,15 @@ int MathArray::size() const
 }
 
 
+void MathArray::erase()
+{
+       erase(0, size());
+}
+
+
 void MathArray::erase(int pos)
 {
-       if (pos < bf_.size())
+       if (pos < static_cast<int>(bf_.size()))
                erase(pos, pos + item_size(pos));
 }
 
@@ -198,7 +240,7 @@ bool MathArray::isInset(int pos) const
 {
        if (pos >= size())
                return false;
-       return MathIsInset(bf_[pos]);
+       return MathIsInset(static_cast<MathTextCodes>(bf_[pos]));
 }
 
 
@@ -208,26 +250,12 @@ MathInset * MathArray::back_inset() const
                int pos = size();
                prev(pos);
                if (isInset(pos))
-                       return GetInset(pos);
+                       return nextInset(pos);
        }
        return 0;
 }
 
 
-MathScriptInset * MathArray::prevScriptInset(int pos) const
-{
-       if (!pos)
-               return 0;
-       prev(pos);
-
-       MathInset * inset = GetInset(pos);
-       if (inset && inset->GetType() == LM_OT_SCRIPT) 
-               return static_cast<MathScriptInset *>(inset);
-
-       return 0;
-}
-
-
 void MathArray::dump2(ostream & os) const
 {
        for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
@@ -241,7 +269,7 @@ void MathArray::dump(ostream & os) const
 {
        for (int pos = 0; pos < size(); next(pos)) {
                if (isInset(pos)) 
-                       os << "<inset: " << GetInset(pos) << ">";
+                       os << "<inset: " << nextInset(pos) << ">";
                else 
                        os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
        }
@@ -261,24 +289,30 @@ void MathArray::Write(ostream & os, bool fragile) const
                return;
 
        int brace = 0;
-       latexkeys const * l;
        
        for (int pos = 0; pos < size(); next(pos)) {
                if (isInset(pos)) {
 
-                       GetInset(pos)->Write(os, fragile);
+                       nextInset(pos)->Write(os, fragile);
 
                } else {
 
-                       char fcode = GetCode(pos);
-                       char c     = GetChar(pos);
+                       MathTextCodes fcode = GetCode(pos);
+                       unsigned char c = GetChar(pos);
 
                        if (MathIsSymbol(fcode)) {
-                               l = lm_get_key_by_id(c, fcode == LM_TC_BSYM ? LM_TK_BIGSYM : LM_TK_SYM);
-                               if (l)
-                                               os << '\\' << l->name << ' ';
+                               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] << '{';
 
@@ -295,7 +329,7 @@ void MathArray::Write(ostream & os, bool fragile) const
                                if (c == '}' && fcode == LM_TC_TEX && brace < 0) 
                                        lyxerr <<"Math warning: Unexpected closing brace.\n";
                                else           
-                                       os << char(c);
+                                       os << c;
                        }
 
                        if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
@@ -319,3 +353,11 @@ void MathArray::WriteNormal(ostream & os) const
        Write(os, true);
 }
 
+
+void MathArray::Validate(LaTeXFeatures & features) const
+{
+       for (int pos = 0; pos < size(); next(pos)) 
+               if (isInset(pos)) 
+                       nextInset(pos)->Validate(features);
+}
+