]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_cursor.C
Fix "pullArg" behaviour when pressing <Backspace> at the beginning of an inset
[lyx.git] / src / mathed / math_cursor.C
index a0f8013fdcb82d729e1f8f7125a14f1851c38c0c..c12b10223e2f66daf339418349505741d50efce9 100644 (file)
 #include <algorithm>
 #include <cctype>
 
-#include "math_inset.h"
-#include "math_arrayinset.h"
-#include "math_parser.h"
-#include "math_cursor.h"
-#include "math_macro.h"
-#include "math_macroarg.h"
-#include "math_macrotable.h"
-#include "math_root.h"
-#include "support/lstrings.h"
 #include "debug.h"
 #include "LColor.h"
 #include "Painter.h"
-#include "math_matrixinset.h"
-#include "math_grid.h"
-#include "math_spaceinset.h"
-#include "math_funcinset.h"
+#include "mathed/support.h"
+#include "formulabase.h"
+#include "math_cursor.h"
+#include "math_arrayinset.h"
 #include "math_bigopinset.h"
-#include "math_fracinset.h"
 #include "math_decorationinset.h"
-#include "math_dotsinset.h"
 #include "math_deliminset.h"
+#include "math_dotsinset.h"
+#include "math_fracinset.h"
+#include "math_funcinset.h"
+#include "math_gridinset.h"
+#include "math_macro.h"
+#include "math_macroarg.h"
+#include "math_macrotable.h"
 #include "math_macrotemplate.h"
+#include "math_matrixinset.h"
+#include "math_rootinset.h"
+#include "math_spaceinset.h"
 #include "math_sqrtinset.h"
+#include "support/lstrings.h"
 #include "math_scriptinset.h"
-#include "mathed/support.h"
-#include "formulabase.h"
-
+#include "math_parser.h"
 
 using std::endl;
 using std::min;
@@ -144,8 +142,8 @@ struct Selection
        std::vector<MathArray> data_;
 };
 
-Selection theSelection;
 
+Selection theSelection;
 
 
 bool IsMacro(short tok, int id)
@@ -161,9 +159,11 @@ bool IsMacro(short tok, int id)
               !(tok == LM_TK_SYM && id < 255);
 }
 
-ostream & operator<<(ostream & os, MathCursorPos const & p)
+
+std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
 {
-       os << "(par: " << p.par_ << " idx: " << p.idx_ << " pos: " << p.pos_ << ")";
+       os << "(par: " << p.par_ << " idx: " << p.idx_
+          << " pos: " << p.pos_ << ")";
        return os;
 }
 
@@ -206,19 +206,22 @@ MathInset * MathCursor::parInset(int i) const
        return Cursor_[i].par_;
 }
 
+
 void MathCursor::dump(char const * what) const
 {
        return;
 
-       lyxerr << "MC: " << what
-               << " cursor.pos: " << cursor().pos_
-               << " cursor.idx: " << cursor().idx_
-               << " cursor.par: " << cursor().par_
-               << " sel: " << selection
-               << " data: " << array()
-               << "\n";
+       lyxerr << "MC: " << what << "\n";
+       for (unsigned i = 0; i < Cursor_.size(); ++i)
+               lyxerr << "  i: " << i 
+                       << " pos: " << Cursor_[i].pos_
+                       << " idx: " << Cursor_[i].idx_
+                       << " par: " << Cursor_[i].par_ << "\n";
+
+       //lyxerr        << " sel: " << selection << " data: " << array() << "\n";
 }
 
+
 void MathCursor::seldump(char const *) const
 {
        //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
@@ -261,6 +264,7 @@ bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
        return true;
 }
 
