]> git.lyx.org Git - lyx.git/blobdiff - src/undo_funcs.C
The "I want this in now" patch.
[lyx.git] / src / undo_funcs.C
index 63cbe37fc91a8a3794116fa191c76df248e8472e..da7255979a16f0707ab8e07fa86fc92b3160b72c 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "undo_funcs.h"
 #include "lyxtext.h"
+#include "funcrequest.h"
 #include "BufferView.h"
 #include "buffer.h"
 #include "insets/updatableinset.h"
@@ -77,12 +78,13 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
 {
        Buffer * b = bv->buffer();
 
-       Paragraph * const before = &*b->getParFromID(undo.number_of_before_par);
-       Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
+       ParIterator const before = b->getParFromID(undo.number_of_before_par);
+       ParIterator const behind = b->getParFromID(undo.number_of_behind_par);
+       ParIterator const end    = b->par_iterator_end();
 
        // If there's no before take the beginning
        // of the document for redoing.
-       if (!before) {
+       if (before == end) {
                LyXText * t = bv->text;
                int num = undo.number_of_inset_id;
                if (undo.number_of_inset_id >= 0) {
@@ -96,10 +98,10 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
                t->setCursorIntern(undoParagraphs(bv, num).begin(), 0);
        }
 
-       // Set the right(new) inset-owner of the paragraph if there is any. 
+       // Set the right(new) inset-owner of the paragraph if there is any.
        if (!undo.pars.empty()) {
                Inset * in = 0;
-               if (before)
+               if (before != end)
                        in = before->inInset();
                else if (undo.number_of_inset_id >= 0)
                        in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
@@ -111,17 +113,20 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
        vector<Paragraph *> deletelist;
 
        // Now add old paragraphs to be deleted.
-       if (before != behind || (!behind && !before)) {
-               Paragraph * deletepar;
-               if (before)
-                       deletepar = before->next();
-               else
-                       deletepar = &undoParagraphs(bv, undo.number_of_inset_id).front();
+       if (before != behind || (behind == end && before == end)) {
+               ParagraphList::iterator deletepar;
+               if (before != end) {
+                       deletepar = *before;
+                       ++deletepar;
+               } else {
+                       deletepar = undoParagraphs(bv, undo.number_of_inset_id).begin();
+               }
                // this surprisingly fills the undo! (Andre')
                size_t par = 0;
-               while (deletepar && deletepar != behind) {
-                       deletelist.push_back(deletepar);
-                       deletepar = deletepar->next();
+               //while (deletepar && deletepar != *behind)
+               while (deletepar != *behind) {
+                       deletelist.push_back(&*deletepar);
+                       ++deletepar;
 
                        // A memory optimization for edit:
                        // Only layout information
@@ -140,51 +145,58 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
 
        // Thread the end of the undo onto the par in front if any.
        if (!undo.pars.empty()) {
-               undo.pars.back()->next(behind);
-               if (behind)
-                       behind->previous(undo.pars.back());
+#warning FIXME
+               //undo.pars.back()->next(&**behind);
+               //if (behind != end)
+               //(&**behind)->previous(undo.pars.back());
        }
 
        // Put the new stuff in the list if there is one.
        Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front();
        if (!undo.pars.empty()) {
-               undo.pars.front()->previous(before);
-               if (before)
-                       before->next(undopar);
-               else {
+#warning FIXME
+               //undo.pars.front()->previous(&**before);
+               if (before != end) {
+#warning FIXME
+                       //(&**before)->next(undopar);
+               } else {
                        int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
-                       Paragraph * op = &*bv->buffer()->getParFromID(id);
-                       if (op && op->inInset()) {
-                               static_cast<InsetText*>(op->inInset())->paragraph(undopar);
+                       ParIterator op = bv->buffer()->getParFromID(id);
+                       if (op != end && op->inInset()) {
+#warning FIXME reimplementaion needed here
+                               //static_cast<InsetText*>(op->inInset())->paragraph(undopar);
                        } else {
-                               bv->buffer()->paragraphs.set(undopar);
+#warning FIXME reimplementation needed here
+                               //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) {
+               if (before == end && behind != end) {
                        int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
-                       Paragraph * op = &*bv->buffer()->getParFromID(id);
-                       if (op && op->inInset()) {
-                               static_cast<InsetText*>(op->inInset())->paragraph(behind);
+                       ParIterator op = bv->buffer()->getParFromID(id);
+                       if (op != end && op->inInset()) {
+#warning FIXME reimplementation needed here
+                               //static_cast<InsetText*>(op->inInset())->paragraph(&**behind);
                        } else {
-                               bv->buffer()->paragraphs.set(behind);
+#warning FIXME reimplementation needed here
+                               //bv->buffer()->paragraphs.set(&**behind);
                        }
-                       undopar = behind;
+                       undopar = &**behind;
                }
        }
 
 
        // Set the cursor for redoing.
        // If we have a par before the undopar.
-       if (before) {
+       if (before != end) {
                Inset * it = before->inInset();
                if (it)
-                       it->getLyXText(bv)->setCursorIntern(before, 0);
+                       it->getLyXText(bv)->setCursorIntern(*before, 0);
                else
-                       bv->text->setCursorIntern(before, 0);
+                       bv->text->setCursorIntern(*before, 0);
        }
 
 // we are not ready for this we cannot set the cursor for a paragraph
@@ -199,68 +211,55 @@ bool textHandleUndo(BufferView * bv, Undo & undo)
        }
 #endif
 
-       Paragraph * endpar = 0;
-
-       // Calculate the endpar for redoing the paragraphs.
-       if (behind)
-               endpar = behind->next();
-
        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;
-                       if (it) {
-                               it->edit(bv);
-                               t = it->getLyXText(bv);
-                       } else {
-                               t = bv->text;
-                       }
-                       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->fitCursor();
+
+       LyXText * text = it ? it->getLyXText(bv) : bv->text;
+
+       ParagraphList::iterator endpar = text->ownerParagraphs().end();
+
+       // Calculate the endpar for redoing the paragraphs.
+       if (behind != end) {
+               endpar = *behind;
+               ++endpar;
+       }
+
+       text->redoParagraphs(text->cursor, endpar);
+       ParIterator tmppar =
+               bv->buffer()->getParFromID(undo.number_of_cursor_par);
+
+       if (tmppar != end) {
+               LyXText * t;
+               Inset * it = tmppar->inInset();
+               if (it) {
+                       FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
+                       it->localDispatch(cmd);
+                       t = it->getLyXText(bv);
+               } else {
+                       t = bv->text;
                }
+               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();
+       }
+
+       if (it) {
+               bv->fitCursor();
                bv->updateInset(it);
                bv->text->setCursorIntern(bv->text->cursor.par(),
                                          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();
-                       if (it) {
-                               it->edit(bv);
-                               t = it->getLyXText(bv);
-                       } else {
-                               t = bv->text;
-                       }
-                       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();
-               }
        }
 
        // And here it's safe enough to delete all removed paragraphs.
        vector<Paragraph *>::iterator pit = deletelist.begin();
        for(; pit != deletelist.end(); ++pit) {
-               (*pit)->previous(0);
-               (*pit)->next(0);
+#warning FIXME
+               //(*pit)->previous(0);
+               //(*pit)->next(0);
                delete (*pit);
        }
 
@@ -285,8 +284,9 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
        int behind_number = -1;
        int inset_id = -1;
 
-       if (first->previous())
-               before_number = first->previous()->id();
+#warning FIXME
+       //if (first->previous())
+       //      before_number = first->previous()->id();
        if (behind)
                behind_number = behind->id();
        if (first->inInset())
@@ -317,40 +317,43 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
 
        Paragraph const * end = 0;
 
-       if (behind)
-               end = behind->previous();
-       else {
+       if (behind) {
+#warning FIXME
+               //end = behind->previous();
+       }else {
                end = first;
-               while (end->next())
-                       end = end->next();
+#warning FIXME
+               //while (end->next())
+               //      end = end->next();
        }
 
-       if (first && end && (first != end->next()) &&
-           ((before_number != behind_number) ||
-                ((before_number < 0) && (behind_number < 0))))
-       {
-               undo_pars.push_back(new Paragraph(*first, true));
-               for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
-                       tmppar = tmppar->next();
-                       undo_pars.push_back(new Paragraph(*tmppar, true));
-                       size_t const n = undo_pars.size();
-                       undo_pars[n - 2]->next(undo_pars[n - 1]);
-                       undo_pars[n - 1]->previous(undo_pars[n - 2]);
-               }
-               undo_pars.back()->next(0);
-       }
+#warning FIXME
+//     if (first && end && (first != end->next()) &&
+//         ((before_number != behind_number) ||
+//              ((before_number < 0) && (behind_number < 0))))
+//     {
+//             undo_pars.push_back(new Paragraph(*first, true));
+//             for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
+//                     tmppar = tmppar->next();
+//                     undo_pars.push_back(new Paragraph(*tmppar, true));
+//                     size_t const n = undo_pars.size();
+//                     undo_pars[n - 2]->next(undo_pars[n - 1]);
+//                     undo_pars[n - 1]->previous(undo_pars[n - 2]);
+//             }
+//             undo_pars.back()->next(0);
+//     }
 
        // A memory optimization: Just store the layout
        // information when only edit.
        if (kind == Undo::EDIT) {
                for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
                        undo_pars[i]->clearContents();
-       }               
+       }
 
        int cursor_par = undoCursor(bv).par()->id();
        int cursor_pos = undoCursor(bv).pos();
 
-       //lyxerr << "createUndo: inset_id: " << inset_id << "  before_number: " 
+       //lyxerr << "createUndo: inset_id: " << inset_id << "  before_number: "
        //      << before_number << "  behind_number: " << behind_number << "\n";
        u.reset(new Undo(kind, inset_id,
                before_number, behind_number,
@@ -364,9 +367,9 @@ bool createUndo(BufferView * bv, Undo::undo_kind kind,
 // Returns false if no undo possible.
 bool textUndoOrRedo(BufferView * bv,
        limited_stack<boost::shared_ptr<Undo> > & stack,
-       limited_stack<boost::shared_ptr<Undo> > & otherstack)
+                   limited_stack<boost::shared_ptr<Undo> > & /*otherstack*/)
 {
-       Buffer * b = bv->buffer();
+       //Buffer * b = bv->buffer();
 
        if (stack.empty()) {
                finishNoUndo(bv);
@@ -378,17 +381,25 @@ bool textUndoOrRedo(BufferView * bv,
        finishUndo();
 
        if (!undo_frozen) {
-               Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
-               if (first && first->next())
-                       first = first->next();
-               else if (!first)
-                       first = &*undoParagraphs(bv, undo->number_of_inset_id).begin();
+/*
+               ParIterator p = b->getParFromID(undo->number_of_before_par);
+               bool ok = false;
+               ParagraphList::iterator first;
+               // default constructed?
+               ParIterator const end = b->par_iterator_end();
+               if (p != end) {
+                       first = p.par();
+                       if (first->next())
+                               first = first->next();
+               } else
+                       first = undoParagraphs(bv, undo->number_of_inset_id).begin();
                if (first) {
                        shared_ptr<Undo> u;
-                       if (createUndo(bv, undo->kind, first,
-                                            b->getParFromID(undo->number_of_behind_par), u))
+                       ParIterator behind = b->getParFromID(undo->number_of_behind_par);
+                       if (createUndo(bv, undo->kind, first, behind.par(), u))
                                otherstack.push(u);
                }
+*/
        }
 
        // Now we can unlock the inset for saftey because the inset