diff --git a/src/cursor.C b/src/cursor.C index 919c68375a..47b1d22152 100644 --- a/src/cursor.C +++ b/src/cursor.C @@ -26,6 +26,8 @@ #include "insets/insettabular.h" #include "insets/insettext.h" +#include "mathed/math_data.h" + #include using std::vector; @@ -227,3 +229,49 @@ BufferView & LCursor::bv() const { return *bv_; } + + +MathAtom const & LCursor::prevAtom() const +{ + BOOST_ASSERT(pos() > 0); + return cell()[pos() - 1]; +} + + +MathAtom & LCursor::prevAtom() +{ + BOOST_ASSERT(pos() > 0); + return cell()[pos() - 1]; +} + + +MathAtom const & LCursor::nextAtom() const +{ + BOOST_ASSERT(pos() < lastpos()); + return cell()[pos()]; +} + + +MathAtom & LCursor::nextAtom() +{ + BOOST_ASSERT(pos() < lastpos()); + return cell()[pos()]; +} + + +bool LCursor::posLeft() +{ + if (pos() == 0) + return false; + --pos(); + return true; +} + + +bool LCursor::posRight() +{ + if (pos() == lastpos()) + return false; + ++pos(); + return true; +} diff --git a/src/cursor.h b/src/cursor.h index 9fa55a711b..35f858f09c 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -19,6 +19,7 @@ class BufferView; class UpdatableInset; +class MathAtom; class DispatchResult; class FuncRequest; class LyXText; @@ -68,10 +69,6 @@ public: idx_type idx() const { return top().idx(); } /// return the text-ed cell this cursor is in idx_type & idx() { return top().idx(); } - /// return the mathed cell this cursor is in - MathArray const & cell() const { return top().cell(); } - /// return the mathed cell this cursor is in - MathArray & cell() { return top().cell(); } /// return the paragraph this cursor is in par_type par() const { return top().par(); } /// return the paragraph this cursor is in @@ -93,6 +90,24 @@ public: /// return the grid row of the current cell col_type col() const { return top().col(); } + // + // math-specific part + // + /// return the mathed cell this cursor is in + MathArray const & cell() const { return top().cell(); } + /// return the mathed cell this cursor is in + MathArray & cell() { return top().cell(); } + /// the mathatom left of the cursor + MathAtom const & prevAtom() const; + /// the mathatom left of the cursor + MathAtom & prevAtom(); + /// the mathatom right of the cursor + MathAtom const & nextAtom() const; + /// the mathatom right of the cursor + MathAtom & nextAtom(); + + // + // text-specific part /// UpdatableInset * innerInset() const; /// @@ -105,6 +120,15 @@ public: void getPos(int & x, int & y) const; /// returns cursor dimension void getDim(int & asc, int & desc) const; + + // + // common part + // + /// move one step to the left + bool posLeft(); + /// move one step to the right + bool posRight(); + /// cache the absolute coordinate from the top inset void updatePos(); /// sets anchor to cursor position diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 438df2d365..da3f51fa1f 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -85,7 +85,7 @@ void MathCursor::pushLeft(LCursor & cur, MathAtom & t) void MathCursor::pushRight(LCursor & cur, MathAtom & t) { //lyxerr << "Entering atom " << t << " right" << endl; - posLeft(cur); + cur.posLeft(); push(cur, t); t->idxLast(cur); } @@ -115,7 +115,7 @@ bool MathCursor::popRight(LCursor & cur) } cur.inset()->asMathInset()->notifyCursorLeaves(cur.idx()); cur.pop(); - posRight(cur); + cur.posRight(); return true; } @@ -182,24 +182,6 @@ bool MathCursor::inNucleus(LCursor & cur) const } -bool MathCursor::posLeft(LCursor & cur) -{ - if (cur.pos() == 0) - return false; - --cur.pos(); - return true; -} - - -bool MathCursor::posRight(LCursor & cur) -{ - if (cur.pos() == cur.lastpos()) - return false; - ++cur.pos(); - return true; -} - - bool MathCursor::left(LCursor & cur, bool sel) { dump("Left 1"); @@ -211,12 +193,12 @@ bool MathCursor::left(LCursor & cur, bool sel) } selHandle(cur, sel); - if (hasPrevAtom(cur) && openable(prevAtom(cur), sel)) { - pushRight(cur, prevAtom(cur)); + if (cur.pos() != 0 && openable(cur.prevAtom(), sel)) { + pushRight(cur, cur.prevAtom()); return true; } - return posLeft(cur) || idxLeft(cur) || popLeft(cur) || selection_; + return cur.posLeft() || idxLeft(cur) || popLeft(cur) || selection_; } @@ -231,12 +213,12 @@ bool MathCursor::right(LCursor & cur, bool sel) } selHandle(cur, sel); - if (hasNextAtom(cur) && openable(nextAtom(cur), sel)) { - pushLeft(cur, nextAtom(cur)); + if (cur.pos() != cur.lastpos() && openable(cur.nextAtom(), sel)) { + pushLeft(cur, cur.nextAtom()); return true; } - return posRight(cur) || idxRight(cur) || popRight(cur) || selection_; + return cur.posRight() || idxRight(cur) || popRight(cur) || selection_; } @@ -400,8 +382,8 @@ void MathCursor::niceInsert(LCursor & cur, MathAtom const & t) plainInsert(cur, t); // enter the new inset and move the contents of the selection if possible if (t->isActive()) { - posLeft(cur); - pushLeft(cur, nextAtom(cur)); + cur.posLeft(); + pushLeft(cur, cur.nextAtom()); paste(cur, safe); } } @@ -449,7 +431,7 @@ bool MathCursor::backspace(LCursor & cur) } } - if (hasPrevAtom(cur) && prevAtom(cur)->nargs() > 0) { + if (cur.pos() != 0 && cur.prevAtom()->nargs() > 0) { // let's require two backspaces for 'big stuff' and // highlight on the first left(cur, true); @@ -490,7 +472,7 @@ bool MathCursor::erase(LCursor & cur) return true; } - if (hasNextAtom(cur) && nextAtom(cur)->nargs() > 0) + if (cur.pos() != cur.lastpos() && cur.nextAtom()->nargs() > 0) right(cur, true); else plainErase(cur); @@ -656,7 +638,7 @@ void MathCursor::handleNest(LCursor & cur, MathAtom const & a, int c) MathAtom at = a; asArray(grabAndEraseSelection(cur), at.nucleus()->cell(c)); insert(cur, at); - pushRight(cur, prevAtom(cur)); + pushRight(cur, cur.prevAtom()); } @@ -700,28 +682,28 @@ void MathCursor::adjust(LCursor & cur, pos_type from, difference_type diff) bool MathCursor::inMacroMode(LCursor & cur) const { - if (!hasPrevAtom(cur)) + if (!cur.pos() != 0) return false; - MathUnknownInset const * p = prevAtom(cur)->asUnknownInset(); + MathUnknownInset const * p = cur.prevAtom()->asUnknownInset(); return p && !p->final(); } MathUnknownInset * MathCursor::activeMacro(LCursor & cur) { - return inMacroMode(cur) ? prevAtom(cur).nucleus()->asUnknownInset() : 0; + return inMacroMode(cur) ? cur.prevAtom().nucleus()->asUnknownInset() : 0; } MathUnknownInset const * MathCursor::activeMacro(LCursor & cur) const { - return inMacroMode(cur) ? prevAtom(cur)->asUnknownInset() : 0; + return inMacroMode(cur) ? cur.prevAtom()->asUnknownInset() : 0; } bool MathCursor::inMacroArgMode(LCursor & cur) const { - return cur.pos() > 0 && prevAtom(cur)->getChar() == '#'; + return cur.pos() > 0 && cur.prevAtom()->getChar() == '#'; } @@ -818,46 +800,6 @@ void MathCursor::normalize(LCursor & cur) } -bool MathCursor::hasPrevAtom(LCursor & cur) const -{ - return cur.pos() > 0; -} - - -bool MathCursor::hasNextAtom(LCursor & cur) const -{ - return cur.pos() < cur.lastpos(); -} - - -MathAtom const & MathCursor::prevAtom(LCursor & cur) const -{ - BOOST_ASSERT(cur.pos() > 0); - return cur.cell()[cur.pos() - 1]; -} - - -MathAtom & MathCursor::prevAtom(LCursor & cur) -{ - BOOST_ASSERT(cur.pos() > 0); - return cur.cell()[cur.pos() - 1]; -} - - -MathAtom const & MathCursor::nextAtom(LCursor & cur) const -{ - BOOST_ASSERT(cur.pos() < cur.lastpos()); - return cur.cell()[cur.pos()]; -} - - -MathAtom & MathCursor::nextAtom(LCursor & cur) -{ - BOOST_ASSERT(cur.pos() < cur.lastpos()); - return cur.cell()[cur.pos()]; -} - - void MathCursor::idxNext(LCursor & cur) { cur.inset()->asMathInset()->idxNext(cur); @@ -918,11 +860,11 @@ bool MathCursor::goUpDown(LCursor & cur, bool up) // try neigbouring script insets if (!selection()) { // try left - if (hasPrevAtom(cur)) { - MathScriptInset const * p = prevAtom(cur)->asScriptInset(); + if (cur.pos() != 0) { + MathScriptInset const * p = cur.prevAtom()->asScriptInset(); if (p && p->has(up)) { --cur.pos(); - push(cur, nextAtom(cur)); + push(cur, cur.nextAtom()); cur.idx() = up; // the superscript has index 1 cur.pos() = cur.lastpos(); //lyxerr << "updown: handled by scriptinset to the left" << endl; @@ -931,10 +873,10 @@ bool MathCursor::goUpDown(LCursor & cur, bool up) } // try right - if (hasNextAtom(cur)) { - MathScriptInset const * p = nextAtom(cur)->asScriptInset(); + if (cur.pos() != cur.lastpos()) { + MathScriptInset const * p = cur.nextAtom()->asScriptInset(); if (p && p->has(up)) { - push(cur, nextAtom(cur)); + push(cur, cur.nextAtom()); cur.idx() = up; cur.pos() = 0; //lyxerr << "updown: handled by scriptinset to the right" << endl; @@ -1090,22 +1032,22 @@ bool MathCursor::script(LCursor & cur, bool up) cur.inset()->asMathInset()->asScriptInset()->ensure(up); cur.idx() = up; cur.pos() = 0; - } else if (hasPrevAtom(cur) && prevAtom(cur)->asScriptInset()) { - prevAtom(cur).nucleus()->asScriptInset()->ensure(up); - pushRight(cur, prevAtom(cur)); + } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) { + cur.prevAtom().nucleus()->asScriptInset()->ensure(up); + pushRight(cur, cur.prevAtom()); cur.idx() = up; cur.pos() = cur.lastpos(); - } else if (hasPrevAtom(cur)) { + } else if (cur.pos() != 0) { --cur.pos(); cur.cell()[cur.pos()] - = MathAtom(new MathScriptInset(nextAtom(cur), up)); - pushLeft(cur, nextAtom(cur)); + = MathAtom(new MathScriptInset(cur.nextAtom(), up)); + pushLeft(cur, cur.nextAtom()); cur.idx() = up; cur.pos() = 0; } else { plainInsert(cur, MathAtom(new MathScriptInset(up))); - prevAtom(cur).nucleus()->asScriptInset()->ensure(up); - pushRight(cur, prevAtom(cur)); + cur.prevAtom().nucleus()->asScriptInset()->ensure(up); + pushRight(cur, cur.prevAtom()); cur.idx() = up; cur.pos() = 0; } @@ -1206,12 +1148,12 @@ bool MathCursor::interpret(LCursor & cur, char c) // but suppress direct insertion of two spaces in a row // the still allows typing 'a' and deleting the 'a', but // it is better than nothing... - if (!hasPrevAtom(cur) || prevAtom(cur)->getChar() != ' ') + if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') insert(cur, c); return true; } - if (hasPrevAtom(cur) && prevAtom(cur)->asSpaceInset()) { - prevAtom(cur).nucleus()->asSpaceInset()->incSpace(); + if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) { + cur.prevAtom().nucleus()->asSpaceInset()->incSpace(); return true; } if (popRight(cur)) @@ -1263,13 +1205,13 @@ void MathCursor::setSelection void MathCursor::insetToggle(LCursor & cur) { - if (hasNextAtom(cur)) { + if (cur.pos() != cur.lastpos()) { // toggle previous inset ... - nextAtom(cur).nucleus()->lock(!nextAtom(cur)->lock()); - } else if (popLeft(cur) && hasNextAtom(cur)) { + cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock()); + } else if (popLeft(cur) && cur.pos() != cur.lastpos()) { // ... or enclosing inset if we are in the last inset position - nextAtom(cur).nucleus()->lock(!nextAtom(cur)->lock()); - posRight(cur); + cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock()); + cur.posRight(); } } @@ -1282,8 +1224,8 @@ string MathCursor::info(LCursor & cur) const cur.cursor_[i].asMathInset()->infoize(os); os << " "; } - if (hasPrevAtom(cur)) - prevAtom(cur)->infoize2(os); + if (cur.pos() != 0) + cur.prevAtom()->infoize2(os); os << " "; return os.str(); } diff --git a/src/mathed/math_cursor.h b/src/mathed/math_cursor.h index 94c851a633..22b92d5615 100644 --- a/src/mathed/math_cursor.h +++ b/src/mathed/math_cursor.h @@ -181,26 +181,11 @@ public: /// leave current MathInset to the left bool popRight(LCursor & cur); - /// - bool hasPrevAtom(LCursor & cur) const; - /// - bool hasNextAtom(LCursor & cur) const; - /// - MathAtom const & prevAtom(LCursor & cur) const; - /// - MathAtom & prevAtom(LCursor & cur); - /// - MathAtom const & nextAtom(LCursor & cur) const; - /// - MathAtom & nextAtom(LCursor & cur); - /// returns the selection void getSelection(LCursor & cur, CursorSlice &, CursorSlice &) const; /// returns the normalized anchor of the selection CursorSlice normalAnchor(LCursor & cur) const; - /// how deep are we nested? - unsigned depth(LCursor & cur) const; /// describe the situation std::string info(LCursor & cur) const; /// dump selection information for debugging @@ -236,10 +221,6 @@ private: bool idxRight(LCursor & bv); /// moves cursor to end of last cell of current line bool idxLineLast(LCursor & bv); - /// moves cursor position one cell to the left - bool posLeft(LCursor & cur); - /// moves cursor position one cell to the right - bool posRight(LCursor & cur); /// moves position somehow up or down bool goUpDown(LCursor & cur, bool up); /// moves position closest to (x, y) in given box