This patch removes some code duplication in DocIterator (in particular
removes forwardPosNoDescend that if nothing else, has an ugly name), by
moving the part of forwardPos that acts on flat cursorslices to
CursorSlice::operator++. Comments?
A/
CursorSlice.cpp | 65 +++++++++++++++++++++++++++++++
CursorSlice.h | 11 ++++-
DocIterator.cpp | 91
+++-----------------------------------------
DocIterator.h | 2
Text.cpp | 2
Text.h | 4 -
Text2.cpp | 12 ++---
insets/InsetCollapsable.cpp | 2
lyxfind.cpp | 17 ++------
9 files changed, 99 insertions(+), 107 deletions(-)
Index: insets/InsetCollapsable.cpp
===================================================================
--- insets/InsetCollapsable.cpp (revision 19477)
+++ insets/InsetCollapsable.cpp (working copy)
@@ -394,7 +394,7 @@
else if (cmd.argument() == "toggle" || cmd.argument().empty())
if (isOpen()) {
setStatus(cur, Collapsed);
- cur.forwardPosNoDescend();
+ ++cur.top();
}
else
setStatus(cur, Open);
Index: Text2.cpp
===================================================================
--- Text2.cpp (revision 19477)
+++ Text2.cpp (working copy)
@@ -327,7 +327,7 @@
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 @@
// 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 @@
// 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) {
if (dit.pos() != dit.lastpos()) {
pit_type const pit = dit.pit();
pos_type const pos = dit.pos();
Index: lyxfind.cpp
===================================================================
--- lyxfind.cpp (revision 19477)
+++ lyxfind.cpp (working copy)
@@ -366,21 +366,16 @@
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) {
+ 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;
// Now put cursor to end of selection:
bv->cursor().setCursor(cur);
Index: Text.cpp
===================================================================
--- Text.cpp (revision 19477)
+++ Text.cpp (working copy)
@@ -1226,7 +1226,7 @@
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();
}
checkBufferStructure(cur.buffer(), cur);
needsUpdate = true;
Index: CursorSlice.h
===================================================================
--- CursorSlice.h (revision 19477)
+++ CursorSlice.h (working copy)
@@ -70,7 +70,7 @@
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 @@
/// write some debug information to \p os
friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
+ /// move to next position
+ CursorSlice & operator++();
+ /// move to previous position
+ CursorSlice & operator--();
+ /// 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.
Index: DocIterator.h
===================================================================
--- DocIterator.h (revision 19477)
+++ DocIterator.h (working copy)
@@ -178,8 +178,6 @@
//
// 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
Index: DocIterator.cpp
===================================================================
--- DocIterator.cpp (revision 19477)
+++ DocIterator.cpp (working copy)
@@ -338,81 +338,17 @@
return;
}
- // otherwise move on one position if possible
- if (tip.pos() < lastp) {
- //lyxerr << "... next pos" << endl;
- ++tip.pos();
+ if (!top().at_end()) {
+ ++top();
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...
- if (!empty())
- ++top().pos();
-}
-
-
void DocIterator::forwardPar()
{
forwardPos();
@@ -492,32 +428,21 @@
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();
+
// 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()) {
Index: Text.h
===================================================================
--- Text.h (revision 19477)
+++ Text.h (working copy)
@@ -103,8 +103,8 @@
/// 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);
///
Index: CursorSlice.cpp
===================================================================
--- CursorSlice.cpp (revision 19477)
+++ CursorSlice.cpp (working copy)
@@ -91,6 +91,71 @@
}
+CursorSlice & CursorSlice::operator++()
+{
+ // move on one position if possible
+ if (pos() < lastpos()) {
+ //lyxerr << "... next pos" << endl;
+ ++pos();
+ return *this;
+ }
+
+ // otherwise move on one paragraph if possible
+ if (pit() < lastpit()) {
+ //lyxerr << "... next par" << endl;
+ ++pit();
+ pos() = 0;
+ return *this;
+ }
+
+ // otherwise try to move on one cell if possible
+ if (idx() < lastidx()) {
+ //lyxerr << "... next idx" << endl;
+ ++idx();
+ pit() = 0;
+ pos() = 0;
+ return *this;
+ }
+ BOOST_ASSERT(false);
+}
+
+
+CursorSlice & CursorSlice::operator--()
+{
+ if (pos() != 0) {
+ --pos();
+ return *this;
+ }
+
+ if (pit() != 0) {
+ --pit();
+ pos() = lastpos();
+ return *this;
+ }
+
+ if (idx() != 0) {
+ --idx();
+ pit() = lastpit();
+ pos() = lastpos();
+ return *this;
+ }
+
+ 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()