+
 bool MathCursor::plainLeft()
 {
        return array().prev(cursor().pos_);
@@ -284,11 +288,11 @@ bool MathCursor::Left(bool sel)
 
        MathInset * p = prevInset();
        if (openable(p, sel, false)) {
-               array().prev(cursor().pos_);
+               plainLeft();
                push(p, false);
                return true;
        } 
-       if (array().prev(cursor().pos_))
+       if (plainLeft())
                return true;
        if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
                return true;
@@ -382,7 +386,7 @@ void MathCursor::SetPos(int x, int y)
                if (openable(n, selection, true) && n->covers(x, y))
                        push(n, true);
                else if (openable(p, selection, true) && p->covers(x, y)) {
-                       array().prev(cursor().pos_);
+                       plainLeft();
                        push(p, false);
                } else 
                        break;
@@ -494,14 +498,6 @@ void MathCursor::Delete()
                        Delete();
        }
 
-#ifdef WITH_WARNINGS
-#warning pullArg disabled
-#endif
-       //if (cursor().pos_ == 0 && Cursor_.size() >= 1) {
-       //      lyxerr << "Delete: popping...\n";
-       //      pop();
-       //}
-
        dump("Delete 2");
 }
 
@@ -555,7 +551,7 @@ bool MathCursor::Up(bool sel)
        if (p) {
                int idx, pos;
                if (p->idxLastUp(idx, pos)) {
-                       array().prev(cursor().pos_);
+                       plainLeft();
                        push(p, false);
                        cursor().par_ = p;
                        cursor().idx_ = idx;
@@ -610,7 +606,7 @@ bool MathCursor::Down(bool sel)
        if (p) {
                int idx, pos;
                if (p->idxLastDown(idx, pos)) {
-                       array().prev(cursor().pos_);
+                       plainLeft();
                        push(p, false);
                        cursor().idx_ = idx;
                        cursor().pos_ = pos;
@@ -658,7 +654,7 @@ in_word_set(s) << " \n";
                if (!p) {
                        p = new MathScriptInset(true, false);
                        insert(p);
-                       array().prev(cursor().pos_);
+                       plainLeft();
                }
                push(p, true);
                p->up(true);
@@ -671,7 +667,7 @@ in_word_set(s) << " \n";
                if (!p) {
                        p = new MathScriptInset(false, true);
                        insert(p);
-                       array().prev(cursor().pos_);
+                       plainLeft();
                }
                push(p, true);
                p->down(true);
@@ -716,12 +712,14 @@ in_word_set(s) << " \n";
                                        p = new MathBigopInset(l->name, l->id);
                                        break;
                                
-                       case LM_TK_SYM: 
-                               if (l->id < 255)
-                                       insert(l->id, MathIsBOPS(l->id) ? LM_TC_BOPS : LM_TC_SYMB);
+                       case LM_TK_SYM: {
+                               MathTextCodes code = static_cast<MathTextCodes>(l->id);
+                               if (code < 255)
+                                       insert(l->id, MathIsBOPS(code) ? LM_TC_BOPS : LM_TC_SYMB);
                                else
                                        p = new MathFuncInset(l->name);
                                break;
+                       }
 
                        case LM_TK_STACK:
                                p = new MathFracInset("stackrel");
@@ -767,7 +765,7 @@ in_word_set(s) << " \n";
                        SelCut();
                insert(p);
                if (p->nargs()) {
-                       array().prev(cursor().pos_);
+                       plainLeft();
                        push(p, true);
                        if (oldsel) 
                                SelPaste();
@@ -824,6 +822,7 @@ void MathCursor::SelCopy()
        }
 }
 
+
 void MathCursor::SelCut()
 {
        seldump("SelCut");
@@ -852,6 +851,7 @@ void MathCursor::SelPaste()
        SelClear();
 }
 
+
 void MathCursor::SelHandle(bool sel)
 {
        if (sel && !selection)
@@ -878,7 +878,6 @@ void MathCursor::SelClear()
 }
 
 
-
 void MathCursor::drawSelection(Painter & pain) const
 {
        if (!selection)
@@ -969,6 +968,7 @@ void MathCursor::handleAccent(string const & name, int code)
        push(p, true);
 }
 
+
 void MathCursor::handleDelim(int l, int r)
 {
        MathDelimInset * p = new MathDelimInset(l, r);
@@ -977,6 +977,7 @@ void MathCursor::handleDelim(int l, int r)
                p->cell(0) = theSelection.glue();
        }
        insert(p);
+       plainLeft();
        push(p, true);
 }
 
@@ -1060,15 +1061,16 @@ MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
        return 0;
 }
 
+
 void MathCursor::pullArg()
 {
        // pullArg
+       dump("pullarg");
        MathArray a = array();
-       if (!Left())
-               return;
-       normalize();
-       array().erase(cursor().pos_);
-       array().insert(cursor().pos_, a);
+       if (pop()) {
+               array().erase(cursor().pos_);
+               array().insert(cursor().pos_, a);
+       }
 }
 
 
@@ -1127,6 +1129,7 @@ string MathCursorPos::readString()
 }
 */
 
