]> 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 577293af301aa96c27998fc8012158208664aca8..3c63678097bc8df029a14be91719615166b8abf4 100644 (file)
@@ -9,15 +9,12 @@
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "undo_funcs.h"
 #include "lyxtext.h"
 #include "BufferView.h"
 #include "buffer.h"
-#include "insets/inset.h"
+#include "insets/updatableinset.h"
+#include "insets/insettext.h"
 #include "debug.h"
 #include "support/LAssert.h"
 
@@ -31,7 +28,7 @@ using boost::shared_ptr;
 
 /// the flag used by FinishUndo();
 bool undo_finished;
-/// FIXME
+/// whether actions are not added to the undo stacks
 bool undo_frozen;
 
 namespace {
@@ -43,7 +40,7 @@ LyXCursor const & undoCursor(BufferView * bv)
                return bv->theLockingInset()->cursor(bv);
        return bv->text->cursor;
 }
+
 /**
  * returns a pointer to the very first Paragraph depending of where we are
  * so it will return the first paragraph of the buffer or the first paragraph
@@ -57,7 +54,7 @@ Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
                if (result)
                        return result;
        }
-       return bv->text->ownerParagraph();
+       return &*bv->text->ownerParagraphs().begin();
 }
 
 
@@ -70,20 +67,18 @@ void finishNoUndo(BufferView * bv)
        freezeUndo();
        bv->unlockInset(bv->theLockingInset());
        finishUndo();
-       bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
+       bv->text->postPaint(0);
        unFreezeUndo();
 }
+
+
 // returns false if no undo possible
 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;
 
@@ -100,12 +95,12 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                                num = -1;
                        }
                }
-               t->setCursorIntern(bv, firstUndoParagraph(bv, num), 0);
+               t->setCursorIntern(firstUndoParagraph(bv, num), 0);
        }
 
        // replace the paragraphs with the undo informations
 
-       Paragraph * tmppar3 = undo.par;
+       Paragraph * undopar = undo.par;
        undo.par = 0;   /* otherwise the undo destructor would
                           delete the paragraph */
 
@@ -113,93 +108,121 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
        // paragraph if there is any. This is not needed if we don't have
        // a paragraph before because then in is automatically done in the
        // function which assigns the first paragraph to an InsetText. (Jug)
