+ finishUndo();
+
+ if (stack.empty()) {
+ // Nothing to do.
+ return false;
+ }
+
+ // 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;