2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file undo.C
|
|
|
|
|
* 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
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
2003-10-14 13:01:49 +00:00
|
|
|
|
* \author John Levon
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
2003-10-14 13:01:49 +00:00
|
|
|
|
* \author J<EFBFBD>rgen Vigna
|
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>
|
|
|
|
|
|
2001-05-08 13:28:44 +00:00
|
|
|
|
#include "undo.h"
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
|
|
|
|
#include "buffer.h"
|
2007-01-31 14:36:15 +00:00
|
|
|
|
#include "buffer_funcs.h"
|
2004-01-20 14:25:24 +00:00
|
|
|
|
#include "cursor.h"
|
2003-10-14 13:01:49 +00:00
|
|
|
|
#include "debug.h"
|
|
|
|
|
#include "BufferView.h"
|
|
|
|
|
#include "lyxtext.h"
|
2003-09-06 12:36:58 +00:00
|
|
|
|
#include "paragraph.h"
|
2006-03-23 20:11:06 +00:00
|
|
|
|
#include "ParagraphList.h"
|
2001-05-08 13:28:44 +00:00
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "mathed/MathSupport.h"
|
2007-04-25 16:11:45 +00:00
|
|
|
|
#include "mathed/MathArray.h"
|
2007-03-27 14:33:19 +00:00
|
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
|
#include "insets/Inset.h"
|
2004-03-08 21:14:45 +00:00
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-10-23 11:04:41 +00:00
|
|
|
|
using std::advance;
|
2004-03-27 12:46:30 +00:00
|
|
|
|
using std::endl;
|
|
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
|
namespace {
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// The flag used by finishUndo().
|
|
|
|
|
bool undo_finished;
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2003-10-15 08:49:44 +00:00
|
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
std::ostream & operator<<(std::ostream & os, Undo const & undo)
|
|
|
|
|
{
|
2004-10-23 11:04:41 +00:00
|
|
|
|
return os << " from: " << undo.from << " end: " << undo.end
|
2004-11-05 06:12:21 +00:00
|
|
|
|
<< " cell:\n" << undo.cell
|
2004-03-08 21:14:45 +00:00
|
|
|
|
<< " cursor:\n" << undo.cursor;
|
2003-10-15 08:49:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-10-13 17:20:30 +00:00
|
|
|
|
bool samePar(StableDocIterator const & i1, StableDocIterator const & i2)
|
|
|
|
|
{
|
|
|
|
|
StableDocIterator tmpi2 = i2;
|
|
|
|
|
tmpi2.pos() = i1.pos();
|
|
|
|
|
return i1 == tmpi2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
void doRecordUndo(Undo::undo_kind kind,
|
|
|
|
|
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,
|
|
|
|
|
BufferParams const & bparams,
|
|
|
|
|
bool isFullBuffer,
|
2003-10-14 13:01:49 +00:00
|
|
|
|
limited_stack<Undo> & stack)
|
|
|
|
|
{
|
2004-11-24 21:53:46 +00:00
|
|
|
|
if (first_pit > last_pit)
|
|
|
|
|
std::swap(first_pit, last_pit);
|
2004-03-08 21:14:45 +00:00
|
|
|
|
// create the position information of the Undo entry
|
|
|
|
|
Undo undo;
|
2007-03-27 14:33:19 +00:00
|
|
|
|
undo.array = 0;
|
|
|
|
|
undo.pars = 0;
|
2004-03-08 21:14:45 +00:00
|
|
|
|
undo.kind = kind;
|
2004-11-05 06:12:21 +00:00
|
|
|
|
undo.cell = cell;
|
|
|
|
|
undo.cursor = cur;
|
2005-07-14 22:09:22 +00:00
|
|
|
|
undo.bparams = bparams ;
|
|
|
|
|
undo.isFullBuffer = isFullBuffer;
|
2005-07-15 00:39:44 +00:00
|
|
|
|
//lyxerr << "recordUndo: cur: " << cur << endl;
|
|
|
|
|
//lyxerr << "recordUndo: pos: " << cur.pos() << endl;
|
2004-11-05 06:12:21 +00:00
|
|
|
|
//lyxerr << "recordUndo: cell: " << cell << endl;
|
2004-11-24 21:53:46 +00:00
|
|
|
|
undo.from = first_pit;
|
|
|
|
|
undo.end = cell.lastpit() - last_pit;
|
2003-10-14 13:01:49 +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.
|
|
|
|
|
if (!undo_finished
|
|
|
|
|
&& kind != Undo::ATOMIC
|
|
|
|
|
&& !stack.empty()
|
2005-10-13 17:20:30 +00:00
|
|
|
|
&& samePar(stack.top().cell, undo.cell)
|
|
|
|
|
&& stack.top().kind == undo.kind
|
|
|
|
|
&& stack.top().from == undo.from
|
|
|
|
|
&& stack.top().end == undo.end)
|
2004-03-08 21:14:45 +00:00
|
|
|
|
return;
|
2004-03-01 17:12:09 +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
|
2007-03-27 14:33:19 +00:00
|
|
|
|
undo.array = new MathArray(cell.cell());
|
2004-03-08 21:14:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
// some more effort needed here as 'the whole cell' of the
|
|
|
|
|
// main LyXText _is_ the whole document.
|
2004-03-01 17:12:09 +00:00
|
|
|
|
// record the relevant paragraphs
|
2005-07-14 22:09:22 +00:00
|
|
|
|
LyXText const * text = cell.text();
|
2004-03-08 21:14:45 +00:00
|
|
|
|
BOOST_ASSERT(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-03-27 14:33:19 +00:00
|
|
|
|
//lyxerr << "undo record: " << stack.top() << std::endl;
|
2004-03-08 21:14:45 +00:00
|
|
|
|
|
|
|
|
|
// next time we'll try again to combine entries if possible
|
2003-10-14 13:01:49 +00:00
|
|
|
|
undo_finished = false;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-30 01:59:49 +00:00
|
|
|
|
|
2004-11-05 06:12:21 +00:00
|
|
|
|
void recordUndo(Undo::undo_kind kind,
|
2004-11-24 21:53:46 +00:00
|
|
|
|
LCursor & cur, pit_type first_pit, pit_type last_pit,
|
2004-11-05 06:12:21 +00:00
|
|
|
|
limited_stack<Undo> & stack)
|
|
|
|
|
{
|
2004-11-24 21:53:46 +00:00
|
|
|
|
BOOST_ASSERT(first_pit <= cur.lastpit());
|
|
|
|
|
BOOST_ASSERT(last_pit <= cur.lastpit());
|
2004-11-05 06:12:21 +00:00
|
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
doRecordUndo(kind, cur, first_pit, last_pit, cur,
|
|
|
|
|
cur.bv().buffer()->params(), false, stack);
|
2004-11-05 06:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
|
|
|
|
|
// Returns false if no undo possible.
|
|
|
|
|
bool textUndoOrRedo(BufferView & bv,
|
|
|
|
|
limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2005-07-14 22:09:22 +00:00
|
|
|
|
finishUndo();
|
|
|
|
|
|
|
|
|
|
if (stack.empty()) {
|
|
|
|
|
// Nothing to do.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adjust undo stack and get hold of current undo data.
|
|
|
|
|
Undo undo = stack.top();
|
|
|
|
|
stack.pop();
|
|
|
|
|
|
|
|
|
|
// We will store in otherstack the part of the document under 'undo'
|
|
|
|
|
Buffer * buf = bv.buffer();
|
|
|
|
|
DocIterator cell_dit = undo.cell.asDocIterator(&buf->inset());
|
|
|
|
|
|
|
|
|
|
doRecordUndo(Undo::ATOMIC, cell_dit,
|
|
|
|
|
undo.from, cell_dit.lastpit() - undo.end, bv.cursor(),
|
|
|
|
|
undo.bparams, undo.isFullBuffer,
|
|
|
|
|
otherstack);
|
|
|
|
|
|
|
|
|
|
// This does the actual undo/redo.
|
2004-11-05 06:12:21 +00:00
|
|
|
|
//lyxerr << "undo, performing: " << undo << std::endl;
|
2005-07-14 22:09:22 +00:00
|
|
|
|
DocIterator dit = undo.cell.asDocIterator(&buf->inset());
|
|
|
|
|
if (undo.isFullBuffer) {
|
2007-03-27 14:33:19 +00:00
|
|
|
|
BOOST_ASSERT(undo.pars);
|
2005-07-14 22:09:22 +00:00
|
|
|
|
// This is a full document
|
|
|
|
|
otherstack.top().bparams = buf->params();
|
|
|
|
|
buf->params() = undo.bparams;
|
2007-03-27 14:33:19 +00:00
|
|
|
|
std::swap(buf->paragraphs(), *undo.pars);
|
|
|
|
|
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-03-27 14:33:19 +00:00
|
|
|
|
//lyxerr << "undo.array: " << *undo.array <<endl;
|
|
|
|
|
BOOST_ASSERT(undo.array);
|
|
|
|
|
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.
|
2004-11-05 06:12:21 +00:00
|
|
|
|
LyXText * text = dit.text();
|
2004-03-08 21:14:45 +00:00
|
|
|
|
BOOST_ASSERT(text);
|
2007-03-27 14:33:19 +00:00
|
|
|
|
BOOST_ASSERT(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;
|
2007-01-31 14:36:15 +00:00
|
|
|
|
updateLabels(*buf);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
2007-03-27 14:33:19 +00:00
|
|
|
|
BOOST_ASSERT(undo.pars == 0);
|
|
|
|
|
BOOST_ASSERT(undo.array == 0);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
// Set cursor
|
2004-11-05 06:12:21 +00:00
|
|
|
|
LCursor & cur = bv.cursor();
|
2005-07-14 22:09:22 +00:00
|
|
|
|
cur.setCursor(undo.cursor.asDocIterator(&buf->inset()));
|
2004-11-05 06:12:21 +00:00
|
|
|
|
cur.selection() = false;
|
2004-03-08 21:14:45 +00:00
|
|
|
|
cur.resetAnchor();
|
2007-03-27 14:33:19 +00:00
|
|
|
|
cur.fixIfBroken();
|
2003-10-14 13:01:49 +00:00
|
|
|
|
finishUndo();
|
2004-12-06 13:06:13 +00:00
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
return true;
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
} // namespace anon
|
|
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
|
|
|
|
void finishUndo()
|
|
|
|
|
{
|
2004-10-23 11:04:41 +00:00
|
|
|
|
// Make sure the next operation will be stored.
|
2003-10-14 13:01:49 +00:00
|
|
|
|
undo_finished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-03 08:56:28 +00:00
|
|
|
|
bool textUndo(BufferView & bv)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2004-02-03 08:56:28 +00:00
|
|
|
|
return textUndoOrRedo(bv, bv.buffer()->undostack(),
|
|
|
|
|
bv.buffer()->redostack());
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-03 08:56:28 +00:00
|
|
|
|
bool textRedo(BufferView & bv)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2004-02-03 08:56:28 +00:00
|
|
|
|
return textUndoOrRedo(bv, bv.buffer()->redostack(),
|
|
|
|
|
bv.buffer()->undostack());
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void recordUndo(Undo::undo_kind kind,
|
2004-11-24 21:53:46 +00:00
|
|
|
|
LCursor & cur, pit_type first, pit_type last)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2004-02-03 08:56:28 +00:00
|
|
|
|
Buffer * buf = cur.bv().buffer();
|
|
|
|
|
recordUndo(kind, cur, first, last, buf->undostack());
|
2003-10-15 08:49:44 +00:00
|
|
|
|
buf->redostack().clear();
|
2004-11-05 06:12:21 +00:00
|
|
|
|
//lyxerr << "undostack:\n";
|
|
|
|
|
//for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
|
|
|
|
|
// lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-03 08:56:28 +00:00
|
|
|
|
void recordUndo(LCursor & cur, Undo::undo_kind kind)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2004-11-24 21:53:46 +00:00
|
|
|
|
recordUndo(kind, cur, cur.pit(), cur.pit());
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-18 19:41:40 +00:00
|
|
|
|
void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
|
|
|
|
|
{
|
|
|
|
|
LCursor c = cur;
|
|
|
|
|
c.pop();
|
2006-08-17 20:13:30 +00:00
|
|
|
|
Buffer * buf = cur.bv().buffer();
|
|
|
|
|
doRecordUndo(kind, c, c.pit(), c.pit(), cur,
|
|
|
|
|
buf->params(), false, buf->undostack());
|
2004-04-18 19:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-13 07:30:59 +00:00
|
|
|
|
void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
|
|
|
|
|
{
|
2004-11-24 21:53:46 +00:00
|
|
|
|
recordUndo(kind, cur, cur.selBegin().pit(), cur.selEnd().pit());
|
2004-02-13 07:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-11-24 21:53:46 +00:00
|
|
|
|
void recordUndo(LCursor & cur, Undo::undo_kind kind, pit_type from)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2004-11-24 21:53:46 +00:00
|
|
|
|
recordUndo(kind, cur, cur.pit(), from);
|
2004-01-15 17:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-03 08:56:28 +00:00
|
|
|
|
void recordUndo(LCursor & cur, Undo::undo_kind kind,
|
2004-11-24 21:53:46 +00:00
|
|
|
|
pit_type from, pit_type to)
|
2004-02-03 08:56:28 +00:00
|
|
|
|
{
|
|
|
|
|
recordUndo(kind, cur, from, to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
void recordUndoFullDocument(BufferView * bv)
|
2004-01-15 17:34:44 +00:00
|
|
|
|
{
|
2005-07-14 22:09:22 +00:00
|
|
|
|
Buffer * buf = bv->buffer();
|
|
|
|
|
doRecordUndo(
|
|
|
|
|
Undo::ATOMIC,
|
|
|
|
|
doc_iterator_begin(buf->inset()),
|
|
|
|
|
0, buf->paragraphs().size() - 1,
|
|
|
|
|
bv->cursor(),
|
|
|
|
|
buf->params(),
|
|
|
|
|
true,
|
|
|
|
|
buf->undostack()
|
|
|
|
|
);
|
|
|
|
|
undo_finished = false;
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|