-       Paragraph * tmppar4 = tmppar3;
-       if (tmppar4) {
+       Paragraph * lastundopar = undopar;
+       if (lastundopar) {
                Inset * in = 0;
                if (before)
                        in = before->inInset();
                else if (undo.number_of_inset_id >= 0)
                        in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
-               tmppar4->setInsetOwner(in);
-               while (tmppar4->next()) {
-                       tmppar4 = tmppar4->next();
-                       tmppar4->setInsetOwner(in);
+               lastundopar->setInsetOwner(in);
+               while (lastundopar->next()) {
+                       lastundopar = lastundopar->next();
+                       lastundopar->setInsetOwner(in);
                }
        }
 
        vector<Paragraph *> deletelist;
+
        Paragraph * deletepar;
+
        // now add old paragraphs to be deleted
        if (before != behind || (!behind && !before)) {
                if (before)
                        deletepar = before->next();
                else
                        deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
-               tmppar2 = tmppar3;
+               tmppar2 = undopar;
                while (deletepar && deletepar != behind) {
                        deletelist.push_back(deletepar);
                        tmppar = deletepar;
                        deletepar = deletepar->next();
+
                        // a memory optimization for edit:
                        // Only layout information
                        // is stored in the undo. So restore
                        // the text informations.
                        if (undo.kind == Undo::EDIT) {
-                               tmppar2->setContentsFromPar(tmppar);
+                               tmppar2->setContentsFromPar(*tmppar);
                                tmppar2 = tmppar2->next();
                        }
                }
        }
 
+       // The order here is VERY IMPORTANT. We have to set the right
+       // next/prev pointer in the paragraphs so that a rebuild of
+       // the LyXText works!!!
+
+       // thread the end of the undo onto the par in front if any
+       if (lastundopar) {
+               lastundopar->next(behind);
+               if (behind)
+                       behind->previous(lastundopar);
+       }
+
        // put the new stuff in the list if there is one
-       if (tmppar3) {
+       if (undopar) {
+               undopar->previous(before);
                if (before)
-                       before->next(tmppar3);
-               else
-                       bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
-                                                tmppar3);
-
-               tmppar3->previous(before);
+                       before->next(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);
-                       tmppar3 = 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;
                }
        }
-       if (tmppar4) {
-               tmppar4->next(behind);
-               if (behind)
-                       behind->previous(tmppar4);
-       }
 
 
        // Set the cursor for redoing
-       if (before) {
+       if (before) { // if we have a par before the undopar
                Inset * it = before->inInset();
                if (it)
-                       it->getLyXText(bv)->setCursorIntern(bv, before, 0);
+                       it->getLyXText(bv)->setCursorIntern(before, 0);
+               else
+                       bv->text->setCursorIntern(before, 0);
+       }
+// we are not ready for this we cannot set the cursor for a paragraph
+// which is not already in a row of LyXText!!!
+#if 0
+       else { // otherwise this is the first one and we start here
+               Inset * it = undopar->inInset();
+               if (it)
+                       it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
                else
-                       bv->text->setCursorIntern(bv, before, 0);
+                       bv->text->setCursorIntern(bv, undopar, 0);
        }
+#endif
 
        Paragraph * endpar = 0;
        // calculate the endpar for redoing the paragraphs.
        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 (tmppar3)
-               it = static_cast<UpdatableInset*>(tmppar3->inInset());
+       if (undopar)
+               it = static_cast<UpdatableInset*>(undopar->inInset());
        if (it) {
-               it->getLyXText(bv)->redoParagraphs(bv,
+               it->getLyXText(bv)->redoParagraphs(
                                                   it->getLyXText(bv)->cursor,
                                                   endpar);
                if (tmppar) {
@@ -211,19 +234,19 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                        } else {
                                t = bv->text;
                        }
-                       t->setCursorIntern(bv, tmppar, undo.cursor_pos);
+                       t->setCursorIntern(tmppar, undo.cursor_pos);
                        // clear any selection and set the selection cursor
                        // for an evt. new selection.
                        t->clearSelection();
-                       t->selection.cursor = t->cursor; 
-                       t->updateCounters(bv, t->cursor.row());
+                       t->selection.cursor = t->cursor;
+                       t->updateCounters();
                        bv->fitCursor();
                }
-               bv->updateInset(it, false);
-               bv->text->setCursorIntern(bv, bv->text->cursor.par(),
+               bv->updateInset(it);
+               bv->text->setCursorIntern(bv->text->cursor.par(),
                                          bv->text->cursor.pos());
        } else {
-               bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
+               bv->text->redoParagraphs(bv->text->cursor, endpar);
                if (tmppar) {
                        LyXText * t;
                        Inset * it = tmppar->inInset();
@@ -233,12 +256,12 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                        } else {
                                t = bv->text;
                        }
-                       t->setCursorIntern(bv, tmppar, undo.cursor_pos);
+                       t->setCursorIntern(tmppar, undo.cursor_pos);
                        // clear any selection and set the selection cursor
                        // for an evt. new selection.
                        t->clearSelection();
-                       t->selection.cursor = t->cursor; 
-                       t->updateCounters(bv, t->cursor.row());
+                       t->selection.cursor = t->cursor;
+                       t->updateCounters();
                }
        }
 
@@ -251,16 +274,19 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                                delete (*pit);
                        }
                }
+
        finishUndo();
-       bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
+       bv->text->postPaint(0);
        return true;
 }
 
+
 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;
@@ -275,7 +301,7 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
                inset_id = first->inInset()->id();
 
        Buffer * b = bv->buffer();
+
        // Undo::EDIT  and Undo::FINISH are
        // always finished. (no overlapping there)
        // overlapping only with insert and delete inside one paragraph:
@@ -293,20 +319,13 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
                        return false;
                }
        }
+
        // 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))))
@@ -324,13 +343,7 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
 
                while (tmppar != end && tmppar->next()) {
                        tmppar = tmppar->next();
-// FIXME: what does this #if 0 mean ?
-#if 0
                        tmppar2->next(new Paragraph(*tmppar, true));
-#else
-                       Paragraph * ptmp = new Paragraph(*tmppar, true);
-                       tmppar2->next(ptmp);
-#endif
                        // a memory optimization: Just store the layout
                        // information when only edit
                        if (kind == Undo::EDIT) {
@@ -341,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,
@@ -382,18 +394,18 @@ void unFreezeUndo()
 bool textUndo(BufferView * bv)
 {
        Buffer * b = bv->buffer();
+
        if (b->undostack.empty()) {
                finishNoUndo(bv);
                return false;
        }
+
        shared_ptr<Undo> undo = b->undostack.top();
        b->undostack.pop();
        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)
@@ -405,7 +417,7 @@ bool textUndo(BufferView * bv)
                                b->redostack.push(u);
                }
        }
+
        // now we can unlock the inset for saftey because the inset pointer could
        // be changed during the undo-function. Anyway if needed we have to lock
        // the right inset/position if this is requested.
@@ -426,13 +438,13 @@ bool textRedo(BufferView * bv)
                finishNoUndo(bv);
                return false;
        }
+
        shared_ptr<Undo> undo = b->redostack.top();
        b->redostack.pop();
        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)
@@ -440,11 +452,11 @@ 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);
                }
        }
+
        // now we can unlock the inset for saftey because the inset pointer could
        // be changed during the undo-function. Anyway if needed we have to lock
        // the right inset/position if this is requested.
@@ -457,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;
@@ -469,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))
@@ -479,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());
 }