]> 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 6c7b9a5c42ce3dc97afc542611603728c0afc7c1..a15ec82dedc3a38db7b14c4cb9c65aeb5c1070e8 100644 (file)
 
-#include <config.h>
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
+#include "math_inset.h"
+#include "debug.h"
 #include "array.h"
-#include "math_defs.h"
+#include "math_scriptinset.h"
+#include "math_parser.h"
+#include "mathed/support.h"
+
+using std::ostream;
+using std::endl;
 
+MathArray::MathArray()
+{}
 
-// Is this still needed? (Lgb)
-static inline
-void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
+
+MathArray::~MathArray()
 {
-       char * ps = static_cast<char *>(ps_in);
-       char const * pt = static_cast<char const *>(pt_in);
-       while (n--) *ps++ = *pt++;
-       return ps_in;
+       for (int pos = 0; pos < size(); next(pos)) 
+               if (isInset(pos)) 
+                       delete nextInset(pos);
 }
 
 
-int MathedArray::empty() const
+MathArray::MathArray(MathArray const & array)
+       : bf_(array.bf_)
 {
-       return (last_ == 0);
+       for (int pos = 0; pos < size(); next(pos)) 
+               if (isInset(pos)) 
+                       replace(pos, nextInset(pos)->clone());
 }
-   
 
-int MathedArray::last() const
+MathArray::MathArray(MathArray const & array, int from, int to)
+       : bf_(array.bf_.begin() + from, array.bf_.begin() + to)
 {
-       return last_;
+       for (int pos = 0; pos < size(); next(pos)) 
+               if (isInset(pos)) 
+                       replace(pos, nextInset(pos)->clone());
 }
 
 
-void MathedArray::last(int l)
+bool MathArray::next(int & pos) const
 {
-       last_ = l;
+       if (pos >= size() - 1)
+               return false;
+
+       pos += item_size(pos);
+       return true;
 }
 
 
-int MathedArray::maxsize() const
+bool MathArray::prev(int & pos) const
 {
-       return maxsize_;
+       if (pos == 0)
+               return false;
+
+       pos -= item_size(pos - 1);
+       return true;
 }
 
 
-void MathedArray::resize(int newsize)
+bool MathArray::last(int & pos) const
 {
-       if (newsize < ARRAY_MIN_SIZE)
-               newsize = ARRAY_MIN_SIZE;
-       newsize += ARRAY_STEP - (newsize % ARRAY_STEP);
-       bf_.resize(newsize);
-       if (last_ >= newsize) last_ = newsize - 1;
-       maxsize_ = newsize;
-       bf_[last_] = 0;
+       pos = bf_.size();
+       return prev(pos);
 }
 
 
-MathedArray::MathedArray(int size) 
+int MathArray::item_size(int pos) const
 {
-       maxsize_ = (size < ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE : size;
-       bf_.resize(maxsize_);
-       last_ = 0;
+       return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
 }
+               
 
 
-void MathedArray::move(int p, int shift)
+void MathArray::substitute(MathMacro const & m)
 {
-       if (p <= last_) {
-               if (last_ + shift >= maxsize_) { 
-                   resize(last_ + shift);
-               }
-               memmove(&bf_[p + shift], &bf_[p], last_ - p);
-               last_ += shift;
-               bf_[last_] = 0;
+       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);
 }
 
 
-void MathedArray::mergeF(MathedArray * a, int p, int dx)
+MathArray & MathArray::operator=(MathArray const & array)
 {
-       my_memcpy(&bf_[p], &a->bf_[0], dx);
+       MathArray tmp(array);
+       swap(tmp);
+       return *this;
 }
 
 
-void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
+MathInset * MathArray::nextInset(int pos) const
 {
-       my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
+       if (!isInset(pos))
+               return 0;
+       MathInset * p;
+       memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
+       return p;
 }
 
+MathInset * MathArray::prevInset(int pos) const
+{
+       if (!pos)
+               return 0;
+       prev(pos);
+       return nextInset(pos);
+}
 
-void MathedArray::raw_pointer_insert(void * p, int pos, int len)
+unsigned char MathArray::GetChar(int pos) const
 {
-       my_memcpy(&bf_[pos], &p, len);
+       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;
+}
 
-void MathedArray::strange_copy(MathedArray * dest, int dpos,
-                               int spos, int len)
+MathTextCodes MathArray::GetCode(int pos) const
 {
-       my_memcpy(&dest[dpos], &bf_[spos], len);
+       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);
+       memcpy(&bf_[pos + 1], &p, sizeof(p));
 }
 
 
