* DocIterator.h (forwardPosNoDescend): remove method

* DocIterator.cpp (forwardPos, backwardPos): move the 'flat' part to...
* CursorSlice.{h,cpp} (forwardPos, backwardPos, at_end, at_begin): new methods, the first two are part of their DocIterator homonym
* Text2.cpp (setFont): receive CursorSlice arguments as the function is not recursive, adapt
* Text.cpp:
* lyxfind.cpp (findNextChange):
* insets/InsetCollapsable.cpp: adapt



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@19500 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Alfredo Braunstein 2007-08-13 13:36:19 +00:00
parent f1923a7b35
commit bc0a64efc3
9 changed files with 100 additions and 107 deletions

View File

@ -91,6 +91,71 @@ CursorSlice::col_type CursorSlice::col() const
}
void CursorSlice::forwardPos()
{
// move on one position if possible
if (pos() < lastpos()) {
//lyxerr << "... next pos" << endl;
++pos();
return;
}
// otherwise move on one paragraph if possible
if (pit() < lastpit()) {
//lyxerr << "... next par" << endl;
++pit();
pos() = 0;
return;
}
// otherwise try to move on one cell if possible
if (idx() < lastidx()) {
//lyxerr << "... next idx" << endl;
++idx();
pit() = 0;
pos() = 0;
return;
}
BOOST_ASSERT(false);
}
void CursorSlice::backwardPos()
{
if (pos() != 0) {
--pos();
return;
}
if (pit() != 0) {
--pit();
pos() = lastpos();
return;
}
if (idx() != 0) {
--idx();
pit() = lastpit();
pos() = lastpos();
return;
}
BOOST_ASSERT(false);
}
bool CursorSlice::at_end() const
{
return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
}
bool CursorSlice::at_begin() const
{
return idx() == 0 && pit() == 0 && pos() == 0;
}
bool operator==(CursorSlice const & p, CursorSlice const & q)
{
return &p.inset() == &q.inset()

View File

@ -70,7 +70,7 @@ public:
pit_type pit() const { return pit_; }
/// set the offset of the paragraph this cursor is in
pit_type & pit() { return pit_; }
/// return the last paragraph offset this cursor is in
/// return the last paragraph offset within the ParagraphList
pit_type lastpit() const;
/// increments the paragraph this cursor is in
void incrementPar();
@ -121,6 +121,15 @@ public:
/// write some debug information to \p os
friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
/// move to next position
void forwardPos();
/// move to previous position
void backwardPos();
/// are we at the end of this slice
bool at_end() const;
/// are we at the start of this slice
bool at_begin() const;
private:
/// pointer to 'owning' inset. This is some kind of cache.

View File

@ -338,76 +338,13 @@ void DocIterator::forwardPos(bool ignorecollapsed)
return;
}
// otherwise move on one position if possible
if (tip.pos() < lastp) {
//lyxerr << "... next pos" << endl;
++tip.pos();
if (!tip.at_end()) {
tip.forwardPos();
return;
}
//lyxerr << "... no next pos" << endl;
// otherwise move on one paragraph if possible
if (tip.pit() < lastpit()) {
//lyxerr << "... next par" << endl;
++tip.pit();
tip.pos() = 0;
return;
}
//lyxerr << "... no next pit" << endl;
// otherwise try to move on one cell if possible
if (tip.idx() < lastidx()) {
//lyxerr << "... next idx" << endl;
++tip.idx();
tip.pit() = 0;
tip.pos() = 0;
return;
}
//lyxerr << "... no next idx" << endl;
// otherwise leave inset and jump over inset as a whole
pop_back();
// 'top' is invalid now...
if (!empty())
++top().pos();
}
void DocIterator::forwardPosNoDescend()
{
CursorSlice & tip = top();
pos_type const lastp = lastpos();
// move on one position if possible
if (tip.pos() < lastp) {
//lyxerr << "... next pos" << endl;
++tip.pos();
return;
}
//lyxerr << "... no next pos" << endl;
// otherwise move on one paragraph if possible
if (tip.pit() < lastpit()) {
//lyxerr << "... next par" << endl;
++tip.pit();
tip.pos() = 0;
return;
}
//lyxerr << "... no next pit" << endl;
// otherwise try to move on one cell if possible
if (tip.idx() < lastidx()) {
//lyxerr << "... next idx" << endl;
++tip.idx();
tip.pit() = 0;
tip.pos() = 0;
return;
}
//lyxerr << "... no next idx" << endl;
// otherwise leave inset and jump over inset as a whole
pop_back();
// 'top' is invalid now...
// 'tip' is invalid now...
if (!empty())
++top().pos();
}
@ -492,32 +429,21 @@ void DocIterator::backwardPos()
return;
}
CursorSlice & tip = top();
if (tip.pos() != 0) {
--tip.pos();
} else if (tip.pit() != 0) {
--tip.pit();
tip.pos() = lastpos();
return;
} else if (tip.idx() != 0) {
--tip.idx();
tip.pit() = lastpit();
tip.pos() = lastpos();
return;
} else {
if (top().at_begin()) {
pop_back();
return;
}
top().backwardPos();
// move into an inset to the left if possible
Inset * n = 0;
if (inMathed()) {
n = (tip.cell().begin() + tip.pos())->nucleus();
n = (top().cell().begin() + top().pos())->nucleus();
} else {
if (paragraph().isInset(tip.pos()))
n = paragraph().getInset(tip.pos());
if (paragraph().isInset(top().pos()))
n = paragraph().getInset(top().pos());
}
if (n && n->isActive()) {

View File

@ -178,8 +178,6 @@ public:
//
// elementary moving
//
/// move on one logical position, do not descend into nested insets
void forwardPosNoDescend();
/**
* move on one logical position, descend into nested insets
* skip collapsed insets if \p ignorecollapsed is true

View File

@ -1226,7 +1226,7 @@ bool Text::erase(Cursor & cur)
recordUndo(cur, Undo::DELETE);
if(!par.eraseChar(cur.pos(), cur.buffer().params().trackChanges)) {
// the character has been logically deleted only => skip it
cur.forwardPosNoDescend();
cur.top().forwardPos();
}
checkBufferStructure(cur.buffer(), cur);
needsUpdate = true;

View File

@ -103,8 +103,8 @@ public:
/// FIXME: replace Cursor with DocIterator.
void setFont(Cursor & cur, Font const &, bool toggleall = false);
/// Set font from \p begin to \p end and rebreak.
void setFont(Buffer const & buffer, DocIterator const & begin,
DocIterator const & end, Font const &,
void setFont(Buffer const & buffer, CursorSlice const & begin,
CursorSlice const & end, Font const &,
bool toggleall = false);
///

View File

@ -327,7 +327,7 @@ void Text::setInsetFont(Buffer const & buffer, pit_type pit,
DocIterator cellend = cellbegin;
cellend.pit() = cellend.lastpit();
cellend.pos() = cellend.lastpos();
text->setFont(buffer, cellbegin, cellend, font, toggleall);
text->setFont(buffer, cellbegin.top(), cellend.top(), font, toggleall);
}
if (dit == end)
break;
@ -492,13 +492,13 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
// Ok, we have a selection.
recordUndoSelection(cur);
setFont(cur.buffer(), cur.selectionBegin(), cur.selectionEnd(), font,
toggleall);
setFont(cur.buffer(), cur.selectionBegin().top(),
cur.selectionEnd().top(), font, toggleall);
}
void Text::setFont(Buffer const & buffer, DocIterator const & begin,
DocIterator const & end, Font const & font,
void Text::setFont(Buffer const & buffer, CursorSlice const & begin,
CursorSlice const & end, Font const & font,
bool toggleall)
{
// Don't use forwardChar here as ditend might have
@ -506,7 +506,7 @@ void Text::setFont(Buffer const & buffer, DocIterator const & begin,
// Can't use forwardPos either as this descends into
// nested insets.
Language const * language = buffer.params().language;
for (DocIterator dit = begin; dit != end; dit.forwardPosNoDescend()) {
for (CursorSlice dit = begin; dit != end; dit.forwardPos()) {
if (dit.pos() != dit.lastpos()) {
pit_type const pit = dit.pit();
pos_type const pos = dit.pos();

View File

@ -401,7 +401,7 @@ void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
else if (cmd.argument() == "toggle" || cmd.argument().empty())
if (isOpen()) {
setStatus(cur, Collapsed);
cur.forwardPosNoDescend();
cur.top().forwardPos();
}
else
setStatus(cur, Open);

View File

@ -366,21 +366,16 @@ bool findNextChange(BufferView * bv)
Change orig_change = cur.paragraph().lookupChange(cur.pos());
DocIterator et = doc_iterator_end(cur.inset());
DocIterator ok = cur; // see below
for (; cur != et; cur.forwardPosNoDescend()) {
ok = cur;
Change change = cur.paragraph().lookupChange(cur.pos());
if (change != orig_change) {
CursorSlice & tip = cur.top();
for (; !tip.at_end(); tip.forwardPos()) {
Change change = tip.paragraph().lookupChange(tip.pos());
if (change != orig_change)
break;
}
}
// avoid crash (assertion violation) if the imaginary end-of-par
// character of the last paragraph of the document is marked as changed
if (cur == et) {
cur = ok;
}
if (tip.at_end())
tip.backwardPos();
// Now put cursor to end of selection:
bv->cursor().setCursor(cur);