+
 MathInset * MathCursor::prevInset() const
 {
        normalize();
@@ -1198,21 +1201,25 @@ int MathCursor::xpos() const
        return xarray().pos2x(cursor().pos_);
 }
 
+
 void MathCursor::gotoX(int x)
 {
        cursor().pos_ = xarray().x2pos(x);      
 }
 
+
 void MathCursor::idxNext()
 {
        cursor().par_->idxNext(cursor().idx_, cursor().pos_);
 }
 
+
 void MathCursor::idxPrev()
 {
        cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
 }
 
+
 void MathCursor::splitCell()
 {
        if (cursor().idx_ == cursor().par_->nargs() - 1) 
@@ -1225,6 +1232,7 @@ void MathCursor::splitCell()
        array().insert(0, ar);
 }
 
+
 void MathCursor::breakLine()
 {
        MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
@@ -1251,6 +1259,7 @@ void MathCursor::breakLine()
        }
 }
 
+
 char MathCursor::valign() const
 {
        int idx;
@@ -1259,6 +1268,7 @@ char MathCursor::valign() const
        return p ? p->valign() : 0;
 }
 
+
 char MathCursor::halign() const
 {
        int idx;
@@ -1311,20 +1321,21 @@ MathCursorPos const & MathCursor::cursor() const
 ////////////////////////////////////////////////////////////////////////
 
 
-bool MathCursorPos::operator==(const MathCursorPos & it) const
+bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
 {
-       return par_ == it.par_ && idx_ == it.idx_ && pos_ == it.pos_;
+       return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
 }
 
-bool MathCursorPos::operator<(const MathCursorPos & it) const
+
+bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
 {
-       if (par_ != it.par_) {
+       if (ti.par_ != it.par_) {
                lyxerr << "can't compare cursor and anchor in different insets\n";
                return true;
        }
-       if (idx_ != it.idx_)
-               return idx_ < it.idx_;
-       return pos_ < it.pos_;
+       if (ti.idx_ != it.idx_)
+               return ti.idx_ < it.idx_;
+       return ti.pos_ < it.pos_;
 }
 
 
@@ -1344,16 +1355,18 @@ MathXArray & MathCursorPos::xcell(int idx) const
        return par_->xcell(idx);
 }
 
+
 MathXArray & MathCursorPos::xcell() const
 {
        return par_->xcell(idx_);
 }
 
+
 MathCursorPos MathCursor::normalAnchor() const
 {
        // use Anchor on the same level as Cursor
        MathCursorPos normal = Anchor_[Cursor_.size() - 1];
-       if (Cursor_.size() < Anchor_.size() && !(cursor() > normal)) {
+       if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
                // anchor is behind cursor -> move anchor behind the inset
                normal.cell().next(normal.pos_);
        }
@@ -1362,21 +1375,25 @@ MathCursorPos MathCursor::normalAnchor() const
        return normal;
 }
 
+
 bool MathCursorPos::idxUp()
 {
        return par_->idxUp(idx_, pos_);
 }
 
+
 bool MathCursorPos::idxDown()
 {
        return par_->idxDown(idx_, pos_);
 }
 
+
 bool MathCursorPos::idxLeft()
 {
        return par_->idxLeft(idx_, pos_);
 }
 
+
 bool MathCursorPos::idxRight()
 {
        return par_->idxRight(idx_, pos_);