-byte MathedArray::operator[](int i) const
+void MathArray::replace(int pos, MathInset * p)
 {
-       return bf_[i];
+       memcpy(&bf_[pos + 1], &p, sizeof(p));
+}
+
+void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
+{
+       bf_.insert(bf_.begin() + pos, 3, t);
+       bf_[pos + 1] = b;
 }
 
 
-byte & MathedArray::operator[](int i)
+void MathArray::insert(int pos, MathArray const & array)
 {
-       return bf_[i];
+       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());
 }
 
 
-void MathedArray::insert(int pos, byte c)
+void MathArray::push_back(MathInset * p)
+{      
+       insert(size(), p);
+}
+
+void MathArray::push_back(unsigned char b, MathTextCodes c)
 {
-       if (pos < 0) pos = last_;
-       if (pos >= maxsize_) 
-               resize(maxsize_ + ARRAY_STEP);
-       bf_[pos] = c;
-       if (pos >= last_)
-               last_ = pos + 1;
+       insert(size(), b, c);
 }
+
+void MathArray::push_back(MathArray const & array)
+{
+       insert(size(), array);
+}
+
+
+
+void MathArray::clear()
+{
+       bf_.clear();
+}
+
+
+void MathArray::swap(MathArray & array)
+{
+       if (this != &array) 
+               bf_.swap(array.bf_);
+}
+
+
+bool MathArray::empty() const
+{
+       return bf_.empty();
+}
+   
+
+int MathArray::size() const
+{
+       return bf_.size();
+}
+
+
+void MathArray::erase()
+{
+       erase(0, size());
+}
+
+
+void MathArray::erase(int pos)
+{
+       if (pos < static_cast<int>(bf_.size()))
+               erase(pos, pos + item_size(pos));
+}
+
+
+void MathArray::erase(int pos1, int pos2)
+{
+       bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
+}
+
+
+bool MathArray::isInset(int pos) const
+{
+       if (pos >= size())
+               return false;
+       return MathIsInset(static_cast<MathTextCodes>(bf_[pos]));
+}
+
+
+MathInset * MathArray::back_inset() const
+{
+       if (!empty()) {
+               int pos = size();
+               prev(pos);
+               if (isInset(pos))
+                       return nextInset(pos);
+       }
+       return 0;
+}
+
+
+void MathArray::dump2(ostream & os) const
+{
+       for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
+               os << int(*it) << ' ';
+       os << endl;
+}
+
+
+
+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]) << ">";
+       }
+}
+
+
+std::ostream & operator<<(std::ostream & os, MathArray const & ar)
+{
+       ar.dump2(os);
+       return os;
+}
+
+
+void MathArray::Write(ostream & os, bool fragile) const
+{
+       if (empty())
+               return;
+
+       int brace = 0;
+       
+       for (int pos = 0; pos < size(); next(pos)) {
+               if (isInset(pos)) {
+
+                       nextInset(pos)->Write(os, fragile);
+
+               } else {
+
+                       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, '}');
+}
+
+
+void MathArray::WriteNormal(ostream & os) const
+{
+       if (empty()) {
+               os << "[par] ";
+               return;
+       }
+
+       Write(os, true);
+}
+
+
+void MathArray::Validate(LaTeXFeatures & features) const
+{
+       for (int pos = 0; pos < size(); next(pos)) 
+               if (isInset(pos)) 
+                       nextInset(pos)->Validate(features);
+}
+