#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"
#include "mathed/MathData.h"
#include "insets/Inset.h"
+#include "insets/InsetText.h"
#include "support/debug.h"
#include "support/gettext.h"
#include <algorithm>
#include <deque>
+#include <set>
using namespace std;
using namespace lyx::support;
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_;
};
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()) {
void Undo::Private::doRecordUndoBufferParams(CursorData const & cur_before,
- UndoElementStack & stack)
+ UndoElementStack & stack)
{
if (!group_level_) {
LYXERR0("There is no group open (creating one)");
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
// 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
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;
}
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;
}
}
+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) {
--d->group_level_;
if (d->group_level_ == 0) {
// real end of the group
+ d->group_cur_before_ = CursorData();
LYXERR(Debug::UNDO, "-------End of group " << d->group_id_);
}
}
-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;
}
/// 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_)
+ 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();
+ }
}