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"
|
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"
|
2001-05-08 13:28:44 +00:00
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
#include "mathed/math_support.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2004-11-24 21:53:46 +00:00
|
|
|
|
using lyx::pit_type;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
void recordUndo(Undo::undo_kind kind,
|
2004-11-26 14:52:54 +00:00
|
|
|
|
DocIterator & cell,
|
2004-11-24 21:53:46 +00:00
|
|
|
|
pit_type first_pit, pit_type last_pit,
|
2004-11-05 06:12:21 +00:00
|
|
|
|
DocIterator & cur,
|
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);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
// create the position information of the Undo entry
|
|
|
|
|
Undo undo;
|
|
|
|
|
undo.kind = kind;
|
2004-11-05 06:12:21 +00:00
|
|
|
|
undo.cell = cell;
|
|
|
|
|
undo.cursor = cur;
|
2004-10-23 11:04:41 +00:00
|
|
|
|
lyxerr << "recordUndo: cur: " << cur << endl;
|
2004-11-05 06:12:21 +00:00
|
|
|
|
lyxerr << "recordUndo: pos: " << cur.pos() << endl;
|
|
|
|
|
//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()
|
2004-11-05 06:12:21 +00:00
|
|
|
|
&& stack.top().cell.size() == undo.cell.size()
|
2004-03-08 21:14:45 +00:00
|
|
|
|
&& stack.top().kind == undo.kind
|
|
|
|
|
&& stack.top().from == undo.from
|
|
|
|
|
&& stack.top().end == undo.end)
|
|
|
|
|
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
|
2004-11-05 06:12:21 +00:00
|
|
|
|
undo.array = asString(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
|
2004-11-05 06:12:21 +00:00
|
|
|
|
LyXText * text = cell.text();
|
2004-03-08 21:14:45 +00:00
|
|
|
|
BOOST_ASSERT(text);
|
2004-03-01 17:12:09 +00:00
|
|
|
|
ParagraphList & plist = text->paragraphs();
|
|
|
|
|
ParagraphList::iterator first = plist.begin();
|
2004-11-24 21:53:46 +00:00
|
|
|
|
advance(first, first_pit);
|
2004-03-01 17:12:09 +00:00
|
|
|
|
ParagraphList::iterator last = plist.begin();
|
2004-11-24 21:53:46 +00:00
|
|
|
|
advance(last, last_pit + 1);
|
2004-03-08 21:14:45 +00:00
|
|
|
|
undo.pars = 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
|
|
|
|
//lyxerr << "undo record: " << stack.top() << std::endl;
|
|
|
|
|
stack.push(undo);
|
|
|
|
|
|
|
|
|
|
// next time we'll try again to combine entries if possible
|
2003-10-14 13:01:49 +00:00
|
|
|
|
undo_finished = false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2004-11-24 21:53:46 +00:00
|
|
|
|
recordUndo(kind, cur, first_pit, last_pit, cur, stack);
|
2004-11-05 06:12:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
void performUndoOrRedo(BufferView & bv, Undo const & undo)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2004-11-05 06:12:21 +00:00
|
|
|
|
//lyxerr << "undo, performing: " << undo << std::endl;
|
|
|
|
|
DocIterator dit = undo.cell.asDocIterator(&bv.buffer()->inset());
|
|
|
|
|
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!)
|
2004-11-05 06:12:21 +00:00
|
|
|
|
asArray(undo.array, dit.cell());
|
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);
|
|
|
|
|
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);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
plist.insert(first, undo.pars.begin(), undo.pars.end());
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-05 06:12:21 +00:00
|
|
|
|
LCursor & cur = bv.cursor();
|
|
|
|
|
cur.setCursor(undo.cursor.asDocIterator(&bv.buffer()->inset()));
|
|
|
|
|
cur.selection() = false;
|
2004-03-08 21:14:45 +00:00
|
|
|
|
cur.resetAnchor();
|
2003-10-14 13:01:49 +00:00
|
|
|
|
finishUndo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-10-23 11:04:41 +00:00
|
|
|
|
// Returns false if no undo possible.
|
2004-02-03 08:56:28 +00:00
|
|
|
|
bool textUndoOrRedo(BufferView & bv,
|
2003-10-14 13:01:49 +00:00
|
|
|
|
limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
|
|
|
|
|
{
|
|
|
|
|
if (stack.empty()) {
|
2004-10-23 11:04:41 +00:00
|
|
|
|
// Nothing to do.
|
2003-10-14 13:01:49 +00:00
|
|
|
|
finishUndo();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-05 06:12:21 +00:00
|
|
|
|
//
|
|
|
|
|
// Adjust undo stack and get hold of current undo data.
|
|
|
|
|
//
|
2003-10-14 13:01:49 +00:00
|
|
|
|
Undo undo = stack.top();
|
|
|
|
|
stack.pop();
|
|
|
|
|
finishUndo();
|
|
|
|
|
|
2004-11-05 06:12:21 +00:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// This implements redo.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// The cursor will be placed at cur_dit after
|
|
|
|
|
// the ongoing undo operation.
|
|
|
|
|
DocIterator cur_dit =
|
|
|
|
|
undo.cursor.asDocIterator(&bv.buffer()->inset());
|
|
|
|
|
|
|
|
|
|
// This is the place the cursor is currently located.
|
|
|
|
|
LCursor & cur = bv.cursor();
|
|
|
|
|
DocIterator cell_dit = cur;
|
|
|
|
|
|
|
|
|
|
// If both places have the same depth we stay in the same
|
|
|
|
|
// cell and store paragraphs from this cell. Otherwise we
|
2004-11-26 14:52:54 +00:00
|
|
|
|
// will drop slices from the more nested iterator and
|
2004-11-05 06:12:21 +00:00
|
|
|
|
// create an undo item from a single paragraph of the common
|
|
|
|
|
// ancestor.
|
|
|
|
|
DocIterator ancestor_dit = cur_dit;
|
|
|
|
|
while (ancestor_dit.size() > cur.size())
|
|
|
|
|
ancestor_dit.pop_back();
|
|
|
|
|
|
|
|
|
|
if (cur_dit.size() == cell_dit.size()) {
|
|
|
|
|
recordUndo(Undo::ATOMIC, cell_dit,
|
2004-11-24 21:53:46 +00:00
|
|
|
|
cell_dit.pit(), cur_dit.pit(),
|
2004-11-05 06:12:21 +00:00
|
|
|
|
cur_dit, otherstack);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
2004-11-05 06:12:21 +00:00
|
|
|
|
else {
|
|
|
|
|
recordUndo(Undo::ATOMIC, ancestor_dit,
|
2004-11-24 21:53:46 +00:00
|
|
|
|
ancestor_dit.pit(),
|
|
|
|
|
ancestor_dit.pit(),
|
2004-11-05 06:12:21 +00:00
|
|
|
|
cur_dit, otherstack);
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-11-05 06:12:21 +00:00
|
|
|
|
//
|
|
|
|
|
// This does the actual undo.
|
|
|
|
|
//
|
2004-03-08 21:14:45 +00:00
|
|
|
|
performUndoOrRedo(bv, undo);
|
|
|
|
|
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();
|
|
|
|
|
recordUndo(c, kind);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void recordUndoFullDocument(LCursor &)
|
2004-01-15 17:34:44 +00:00
|
|
|
|
{
|
2004-02-03 08:56:28 +00:00
|
|
|
|
//recordUndo(Undo::ATOMIC,
|
|
|
|
|
// cur, 0, cur.bv().text()->paragraphs().size() - 1);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|