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-03-26 08:27:14 +00:00
|
|
|
|
using std::advance;
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
using lyx::par_type;
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
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-03-08 21:14:45 +00:00
|
|
|
|
return os << " from: " << undo.from
|
|
|
|
|
<< " end: " << undo.end
|
|
|
|
|
<< " 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-03-25 09:16:36 +00:00
|
|
|
|
LCursor & cur, par_type first_par, par_type last_par,
|
2003-10-14 13:01:49 +00:00
|
|
|
|
limited_stack<Undo> & stack)
|
|
|
|
|
{
|
2004-03-08 21:14:45 +00:00
|
|
|
|
BOOST_ASSERT(first_par <= cur.lastpar());
|
|
|
|
|
BOOST_ASSERT(last_par <= cur.lastpar());
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
if (first_par > last_par)
|
|
|
|
|
std::swap(first_par, last_par);
|
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-03-31 19:11:56 +00:00
|
|
|
|
undo.cursor = StableDocIterator(cur);
|
2004-03-08 21:14:45 +00:00
|
|
|
|
undo.from = first_par;
|
|
|
|
|
undo.end = cur.lastpar() - last_par;
|
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()
|
|
|
|
|
&& stack.top().cursor.size() == undo.cursor.size()
|
|
|
|
|
&& 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
|
|
|
|
|
if (cur.inMathed()) {
|
|
|
|
|
// simply use the whole cell
|
|
|
|
|
undo.array = asString(cur.cell());
|
|
|
|
|
} 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-03-08 21:14:45 +00:00
|
|
|
|
LyXText * text = cur.text();
|
|
|
|
|
BOOST_ASSERT(text);
|
2004-03-01 17:12:09 +00:00
|
|
|
|
ParagraphList & plist = text->paragraphs();
|
|
|
|
|
ParagraphList::iterator first = plist.begin();
|
|
|
|
|
advance(first, first_par);
|
|
|
|
|
ParagraphList::iterator last = plist.begin();
|
2004-03-08 21:14:45 +00:00
|
|
|
|
advance(last, last_par + 1);
|
|
|
|
|
undo.pars = ParagraphList(first, last);
|
2004-03-01 17:12:09 +00:00
|
|
|
|
}
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
// push the undo entry to undo stack
|
|
|
|
|
//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-03-08 21:14:45 +00:00
|
|
|
|
void performUndoOrRedo(BufferView & bv, Undo const & undo)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2004-03-08 21:14:45 +00:00
|
|
|
|
LCursor & cur = bv.cursor();
|
2003-10-15 08:49:44 +00:00
|
|
|
|
lyxerr << "undo, performing: " << undo << std::endl;
|
2004-03-31 19:11:56 +00:00
|
|
|
|
cur.setCursor(undo.cursor.asDocIterator(&bv.buffer()->inset()), false);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
if (cur.inMathed()) {
|
|
|
|
|
// 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!)
|
|
|
|
|
asArray(undo.array, cur.cell());
|
|
|
|
|
} else {
|
|
|
|
|
// Some finer machinery is needed here.
|
|
|
|
|
LyXText * text = cur.text();
|
|
|
|
|
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-03-08 21:14:45 +00:00
|
|
|
|
cur.resetAnchor();
|
2003-10-14 13:01:49 +00:00
|
|
|
|
finishUndo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-10-15 08:49:44 +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()) {
|
2003-10-15 08:49:44 +00:00
|
|
|
|
// nothing to do
|
2003-10-14 13:01:49 +00:00
|
|
|
|
finishUndo();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Undo undo = stack.top();
|
|
|
|
|
stack.pop();
|
|
|
|
|
finishUndo();
|
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
// this implements redo
|
2004-03-18 16:41:45 +00:00
|
|
|
|
otherstack.push(undo);
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator dit =
|
|
|
|
|
undo.cursor.asDocIterator(&bv.buffer()->inset());
|
2004-03-18 16:41:45 +00:00
|
|
|
|
if (dit.inMathed()) {
|
2004-03-27 12:46:30 +00:00
|
|
|
|
// Easy way out: store a full cell.
|
|
|
|
|
otherstack.top().array = asString(dit.cell());
|
2004-03-18 16:41:45 +00:00
|
|
|
|
} else {
|
2004-03-27 12:46:30 +00:00
|
|
|
|
// As cells might be too large in texted, store just a part
|
|
|
|
|
// of the paragraph list.
|
2004-03-18 16:41:45 +00:00
|
|
|
|
otherstack.top().pars.clear();
|
|
|
|
|
LyXText * text = dit.text();
|
|
|
|
|
BOOST_ASSERT(text);
|
|
|
|
|
ParagraphList & plist = text->paragraphs();
|
|
|
|
|
if (undo.from + undo.end <= int(plist.size())) {
|
|
|
|
|
ParagraphList::iterator first = plist.begin();
|
|
|
|
|
advance(first, undo.from);
|
|
|
|
|
ParagraphList::iterator last = plist.begin();
|
|
|
|
|
advance(last, plist.size() - undo.end);
|
|
|
|
|
otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-03-18 16:41:45 +00:00
|
|
|
|
otherstack.top().cursor = bv.cursor();
|
|
|
|
|
//lyxerr << " undo other: " << otherstack.top() << std::endl;
|
2003-10-14 13:01:49 +00:00
|
|
|
|
|
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()
|
|
|
|
|
{
|
2003-10-15 08:49:44 +00:00
|
|
|
|
// makes 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-03-25 09:16:36 +00:00
|
|
|
|
LCursor & cur, par_type first, par_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-03-25 09:16:36 +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-02-03 08:56:28 +00:00
|
|
|
|
recordUndo(kind, cur, cur.par(), cur.par());
|
2003-10-14 13:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-13 07:30:59 +00:00
|
|
|
|
void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
|
|
|
|
|
{
|
|
|
|
|
recordUndo(kind, cur, cur.selBegin().par(), cur.selEnd().par());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
void recordUndo(LCursor & cur, Undo::undo_kind kind, par_type from)
|
2003-10-14 13:01:49 +00:00
|
|
|
|
{
|
2004-02-03 08:56:28 +00:00
|
|
|
|
recordUndo(kind, cur, cur.par(), 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-03-25 09:16:36 +00:00
|
|
|
|
par_type from, par_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
|
|
|
|
}
|