]> git.lyx.org Git - lyx.git/blobdiff - src/undo_funcs.C
Alfredo's second patch
[lyx.git] / src / undo_funcs.C
index 6235df6fa9b6c19f88ebc0bbbfccada51a167bff..d0d9571881421bea5fd5dc07114d3b7cf39ab366 100644 (file)
@@ -77,12 +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 * tmppar;
-       Paragraph * tmppar2;
+       Paragraph * const before = &*b->getParFromID(undo.number_of_before_par);
+       Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
 
        // if there's no before take the beginning
        // of the document for redoing
@@ -104,7 +100,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
 
        Paragraph * undopar = undo.par;
        undo.par = 0;   /* otherwise the undo destructor would
-                          delete the paragraph */
+                        delete the paragraph */
 
        // get last undo par and set the right(new) inset-owner of the
        // paragraph if there is any. This is not needed if we don't have
@@ -126,18 +122,17 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
 
        vector<Paragraph *> deletelist;
 
-       Paragraph * deletepar;
-
        // now add old paragraphs to be deleted
        if (before != behind || (!behind && !before)) {
+               Paragraph * deletepar;
                if (before)
                        deletepar = before->next();
                else
                        deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
-               tmppar2 = undopar;
+               Paragraph * tmppar2 = undopar;
                while (deletepar && deletepar != behind) {
                        deletelist.push_back(deletepar);
-                       tmppar = deletepar;
+                       Paragraph * tmppar = deletepar;
                        deletepar = deletepar->next();
 
                        // a memory optimization for edit:
@@ -145,7 +140,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();
                        }
                }
@@ -169,7 +164,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                        before->next(undopar);
                else {
                        int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
-                       Paragraph * op = bv->buffer()->getParFromID(id);
+                       Paragraph * op = &*bv->buffer()->getParFromID(id);
                        if (op && op->inInset()) {
                                static_cast<InsetText*>(op->inInset())->paragraph(undopar);
                        } else {
@@ -182,7 +177,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                // one is the first!
                if (!before && behind) {
                        int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
-                       Paragraph * op = bv->buffer()->getParFromID(id);
+                       Paragraph * op = &*bv->buffer()->getParFromID(id);
                        if (op && op->inInset()) {
                                static_cast<InsetText*>(op->inInset())->paragraph(behind);
                        } else {
@@ -219,14 +214,15 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
        if (behind)
                endpar = behind->next();
 
-       tmppar = bv->buffer()->getParFromID(undo.number_of_cursor_par);
-       UpdatableInset* it = 0;
+       UpdatableInset * it = 0;
        if (undopar)
                it = static_cast<UpdatableInset*>(undopar->inInset());
        if (it) {
                it->getLyXText(bv)->redoParagraphs(
                                                   it->getLyXText(bv)->cursor,
                                                   endpar);
+               Paragraph * tmppar =
+                       &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
                if (tmppar) {
                        it = static_cast<UpdatableInset*>(tmppar->inInset());
                        LyXText * t;
@@ -249,6 +245,8 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                                          bv->text->cursor.pos());
        } else {
                bv->text->redoParagraphs(bv->text->cursor, endpar);
+               Paragraph * tmppar =
+                       &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
                if (tmppar) {
                        LyXText * t;
                        Inset * it = tmppar->inInset();
@@ -284,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;
@@ -320,9 +321,9 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
        }
 
        // create a new Undo
-       Paragraph * undopar;
+       Paragraph * undopar = 0; // nothing to replace (undo of delete maybe)
 
-       Paragraph * start = const_cast<Paragraph *>(first);
+       Paragraph * start = first;
        Paragraph * end = 0;
 
        if (behind)
@@ -360,11 +361,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,
@@ -374,45 +374,25 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
        return true;
 }
 
-} // namespace anon
-
-void finishUndo()
-{
-       // makes sure the next operation will be stored
-       undo_finished = true;
-}
-
-
-void freezeUndo()
-{
-       // this is dangerous and for internal use only
-       undo_frozen = true;
-}
-
-
-void unFreezeUndo()
-{
-       // this is dangerous and for internal use only
-       undo_frozen = false;
-}
-
 
 // returns false if no undo possible
-bool textUndo(BufferView * bv)
+bool textUndoOrRedo(BufferView * bv,
+       limited_stack<boost::shared_ptr<Undo> > & stack,
+       limited_stack<boost::shared_ptr<Undo> > & otherstack)
 {
        Buffer * b = bv->buffer();
 
-       if (b->undostack.empty()) {
+       if (stack.empty()) {
                finishNoUndo(bv);
                return false;
        }
 
-       shared_ptr<Undo> undo = b->undostack.top();
-       b->undostack.pop();
+       shared_ptr<Undo> undo = stack.top();
+       stack.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)
@@ -420,8 +400,8 @@ bool textUndo(BufferView * bv)
                if (first) {
                        shared_ptr<Undo> u;
                        if (createUndo(bv, undo->kind, first,
-                               b->getParFromID(undo->number_of_behind_par), u))
-                               b->redostack.push(u);
+                                            b->getParFromID(undo->number_of_behind_par), u))
+                               otherstack.push(u);
                }
        }
 
@@ -435,48 +415,43 @@ bool textUndo(BufferView * bv)
        return ret;
 }
 
+} // namespace anon
 
-// returns false if no redo possible
-bool textRedo(BufferView * bv)
+void finishUndo()
 {
-       Buffer * b = bv->buffer();
+       // makes sure the next operation will be stored
+       undo_finished = true;
+}
 
-       if (b->redostack.empty()) {
-               finishNoUndo(bv);
-               return false;
-       }
 
-       shared_ptr<Undo> undo = b->redostack.top();
-       b->redostack.pop();
-       finishUndo();
+void freezeUndo()
+{
+       // this is dangerous and for internal use only
+       undo_frozen = true;
+}
 
-       if (!undo_frozen) {
-       Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
-               if (first && first->next())
-                       first = first->next();
-               else if (!first)
-                       first = firstUndoParagraph(bv, undo->number_of_inset_id);
-               if (first) {
-                       shared_ptr<Undo> u;
-                       if (createUndo(bv, undo->kind, first,
-                               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.
-       freezeUndo();
-       bv->unlockInset(bv->theLockingInset());
-       bool ret = textHandleUndo(bv, *undo.get());
-       unFreezeUndo();
-       return ret;
+void unFreezeUndo()
+{
+       // this is dangerous and for internal use only
+       undo_frozen = false;
+}
+
+
+bool textUndo(BufferView * bv)
+{
+       return textUndoOrRedo(bv, bv->buffer()->undostack, bv->buffer()->redostack);
+}
+
+
+bool textRedo(BufferView * bv)
+{
+       return textUndoOrRedo(bv, bv->buffer()->redostack, bv->buffer()->undostack);
 }
 
 
 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;
@@ -488,7 +463,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))
@@ -498,6 +473,6 @@ void setRedo(BufferView * bv, Undo::undo_kind kind,
 
 void setCursorParUndo(BufferView * bv)
 {
-       setUndo(bv, Undo::FINISH, &*bv->text->cursor.par(),
-               bv->text->cursor.par()->next());
+       setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
+               boost::next(bv->text->cursor.par()));
 }