2003-08-23 00:17:00 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Undo.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2003-10-14 13:01:49 +00:00
|
|
|
* \author Asger Alstrup
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-10-14 13:01:49 +00:00
|
|
|
* \author John Levon
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
|
|
|
* \author Jürgen Vigna
|
2007-10-18 11:51:17 +00:00
|
|
|
* \author Abdelrazak Younes
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
1999-11-15 12:01:38 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Undo.h"
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
2007-10-18 11:51:17 +00:00
|
|
|
#include "BufferParams.h"
|
2007-01-31 14:36:15 +00:00
|
|
|
#include "buffer_funcs.h"
|
2007-10-18 11:51:17 +00:00
|
|
|
#include "DocIterator.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Paragraph.h"
|
2006-03-23 20:11:06 +00:00
|
|
|
#include "ParagraphList.h"
|
2007-10-18 11:51:17 +00:00
|
|
|
#include "Text.h"
|
2001-05-08 13:28:44 +00:00
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "mathed/MathSupport.h"
|
2007-04-26 16:06:39 +00:00
|
|
|
#include "mathed/MathData.h"
|
2007-03-27 14:33:19 +00:00
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
#include "insets/Inset.h"
|
2004-03-08 21:14:45 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2008-04-16 22:02:26 +00:00
|
|
|
#include "support/debug.h"
|
2008-03-15 00:22:54 +00:00
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
#include <algorithm>
|
2008-04-16 22:02:26 +00:00
|
|
|
#include <deque>
|
2004-03-08 21:14:45 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2008-03-15 00:22:54 +00:00
|
|
|
namespace lyx {
|
2007-10-18 11:51:17 +00:00
|
|
|
|
|
|
|
/**
|
2010-12-08 10:04:07 +00:00
|
|
|
These are the elements put on the undo stack. Each object contains
|
|
|
|
complete paragraphs from some cell and sufficient information to
|
|
|
|
restore the cursor state.
|
2007-10-18 11:51:17 +00:00
|
|
|
|
2010-12-08 10:04:07 +00:00
|
|
|
The cell is given by a DocIterator pointing to this cell, the
|
|
|
|
'interesting' range of paragraphs by counting them from begin and end
|
|
|
|
of cell, respectively.
|
2007-10-18 11:51:17 +00:00
|
|
|
|
2010-12-08 10:04:07 +00:00
|
|
|
The cursor is also given as DocIterator and should point to some place
|
|
|
|
in the stored paragraph range. In case of math, we simply store the
|
|
|
|
whole cell, as there usually is just a simple paragraph in a cell.
|
2007-10-18 11:51:17 +00:00
|
|
|
|
|
|
|
The idea is to store the contents of 'interesting' paragraphs in some
|
|
|
|
structure ('Undo') _before_ it is changed in some edit operation.
|
2010-12-08 10:04:07 +00:00
|
|
|
Obviously, the stored range should be as small as possible. However,
|
|
|
|
there is a lower limit: The StableDocIterator stored in the undo class
|
|
|
|
must be valid after the changes, too, as it will used as a pointer
|
|
|
|
where to insert the stored bits when performining undo.
|
2007-10-18 11:51:17 +00:00
|
|
|
*/
|
|
|
|
struct UndoElement
|
|
|
|
{
|
2008-07-09 18:42:41 +00:00
|
|
|
///
|
|
|
|
UndoElement(UndoKind kin, StableDocIterator const & cur,
|
|
|
|
StableDocIterator const & cel,
|
|
|
|
pit_type fro, pit_type en, ParagraphList * pl,
|
|
|
|
MathData * ar, BufferParams const & bp,
|
2010-08-06 23:54:04 +00:00
|
|
|
bool ifb, bool lc, size_t gid) :
|
2008-07-09 18:42:41 +00:00
|
|
|
kind(kin), cursor(cur), cell(cel), from(fro), end(en),
|
2010-12-08 10:04:07 +00:00
|
|
|
pars(pl), array(ar), bparams(0), isFullBuffer(ifb),
|
|
|
|
lyx_clean(lc), group_id(gid)
|
2008-07-14 17:11:19 +00:00
|
|
|
{
|
|
|
|
if (isFullBuffer)
|
|
|
|
bparams = new BufferParams(bp);
|
|
|
|
}
|
|
|
|
///
|
|
|
|
UndoElement(UndoElement const & ue)
|
|
|
|
{
|
|
|
|
kind = ue.kind;
|
|
|
|
cursor = ue.cursor;
|
|
|
|
cell = ue.cell;
|
|
|
|
from = ue.from;
|
|
|
|
end = ue.end;
|
|
|
|
pars = ue.pars;
|
|
|
|
array = ue.array;
|
|
|
|
bparams = ue.isFullBuffer
|
|
|
|
? new BufferParams(*ue.bparams) : ue.bparams;
|
|
|
|
isFullBuffer = ue.isFullBuffer;
|
2010-08-06 23:54:04 +00:00
|
|
|
lyx_clean = ue.lyx_clean;
|
2008-08-15 19:24:56 +00:00
|
|
|
group_id = ue.group_id;
|
2008-07-14 17:11:19 +00:00
|
|
|
}
|
|
|
|
///
|
|
|
|
~UndoElement()
|
|
|
|
{
|
|
|
|
if (isFullBuffer)
|
|
|
|
delete bparams;
|
|
|
|
}
|
2007-10-18 11:51:17 +00:00
|
|
|
/// Which kind of operation are we recording for?
|
|
|
|
UndoKind kind;
|
|
|
|
/// the position of the cursor
|
|
|
|
StableDocIterator cursor;
|
|
|
|
/// the position of the cell described
|
|
|
|
StableDocIterator cell;
|
|
|
|
/// counted from begin of cell
|
|
|
|
pit_type from;
|
|
|
|
/// complement to end of this cell
|
|
|
|
pit_type end;
|
|
|
|
/// the contents of the saved Paragraphs (for texted)
|
|
|
|
ParagraphList * pars;
|
|
|
|
/// the contents of the saved MathData (for mathed)
|
|
|
|
MathData * array;
|
|
|
|
/// Only used in case of full backups
|
2008-07-14 17:11:19 +00:00
|
|
|
BufferParams const * bparams;
|
2007-10-18 11:51:17 +00:00
|
|
|
/// Only used in case of full backups
|
|
|
|
bool isFullBuffer;
|
2010-08-06 23:54:04 +00:00
|
|
|
/// Was the buffer clean at this point?
|
|
|
|
bool lyx_clean;
|
2008-08-15 19:24:56 +00:00
|
|
|
/// the element's group id
|
|
|
|
size_t group_id;
|
2008-07-09 18:42:41 +00:00
|
|
|
private:
|
|
|
|
/// Protect construction
|
|
|
|
UndoElement();
|
2007-10-18 11:51:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-04-16 22:02:26 +00:00
|
|
|
class UndoElementStack
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// limit is the maximum size of the stack
|
|
|
|
UndoElementStack(size_t limit = 100) { limit_ = limit; }
|
|
|
|
/// limit is the maximum size of the stack
|
|
|
|
~UndoElementStack() { clear(); }
|
|
|
|
|
|
|
|
/// Return the top element.
|
|
|
|
UndoElement & top() { return c_.front(); }
|
|
|
|
|
|
|
|
/// Pop and throw away the top element.
|
|
|
|
void pop() { c_.pop_front(); }
|
|
|
|
|
|
|
|
/// Return true if the stack is empty.
|
|
|
|
bool empty() const { return c_.empty(); }
|
|
|
|
|
|
|
|
/// Clear all elements, deleting them.
|
|
|
|
void clear() {
|
|
|
|
for (size_t i = 0; i != c_.size(); ++i) {
|
|
|
|
delete c_[i].array;
|
|
|
|
delete c_[i].pars;
|
|
|
|
}
|
|
|
|
c_.clear();
|
|
|
|
}
|
|
|
|
|
2008-08-15 19:24:56 +00:00
|
|
|
/// Push an item on to the stack, deleting the bottom group on
|
|
|
|
/// overflow.
|
2008-04-16 22:02:26 +00:00
|
|
|
void push(UndoElement const & v) {
|
|
|
|
c_.push_front(v);
|
2008-08-15 19:24:56 +00:00
|
|
|
if (c_.size() > limit_) {
|
|
|
|
// remove a whole group at once.
|
|
|
|
const size_t gid = c_.back().group_id;
|
|
|
|
while (!c_.empty() && c_.back().group_id == gid)
|
|
|
|
c_.pop_back();
|
|
|
|
}
|
2008-04-16 22:02:26 +00:00
|
|
|
}
|
|
|
|
|
2010-08-06 23:54:04 +00:00
|
|
|
/// Mark all the elements of the stack as dirty
|
|
|
|
void markDirty() {
|
|
|
|
for (size_t i = 0; i != c_.size(); ++i)
|
|
|
|
c_[i].lyx_clean = false;
|
|
|
|
}
|
|
|
|
|
2008-04-16 22:02:26 +00:00
|
|
|
private:
|
|
|
|
/// Internal contents.
|
|
|
|
std::deque<UndoElement> c_;
|
|
|
|
/// The maximum number elements stored.
|
|
|
|
size_t limit_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
struct Undo::Private
|
|
|
|
{
|
2008-08-15 19:24:56 +00:00
|
|
|
Private(Buffer & buffer) : buffer_(buffer), undo_finished_(true),
|
|
|
|
group_id(0), group_level(0) {}
|
2007-10-18 11:51:17 +00:00
|
|
|
|
2008-08-15 19:24:56 +00:00
|
|
|
// Do one undo/redo step
|
2010-12-08 10:04:07 +00:00
|
|
|
void doTextUndoOrRedo(DocIterator & cur, UndoElementStack & stack,
|
|
|
|
UndoElementStack & otherStack);
|
2008-08-15 19:24:56 +00:00
|
|
|
// Apply one undo/redo group. Returns false if no undo possible.
|
2007-10-18 11:51:17 +00:00
|
|
|
bool textUndoOrRedo(DocIterator & cur, bool isUndoOperation);
|
2008-08-15 19:24:56 +00:00
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
///
|
|
|
|
void doRecordUndo(UndoKind kind,
|
|
|
|
DocIterator const & cell,
|
|
|
|
pit_type first_pit,
|
|
|
|
pit_type last_pit,
|
|
|
|
DocIterator const & cur,
|
|
|
|
bool isFullBuffer,
|
2008-08-15 19:24:56 +00:00
|
|
|
UndoElementStack & stack);
|
2007-10-18 11:51:17 +00:00
|
|
|
///
|
|
|
|
void recordUndo(UndoKind kind,
|
2010-12-08 10:04:07 +00:00
|
|
|
DocIterator const & cell,
|
2007-10-18 11:51:17 +00:00
|
|
|
pit_type first_pit,
|
2010-12-08 10:04:07 +00:00
|
|
|
pit_type last_pit,
|
|
|
|
DocIterator const & cur,
|
|
|
|
bool isFullBuffer);
|
2007-10-18 11:51:17 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
Buffer & buffer_;
|
|
|
|
/// Undo stack.
|
2008-04-16 22:02:26 +00:00
|
|
|
UndoElementStack undostack_;
|
2007-10-18 11:51:17 +00:00
|
|
|
/// Redo stack.
|
2008-04-16 22:02:26 +00:00
|
|
|
UndoElementStack redostack_;
|
2007-10-18 11:51:17 +00:00
|
|
|
|
|
|
|
/// The flag used by Undo::finishUndo().
|
2008-04-16 20:37:23 +00:00
|
|
|
bool undo_finished_;
|
2008-08-15 19:24:56 +00:00
|
|
|
|
|
|
|
/// Current group Id.
|
|
|
|
size_t group_id;
|
|
|
|
/// Current group nesting nevel.
|
|
|
|
size_t group_level;
|
2007-10-18 11:51:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-03-15 00:22:54 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Undo
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
Undo::Undo(Buffer & buffer)
|
|
|
|
: d(new Undo::Private(buffer))
|
|
|
|
{}
|
2004-03-27 12:46:30 +00:00
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
Undo::~Undo()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-09 17:55:16 +00:00
|
|
|
void Undo::clear()
|
|
|
|
{
|
|
|
|
d->undostack_.clear();
|
|
|
|
d->redostack_.clear();
|
|
|
|
d->undo_finished_ = true;
|
|
|
|
d->group_id = 0;
|
2011-01-10 09:44:55 +00:00
|
|
|
d->group_level = 0;
|
2011-01-09 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
bool Undo::hasUndoStack() const
|
|
|
|
{
|
2008-04-16 20:37:23 +00:00
|
|
|
return !d->undostack_.empty();
|
2007-10-18 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Undo::hasRedoStack() const
|
|
|
|
{
|
2008-04-16 20:37:23 +00:00
|
|
|
return !d->redostack_.empty();
|
2007-10-18 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2010-08-06 23:54:04 +00:00
|
|
|
void Undo::markDirty()
|
2005-10-13 17:20:30 +00:00
|
|
|
{
|
2010-08-07 10:40:25 +00:00
|
|
|
d->undo_finished_ = true;
|
2010-08-06 23:54:04 +00:00
|
|
|
d->undostack_.markDirty();
|
|
|
|
d->redostack_.markDirty();
|
2005-10-13 17:20:30 +00:00
|
|
|
}
|
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
|
2008-03-15 00:22:54 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Undo::Private
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2005-10-13 17:20:30 +00:00
|
|
|
|
2010-08-06 23:54:04 +00:00
|
|
|
static bool samePar(StableDocIterator const & i1, StableDocIterator const & i2)
|
|
|
|
{
|
|
|
|
StableDocIterator tmpi2 = i2;
|
|
|
|
tmpi2.pos() = i1.pos();
|
|
|
|
return i1 == tmpi2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
void Undo::Private::doRecordUndo(UndoKind kind,
|
2005-07-14 22:09:22 +00:00
|
|
|
DocIterator const & cell,
|
2004-11-24 21:53:46 +00:00
|
|
|
pit_type first_pit, pit_type last_pit,
|
2005-07-14 22:09:22 +00:00
|
|
|
DocIterator const & cur,
|
|
|
|
bool isFullBuffer,
|
2008-08-15 19:24:56 +00:00
|
|
|
UndoElementStack & stack)
|
2003-10-14 13:01:49 +00:00
|
|
|
{
|
2008-08-15 19:24:56 +00:00
|
|
|
if (!group_level) {
|
|
|
|
LYXERR0("There is no group open (creating one)");
|
|
|
|
++group_id;
|
|
|
|
}
|
|
|
|
|
2004-11-24 21:53:46 +00:00
|
|
|
if (first_pit > last_pit)
|
2007-12-12 19:28:07 +00:00
|
|
|
swap(first_pit, last_pit);
|
2007-10-18 11:51:17 +00:00
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
// Undo::ATOMIC are always recorded (no overlapping there).
|
|
|
|
// As nobody wants all removed character appear one by one when undoing,
|
|
|
|
// we want combine 'similar' non-ATOMIC undo recordings to one.
|
2008-07-09 22:59:16 +00:00
|
|
|
pit_type from = first_pit;
|
|
|
|
pit_type end = cell.lastpit() - last_pit;
|
2008-04-16 20:37:23 +00:00
|
|
|
if (!undo_finished_
|
2007-10-18 11:51:17 +00:00
|
|
|
&& kind != ATOMIC_UNDO
|
2004-03-08 21:14:45 +00:00
|
|
|
&& !stack.empty()
|
2008-07-09 22:59:16 +00:00
|
|
|
&& samePar(stack.top().cell, cell)
|
|
|
|
&& stack.top().kind == kind
|
|
|
|
&& stack.top().from == from
|
|
|
|
&& stack.top().end == end)
|
2004-03-08 21:14:45 +00:00
|
|
|
return;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
2008-09-18 10:46:31 +00:00
|
|
|
if (isFullBuffer)
|
|
|
|
LYXERR(Debug::UNDO, "Create full buffer undo element of group " << group_id);
|
|
|
|
else
|
|
|
|
LYXERR(Debug::UNDO, "Create undo element of group " << group_id);
|
2008-07-09 22:59:16 +00:00
|
|
|
// create the position information of the Undo entry
|
|
|
|
UndoElement undo(kind, cur, cell, from, end, 0, 0,
|
2010-08-06 23:54:04 +00:00
|
|
|
buffer_.params(), isFullBuffer, buffer_.isClean(), group_id);
|
2008-07-09 22:59:16 +00:00
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
// fill in the real data to be saved
|
2004-11-05 06:12:21 +00:00
|
|
|
if (cell.inMathed()) {
|
2004-03-08 21:14:45 +00:00
|
|
|
// simply use the whole cell
|
2010-05-04 16:16:20 +00:00
|
|
|
MathData & ar = cell.cell();
|
|
|
|
undo.array = new MathData(ar.buffer(), ar.begin(), ar.end());
|
2004-03-08 21:14:45 +00:00
|
|
|
} else {
|
|
|
|
// some more effort needed here as 'the whole cell' of the
|
2007-04-29 23:33:02 +00:00
|
|
|
// main Text _is_ the whole document.
|
2004-03-01 17:12:09 +00:00
|
|
|
// record the relevant paragraphs
|
2007-04-29 23:33:02 +00:00
|
|
|
Text const * text = cell.text();
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(text, /**/);
|
2005-07-18 11:00:15 +00:00
|
|
|
ParagraphList const & plist = text->paragraphs();
|
|
|
|
ParagraphList::const_iterator first = plist.begin();
|
2004-11-24 21:53:46 +00:00
|
|
|
advance(first, first_pit);
|
2005-07-18 11:00:15 +00:00
|
|
|
ParagraphList::const_iterator last = plist.begin();
|
2004-11-24 21:53:46 +00:00
|
|
|
advance(last, last_pit + 1);
|
2007-03-27 14:33:19 +00:00
|
|
|
undo.pars = new ParagraphList(first, last);
|
2004-03-01 17:12:09 +00:00
|
|
|
}
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2004-04-03 08:37:12 +00:00
|
|
|
// push the undo entry to undo stack
|
2004-03-08 21:14:45 +00:00
|
|
|
stack.push(undo);
|
2007-12-12 19:28:07 +00:00
|
|
|
//lyxerr << "undo record: " << stack.top() << endl;
|
2003-10-14 13:01:49 +00:00
|
|
|
}
|
|
|
|
|
2004-11-30 01:59:49 +00:00
|
|
|
|
2010-12-08 10:04:07 +00:00
|
|
|
void Undo::Private::recordUndo(UndoKind kind,
|
|
|
|
DocIterator const & cell,
|
|
|
|
pit_type first_pit, pit_type last_pit,
|
|
|
|
DocIterator const & cur,
|
|
|
|
bool isFullBuffer)
|
2004-11-05 06:12:21 +00:00
|
|
|
{
|
2010-12-08 10:04:07 +00:00
|
|
|
LASSERT(first_pit <= cell.lastpit(), /**/);
|
|
|
|
LASSERT(last_pit <= cell.lastpit(), /**/);
|
2004-11-05 06:12:21 +00:00
|
|
|
|
2010-12-08 10:04:07 +00:00
|
|
|
doRecordUndo(kind, cell, first_pit, last_pit, cur,
|
|
|
|
isFullBuffer, undostack_);
|
2004-11-05 06:12:21 +00:00
|
|
|
|
2010-12-08 10:04:07 +00:00
|
|
|
// next time we'll try again to combine entries if possible
|
2008-04-16 20:37:23 +00:00
|
|
|
undo_finished_ = false;
|
2010-12-08 10:04:07 +00:00
|
|
|
|
2010-12-08 10:30:45 +00:00
|
|
|
// If we ran recordUndo, it means that we plan to change the buffer
|
|
|
|
buffer_.markDirty();
|
|
|
|
|
2008-04-16 20:37:23 +00:00
|
|
|
redostack_.clear();
|
2007-10-18 11:51:17 +00:00
|
|
|
//lyxerr << "undostack:\n";
|
|
|
|
//for (size_t i = 0, n = buf.undostack().size(); i != n && i < 6; ++i)
|
2007-12-12 19:28:07 +00:00
|
|
|
// lyxerr << " " << i << ": " << buf.undostack()[i] << endl;
|
2007-10-18 11:51:17 +00:00
|
|
|
}
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
2008-08-15 19:24:56 +00:00
|
|
|
void Undo::Private::doTextUndoOrRedo(DocIterator & cur, UndoElementStack & stack, UndoElementStack & otherstack)
|
2003-10-14 13:01:49 +00:00
|
|
|
{
|
2005-07-14 22:09:22 +00:00
|
|
|
// Adjust undo stack and get hold of current undo data.
|
2008-07-13 19:00:55 +00:00
|
|
|
UndoElement & undo = stack.top();
|
2008-08-15 19:24:56 +00:00
|
|
|
LYXERR(Debug::UNDO, "Undo element of group " << undo.group_id);
|
2008-07-13 19:00:55 +00:00
|
|
|
// We'll pop the stack only when we're done with this element. So do NOT
|
|
|
|
// try to return early.
|
2005-07-14 22:09:22 +00:00
|
|
|
|
|
|
|
// We will store in otherstack the part of the document under 'undo'
|
2008-11-17 11:46:07 +00:00
|
|
|
DocIterator cell_dit = undo.cell.asDocIterator(&buffer_);
|
2005-07-14 22:09:22 +00:00
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
doRecordUndo(ATOMIC_UNDO, cell_dit,
|
|
|
|
undo.from, cell_dit.lastpit() - undo.end, cur,
|
2008-08-15 19:24:56 +00:00
|
|
|
undo.isFullBuffer, otherstack);
|
2005-07-14 22:09:22 +00:00
|
|
|
|
|
|
|
// This does the actual undo/redo.
|
2007-11-28 22:12:03 +00:00
|
|
|
//LYXERR0("undo, performing: " << undo);
|
2008-11-17 11:46:07 +00:00
|
|
|
DocIterator dit = undo.cell.asDocIterator(&buffer_);
|
2005-07-14 22:09:22 +00:00
|
|
|
if (undo.isFullBuffer) {
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(undo.pars, /**/);
|
2005-07-14 22:09:22 +00:00
|
|
|
// This is a full document
|
2008-07-14 17:11:19 +00:00
|
|
|
delete otherstack.top().bparams;
|
|
|
|
otherstack.top().bparams = new BufferParams(buffer_.params());
|
|
|
|
buffer_.params() = *undo.bparams;
|
2007-12-12 19:28:07 +00:00
|
|
|
swap(buffer_.paragraphs(), *undo.pars);
|
2007-03-27 14:33:19 +00:00
|
|
|
delete undo.pars;
|
|
|
|
undo.pars = 0;
|
2005-07-14 22:09:22 +00:00
|
|
|
} else if (dit.inMathed()) {
|
2004-03-08 21:14:45 +00:00
|
|
|
// We stored the full cell here as there is not much to be
|
|
|
|
// gained by storing just 'a few' paragraphs (most if not
|
|
|
|
// all math inset cells have just one paragraph!)
|
2007-11-28 22:12:03 +00:00
|
|
|
//LYXERR0("undo.array: " << *undo.array);
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(undo.array, /**/);
|
2007-03-27 14:33:19 +00:00
|
|
|
dit.cell().swap(*undo.array);
|
|
|
|
delete undo.array;
|
|
|
|
undo.array = 0;
|
2004-03-08 21:14:45 +00:00
|
|
|
} else {
|
|
|
|
// Some finer machinery is needed here.
|
2007-04-29 23:33:02 +00:00
|
|
|
Text * text = dit.text();
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(text, /**/);
|
|
|
|
LASSERT(undo.pars, /**/);
|
2004-03-08 21:14:45 +00:00
|
|
|
ParagraphList & plist = text->paragraphs();
|
|
|
|
|
|
|
|
// remove new stuff between first and last
|
2003-10-14 13:01:49 +00:00
|
|
|
ParagraphList::iterator first = plist.begin();
|
2004-03-08 21:14:45 +00:00
|
|
|
advance(first, undo.from);
|
2003-10-14 13:01:49 +00:00
|
|
|
ParagraphList::iterator last = plist.begin();
|
2004-03-08 21:14:45 +00:00
|
|
|
advance(last, plist.size() - undo.end);
|
|
|
|
plist.erase(first, last);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
// re-insert old stuff instead
|
|
|
|
first = plist.begin();
|
|
|
|
advance(first, undo.from);
|
2004-12-06 13:06:13 +00:00
|
|
|
|
|
|
|
// this ugly stuff is needed until we get rid of the
|
|
|
|
// inset_owner backpointer
|
2007-03-27 14:33:19 +00:00
|
|
|
ParagraphList::iterator pit = undo.pars->begin();
|
|
|
|
ParagraphList::iterator const end = undo.pars->end();
|
2004-12-06 13:06:13 +00:00
|
|
|
for (; pit != end; ++pit)
|
2005-11-24 16:22:39 +00:00
|
|
|
pit->setInsetOwner(dit.realInset());
|
2007-03-27 14:33:19 +00:00
|
|
|
plist.insert(first, undo.pars->begin(), undo.pars->end());
|
|
|
|
delete undo.pars;
|
|
|
|
undo.pars = 0;
|
2003-10-14 13:01:49 +00:00
|
|
|
}
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(undo.pars == 0, /**/);
|
|
|
|
LASSERT(undo.array == 0, /**/);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2008-11-17 11:46:07 +00:00
|
|
|
cur = undo.cursor.asDocIterator(&buffer_);
|
2010-08-06 23:54:04 +00:00
|
|
|
if (undo.lyx_clean)
|
|
|
|
buffer_.markClean();
|
|
|
|
else
|
|
|
|
buffer_.markDirty();
|
2008-07-13 19:00:55 +00:00
|
|
|
// Now that we're done with undo, we pop it off the stack.
|
|
|
|
stack.pop();
|
2008-08-15 19:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Undo::Private::textUndoOrRedo(DocIterator & cur, bool isUndoOperation)
|
|
|
|
{
|
|
|
|
undo_finished_ = true;
|
|
|
|
|
|
|
|
UndoElementStack & stack = isUndoOperation ? undostack_ : redostack_;
|
|
|
|
|
|
|
|
if (stack.empty())
|
|
|
|
// Nothing to do.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UndoElementStack & otherstack = isUndoOperation ? redostack_ : undostack_;
|
|
|
|
|
|
|
|
const size_t gid = stack.top().group_id;
|
|
|
|
while (!stack.empty() && stack.top().group_id == gid)
|
|
|
|
doTextUndoOrRedo(cur, stack, otherstack);
|
|
|
|
|
2009-03-12 20:02:12 +00:00
|
|
|
// Adapt the new material to current buffer.
|
2009-03-12 20:10:46 +00:00
|
|
|
buffer_.setBuffersForInsets(); // FIXME This shouldn't be here.
|
2004-03-08 21:14:45 +00:00
|
|
|
return true;
|
2003-10-14 13:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
void Undo::finishUndo()
|
2003-10-14 13:01:49 +00:00
|
|
|
{
|
2004-10-23 11:04:41 +00:00
|
|
|
// Make sure the next operation will be stored.
|
2008-04-16 20:37:23 +00:00
|
|
|
d->undo_finished_ = true;
|
2003-10-14 13:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
bool Undo::textUndo(DocIterator & cur)
|
2003-10-14 13:01:49 +00:00
|
|
|
{
|
2007-10-18 11:51:17 +00:00
|
|
|
return d->textUndoOrRedo(cur, true);
|
2003-10-14 13:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-18 11:51:17 +00:00
|
|
|
bool Undo::textRedo(DocIterator & cur)
|
2003-10-14 13:01:49 +00:00
|
|
|
{
|
2007-10-18 11:51:17 +00:00
|
|
|
return d->textUndoOrRedo(cur, false);
|
2003-10-14 13:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-15 19:24:56 +00:00
|
|
|
void Undo::beginUndoGroup()
|
|
|
|
{
|
|
|
|
if (d->group_level == 0) {
|
|
|
|
// create a new group
|
|
|
|
++d->group_id;
|
|
|
|
LYXERR(Debug::UNDO, "+++++++Creating new group " << d->group_id);
|
|
|
|
}
|
|
|
|
++d->group_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Undo::endUndoGroup()
|
|
|
|
{
|
|
|
|
if (d->group_level == 0)
|
|
|
|
LYXERR0("There is no undo group to end here");
|
|
|
|
--d->group_level;
|
|
|
|
if (d->group_level == 0) {
|
|
|
|
// real end of the group
|
|
|
|
LYXERR(Debug::UNDO, "-------End of group " << d->group_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 10:30:45 +00:00
|
|
|
// FIXME: remove these convenience functions and make
|
|
|
|
// Private::recordUndo public as sole interface. The code in the
|
|
|
|
// convenience functions can move to Cursor.cpp.
|
2008-08-15 19:24:56 +00:00
|
|
|
|
2008-08-13 13:46:19 +00:00
|
|
|
void Undo::recordUndo(DocIterator const & cur, UndoKind kind)
|
2004-04-18 19:41:40 +00:00
|
|
|
{
|
2010-12-08 10:04:07 +00:00
|
|
|
d->recordUndo(kind, cur, cur.pit(), cur.pit(), cur, false);
|
2004-04-18 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-29 09:47:46 +00:00
|
|
|
void Undo::recordUndoInset(DocIterator const & cur, UndoKind kind,
|
|
|
|
Inset const * inset)
|
2004-02-13 07:30:59 +00:00
|
|
|
{
|
2010-11-29 09:47:46 +00:00
|
|
|
if (!inset || inset == &cur.inset()) {
|
|
|
|
DocIterator c = cur;
|
|
|
|
c.pop_back();
|
2010-12-08 10:04:07 +00:00
|
|
|
d->recordUndo(kind, c, c.pit(), c.pit(), cur, false);
|
2010-11-29 09:47:46 +00:00
|
|
|
} else if (inset == cur.nextInset())
|
|
|
|
recordUndo(cur, kind);
|
|
|
|
else
|
|
|
|
LYXERR0("Inset not found, no undo stack added.");
|
2004-02-13 07:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 13:46:19 +00:00
|
|
|
void Undo::recordUndo(DocIterator const & cur, UndoKind kind, pit_type from)
|
2003-10-14 13:01:49 +00:00
|
|
|
{
|
2010-12-08 10:04:07 +00:00
|
|
|
d->recordUndo(kind, cur, cur.pit(), from, cur, false);
|
2004-01-15 17:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 13:46:19 +00:00
|
|
|
void Undo::recordUndo(DocIterator const & cur, UndoKind kind,
|
2004-11-24 21:53:46 +00:00
|
|
|
pit_type from, pit_type to)
|
2004-02-03 08:56:28 +00:00
|
|
|
{
|
2010-12-08 10:04:07 +00:00
|
|
|
d->recordUndo(kind, cur, from, to, cur, false);
|
2004-02-03 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 13:46:19 +00:00
|
|
|
void Undo::recordUndoFullDocument(DocIterator const & cur)
|
2004-01-15 17:34:44 +00:00
|
|
|
{
|
2008-08-15 19:24:56 +00:00
|
|
|
// This one may happen outside of the main undo group, so we
|
|
|
|
// put it in its own subgroup to avoid complaints.
|
|
|
|
beginUndoGroup();
|
2010-12-08 10:04:07 +00:00
|
|
|
d->recordUndo(ATOMIC_UNDO, doc_iterator_begin(&d->buffer_),
|
|
|
|
0, d->buffer_.paragraphs().size() - 1, cur, true);
|
2008-08-15 19:24:56 +00:00
|
|
|
endUndoGroup();
|
2003-10-14 13:01:49 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|