]> git.lyx.org Git - lyx.git/blobdiff - src/undo_funcs.C
simpler logic to create 'end' from 'behind'
[lyx.git] / src / undo_funcs.C
index 8d15fed249e471e83c8f8e7406f3aefab029e192..3c63678097bc8df029a14be91719615166b8abf4 100644 (file)
@@ -14,6 +14,7 @@
 #include "BufferView.h"
 #include "buffer.h"
 #include "insets/updatableinset.h"
+#include "insets/insettext.h"
 #include "debug.h"
 #include "support/LAssert.h"
 
@@ -76,10 +77,8 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
 {
        Buffer * b = bv->buffer();
 
-       Paragraph * before =
-               b->getParFromID(undo.number_of_before_par);
-       Paragraph * behind =
-               b->getParFromID(undo.number_of_behind_par);
+       Paragraph * before = &*b->getParFromID(undo.number_of_before_par);
+       Paragraph * behind = &*b->getParFromID(undo.number_of_behind_par);
        Paragraph * tmppar;
        Paragraph * tmppar2;
 
@@ -144,7 +143,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                        // is stored in the undo. So restore
                        // the text informations.
                        if (undo.kind == Undo::EDIT) {
-                               tmppar2->setContentsFromPar(tmppar);
+                               tmppar2->setContentsFromPar(*tmppar);
                                tmppar2 = tmppar2->next();
                        }
                }
@@ -166,17 +165,28 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                undopar->previous(before);
                if (before)
                        before->next(undopar);
-               else
-                       bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
-                                                undopar);
-
+               else {
+                       int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
+                       Paragraph * op = &*bv->buffer()->getParFromID(id);
+                       if (op && op->inInset()) {
+                               static_cast<InsetText*>(op->inInset())->paragraph(undopar);
+                       } else {
+                               bv->buffer()->paragraphs.set(undopar);
+                       }
+               }
        } else {
                // We enter here on DELETE undo operations where we have to
                // substitue the second paragraph with the first if the removed
                // one is the first!
                if (!before && behind) {
-                       bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
-                                                behind);
+                       int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
+                       Paragraph * op = &*bv->buffer()->getParFromID(id);
+                       if (op && op->inInset()) {
+                               static_cast<InsetText*>(op->inInset())->paragraph(behind);
+                       } else {
+                               bv->buffer()->paragraphs.set(behind);
+                       }
+
                        undopar = behind;
                }
        }
@@ -207,7 +217,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
        if (behind)
                endpar = behind->next();
 
-       tmppar = bv->buffer()->getParFromID(undo.number_of_cursor_par);
+       tmppar = &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
        UpdatableInset* it = 0;
        if (undopar)
                it = static_cast<UpdatableInset*>(undopar->inInset());
@@ -272,8 +282,11 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
 
 
 bool createUndo(BufferView * bv, Undo::undo_kind kind,
-       Paragraph const * first, Paragraph const * behind, shared_ptr<Undo> & u)
+       ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
+       shared_ptr<Undo> & u)
 {
+       Paragraph * first = &*itfirst;
+       Paragraph * behind = &*itbehind;
        lyx::Assert(first);
 
        int before_number = -1;
@@ -308,18 +321,11 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
        }
 
        // create a new Undo
-       Paragraph * undopar;
+       Paragraph * undopar = 0; // nothing to replace yet (undo of delete maybe)
 
-       Paragraph * start = const_cast<Paragraph *>(first);
-       Paragraph * end = 0;
+       Paragraph * start = first;
+       Paragraph * end = &*boost::prior(itbehind);
 
-       if (behind)
-               end = const_cast<Paragraph*>(behind->previous());
-       else {
-               end = start;
-               while (end->next())
-                       end = end->next();
-       }
        if (start && end && (start != end->next()) &&
            ((before_number != behind_number) ||
                 ((before_number < 0) && (behind_number < 0))))
@@ -348,11 +354,10 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
                        tmppar2 = tmppar2->next();
                }
                tmppar2->next(0);
-       } else
-               undopar = 0; // nothing to replace (undo of delete maybe)
+       }
 
        int cursor_par = undoCursor(bv).par()->id();
-       int cursor_pos =  undoCursor(bv).pos();
+       int cursor_pos = undoCursor(bv).pos();
 
        u.reset(new Undo(kind, inset_id,
                before_number, behind_number,
@@ -400,7 +405,7 @@ bool textUndo(BufferView * bv)
        finishUndo();
 
        if (!undo_frozen) {
-               Paragraph * first = b->getParFromID(undo->number_of_before_par);
+               Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
                if (first && first->next())
                        first = first->next();
                else if (!first)
@@ -439,7 +444,7 @@ bool textRedo(BufferView * bv)
        finishUndo();
 
        if (!undo_frozen) {
-       Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
+       Paragraph * first = &*bv->buffer()->getParFromID(undo->number_of_before_par);
                if (first && first->next())
                        first = first->next();
                else if (!first)
@@ -447,7 +452,7 @@ bool textRedo(BufferView * bv)
                if (first) {
                        shared_ptr<Undo> u;
                        if (createUndo(bv, undo->kind, first,
-                               bv->buffer()->getParFromID(undo->number_of_behind_par), u))
+                               &*bv->buffer()->getParFromID(undo->number_of_behind_par), u))
                                bv->buffer()->undostack.push(u);
                }
        }
@@ -464,7 +469,7 @@ bool textRedo(BufferView * bv)
 
 
 void setUndo(BufferView * bv, Undo::undo_kind kind,
-            Paragraph const * first, Paragraph const * behind)
+            ParagraphList::iterator first, ParagraphList::iterator behind)
 {
        if (!undo_frozen) {
                shared_ptr<Undo> u;
@@ -476,7 +481,7 @@ void setUndo(BufferView * bv, Undo::undo_kind kind,
 
 
 void setRedo(BufferView * bv, Undo::undo_kind kind,
-            Paragraph const * first, Paragraph const * behind)
+            ParagraphList::iterator first, ParagraphList::iterator behind)
 {
        shared_ptr<Undo> u;
        if (createUndo(bv, kind, first, behind, u))
@@ -486,6 +491,6 @@ void setRedo(BufferView * bv, Undo::undo_kind kind,
 
 void setCursorParUndo(BufferView * bv)
 {
-       setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
+       setUndo(bv, Undo::FINISH, &*bv->text->cursor.par(),
                bv->text->cursor.par()->next());
 }