2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file undo_funcs.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-08-23 00:17:00 +00:00
|
|
|
|
* \author Asger Alstrup
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author John Levon
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
* \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.
|
|
|
|
|
*/
|
2001-07-06 15:57:54 +00:00
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "undo_funcs.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
|
2001-07-06 15:57:54 +00:00
|
|
|
|
#include "buffer.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "BufferView.h"
|
|
|
|
|
#include "funcrequest.h"
|
2001-12-19 08:59:15 +00:00
|
|
|
|
#include "iterators.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "lyxtext.h"
|
2003-09-06 17:23:08 +00:00
|
|
|
|
#include "paragraph.h"
|
2001-12-19 08:59:15 +00:00
|
|
|
|
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "insets/updatableinset.h"
|
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
|
/// The flag used by FinishUndo().
|
2001-07-06 15:57:54 +00:00
|
|
|
|
bool undo_finished;
|
2003-07-25 19:18:43 +00:00
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
|
/// Whether actions are not added to the undo stacks.
|
2001-07-06 15:57:54 +00:00
|
|
|
|
bool undo_frozen;
|
|
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
|
namespace {
|
2001-12-13 17:19:53 +00:00
|
|
|
|
|
2001-07-06 15:57:54 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
void recordUndo(BufferView * bv, Undo::undo_kind kind,
|
2003-06-04 12:45:26 +00:00
|
|
|
|
ParagraphList::iterator first, ParagraphList::iterator last,
|
2003-06-04 07:14:05 +00:00
|
|
|
|
limited_stack<Undo> & stack)
|
2001-07-06 15:57:54 +00:00
|
|
|
|
{
|
2003-06-04 07:14:05 +00:00
|
|
|
|
Buffer * buf = bv->buffer();
|
2001-07-06 15:57:54 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// First, record inset id, if cursor is in one
|
2003-06-04 16:14:36 +00:00
|
|
|
|
UpdatableInset * inset = first->inInset();
|
|
|
|
|
LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
|
|
|
|
|
int const inset_id = inset ? inset->id() : -1;
|
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// We simply record the entire outer paragraphs
|
2003-09-09 09:47:59 +00:00
|
|
|
|
ParagraphList & plist = buf->paragraphs();
|
2003-07-25 19:18:43 +00:00
|
|
|
|
ParIterator null = buf->par_iterator_end();
|
2003-06-04 12:45:26 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// First, identify the outer paragraphs
|
2003-06-04 12:45:26 +00:00
|
|
|
|
for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
|
2003-06-30 23:56:22 +00:00
|
|
|
|
if (it->id() == first->id())
|
|
|
|
|
first = it.outerPar();
|
2003-06-05 12:16:06 +00:00
|
|
|
|
if (it->id() == last->id())
|
2003-06-30 23:56:22 +00:00
|
|
|
|
last = it.outerPar();
|
2003-05-23 10:33:40 +00:00
|
|
|
|
}
|
2001-07-06 15:57:54 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// And calculate a stable reference to them
|
2003-09-09 09:47:59 +00:00
|
|
|
|
int const first_offset = std::distance(plist.begin(), first);
|
|
|
|
|
int const last_offset = std::distance(last, plist.end());
|
2003-05-23 10:33:40 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// Undo::ATOMIC are always recorded (no overlapping there).
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// Overlapping only with insert and delete inside one paragraph:
|
2003-07-25 11:58:47 +00:00
|
|
|
|
// Nobody wants all removed character appear one by one when undoing.
|
2003-07-25 19:18:43 +00:00
|
|
|
|
if (! undo_finished && kind != Undo::ATOMIC) {
|
2003-05-01 23:24:30 +00:00
|
|
|
|
// Check whether storing is needed.
|
2003-09-09 18:27:24 +00:00
|
|
|
|
if (! buf->undostack().empty()
|
|
|
|
|
&& buf->undostack().top().kind == kind
|
2003-09-09 09:47:59 +00:00
|
|
|
|
&& buf->undostack().top().first_par_offset == first_offset
|
|
|
|
|
&& buf->undostack().top().last_par_offset == last_offset) {
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// No additonal undo recording needed -
|
|
|
|
|
// effectively, we combine undo recordings to one.
|
2003-06-04 07:14:05 +00:00
|
|
|
|
return;
|
2001-07-06 15:57:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// Record the cursor position in a stable way.
|
2003-06-04 16:14:36 +00:00
|
|
|
|
int const cursor_offset = std::distance
|
|
|
|
|
(text->ownerParagraphs().begin(), text->cursor.par());
|
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// Make and push the Undo entry
|
|
|
|
|
stack.push(Undo(kind, inset_id,
|
2003-06-04 16:14:36 +00:00
|
|
|
|
first_offset, last_offset,
|
|
|
|
|
cursor_offset, text->cursor.pos(),
|
|
|
|
|
ParagraphList()));
|
2003-06-04 07:14:05 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// Record the relevant paragraphs
|
2003-06-04 07:14:05 +00:00
|
|
|
|
ParagraphList & undo_pars = stack.top().pars;
|
2001-07-06 15:57:54 +00:00
|
|
|
|
|
2003-06-04 12:45:26 +00:00
|
|
|
|
for (ParagraphList::iterator it = first; it != last; ++it) {
|
2003-06-04 07:14:05 +00:00
|
|
|
|
undo_pars.push_back(*it);
|
|
|
|
|
undo_pars.back().id(it->id());
|
2003-05-25 08:49:48 +00:00
|
|
|
|
}
|
2003-06-04 12:45:26 +00:00
|
|
|
|
undo_pars.push_back(*last);
|
2003-06-04 07:14:05 +00:00
|
|
|
|
undo_pars.back().id(last->id());
|
2001-07-06 15:57:54 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// And make sure that next time, we should be combining if possible
|
2001-07-06 15:57:54 +00:00
|
|
|
|
undo_finished = false;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// Returns false if no undo possible.
|
|
|
|
|
bool performUndoOrRedo(BufferView * bv, Undo & undo)
|
|
|
|
|
{
|
|
|
|
|
Buffer * buf = bv->buffer();
|
2003-09-09 09:47:59 +00:00
|
|
|
|
ParagraphList & plist = buf->paragraphs();
|
2003-07-25 19:18:43 +00:00
|
|
|
|
|
|
|
|
|
// Remove new stuff between first and last
|
|
|
|
|
{
|
2003-09-09 09:47:59 +00:00
|
|
|
|
ParagraphList::iterator first = plist.begin();
|
2003-07-25 19:18:43 +00:00
|
|
|
|
advance(first, undo.first_par_offset);
|
2003-09-09 09:47:59 +00:00
|
|
|
|
ParagraphList::iterator last = plist.begin();
|
|
|
|
|
advance(last, plist.size() - undo.last_par_offset);
|
|
|
|
|
plist.erase(first, ++last);
|
2003-07-25 19:18:43 +00:00
|
|
|
|
}
|
2003-09-09 18:27:24 +00:00
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
// Re-insert old stuff instead
|
|
|
|
|
{
|
2003-09-09 09:47:59 +00:00
|
|
|
|
if (plist.empty()) {
|
|
|
|
|
plist.assign(undo.pars.begin(), undo.pars.end());
|
2003-07-25 19:18:43 +00:00
|
|
|
|
} else {
|
2003-09-09 09:47:59 +00:00
|
|
|
|
ParagraphList::iterator first = plist.begin();
|
2003-07-25 19:18:43 +00:00
|
|
|
|
advance(first, undo.first_par_offset);
|
2003-09-09 09:47:59 +00:00
|
|
|
|
plist.insert(first, undo.pars.begin(), undo.pars.end());
|
2003-07-25 19:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rebreak the entire document
|
|
|
|
|
bv->text->fullRebreak();
|
|
|
|
|
|
|
|
|
|
// set cursor
|
|
|
|
|
{
|
|
|
|
|
// Get a hold of the inset for the cursor, if relevant
|
|
|
|
|
UpdatableInset * inset =
|
|
|
|
|
static_cast<UpdatableInset *>(
|
|
|
|
|
buf->getInsetFromID(undo.inset_id));
|
|
|
|
|
|
|
|
|
|
LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
|
|
|
|
|
ParagraphList::iterator cursor = text->ownerParagraphs().begin();
|
|
|
|
|
advance(cursor, undo.cursor_par_offset);
|
|
|
|
|
text->setCursorIntern(cursor, undo.cursor_pos);
|
|
|
|
|
|
|
|
|
|
if (inset) {
|
|
|
|
|
// Magic needed to update inset internal state
|
|
|
|
|
FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
|
|
|
|
|
inset->localDispatch(cmd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set cursor again to force the position to be the right one
|
|
|
|
|
text->setCursorIntern(cursor, undo.cursor_pos);
|
|
|
|
|
|
|
|
|
|
// Clear any selection and set the selection
|
|
|
|
|
// cursor for any new selection.
|
|
|
|
|
text->clearSelection();
|
|
|
|
|
text->selection.cursor = text->cursor;
|
|
|
|
|
text->updateCounters();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finishUndo();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
|
// Returns false if no undo possible.
|
2003-04-29 14:50:11 +00:00
|
|
|
|
bool textUndoOrRedo(BufferView * bv,
|
2003-06-04 07:14:05 +00:00
|
|
|
|
limited_stack<Undo> & stack,
|
2003-06-04 12:45:26 +00:00
|
|
|
|
limited_stack<Undo> & otherstack)
|
2002-05-26 19:40:17 +00:00
|
|
|
|
{
|
2003-04-29 14:50:11 +00:00
|
|
|
|
if (stack.empty()) {
|
2003-07-25 19:18:43 +00:00
|
|
|
|
/*
|
|
|
|
|
* Finish the undo operation in the case there was no entry
|
|
|
|
|
* on the stack to perform.
|
|
|
|
|
*/
|
|
|
|
|
freezeUndo();
|
|
|
|
|
bv->unlockInset(bv->theLockingInset());
|
|
|
|
|
finishUndo();
|
|
|
|
|
unFreezeUndo();
|
2002-05-26 19:40:17 +00:00
|
|
|
|
return false;
|
2001-07-06 15:57:54 +00:00
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
2003-06-04 07:14:05 +00:00
|
|
|
|
Undo undo = stack.top();
|
2003-04-29 14:50:11 +00:00
|
|
|
|
stack.pop();
|
2002-05-26 19:40:17 +00:00
|
|
|
|
finishUndo();
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
2003-05-27 13:55:03 +00:00
|
|
|
|
if (!undo_frozen) {
|
2003-06-04 12:45:26 +00:00
|
|
|
|
otherstack.push(undo);
|
|
|
|
|
otherstack.top().pars.clear();
|
2003-05-27 13:55:03 +00:00
|
|
|
|
Buffer * buf = bv->buffer();
|
2003-09-09 09:47:59 +00:00
|
|
|
|
ParagraphList & plist = buf->paragraphs();
|
2003-06-04 12:45:26 +00:00
|
|
|
|
if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
|
|
|
|
|
ParagraphList::iterator first = plist.begin();
|
|
|
|
|
advance(first, undo.first_par_offset);
|
|
|
|
|
ParagraphList::iterator last = plist.begin();
|
|
|
|
|
advance(last, plist.size() - undo.last_par_offset + 1);
|
|
|
|
|
otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
|
2002-05-26 19:40:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
2003-06-04 12:45:26 +00:00
|
|
|
|
// Now we can unlock the inset for safety because the inset
|
2003-05-01 23:24:30 +00:00
|
|
|
|
// pointer could be changed during the undo-function. Anyway
|
|
|
|
|
// if needed we have to lock the right inset/position if this
|
|
|
|
|
// is requested.
|
2002-05-26 19:40:17 +00:00
|
|
|
|
freezeUndo();
|
|
|
|
|
bv->unlockInset(bv->theLockingInset());
|
2003-07-25 19:18:43 +00:00
|
|
|
|
bool const ret = performUndoOrRedo(bv, undo);
|
2002-05-26 19:40:17 +00:00
|
|
|
|
unFreezeUndo();
|
|
|
|
|
return ret;
|
2001-07-06 15:57:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
|
} // namespace anon
|
2001-12-13 17:19:53 +00:00
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
|
void freezeUndo()
|
|
|
|
|
{
|
2003-05-01 23:24:30 +00:00
|
|
|
|
// This is dangerous and for internal use only.
|
2003-04-29 14:50:11 +00:00
|
|
|
|
undo_frozen = true;
|
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
|
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
|
void unFreezeUndo()
|
|
|
|
|
{
|
2003-05-01 23:24:30 +00:00
|
|
|
|
// This is dangerous and for internal use only.
|
2003-04-29 14:50:11 +00:00
|
|
|
|
undo_frozen = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
void finishUndo()
|
|
|
|
|
{
|
|
|
|
|
// Makes sure the next operation will be stored.
|
|
|
|
|
undo_finished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
|
bool textUndo(BufferView * bv)
|
|
|
|
|
{
|
2003-09-09 09:47:59 +00:00
|
|
|
|
return textUndoOrRedo(bv, bv->buffer()->undostack(),
|
|
|
|
|
bv->buffer()->redostack());
|
2003-04-29 14:50:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool textRedo(BufferView * bv)
|
|
|
|
|
{
|
2003-09-09 09:47:59 +00:00
|
|
|
|
return textUndoOrRedo(bv, bv->buffer()->redostack(),
|
|
|
|
|
bv->buffer()->undostack());
|
2002-05-26 19:40:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
void recordUndo(BufferView * bv, Undo::undo_kind kind,
|
2003-07-25 11:58:47 +00:00
|
|
|
|
ParagraphList::iterator first, ParagraphList::iterator last)
|
2003-06-27 11:53:41 +00:00
|
|
|
|
{
|
2003-07-25 11:58:47 +00:00
|
|
|
|
if (!undo_frozen) {
|
2003-09-09 09:47:59 +00:00
|
|
|
|
recordUndo(bv, kind, first, last, bv->buffer()->undostack());
|
|
|
|
|
bv->buffer()->redostack().clear();
|
2003-07-25 11:58:47 +00:00
|
|
|
|
}
|
2003-06-27 11:53:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
void recordUndo(BufferView * bv, Undo::undo_kind kind,
|
2003-05-23 10:33:40 +00:00
|
|
|
|
ParagraphList::iterator first)
|
2002-05-26 19:40:17 +00:00
|
|
|
|
{
|
2003-07-25 19:18:43 +00:00
|
|
|
|
recordUndo(bv, kind, first, first);
|
2003-05-23 10:33:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-25 19:18:43 +00:00
|
|
|
|
void recordUndo(BufferView * bv, Undo::undo_kind kind)
|
2003-05-23 10:33:40 +00:00
|
|
|
|
{
|
2003-07-25 19:18:43 +00:00
|
|
|
|
recordUndo(bv, kind, bv->text->cursor.par());
|
2002-05-26 19:40:17 +00:00
|
|
|
|
}
|
|
|
|
|
|