X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Fmath_scriptinset.C;h=11f858c31a6b16a329471a8eeeaff9087edbc80c;hb=57501b93064a6deed43e415beed45606054d86ad;hp=a2c397ad537f7cbaf958d1d943490309313ed166;hpb=68b7ef25fb7d2da230d3eb11d6737701abfc2e64;p=lyx.git diff --git a/src/mathed/math_scriptinset.C b/src/mathed/math_scriptinset.C index a2c397ad53..11f858c31a 100644 --- a/src/mathed/math_scriptinset.C +++ b/src/mathed/math_scriptinset.C @@ -42,6 +42,7 @@ MathScriptInset::MathScriptInset(bool up) MathScriptInset::MathScriptInset(MathAtom const & at, bool up) : MathNestInset(2), cell_1_is_up_(up), limits_(0) { + BOOST_ASSERT(nargs() >= 1); cell(0).push_back(at); } @@ -83,24 +84,32 @@ bool MathScriptInset::idxLast(LCursor & cur) const MathArray const & MathScriptInset::down() const { - return nargs() == 2 ? cell(2) : cell(1); + if (nargs() == 3) + return cell(2); + BOOST_ASSERT(nargs() > 1); + return cell(1); } MathArray & MathScriptInset::down() { - return nargs() == 2 ? cell(2) : cell(1); + if (nargs() == 3) + return cell(2); + BOOST_ASSERT(nargs() > 1); + return cell(1); } MathArray const & MathScriptInset::up() const { + BOOST_ASSERT(nargs() > 1); return cell(1); } MathArray & MathScriptInset::up() { + BOOST_ASSERT(nargs() > 1); return cell(1); } @@ -296,9 +305,12 @@ bool MathScriptInset::hasLimits() const void MathScriptInset::removeScript(bool up) { + lyxerr << "MathNestInset::removeScript: 1 up: " << up << endl; if (nargs() == 2) { + lyxerr << "MathNestInset::removeScript: a up: " << up << endl; if (up == cell_1_is_up_) cells_.pop_back(); + lyxerr << "MathNestInset::removeScript: b up: " << up << endl; } else if (nargs() == 3) { if (up == true) { swap(cells_[1], cells_[2]); @@ -306,7 +318,9 @@ void MathScriptInset::removeScript(bool up) } else { cell_1_is_up_ = true; } + cells_.pop_back(); } + lyxerr << "MathNestInset::removeScript: 2 up: " << up << endl; } @@ -318,16 +332,16 @@ bool MathScriptInset::has(bool up) const bool MathScriptInset::hasUp() const { - //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl; //lyxerr << "1up: " << bool(cell_1_is_up_) << endl; + //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl; return idxOfScript(true); } bool MathScriptInset::hasDown() const { - //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl; //lyxerr << "1up: " << bool(cell_1_is_up_) << endl; + //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl; return idxOfScript(false); } @@ -337,7 +351,7 @@ InsetBase::idx_type MathScriptInset::idxOfScript(bool up) const if (nargs() == 1) return 0; if (nargs() == 2) - return cell_1_is_up_ == up ? 1 : 0; + return (cell_1_is_up_ == up) ? 1 : 0; if (nargs() == 3) return up ? 1 : 2; BOOST_ASSERT(false); @@ -376,7 +390,7 @@ bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const } // Are we 'up'? - if (cur.idx() == idxOfScript(true)) { + if (has(up) && cur.idx() == idxOfScript(true)) { // can't go further up if (up) return false; @@ -387,7 +401,7 @@ bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const } // Are we 'down'? - if (cur.idx() == idxOfScript(false)) { + if (has(up) && cur.idx() == idxOfScript(false)) { // can't go further down if (!up) return false; @@ -539,21 +553,23 @@ void MathScriptInset::infoize2(std::ostream & os) const } -void MathScriptInset::notifyCursorLeaves(idx_type idx) +void MathScriptInset::notifyCursorLeaves(LCursor & cur) { - MathNestInset::notifyCursorLeaves(idx); + MathNestInset::notifyCursorLeaves(cur); // remove empty scripts if possible - if (idx == 2 && cell(2).empty()) { - removeScript(false); // must be a subscript... - } else if (idx == 1 && cell(1).empty()) { - if (nargs() == 2) { - cell_1_is_up_ = false; - cell(1) = cell(2); - cells_.pop_back(); - } else if (nargs() == 1) { - cells_.pop_back(); - } + if (nargs() > 2 && cur.idx() == 2 && cell(2).empty()) { + // must be a subscript... + removeScript(false); + // sanitize cursor, even if this slice will be removed immediately + cur.idx() = 0; + cur.pos() = 0; + } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) { + // could be either subscript or super script + removeScript(cell_1_is_up_); + // sanitize cursor, even if this slice will be removed immediately + cur.idx() = 0; + cur.pos() = 0; } }