]> git.lyx.org Git - lyx.git/blobdiff - src/Undo.cpp
Fixup c7496a11: test emptiness before accessing elements
[lyx.git] / src / Undo.cpp
index a479728ebde2b280e519e3c09734f292f709386f..693008268a159b2753359ddfcee1a02954dfaa36 100644 (file)
@@ -18,6 +18,7 @@
 #include "Undo.h"
 
 #include "Buffer.h"
+#include "BufferList.h"
 #include "BufferParams.h"
 #include "buffer_funcs.h"
 #include "Cursor.h"
@@ -40,6 +41,7 @@
 
 #include <algorithm>
 #include <deque>
+#include <set>
 
 using namespace std;
 using namespace lyx::support;
@@ -470,6 +472,7 @@ void Undo::Private::doTextUndoOrRedo(CursorData & cur, UndoElementStack & stack,
                //LYXERR0("undo.array: " << *undo.array);
                LBUFERR(undo.array);
                dit.cell().swap(*undo.array);
+               dit.inset().setBuffer(buffer_);
                delete undo.array;
                undo.array = 0;
        } else {
@@ -497,6 +500,15 @@ void Undo::Private::doTextUndoOrRedo(CursorData & cur, UndoElementStack & stack,
                for (; pit != end; ++pit)
                        pit->setInsetOwner(dit.realInset());
                plist.insert(first, undo.pars->begin(), undo.pars->end());
+
+               // set the buffers for insets we created
+               ParagraphList::iterator fpit = plist.begin();
+               advance(fpit, undo.from);
+               ParagraphList::iterator fend = fpit;
+               advance(fend, undo.pars->size());
+               for (; fpit != fend; ++fpit)
+                       fpit->setInsetBuffers(buffer_);
+
                delete undo.pars;
                undo.pars = 0;
        }
@@ -531,9 +543,6 @@ bool Undo::Private::textUndoOrRedo(CursorData & cur, bool isUndoOperation)
        const size_t gid = stack.top().group_id;
        while (!stack.empty() && stack.top().group_id == gid)
                doTextUndoOrRedo(cur, stack, otherstack);
-
-       // Adapt the new material to current buffer.
-       buffer_.setBuffersForInsets(); // FIXME This shouldn't be here.
        return true;
 }
 
@@ -562,7 +571,8 @@ void Undo::beginUndoGroup()
        if (d->group_level_ == 0) {
                // create a new group
                ++d->group_id_;
-               LYXERR(Debug::UNDO, "+++++++Creating new group " << d->group_id_);
+               LYXERR(Debug::UNDO, "+++++++ Creating new group " << d->group_id_
+                      << " for buffer " << &d->buffer_);
        }
        ++d->group_level_;
 }
@@ -586,7 +596,8 @@ void Undo::endUndoGroup()
        if (d->group_level_ == 0) {
                // real end of the group
                d->group_cur_before_ = CursorData();
-               LYXERR(Debug::UNDO, "-------End of group " << d->group_id_);
+               LYXERR(Debug::UNDO, "------- End of group " << d->group_id_
+                      << " of buffer " << &d->buffer_);
        }
 }
 
@@ -599,6 +610,14 @@ void Undo::endUndoGroup(CursorData const & cur_after)
 }
 
 
+bool Undo::activeUndoGroup() const
+{
+       return d->group_level_ > 0
+               && !d->undostack_.empty()
+               && d->undostack_.top().group_id == d->group_id_;
+}
+
+
 void Undo::recordUndo(CursorData const & cur, UndoKind kind)
 {
        d->recordUndo(kind, cur, cur.pit(), cur.pit(), cur);
@@ -643,22 +662,32 @@ void Undo::recordUndoFullBuffer(CursorData const & cur)
 
 /// UndoGroupHelper class stuff
 
-/** FIXME: handle restarted groups
- * It may happen that the buffers are visited in order buffer1,
- * buffer2, buffer1. In this case, we want to have only one undo group
- * in buffer1. One solution is to replace buffer_ with a set<Buffer*>,
- * but I am not sure yet how to do it. A use case is
- * InsetLabel::updateReferences.
- */
+class UndoGroupHelper::Impl {
+       friend class UndoGroupHelper;
+       set<Buffer *> buffers_;
+};
+
+
+UndoGroupHelper::UndoGroupHelper(Buffer * buf) : d(new UndoGroupHelper::Impl)
+{
+       resetBuffer(buf);
+}
+
+
+UndoGroupHelper::~UndoGroupHelper()
+{
+       for (Buffer * buf : d->buffers_)
+               if (theBufferList().isLoaded(buf) || theBufferList().isInternal(buf))
+                       buf->undo().endUndoGroup();
+       delete d;
+}
+
 void UndoGroupHelper::resetBuffer(Buffer * buf)
 {
-       if (buf == buffer_)
-               return;
-       if (buffer_)
-               buffer_->undo().endUndoGroup();
-       buffer_ = buf;
-       if (buffer_)
-               buffer_->undo().beginUndoGroup();
+       if (buf && d->buffers_.count(buf) == 0) {
+               d->buffers_.insert(buf);
+               buf->undo().beginUndoGroup();
+       }
 }