shift small stuff from math cursor to cursor

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8360 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2004-01-16 13:35:10 +00:00
parent ea53de7c85
commit 335e83b1a8
4 changed files with 118 additions and 123 deletions

View File

@ -26,6 +26,8 @@
#include "insets/insettabular.h" #include "insets/insettabular.h"
#include "insets/insettext.h" #include "insets/insettext.h"
#include "mathed/math_data.h"
#include <boost/assert.hpp> #include <boost/assert.hpp>
using std::vector; using std::vector;
@ -227,3 +229,49 @@ BufferView & LCursor::bv() const
{ {
return *bv_; 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;
}

View File

@ -19,6 +19,7 @@
class BufferView; class BufferView;
class UpdatableInset; class UpdatableInset;
class MathAtom;
class DispatchResult; class DispatchResult;
class FuncRequest; class FuncRequest;
class LyXText; class LyXText;
@ -68,10 +69,6 @@ public:
idx_type idx() const { return top().idx(); } idx_type idx() const { return top().idx(); }
/// return the text-ed cell this cursor is in /// return the text-ed cell this cursor is in
idx_type & idx() { return top().idx(); } 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 /// return the paragraph this cursor is in
par_type par() const { return top().par(); } par_type par() const { return top().par(); }
/// return the paragraph this cursor is in /// return the paragraph this cursor is in
@ -93,6 +90,24 @@ public:
/// return the grid row of the current cell /// return the grid row of the current cell
col_type col() const { return top().col(); } 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; UpdatableInset * innerInset() const;
/// ///
@ -105,6 +120,15 @@ public:
void getPos(int & x, int & y) const; void getPos(int & x, int & y) const;
/// returns cursor dimension /// returns cursor dimension
void getDim(int & asc, int & desc) const; 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 /// cache the absolute coordinate from the top inset
void updatePos(); void updatePos();
/// sets anchor to cursor position /// sets anchor to cursor position

View File

@ -85,7 +85,7 @@ void MathCursor::pushLeft(LCursor & cur, MathAtom & t)
void MathCursor::pushRight(LCursor & cur, MathAtom & t) void MathCursor::pushRight(LCursor & cur, MathAtom & t)
{ {
//lyxerr << "Entering atom " << t << " right" << endl; //lyxerr << "Entering atom " << t << " right" << endl;
posLeft(cur); cur.posLeft();
push(cur, t); push(cur, t);
t->idxLast(cur); t->idxLast(cur);
} }
@ -115,7 +115,7 @@ bool MathCursor::popRight(LCursor & cur)
} }
cur.inset()->asMathInset()->notifyCursorLeaves(cur.idx()); cur.inset()->asMathInset()->notifyCursorLeaves(cur.idx());
cur.pop(); cur.pop();
posRight(cur); cur.posRight();
return true; 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) bool MathCursor::left(LCursor & cur, bool sel)
{ {
dump("Left 1"); dump("Left 1");
@ -211,12 +193,12 @@ bool MathCursor::left(LCursor & cur, bool sel)
} }
selHandle(cur, sel); selHandle(cur, sel);
if (hasPrevAtom(cur) && openable(prevAtom(cur), sel)) { if (cur.pos() != 0 && openable(cur.prevAtom(), sel)) {
pushRight(cur, prevAtom(cur)); pushRight(cur, cur.prevAtom());
return true; 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); selHandle(cur, sel);
if (hasNextAtom(cur) && openable(nextAtom(cur), sel)) { if (cur.pos() != cur.lastpos() && openable(cur.nextAtom(), sel)) {
pushLeft(cur, nextAtom(cur)); pushLeft(cur, cur.nextAtom());
return true; 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); plainInsert(cur, t);
// enter the new inset and move the contents of the selection if possible // enter the new inset and move the contents of the selection if possible
if (t->isActive()) { if (t->isActive()) {
posLeft(cur); cur.posLeft();
pushLeft(cur, nextAtom(cur)); pushLeft(cur, cur.nextAtom());
paste(cur, safe); 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 // let's require two backspaces for 'big stuff' and
// highlight on the first // highlight on the first
left(cur, true); left(cur, true);
@ -490,7 +472,7 @@ bool MathCursor::erase(LCursor & cur)
return true; return true;
} }
if (hasNextAtom(cur) && nextAtom(cur)->nargs() > 0) if (cur.pos() != cur.lastpos() && cur.nextAtom()->nargs() > 0)
right(cur, true); right(cur, true);
else else
plainErase(cur); plainErase(cur);
@ -656,7 +638,7 @@ void MathCursor::handleNest(LCursor & cur, MathAtom const & a, int c)
MathAtom at = a; MathAtom at = a;
asArray(grabAndEraseSelection(cur), at.nucleus()->cell(c)); asArray(grabAndEraseSelection(cur), at.nucleus()->cell(c));
insert(cur, at); 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 bool MathCursor::inMacroMode(LCursor & cur) const
{ {
if (!hasPrevAtom(cur)) if (!cur.pos() != 0)
return false; return false;
MathUnknownInset const * p = prevAtom(cur)->asUnknownInset(); MathUnknownInset const * p = cur.prevAtom()->asUnknownInset();
return p && !p->final(); return p && !p->final();
} }
MathUnknownInset * MathCursor::activeMacro(LCursor & cur) 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 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 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) void MathCursor::idxNext(LCursor & cur)
{ {
cur.inset()->asMathInset()->idxNext(cur); cur.inset()->asMathInset()->idxNext(cur);
@ -918,11 +860,11 @@ bool MathCursor::goUpDown(LCursor & cur, bool up)
// try neigbouring script insets // try neigbouring script insets
if (!selection()) { if (!selection()) {
// try left // try left
if (hasPrevAtom(cur)) { if (cur.pos() != 0) {
MathScriptInset const * p = prevAtom(cur)->asScriptInset(); MathScriptInset const * p = cur.prevAtom()->asScriptInset();
if (p && p->has(up)) { if (p && p->has(up)) {
--cur.pos(); --cur.pos();
push(cur, nextAtom(cur)); push(cur, cur.nextAtom());
cur.idx() = up; // the superscript has index 1 cur.idx() = up; // the superscript has index 1
cur.pos() = cur.lastpos(); cur.pos() = cur.lastpos();
//lyxerr << "updown: handled by scriptinset to the left" << endl; //lyxerr << "updown: handled by scriptinset to the left" << endl;
@ -931,10 +873,10 @@ bool MathCursor::goUpDown(LCursor & cur, bool up)
} }
// try right // try right
if (hasNextAtom(cur)) { if (cur.pos() != cur.lastpos()) {
MathScriptInset const * p = nextAtom(cur)->asScriptInset(); MathScriptInset const * p = cur.nextAtom()->asScriptInset();
if (p && p->has(up)) { if (p && p->has(up)) {
push(cur, nextAtom(cur)); push(cur, cur.nextAtom());
cur.idx() = up; cur.idx() = up;
cur.pos() = 0; cur.pos() = 0;
//lyxerr << "updown: handled by scriptinset to the right" << endl; //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.inset()->asMathInset()->asScriptInset()->ensure(up);
cur.idx() = up; cur.idx() = up;
cur.pos() = 0; cur.pos() = 0;
} else if (hasPrevAtom(cur) && prevAtom(cur)->asScriptInset()) { } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
prevAtom(cur).nucleus()->asScriptInset()->ensure(up); cur.prevAtom().nucleus()->asScriptInset()->ensure(up);
pushRight(cur, prevAtom(cur)); pushRight(cur, cur.prevAtom());
cur.idx() = up; cur.idx() = up;
cur.pos() = cur.lastpos(); cur.pos() = cur.lastpos();
} else if (hasPrevAtom(cur)) { } else if (cur.pos() != 0) {
--cur.pos(); --cur.pos();
cur.cell()[cur.pos()] cur.cell()[cur.pos()]
= MathAtom(new MathScriptInset(nextAtom(cur), up)); = MathAtom(new MathScriptInset(cur.nextAtom(), up));
pushLeft(cur, nextAtom(cur)); pushLeft(cur, cur.nextAtom());
cur.idx() = up; cur.idx() = up;
cur.pos() = 0; cur.pos() = 0;
} else { } else {
plainInsert(cur, MathAtom(new MathScriptInset(up))); plainInsert(cur, MathAtom(new MathScriptInset(up)));
prevAtom(cur).nucleus()->asScriptInset()->ensure(up); cur.prevAtom().nucleus()->asScriptInset()->ensure(up);
pushRight(cur, prevAtom(cur)); pushRight(cur, cur.prevAtom());
cur.idx() = up; cur.idx() = up;
cur.pos() = 0; cur.pos() = 0;
} }
@ -1206,12 +1148,12 @@ bool MathCursor::interpret(LCursor & cur, char c)
// but suppress direct insertion of two spaces in a row // but suppress direct insertion of two spaces in a row
// the still allows typing '<space>a<space>' and deleting the 'a', but // the still allows typing '<space>a<space>' and deleting the 'a', but
// it is better than nothing... // it is better than nothing...
if (!hasPrevAtom(cur) || prevAtom(cur)->getChar() != ' ') if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
insert(cur, c); insert(cur, c);
return true; return true;
} }
if (hasPrevAtom(cur) && prevAtom(cur)->asSpaceInset()) { if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
prevAtom(cur).nucleus()->asSpaceInset()->incSpace(); cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
return true; return true;
} }
if (popRight(cur)) if (popRight(cur))
@ -1263,13 +1205,13 @@ void MathCursor::setSelection
void MathCursor::insetToggle(LCursor & cur) void MathCursor::insetToggle(LCursor & cur)
{ {
if (hasNextAtom(cur)) { if (cur.pos() != cur.lastpos()) {
// toggle previous inset ... // toggle previous inset ...
nextAtom(cur).nucleus()->lock(!nextAtom(cur)->lock()); cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
} else if (popLeft(cur) && hasNextAtom(cur)) { } else if (popLeft(cur) && cur.pos() != cur.lastpos()) {
// ... or enclosing inset if we are in the last inset position // ... or enclosing inset if we are in the last inset position
nextAtom(cur).nucleus()->lock(!nextAtom(cur)->lock()); cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
posRight(cur); cur.posRight();
} }
} }
@ -1282,8 +1224,8 @@ string MathCursor::info(LCursor & cur) const
cur.cursor_[i].asMathInset()->infoize(os); cur.cursor_[i].asMathInset()->infoize(os);
os << " "; os << " ";
} }
if (hasPrevAtom(cur)) if (cur.pos() != 0)
prevAtom(cur)->infoize2(os); cur.prevAtom()->infoize2(os);
os << " "; os << " ";
return os.str(); return os.str();
} }

View File

@ -181,26 +181,11 @@ public:
/// leave current MathInset to the left /// leave current MathInset to the left
bool popRight(LCursor & cur); 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 /// returns the selection
void getSelection(LCursor & cur, CursorSlice &, CursorSlice &) const; void getSelection(LCursor & cur, CursorSlice &, CursorSlice &) const;
/// returns the normalized anchor of the selection /// returns the normalized anchor of the selection
CursorSlice normalAnchor(LCursor & cur) const; CursorSlice normalAnchor(LCursor & cur) const;
/// how deep are we nested?
unsigned depth(LCursor & cur) const;
/// describe the situation /// describe the situation
std::string info(LCursor & cur) const; std::string info(LCursor & cur) const;
/// dump selection information for debugging /// dump selection information for debugging
@ -236,10 +221,6 @@ private:
bool idxRight(LCursor & bv); bool idxRight(LCursor & bv);
/// moves cursor to end of last cell of current line /// moves cursor to end of last cell of current line
bool idxLineLast(LCursor & bv); 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 /// moves position somehow up or down
bool goUpDown(LCursor & cur, bool up); bool goUpDown(LCursor & cur, bool up);
/// moves position closest to (x, y) in given box /// moves position closest to (x, y) in given box