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"
|
2017-12-13 10:10:49 +00:00
|
|
|
#include "BufferList.h"
|
2007-10-18 11:51:17 +00:00
|
|
|
#include "BufferParams.h"
|
2007-01-31 14:36:15 +00:00
|
|
|
#include "buffer_funcs.h"
|
2012-07-15 16:16:09 +00:00
|
|
|
#include "Cursor.h"
|
2016-01-19 15:31:35 +00:00
|
|
|
#include "CutAndPaste.h"
|
|
|
|
#include "ErrorList.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"
|
2016-01-19 15:31:35 +00:00
|
|
|
#include "insets/InsetText.h"
|
2004-03-08 21:14:45 +00:00
|
|
|
|
2008-04-16 22:02:26 +00:00
|
|
|
#include "support/debug.h"
|
2013-04-25 21:27:10 +00:00
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lassert.h"
|
2015-03-12 13:47:39 +00:00
|
|
|
#include "support/lyxtime.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>
|
2017-11-29 13:10:33 +00:00
|
|
|
#include <set>
|
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
|
2014-02-14 11:00:56 +00:00
|
|
|
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
|
|
|
///
|
2014-02-14 11:00:56 +00:00
|
|
|
UndoElement(UndoKind kin, CursorData const & cb,
|
2008-07-09 18:42:41 +00:00
|
|
|
StableDocIterator const & cel,
|
2015-01-17 19:38:22 +00:00
|
|
|
pit_type fro, pit_type en, ParagraphList * pl, MathData * ar,
|
|
|
|
bool lc, size_t gid) :
|
|
|
|
kind(kin), cur_before(cb), cell(cel), from(fro), end(en),
|
|
|
|
pars(pl), array(ar), bparams(0),
|
2015-03-12 13:47:39 +00:00
|
|
|
lyx_clean(lc), group_id(gid), time(current_time())
|
2015-01-17 19:38:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
///
|
|
|
|
UndoElement(CursorData const & cb, BufferParams const & bp,
|
|
|
|
bool lc, size_t gid) :
|
|
|
|
kind(ATOMIC_UNDO), cur_before(cb), cell(), from(0), end(0),
|
|
|
|
pars(0), array(0), bparams(new BufferParams(bp)),
|
2015-03-12 13:47:39 +00:00
|
|
|
lyx_clean(lc), group_id(gid), time(current_time())
|
2008-07-14 17:11:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
///
|
2015-03-12 13:47:39 +00:00
|
|
|
UndoElement(UndoElement const & ue) : time(current_time())
|
2008-07-14 17:11:19 +00:00
|
|
|
{
|
|
|
|
kind = ue.kind;
|
2012-02-07 14:56:16 +00:00
|
|
|
cur_before = ue.cur_before;
|
|
|
|
cur_after = ue.cur_after;
|
2008-07-14 17:11:19 +00:00
|
|
|
cell = ue.cell;
|
|
|
|
from = ue.from;
|
|
|
|
end = ue.end;
|
|
|
|
pars = ue.pars;
|
|
|
|
array = ue.array;
|
2015-01-17 19:38:22 +00:00
|
|
|
bparams = ue.bparams
|
|
|
|
? new BufferParams(*ue.bparams) : 0;
|
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()
|
|
|
|
{
|
2015-01-17 19:38:22 +00:00
|
|
|
if (bparams)
|
2008-07-14 17:11:19 +00:00
|
|
|
delete bparams;
|
|
|
|
}
|
2007-10-18 11:51:17 +00:00
|
|
|
/// Which kind of operation are we recording for?
|
|
|
|
UndoKind kind;
|
2012-02-07 14:56:16 +00:00
|
|
|
/// the position of the cursor before recordUndo
|
2012-07-15 16:16:09 +00:00
|
|
|
CursorData cur_before;
|
2012-02-07 14:56:16 +00:00
|
|
|
/// the position of the cursor at the end of the undo group
|
2012-07-15 16:16:09 +00:00
|
|
|
CursorData cur_after;
|
2007-10-18 11:51:17 +00:00
|
|
|
/// 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;
|
2015-03-11 15:55:51 +00:00
|
|
|
/// Only used in case of params undo
|
2008-07-14 17:11:19 +00:00
|
|
|
BufferParams const * bparams;
|
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;
|
2015-03-12 13:47:39 +00:00
|
|
|
/// timestamp
|
|
|
|
time_t time;
|
2008-07-09 18:42:41 +00:00
|
|
|
private:
|
|
|
|
/// Protect construction
|
2014-02-14 11:00:56 +00:00
|
|
|
UndoElement();
|
2007-10-18 11:51:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-14 11:00:56 +00:00
|
|
|
class UndoElementStack
|
2008-04-16 22:02:26 +00:00
|
|
|
{
|
|
|
|
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) {
|
2014-04-07 14:46:47 +00:00
|
|
|
// Remove some entries if the limit has been reached.
|
|
|
|
// However, if the only group on the stack is the one
|
|
|
|
// we are currently populating, do nothing.
|
|
|
|
if (c_.size() >= limit_
|
|
|
|
&& c_.front().group_id != v.group_id) {
|
2008-08-15 19:24:56 +00:00
|
|
|
// 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();
|
|
|
|
}
|
2014-04-07 14:46:47 +00:00
|
|
|
c_.push_front(v);
|
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;
|
2014-02-14 11:00:56 +00:00
|
|
|
}
|
2010-08-06 23:54:04 +00:00
|
|
|
|
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
|
|
|
|
{
|
2014-02-14 11:00:56 +00:00
|
|
|
Private(Buffer & buffer) : buffer_(buffer), undo_finished_(true),
|
2015-07-16 12:48:23 +00:00
|
|
|
group_id_(0), group_level_(0) {}
|
2014-02-14 11:00:56 +00:00
|
|
|
|
2008-08-15 19:24:56 +00:00
|
|
|
// Do one undo/redo step
|
2014-02-14 11:00:56 +00:00
|
|
|
void doTextUndoOrRedo(CursorData & cur, UndoElementStack & stack,
|
2010-12-08 10:04:07 +00:00
|
|
|
UndoElementStack & otherStack);
|
2008-08-15 19:24:56 +00:00
|
|
|
// Apply one undo/redo group. Returns false if no undo possible.
|
2012-07-15 16:16:09 +00:00
|
|
|
bool textUndoOrRedo(CursorData & 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,
|
2012-07-15 16:16:09 +00:00
|
|
|
CursorData const & cur,
|
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,
|
2015-01-17 19:38:22 +00:00
|
|
|
CursorData const & cur);
|
|
|
|
///
|
|
|
|
void doRecordUndoBufferParams(CursorData const & cur, UndoElementStack & stack);
|
|
|
|
///
|
|
|
|
void recordUndoBufferParams(CursorData const & cur);
|
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.
|
2015-07-16 12:48:23 +00:00
|
|
|
size_t group_id_;
|
2008-08-15 19:24:56 +00:00
|
|
|
/// Current group nesting nevel.
|
2015-07-16 12:48:23 +00:00
|
|
|
size_t group_level_;
|
2015-07-16 12:13:13 +00:00
|
|
|
/// the position of cursor before the group was created
|
|
|
|
CursorData group_cur_before_;
|
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;
|
2012-09-17 09:24:39 +00:00
|
|
|
// We used to do that, but I believe it is better to keep
|
|
|
|
// groups (only used in Buffer::reload for now (JMarc)
|
2015-07-16 12:48:23 +00:00
|
|
|
//d->group_id_ = 0;
|
|
|
|
//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();
|
2014-02-14 11:00:56 +00:00
|
|
|
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,
|
2012-07-15 16:16:09 +00:00
|
|
|
CursorData const & cur_before,
|
2008-08-15 19:24:56 +00:00
|
|
|
UndoElementStack & stack)
|
2003-10-14 13:01:49 +00:00
|
|
|
{
|
2015-07-16 12:48:23 +00:00
|
|
|
if (!group_level_) {
|
2008-08-15 19:24:56 +00:00
|
|
|
LYXERR0("There is no group open (creating one)");
|
2015-07-16 12:48:23 +00:00
|
|
|
++group_id_;
|
2008-08-15 19:24:56 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2015-03-11 15:55:51 +00:00
|
|
|
&& !stack.top().bparams
|
2008-07-09 22:59:16 +00:00
|
|
|
&& samePar(stack.top().cell, cell)
|
|
|
|
&& stack.top().kind == kind
|
|
|
|
&& stack.top().from == from
|
2015-03-12 13:47:39 +00:00
|
|
|
&& stack.top().end == end
|
|
|
|
&& stack.top().cur_after == cur_before
|
|
|
|
&& current_time() - stack.top().time <= 2) {
|
2012-02-07 14:56:16 +00:00
|
|
|
// reset cur_after; it will be filled correctly by endUndoGroup.
|
2012-07-15 16:16:09 +00:00
|
|
|
stack.top().cur_after = CursorData();
|
2015-03-12 13:47:39 +00:00
|
|
|
// update the timestamp of the undo element
|
|
|
|
stack.top().time = current_time();
|
2004-03-08 21:14:45 +00:00
|
|
|
return;
|
2012-02-07 14:56:16 +00:00
|
|
|
}
|
2004-03-01 17:12:09 +00:00
|
|
|
|
2015-07-16 12:48:23 +00:00
|
|
|
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
|
2015-07-16 12:13:13 +00:00
|
|
|
UndoElement undo(kind,
|
|
|
|
group_cur_before_.empty() ? cur_before : group_cur_before_,
|
|
|
|
cell, from, end, 0, 0, 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();
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(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,
|
2015-01-17 19:38:22 +00:00
|
|
|
CursorData const & cur)
|
2004-11-05 06:12:21 +00:00
|
|
|
{
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(first_pit <= cell.lastpit(), return);
|
|
|
|
LASSERT(last_pit <= cell.lastpit(), return);
|
2004-11-05 06:12:21 +00:00
|
|
|
|
2010-12-08 10:04:07 +00:00
|
|
|
doRecordUndo(kind, cell, first_pit, last_pit, cur,
|
2015-01-17 19:38:22 +00:00
|
|
|
undostack_);
|
|
|
|
|
|
|
|
// next time we'll try again to combine entries if possible
|
|
|
|
undo_finished_ = false;
|
|
|
|
|
|
|
|
// If we ran recordUndo, it means that we plan to change the buffer
|
|
|
|
buffer_.markDirty();
|
|
|
|
|
|
|
|
redostack_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Undo::Private::doRecordUndoBufferParams(CursorData const & cur_before,
|
2015-07-16 12:13:13 +00:00
|
|
|
UndoElementStack & stack)
|
2015-01-17 19:38:22 +00:00
|
|
|
{
|
2015-07-16 12:48:23 +00:00
|
|
|
if (!group_level_) {
|
2015-01-17 19:38:22 +00:00
|
|
|
LYXERR0("There is no group open (creating one)");
|
2015-07-16 12:48:23 +00:00
|
|
|
++group_id_;
|
2015-01-17 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 12:48:23 +00:00
|
|
|
LYXERR(Debug::UNDO, "Create full buffer undo element of group " << group_id_);
|
2015-01-17 19:38:22 +00:00
|
|
|
// create the position information of the Undo entry
|
2015-07-16 12:13:13 +00:00
|
|
|
UndoElement undo(group_cur_before_.empty() ? cur_before : group_cur_before_,
|
|
|
|
buffer_.params(), buffer_.isClean(),
|
2015-07-16 12:48:23 +00:00
|
|
|
group_id_);
|
2015-01-17 19:38:22 +00:00
|
|
|
|
|
|
|
// push the undo entry to undo stack
|
|
|
|
stack.push(undo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Undo::Private::recordUndoBufferParams(CursorData const & cur)
|
|
|
|
{
|
|
|
|
doRecordUndoBufferParams(cur, 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
|
|
|
}
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
2012-07-15 16:16:09 +00:00
|
|
|
void Undo::Private::doTextUndoOrRedo(CursorData & 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
|
|
|
|
2015-01-17 19:38:22 +00:00
|
|
|
if (undo.bparams)
|
|
|
|
doRecordUndoBufferParams(undo.cur_after, otherstack);
|
2015-05-22 16:16:23 +00:00
|
|
|
else {
|
|
|
|
LATTEST(undo.end <= cell_dit.lastpit());
|
2015-01-17 19:38:22 +00:00
|
|
|
doRecordUndo(ATOMIC_UNDO, cell_dit,
|
|
|
|
undo.from, cell_dit.lastpit() - undo.end, undo.cur_after,
|
|
|
|
otherstack);
|
2015-05-22 16:16:23 +00:00
|
|
|
}
|
2012-02-07 14:56:16 +00:00
|
|
|
otherstack.top().cur_after = undo.cur_before;
|
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_);
|
2015-01-17 19:38:22 +00:00
|
|
|
if (undo.bparams) {
|
|
|
|
// This is a params undo element
|
2008-07-14 17:11:19 +00:00
|
|
|
delete otherstack.top().bparams;
|
|
|
|
otherstack.top().bparams = new BufferParams(buffer_.params());
|
2016-01-19 15:31:35 +00:00
|
|
|
DocumentClassConstPtr olddc = buffer_.params().documentClassPtr();
|
2008-07-14 17:11:19 +00:00
|
|
|
buffer_.params() = *undo.bparams;
|
2016-01-19 15:31:35 +00:00
|
|
|
// 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());
|
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);
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(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();
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(text);
|
|
|
|
LBUFERR(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());
|
2017-10-16 02:22:22 +00:00
|
|
|
|
|
|
|
// 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)
|
2017-10-16 02:14:08 +00:00
|
|
|
fpit->setInsetBuffers(buffer_);
|
2017-10-16 02:22:22 +00:00
|
|
|
|
2007-03-27 14:33:19 +00:00
|
|
|
delete undo.pars;
|
|
|
|
undo.pars = 0;
|
2003-10-14 13:01:49 +00:00
|
|
|
}
|
2013-04-25 21:27:10 +00:00
|
|
|
|
|
|
|
// We'll clean up in release mode.
|
|
|
|
LASSERT(undo.pars == 0, undo.pars = 0);
|
|
|
|
LASSERT(undo.array == 0, undo.array = 0);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
2012-07-15 16:16:09 +00:00
|
|
|
if (!undo.cur_before.empty())
|
|
|
|
cur = undo.cur_before;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-15 16:16:09 +00:00
|
|
|
bool Undo::Private::textUndoOrRedo(CursorData & cur, bool isUndoOperation)
|
2008-08-15 19:24:56 +00:00
|
|
|
{
|
|
|
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-15 16:16:09 +00:00
|
|
|
bool Undo::textUndo(CursorData & 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-15 16:16:09 +00:00
|
|
|
bool Undo::textRedo(CursorData & 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()
|
|
|
|
{
|
2015-07-16 12:48:23 +00:00
|
|
|
if (d->group_level_ == 0) {
|
2008-08-15 19:24:56 +00:00
|
|
|
// create a new group
|
2015-07-16 12:48:23 +00:00
|
|
|
++d->group_id_;
|
2017-12-13 09:38:47 +00:00
|
|
|
LYXERR(Debug::UNDO, "+++++++ Creating new group " << d->group_id_
|
|
|
|
<< " for buffer " << &d->buffer_);
|
2008-08-15 19:24:56 +00:00
|
|
|
}
|
2015-07-16 12:48:23 +00:00
|
|
|
++d->group_level_;
|
2008-08-15 19:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-16 12:13:13 +00:00
|
|
|
void Undo::beginUndoGroup(CursorData const & cur_before)
|
|
|
|
{
|
|
|
|
beginUndoGroup();
|
|
|
|
if (d->group_cur_before_.empty())
|
|
|
|
d->group_cur_before_ = cur_before;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-15 19:24:56 +00:00
|
|
|
void Undo::endUndoGroup()
|
|
|
|
{
|
2015-07-16 12:48:23 +00:00
|
|
|
if (d->group_level_ == 0) {
|
2008-08-15 19:24:56 +00:00
|
|
|
LYXERR0("There is no undo group to end here");
|
2012-09-17 09:24:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-16 12:48:23 +00:00
|
|
|
--d->group_level_;
|
|
|
|
if (d->group_level_ == 0) {
|
2008-08-15 19:24:56 +00:00
|
|
|
// real end of the group
|
2015-07-16 12:13:13 +00:00
|
|
|
d->group_cur_before_ = CursorData();
|
2017-12-13 09:38:47 +00:00
|
|
|
LYXERR(Debug::UNDO, "------- End of group " << d->group_id_
|
|
|
|
<< " of buffer " << &d->buffer_);
|
2008-08-15 19:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-07 14:56:16 +00:00
|
|
|
|
2015-07-16 12:13:13 +00:00
|
|
|
void Undo::endUndoGroup(CursorData const & cur_after)
|
2012-02-07 14:56:16 +00:00
|
|
|
{
|
|
|
|
endUndoGroup();
|
2012-07-15 16:16:09 +00:00
|
|
|
if (!d->undostack_.empty() && d->undostack_.top().cur_after.empty())
|
2015-07-16 12:13:13 +00:00
|
|
|
d->undostack_.top().cur_after = cur_after;
|
2012-02-07 14:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-15 16:16:09 +00:00
|
|
|
void Undo::recordUndo(CursorData const & cur, UndoKind kind)
|
2004-04-18 19:41:40 +00:00
|
|
|
{
|
2015-01-17 19:38:22 +00:00
|
|
|
d->recordUndo(kind, cur, cur.pit(), cur.pit(), cur);
|
2004-04-18 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-12 14:57:29 +00:00
|
|
|
void Undo::recordUndo(CursorData const & cur, pit_type from, pit_type to)
|
2004-02-03 08:56:28 +00:00
|
|
|
{
|
2015-03-12 14:57:29 +00:00
|
|
|
d->recordUndo(ATOMIC_UNDO, cur, from, to, cur);
|
2015-01-17 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-20 10:09:12 +00:00
|
|
|
void Undo::recordUndoInset(CursorData const & cur, Inset const * inset)
|
|
|
|
{
|
|
|
|
if (!inset || inset == &cur.inset()) {
|
|
|
|
DocIterator c = cur;
|
|
|
|
c.pop_back();
|
|
|
|
d->recordUndo(ATOMIC_UNDO, c, c.pit(), c.pit(), cur);
|
|
|
|
} else if (inset == cur.nextInset())
|
|
|
|
recordUndo(cur);
|
|
|
|
else
|
|
|
|
LYXERR0("Inset not found, no undo stack added.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-17 19:38:22 +00:00
|
|
|
void Undo::recordUndoBufferParams(CursorData const & cur)
|
|
|
|
{
|
|
|
|
d->recordUndoBufferParams(cur);
|
2004-02-03 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-17 19:38:22 +00:00
|
|
|
void Undo::recordUndoFullBuffer(CursorData 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();
|
2014-02-14 11:00:56 +00:00
|
|
|
d->recordUndo(ATOMIC_UNDO, doc_iterator_begin(&d->buffer_),
|
2015-01-17 19:38:22 +00:00
|
|
|
0, d->buffer_.paragraphs().size() - 1, cur);
|
|
|
|
d->recordUndoBufferParams(cur);
|
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
|
|
|
|
2015-05-21 14:49:19 +00:00
|
|
|
/// UndoGroupHelper class stuff
|
|
|
|
|
2017-11-29 13:10:33 +00:00
|
|
|
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_)
|
2017-12-13 10:10:49 +00:00
|
|
|
if (theBufferList().isLoaded(buf) || theBufferList().isInternal(buf))
|
|
|
|
buf->undo().endUndoGroup();
|
2017-11-29 13:10:33 +00:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2015-05-21 14:49:19 +00:00
|
|
|
void UndoGroupHelper::resetBuffer(Buffer * buf)
|
|
|
|
{
|
2017-11-29 13:10:33 +00:00
|
|
|
if (buf && d->buffers_.count(buf) == 0) {
|
|
|
|
d->buffers_.insert(buf);
|
|
|
|
buf->undo().beginUndoGroup();
|
|
|
|
}
|
2015-05-21 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
} // namespace lyx
|