]> git.lyx.org Git - lyx.git/blobdiff - src/Undo.cpp
Add Benjamin Piwowarski to contributors. Welcome to LyX!
[lyx.git] / src / Undo.cpp
index a80c01c701154d6e393f2475759208743d5746b2..ee87fdb0ced4ec9fd9977598d53949161e93e419 100644 (file)
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "buffer_funcs.h"
-#include "DocIterator.h"
+#include "Cursor.h"
 #include "Paragraph.h"
 #include "ParagraphList.h"
-#include "ParagraphParameters.h"
 #include "Text.h"
 
 #include "mathed/MathSupport.h"
@@ -66,7 +65,7 @@ where to insert the stored bits when performining undo.
 struct UndoElement
 {
        ///
-       UndoElement(UndoKind kin, StableDocIterator const & cb, 
+       UndoElement(UndoKind kin, CursorData const & cb, 
                    StableDocIterator const & cel,
                    pit_type fro, pit_type en, ParagraphList * pl, 
                    MathData * ar, BufferParams const & bp, 
@@ -104,9 +103,9 @@ struct UndoElement
        /// Which kind of operation are we recording for?
        UndoKind kind;
        /// the position of the cursor before recordUndo
-       StableDocIterator cur_before;
+       CursorData cur_before;
        /// the position of the cursor at the end of the undo group
-       StableDocIterator cur_after;
+       CursorData cur_after;
        /// the position of the cell described
        StableDocIterator cell;
        /// counted from begin of cell
@@ -189,17 +188,17 @@ struct Undo::Private
                                   group_id(0), group_level(0) {}
        
        // Do one undo/redo step
-       void doTextUndoOrRedo(DocIterator & cur, UndoElementStack & stack, 
+       void doTextUndoOrRedo(CursorData & cur, UndoElementStack & stack, 
                              UndoElementStack & otherStack);
        // Apply one undo/redo group. Returns false if no undo possible.
-       bool textUndoOrRedo(DocIterator & cur, bool isUndoOperation);
+       bool textUndoOrRedo(CursorData & cur, bool isUndoOperation);
 
        ///
        void doRecordUndo(UndoKind kind,
                DocIterator const & cell,
                pit_type first_pit,
                pit_type last_pit,
-               StableDocIterator const & cur,
+               CursorData const & cur,
                bool isFullBuffer,
                UndoElementStack & stack);
        ///
@@ -207,7 +206,7 @@ struct Undo::Private
                DocIterator const & cell,
                pit_type first_pit,
                pit_type last_pit,
-               DocIterator const & cur,
+               CursorData const & cur,
                bool isFullBuffer);
 
        ///
@@ -250,8 +249,10 @@ void Undo::clear()
        d->undostack_.clear();
        d->redostack_.clear();
        d->undo_finished_ = true;
-       d->group_id = 0;
-       d->group_level = 0;
+       // We used to do that, but I believe it is better to keep
+       // groups (only used in Buffer::reload for now (JMarc)
+       //d->group_id = 0;
+       //d->group_level = 0;
 }
 
 
@@ -292,7 +293,7 @@ static bool samePar(StableDocIterator const & i1, StableDocIterator const & i2)
 void Undo::Private::doRecordUndo(UndoKind kind,
        DocIterator const & cell,
        pit_type first_pit, pit_type last_pit,
-       StableDocIterator const & cur_before,
+       CursorData const & cur_before,
        bool isFullBuffer,
        UndoElementStack & stack)
 {
@@ -317,7 +318,7 @@ void Undo::Private::doRecordUndo(UndoKind kind,
            && stack.top().from == from
            && stack.top().end == end) {
                // reset cur_after; it will be filled correctly by endUndoGroup.
-               stack.top().cur_after = StableDocIterator();
+               stack.top().cur_after = CursorData();
                return;
        }
 
@@ -345,15 +346,6 @@ void Undo::Private::doRecordUndo(UndoKind kind,
                advance(first, first_pit);
                ParagraphList::const_iterator last = plist.begin();
                advance(last, last_pit + 1);
-               // If the paragraphs after the last one have a
-               // non-zero depth and the depth of last paragraph is
-               // decremented, then these paragraphs may be affected
-               // (ticket #8159). We guard against that by saving
-               // these extra paragraphs.
-               while (last != plist.end() && last->params().depth() > 0) {
-                       ++last;
-                       --undo.end;
-               }
                undo.pars = new ParagraphList(first, last);
        }
 
@@ -366,7 +358,7 @@ void Undo::Private::doRecordUndo(UndoKind kind,
 void Undo::Private::recordUndo(UndoKind kind,
                               DocIterator const & cell,
                               pit_type first_pit, pit_type last_pit,
-                              DocIterator const & cur,
+                              CursorData const & cur,
                               bool isFullBuffer)
 {
        LASSERT(first_pit <= cell.lastpit(), /**/);
@@ -388,7 +380,7 @@ void Undo::Private::recordUndo(UndoKind kind,
 }
 
 
-void Undo::Private::doTextUndoOrRedo(DocIterator & cur, UndoElementStack & stack, UndoElementStack & otherstack)
+void Undo::Private::doTextUndoOrRedo(CursorData & cur, UndoElementStack & stack, UndoElementStack & otherstack)
 {
        // Adjust undo stack and get hold of current undo data.
        UndoElement & undo = stack.top();
@@ -456,8 +448,8 @@ void Undo::Private::doTextUndoOrRedo(DocIterator & cur, UndoElementStack & stack
        LASSERT(undo.pars == 0, /**/);
        LASSERT(undo.array == 0, /**/);
 
-       if (undo.cur_before.size())
-               cur = undo.cur_before.asDocIterator(&buffer_);
+       if (!undo.cur_before.empty())
+               cur = undo.cur_before;
        if (undo.lyx_clean)
                buffer_.markClean();
        else
@@ -467,7 +459,7 @@ void Undo::Private::doTextUndoOrRedo(DocIterator & cur, UndoElementStack & stack
 }
 
 
-bool Undo::Private::textUndoOrRedo(DocIterator & cur, bool isUndoOperation)
+bool Undo::Private::textUndoOrRedo(CursorData & cur, bool isUndoOperation)
 {
        undo_finished_ = true;
 
@@ -496,13 +488,13 @@ void Undo::finishUndo()
 }
 
 
-bool Undo::textUndo(DocIterator & cur)
+bool Undo::textUndo(CursorData & cur)
 {
        return d->textUndoOrRedo(cur, true);
 }
 
 
-bool Undo::textRedo(DocIterator & cur)
+bool Undo::textRedo(CursorData & cur)
 {
        return d->textUndoOrRedo(cur, false);
 }
@@ -521,8 +513,10 @@ void Undo::beginUndoGroup()
 
 void Undo::endUndoGroup()
 {
-       if (d->group_level == 0)
+       if (d->group_level == 0) {
                LYXERR0("There is no undo group to end here");
+               return;
+       }
        --d->group_level;
        if (d->group_level == 0) {
                // real end of the group
@@ -531,10 +525,10 @@ void Undo::endUndoGroup()
 }
 
 
-void Undo::endUndoGroup(DocIterator const & cur)
+void Undo::endUndoGroup(CursorData const & cur)
 {
        endUndoGroup();
-       if (!d->undostack_.empty() && !d->undostack_.top().cur_after.size())
+       if (!d->undostack_.empty() && d->undostack_.top().cur_after.empty())
                d->undostack_.top().cur_after = cur;
 }
 
@@ -543,13 +537,13 @@ void Undo::endUndoGroup(DocIterator const & cur)
 // Private::recordUndo public as sole interface. The code in the
 // convenience functions can move to Cursor.cpp.
 
-void Undo::recordUndo(DocIterator const & cur, UndoKind kind)
+void Undo::recordUndo(CursorData const & cur, UndoKind kind)
 {
        d->recordUndo(kind, cur, cur.pit(), cur.pit(), cur, false);
 }
 
 
-void Undo::recordUndoInset(DocIterator const & cur, UndoKind kind,
+void Undo::recordUndoInset(CursorData const & cur, UndoKind kind,
                           Inset const * inset)
 {
        if (!inset || inset == &cur.inset()) {
@@ -563,20 +557,20 @@ void Undo::recordUndoInset(DocIterator const & cur, UndoKind kind,
 }
 
 
-void Undo::recordUndo(DocIterator const & cur, UndoKind kind, pit_type from)
+void Undo::recordUndo(CursorData const & cur, UndoKind kind, pit_type from)
 {
        d->recordUndo(kind, cur, cur.pit(), from, cur, false);
 }
 
 
-void Undo::recordUndo(DocIterator const & cur, UndoKind kind,
+void Undo::recordUndo(CursorData const & cur, UndoKind kind,
        pit_type from, pit_type to)
 {
        d->recordUndo(kind, cur, from, to, cur, false);
 }
 
 
-void Undo::recordUndoFullDocument(DocIterator const & cur)
+void Undo::recordUndoFullDocument(CursorData const & cur)
 {
        // This one may happen outside of the main undo group, so we
        // put it in its own subgroup to avoid complaints.