]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_cursor.C
fix for the first item on Martin's list
[lyx.git] / src / mathed / math_cursor.C
index d2381c8fbd55209dc96810d226e7606ed1a71cbc..e62ac1920d6f343362eaed166eae004ff5ad6eff 100644 (file)
@@ -38,6 +38,7 @@
 #include "math_hullinset.h"
 #include "math_iterator.h"
 #include "math_macroarg.h"
+#include "math_macrotemplate.h"
 #include "math_mathmlstream.h"
 #include "math_parser.h"
 #include "math_replace.h"
@@ -56,6 +57,8 @@ using std::min;
 using std::max;
 using std::swap;
 using std::isalnum;
+using std::vector;
+using std::ostringstream;
 
 namespace {
 
@@ -76,11 +79,11 @@ struct Selection
                c1 = p->col(i1.idx_);
                c2 = p->col(i2.idx_);
                if (c1 > c2)
-                       std::swap(c1, c2);
+                       swap(c1, c2);
                r1 = p->row(i1.idx_);
                r2 = p->row(i2.idx_);
                if (r1 > r2)
-                       std::swap(r1, r2);
+                       swap(r1, r2);
        }
 
        void grab(MathCursor const & cursor)
@@ -200,11 +203,9 @@ void MathCursor::pushRight(MathAtom & t)
 
 bool MathCursor::popLeft()
 {
-       //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " left\n";
+       //cerr << "Leaving atom to the left\n";
        if (Cursor_.size() <= 1)
                return false;
-       if (par()->asScriptInset())
-               par()->asScriptInset()->removeEmptyScripts();
        Cursor_.pop_back();
        return true;
 }
@@ -215,8 +216,6 @@ bool MathCursor::popRight()
        //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
        if (Cursor_.size() <= 1)
                return false;
-       if (par()->asScriptInset())
-               par()->asScriptInset()->removeEmptyScripts();
        Cursor_.pop_back();
        posRight();
        return true;
@@ -422,6 +421,20 @@ void MathCursor::plainErase()
 }
 
 
+void MathCursor::markInsert()
+{
+       //lyxerr << "inserting mark\n";
+       array().insert(pos(), MathAtom(new MathCharInset(0, lastcode_)));
+}
+
+
+void MathCursor::markErase()
+{
+       //lyxerr << "deleting mark\n";
+       array().erase(pos());
+}
+
+
 void MathCursor::plainInsert(MathAtom const & t)
 {
        array().insert(pos(), t);
@@ -436,6 +449,12 @@ void MathCursor::insert(char c, MathTextCodes t)
 }
 
 
+void MathCursor::insert(char c)
+{
+       insert(c, lastcode_);
+}
+
+
 void MathCursor::insert(MathAtom const & t)
 {
        macroModeClose();
@@ -559,8 +578,8 @@ void MathCursor::delLine()
                par()->asGridInset()->delRow(hullRow());
        }
 
-       if (idx() > par()->nargs())
-               idx() = par()->nargs();
+       if (idx() >= par()->nargs())
+               idx() = par()->nargs() - 1;
 
        if (pos() > size())
                pos() = size();
@@ -633,7 +652,7 @@ string MathCursor::macroName() const
 {
        string s;
        MathInset::difference_type i = macroNamePos();
-       for ( ; i >= 0 && i < int(pos()); ++i)
+       for (; i >= 0 && i < int(pos()); ++i)
                s += array().at(i)->getChar();
        return s;
 }
@@ -740,7 +759,7 @@ void MathCursor::drawSelection(Painter & pain) const
                int y2 = c.yo() + c.descent();
                pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
        } else {
-               std::vector<MathInset::idx_type> indices
+               vector<MathInset::idx_type> indices
                        = i1.par_->idxBetween(i1.idx_, i2.idx_);
                for (unsigned i = 0; i < indices.size(); ++i) {
                        MathXArray & c = i1.xcell(indices[i]);
@@ -900,20 +919,33 @@ void MathCursor::pullArg(bool goright)
                array().insert(pos(), a);
                if (goright)
                        pos() += a.size();
+       } else {
+               formula()->mutateToText();
        }
 }
 
 
