]> git.lyx.org Git - lyx.git/blobdiff - src/Undo.cpp
Fix quotation marks in RTL script
[lyx.git] / src / Undo.cpp
index 772b9d4745126629e62bf064af10e980ae8aa5d1..33ab923e17f9b18cd5e733936e108c0a536e2c10 100644 (file)
 #include "Undo.h"
 
 #include "Buffer.h"
+#include "BufferList.h"
 #include "BufferParams.h"
 #include "buffer_funcs.h"
 #include "Cursor.h"
+#include "CutAndPaste.h"
+#include "ErrorList.h"
 #include "Paragraph.h"
 #include "ParagraphList.h"
 #include "Text.h"
@@ -29,6 +32,7 @@
 #include "mathed/MathData.h"
 
 #include "insets/Inset.h"
+#include "insets/InsetText.h"
 
 #include "support/debug.h"
 #include "support/gettext.h"
@@ -37,6 +41,7 @@
 
 #include <algorithm>
 #include <deque>
+#include <set>
 
 using namespace std;
 using namespace lyx::support;
@@ -235,6 +240,8 @@ struct Undo::Private
        size_t group_id_;
        /// Current group nesting nevel.
        size_t group_level_;
+       /// the position of cursor before the group was created
+       CursorData group_cur_before_;
 };
 
 
@@ -340,8 +347,9 @@ void Undo::Private::doRecordUndo(UndoKind kind,
 
        LYXERR(Debug::UNDO, "Create undo element of group " << group_id_);
        // create the position information of the Undo entry
-       UndoElement undo(kind, cur_before, cell, from, end, 0, 0,
-                        buffer_.isClean(), group_id_);
+       UndoElement undo(kind,
+             group_cur_before_.empty() ? cur_before : group_cur_before_,
+             cell, from, end, 0, 0, buffer_.isClean(), group_id_);
 
        // fill in the real data to be saved
        if (cell.inMathed()) {
@@ -390,7 +398,7 @@ void Undo::Private::recordUndo(UndoKind kind,
 
 
 void Undo::Private::doRecordUndoBufferParams(CursorData const & cur_before,
-                                                                          UndoElementStack & stack)
+                                             UndoElementStack & stack)
 {
        if (!group_level_) {
                LYXERR0("There is no group open (creating one)");
@@ -399,7 +407,8 @@ void Undo::Private::doRecordUndoBufferParams(CursorData const & cur_before,
 
        LYXERR(Debug::UNDO, "Create full buffer undo element of group " << group_id_);
        // create the position information of the Undo entry
-       UndoElement undo(cur_before, buffer_.params(), buffer_.isClean(),
+       UndoElement undo(group_cur_before_.empty() ? cur_before : group_cur_before_,
+                     buffer_.params(), buffer_.isClean(),
                                         group_id_);
 
        // push the undo entry to undo stack
@@ -449,7 +458,13 @@ void Undo::Private::doTextUndoOrRedo(CursorData & cur, UndoElementStack & stack,
                // This is a params undo element
                delete otherstack.top().bparams;
                otherstack.top().bparams = new BufferParams(buffer_.params());
+               DocumentClassConstPtr olddc = buffer_.params().documentClassPtr();
                buffer_.params() = *undo.bparams;
+               // The error list is not supposed to be helpful here.
+               ErrorList el;
+               cap::switchBetweenClasses(olddc, buffer_.params().documentClassPtr(),
+                       static_cast<InsetText &>(buffer_.inset()), el);
+               LATTEST(el.empty());
        } else if (dit.inMathed()) {
                // We stored the full cell here as there is not much to be
                // gained by storing just 'a few' paragraphs (most if not
@@ -457,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 {
@@ -549,12 +565,21 @@ 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_;
 }
 
 
+void Undo::beginUndoGroup(CursorData const & cur_before)
+{
+       beginUndoGroup();
+       if (d->group_cur_before_.empty())
+               d->group_cur_before_ = cur_before;
+}
+
+
 void Undo::endUndoGroup()
 {
        if (d->group_level_ == 0) {
@@ -564,16 +589,26 @@ void Undo::endUndoGroup()
        --d->group_level_;
        if (d->group_level_ == 0) {
                // real end of the group
-               LYXERR(Debug::UNDO, "-------End of group " << d->group_id_);
+               d->group_cur_before_ = CursorData();
+               LYXERR(Debug::UNDO, "------- End of group " << d->group_id_
+                      << " of buffer " << &d->buffer_);
        }
 }
 
 
-void Undo::endUndoGroup(CursorData const & cur)
+void Undo::endUndoGroup(CursorData const & cur_after)
 {
        endUndoGroup();
        if (!d->undostack_.empty() && d->undostack_.top().cur_after.empty())
-               d->undostack_.top().cur_after = cur;
+               d->undostack_.top().cur_after = cur_after;
+}
+
+
+bool Undo::activeUndoGroup() const
+{
+       return d->group_level_ > 0
+               && !d->undostack_.empty()
+               && d->undostack_.top().group_id == d->group_id_;
 }
 
 
@@ -621,15 +656,32 @@ void Undo::recordUndoFullBuffer(CursorData const & cur)
 
 /// UndoGroupHelper class stuff
 
+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();
+       }
 }