+
+ // 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.
+ //lyxerr << "undo, performing: " << undo << std::endl;
+ DocIterator dit = undo.cell.asDocIterator(&buf->inset());
+ if (undo.isFullBuffer) {
+ // This is a full document
+ otherstack.top().bparams = buf->params();
+ buf->params() = undo.bparams;
+ buf->paragraphs() = undo.pars;
+ } else if (dit.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!)
+ //lyxerr << "undo.array=" << undo.array <<endl;
+ asArray(undo.array, dit.cell());
+ } else {
+ // Some finer machinery is needed here.
+ LyXText * text = dit.text();
+ BOOST_ASSERT(text);
+ ParagraphList & plist = text->paragraphs();
+
+ // remove new stuff between first and last
+ ParagraphList::iterator first = plist.begin();
+ advance(first, undo.from);
+ ParagraphList::iterator last = plist.begin();
+ advance(last, plist.size() - undo.end);
+ plist.erase(first, last);
+
+ // re-insert old stuff instead
+ first = plist.begin();
+ advance(first, undo.from);
+
+ // this ugly stuff is needed until we get rid of the
+ // inset_owner backpointer
+ ParagraphList::iterator pit = undo.pars.begin();
+ ParagraphList::iterator const end = undo.pars.end();
+ for (; pit != end; ++pit)
+ pit->setInsetOwner(dit.realInset());
+ plist.insert(first, undo.pars.begin(), undo.pars.end());
+ }
+
+ // Set cursor
+ LCursor & cur = bv.cursor();
+ cur.setCursor(undo.cursor.asDocIterator(&buf->inset()));
+ cur.selection() = false;
+ cur.resetAnchor();
+ finishUndo();
+
+ return true;
+}
+
+} // namespace anon
+
+
+void finishUndo()
+{
+ // Make sure the next operation will be stored.
+ undo_finished = true;
+}
+
+
+bool textUndo(BufferView & bv)
+{
+ return textUndoOrRedo(bv, bv.buffer()->undostack(),
+ bv.buffer()->redostack());
+}
+
+
+bool textRedo(BufferView & bv)
+{
+ return textUndoOrRedo(bv, bv.buffer()->redostack(),
+ bv.buffer()->undostack());
+}
+
+
+void recordUndo(Undo::undo_kind kind,
+ LCursor & cur, pit_type first, pit_type last)
+{
+ Buffer * buf = cur.bv().buffer();
+ recordUndo(kind, cur, first, last, buf->undostack());
+ buf->redostack().clear();
+ //lyxerr << "undostack:\n";
+ //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
+ // lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
+}
+
+
+void recordUndo(LCursor & cur, Undo::undo_kind kind)
+{
+ recordUndo(kind, cur, cur.pit(), cur.pit());
+}
+
+
+void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
+{
+ LCursor c = cur;
+ c.pop();
+ Buffer * buf = cur.bv().buffer();
+ doRecordUndo(kind, c, c.pit(), c.pit(), cur,
+ buf->params(), false, buf->undostack());
+}
+
+
+void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
+{
+ recordUndo(kind, cur, cur.selBegin().pit(), cur.selEnd().pit());
+}
+
+
+void recordUndo(LCursor & cur, Undo::undo_kind kind, pit_type from)
+{
+ recordUndo(kind, cur, cur.pit(), from);