+void MathCursor::touch()
+{
+       cursor_type::const_iterator it = Cursor_.begin();
+       cursor_type::const_iterator et = Cursor_.end();
+       for ( ; it != et; ++it)
+               it->xcell().touch();
+}
+
+
 void MathCursor::normalize()
 {
+#if 0
        // rebreak
        {
                MathIterator it = ibegin(formula()->par().nucleus());
                MathIterator et = iend(formula()->par().nucleus());
-               for ( ; it != et; ++it)
+               for (; it != et; ++it)
                        if (it.par()->asBoxInset())
                                it.par()->asBoxInset()->rebreak();
        }
+#endif
 
        if (idx() >= par()->nargs()) {
                lyxerr << "this should not really happen - 1: "
@@ -932,6 +964,19 @@ void MathCursor::normalize()
                dump("error 4");
        }
        pos() = min(pos(), size());
+
+       // remove empty scripts if possible
+       for (pos_type i = 0; i < size(); ++i) {
+               MathScriptInset * p = array().at(i)->asScriptInset();
+               if (p) {
+                       p->removeEmptyScripts();
+                       if (p->empty())
+                               array().erase(i);
+               }
+       }
+
+       // fix again position
+       pos() = min(pos(), size());
 }
 
 
@@ -1180,22 +1225,28 @@ bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhig
 
        MathIterator it = ibegin(formula()->par().nucleus());
        MathIterator et = iend(formula()->par().nucleus());
-       for ( ; it != et; ++it) {
-               // avoid invalid nesting hen selecting
-               if (selection_ && !positionable(it.cursor(), Anchor_))
-                       continue;
-               int xo = it.position().xpos();
-               int yo = it.position().ypos();
-               if (xlow - 2 <= xo && xo <= xhigh + 2 &&
-                   ylow - 2 <= yo && yo <= yhigh + 2)
-               {
-                       double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
-                       if (d < best_dist) {
-                               best_dist   = d;
-                               best_cursor = it.cursor();
+       while (1) {
+               // avoid invalid nesting when selecting
+               if (!selection_ || positionable(it.cursor(), Anchor_)) {
+                       MathCursorPos const & top = it.position();
+                       int xo = top.xpos();
+                       int yo = top.ypos();
+                       if (xlow - 2 <= xo && xo <= xhigh + 2 &&
+                                       ylow - 2 <= yo && yo <= yhigh + 2)
+                       {
+                               double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
+                               if (d < best_dist) {
+                                       best_dist   = d;
+                                       best_cursor = it.cursor();
+                               }
                        }
                }
+
+               if (it == et)
+                       break;
+               ++it;
        }
+
        if (best_dist < 1e10)
                Cursor_ = best_cursor;
        return best_dist < 1e10;
@@ -1231,7 +1282,7 @@ bool MathCursor::interpret(string const & s)
                unsigned int n = 1;
                istringstream is(s.substr(5).c_str());
                is >> n;
-               n = std::max(1u, n);
+               n = max(1u, n);
                niceInsert(MathAtom(new MathCasesInset(n)));
                return true;
        }
@@ -1243,10 +1294,10 @@ bool MathCursor::interpret(string const & s)
                string h_align;
                istringstream is(s.substr(6).c_str());
                is >> m >> n >> v_align >> h_align;
-               m = std::max(1u, m);
-               n = std::max(1u, n);
+               m = max(1u, m);
+               n = max(1u, n);
                v_align += 'c';
-               niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
+               niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
                return true;
        }
 
@@ -1279,6 +1330,14 @@ bool MathCursor::interpret(string const & s)
                return true;
        }
 
+       // prevent entering of recursive macros
+       if (formula()->lyxCode() == Inset::MATHMACRO_CODE 
+               && formula()->getInsetName() == s.substr(1))
+       {
+               lyxerr << "can't enter recursive macro\n";
+               return true;
+       }
+
        niceInsert(createMathInset(s.substr(1)));
        return true;
 }
@@ -1316,7 +1375,9 @@ bool MathCursor::interpret(char c)
        if (inMacroArgMode()) {
                --pos();
                plainErase();
-               if ('1' <= c && c <= '9')
+               int n = c - '0';
+               MathMacroTemplate * p = formula()->par()->asMacroTemplate();
+               if (p && 1 <= n && n <= p->numargs())
                        insert(MathAtom(new MathMacroArgument(c - '0')));
                else {
                        insert(MathAtom(new MathSpecialCharInset('#')));
@@ -1347,6 +1408,12 @@ bool MathCursor::interpret(char c)
                }
 
                macroModeClose();
+
+               if (c == '\\')
+                       insert(c, LM_TC_TEX);
+               else if (c != ' ')
+                       insert(c, lastcode_);
+
                return true;
        }
 
@@ -1474,7 +1541,7 @@ void MathCursor::setSelection(cursor_type const & where, size_type n)
 
 string MathCursor::info() const
 {
-       std::ostringstream os;
+       ostringstream os;
        if (pos() > 0)
                prevAtom()->infoize(os);
        return os.str();