2001-07-06 15:57:54 +00:00
|
|
|
/* This file is part of
|
2002-03-21 17:27:08 +00:00
|
|
|
* ======================================================
|
|
|
|
*
|
2001-07-06 15:57:54 +00:00
|
|
|
* LyX, The Document Processor
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2001-07-06 15:57:54 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
|
|
|
*
|
|
|
|
* ====================================================== */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "undo_funcs.h"
|
|
|
|
#include "lyxtext.h"
|
2003-05-16 07:44:00 +00:00
|
|
|
#include "funcrequest.h"
|
2001-07-06 15:57:54 +00:00
|
|
|
#include "BufferView.h"
|
|
|
|
#include "buffer.h"
|
2003-02-20 17:39:48 +00:00
|
|
|
#include "insets/updatableinset.h"
|
2003-04-16 00:02:38 +00:00
|
|
|
#include "insets/insettext.h"
|
2001-07-06 15:57:54 +00:00
|
|
|
#include "debug.h"
|
2001-07-29 15:34:18 +00:00
|
|
|
#include "support/LAssert.h"
|
2001-12-19 08:59:15 +00:00
|
|
|
#include "iterators.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
|
|
|
|
ParagraphList * plist = &buf->paragraphs;
|
|
|
|
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-06-05 12:16:06 +00:00
|
|
|
int const first_offset = std::distance(plist->begin(), first);
|
2003-07-25 19:18:43 +00:00
|
|
|
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-07-25 11:58:47 +00:00
|
|
|
if (! buf->undostack.empty()
|
|
|
|
&& buf->undostack.top().kind == kind
|
|
|
|
&& 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();
|
|
|
|
ParagraphList * plist = &buf->paragraphs;
|
|
|
|
|
|
|
|
// Remove new stuff between first and last
|
|
|
|
{
|
|
|
|
ParagraphList::iterator first = plist->begin();
|
|
|
|
advance(first, undo.first_par_offset);
|
|
|
|
ParagraphList::iterator last = plist->begin();
|
|
|
|
advance(last, plist->size() - undo.last_par_offset);
|
|
|
|
plist->erase(first, ++last);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-insert old stuff instead
|
|
|
|
{
|
|
|
|
if (plist->empty()) {
|
|
|
|
plist->assign(undo.pars.begin(), undo.pars.end());
|
|
|
|
} else {
|
|
|
|
ParagraphList::iterator first = plist->begin();
|
|
|
|
advance(first, undo.first_par_offset);
|
|
|
|
plist->insert(first, undo.pars.begin(), undo.pars.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// And repaint the lot
|
|
|
|
bv->text->postPaint();
|
|
|
|
|
|
|
|
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();
|
|
|
|
bv->text->postPaint();
|
|
|
|
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-06-04 12:45:26 +00:00
|
|
|
ParagraphList & plist = buf->paragraphs;
|
|
|
|
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-05-01 23:24:30 +00:00
|
|
|
return textUndoOrRedo(bv, bv->buffer()->undostack,
|
|
|
|
bv->buffer()->redostack);
|
2003-04-29 14:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool textRedo(BufferView * bv)
|
|
|
|
{
|
2003-05-01 23:24:30 +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-07-25 19:18:43 +00:00
|
|
|
recordUndo(bv, kind, first, last, bv->buffer()->undostack);
|
2003-07-25 11:58:47 +00:00
|
|
|
bv->buffer()->redostack.clear();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|