3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
10 * \author Jürgen Vigna
11 * \author Abdelrazak Younes
13 * Full author contact details are available in file CREDITS.
21 #include "BufferParams.h"
22 #include "buffer_funcs.h"
23 #include "DocIterator.h"
24 #include "Paragraph.h"
25 #include "ParagraphList.h"
28 #include "mathed/MathSupport.h"
29 #include "mathed/MathData.h"
31 #include "insets/Inset.h"
33 #include "support/lassert.h"
34 #include "support/debug.h"
40 using namespace lyx::support;
46 These are the elements put on the undo stack. Each object contains
47 complete paragraphs from some cell and sufficient information to
48 restore the cursor state.
50 The cell is given by a DocIterator pointing to this cell, the
51 'interesting' range of paragraphs by counting them from begin and end
52 of cell, respectively.
54 The cursor is also given as DocIterator and should point to some place
55 in the stored paragraph range. In case of math, we simply store the
56 whole cell, as there usually is just a simple paragraph in a cell.
58 The idea is to store the contents of 'interesting' paragraphs in some
59 structure ('Undo') _before_ it is changed in some edit operation.
60 Obviously, the stored range should be as small as possible. However,
61 there is a lower limit: The StableDocIterator stored in the undo class
62 must be valid after the changes, too, as it will used as a pointer
63 where to insert the stored bits when performining undo.
68 UndoElement(UndoKind kin, StableDocIterator const & cb,
69 StableDocIterator const & cel,
70 pit_type fro, pit_type en, ParagraphList * pl,
71 MathData * ar, BufferParams const & bp,
72 bool ifb, bool lc, size_t gid) :
73 kind(kin), cur_before(cb), cell(cel), from(fro), end(en),
74 pars(pl), array(ar), bparams(0), isFullBuffer(ifb),
75 lyx_clean(lc), group_id(gid)
78 bparams = new BufferParams(bp);
81 UndoElement(UndoElement const & ue)
84 cur_before = ue.cur_before;
85 cur_after = ue.cur_after;
91 bparams = ue.isFullBuffer
92 ? new BufferParams(*ue.bparams) : ue.bparams;
93 isFullBuffer = ue.isFullBuffer;
94 lyx_clean = ue.lyx_clean;
95 group_id = ue.group_id;
103 /// Which kind of operation are we recording for?
105 /// the position of the cursor before recordUndo
106 StableDocIterator cur_before;
107 /// the position of the cursor at the end of the undo group
108 StableDocIterator cur_after;
109 /// the position of the cell described
110 StableDocIterator cell;
111 /// counted from begin of cell
113 /// complement to end of this cell
115 /// the contents of the saved Paragraphs (for texted)
116 ParagraphList * pars;
117 /// the contents of the saved MathData (for mathed)
119 /// Only used in case of full backups
120 BufferParams const * bparams;
121 /// Only used in case of full backups
123 /// Was the buffer clean at this point?
125 /// the element's group id
128 /// Protect construction
133 class UndoElementStack
136 /// limit is the maximum size of the stack
137 UndoElementStack(size_t limit = 100) { limit_ = limit; }
138 /// limit is the maximum size of the stack
139 ~UndoElementStack() { clear(); }
141 /// Return the top element.
142 UndoElement & top() { return c_.front(); }
144 /// Pop and throw away the top element.
145 void pop() { c_.pop_front(); }
147 /// Return true if the stack is empty.
148 bool empty() const { return c_.empty(); }
150 /// Clear all elements, deleting them.
152 for (size_t i = 0; i != c_.size(); ++i) {
159 /// Push an item on to the stack, deleting the bottom group on
161 void push(UndoElement const & v) {
163 if (c_.size() > limit_) {
164 // remove a whole group at once.
165 const size_t gid = c_.back().group_id;
166 while (!c_.empty() && c_.back().group_id == gid)
171 /// Mark all the elements of the stack as dirty
173 for (size_t i = 0; i != c_.size(); ++i)
174 c_[i].lyx_clean = false;
178 /// Internal contents.
179 std::deque<UndoElement> c_;
180 /// The maximum number elements stored.
187 Private(Buffer & buffer) : buffer_(buffer), undo_finished_(true),
188 group_id(0), group_level(0) {}
190 // Do one undo/redo step
191 void doTextUndoOrRedo(DocIterator & cur, UndoElementStack & stack,
192 UndoElementStack & otherStack);
193 // Apply one undo/redo group. Returns false if no undo possible.
194 bool textUndoOrRedo(DocIterator & cur, bool isUndoOperation);
197 void doRecordUndo(UndoKind kind,
198 DocIterator const & cell,
201 StableDocIterator const & cur,
203 UndoElementStack & stack);
205 void recordUndo(UndoKind kind,
206 DocIterator const & cell,
209 DocIterator const & cur,
215 UndoElementStack undostack_;
217 UndoElementStack redostack_;
219 /// The flag used by Undo::finishUndo().
222 /// Current group Id.
224 /// Current group nesting nevel.
229 /////////////////////////////////////////////////////////////////////
233 /////////////////////////////////////////////////////////////////////
236 Undo::Undo(Buffer & buffer)
237 : d(new Undo::Private(buffer))
249 d->undostack_.clear();
250 d->redostack_.clear();
251 d->undo_finished_ = true;
257 bool Undo::hasUndoStack() const
259 return !d->undostack_.empty();
263 bool Undo::hasRedoStack() const
265 return !d->redostack_.empty();
269 void Undo::markDirty()
271 d->undo_finished_ = true;
272 d->undostack_.markDirty();
273 d->redostack_.markDirty();
277 /////////////////////////////////////////////////////////////////////
281 ///////////////////////////////////////////////////////////////////////
283 static bool samePar(StableDocIterator const & i1, StableDocIterator const & i2)
285 StableDocIterator tmpi2 = i2;
286 tmpi2.pos() = i1.pos();
291 void Undo::Private::doRecordUndo(UndoKind kind,
292 DocIterator const & cell,
293 pit_type first_pit, pit_type last_pit,
294 StableDocIterator const & cur_before,
296 UndoElementStack & stack)
299 LYXERR0("There is no group open (creating one)");
303 if (first_pit > last_pit)
304 swap(first_pit, last_pit);
306 // Undo::ATOMIC are always recorded (no overlapping there).
307 // As nobody wants all removed character appear one by one when undoing,
308 // we want combine 'similar' non-ATOMIC undo recordings to one.
309 pit_type from = first_pit;
310 pit_type end = cell.lastpit() - last_pit;
312 && kind != ATOMIC_UNDO
314 && samePar(stack.top().cell, cell)
315 && stack.top().kind == kind
316 && stack.top().from == from
317 && stack.top().end == end) {
318 // reset cur_after; it will be filled correctly by endUndoGroup.
319 stack.top().cur_after = StableDocIterator();
324 LYXERR(Debug::UNDO, "Create full buffer undo element of group " << group_id);
326 LYXERR(Debug::UNDO, "Create undo element of group " << group_id);
327 // create the position information of the Undo entry
328 UndoElement undo(kind, cur_before, cell, from, end, 0, 0,
329 buffer_.params(), isFullBuffer, buffer_.isClean(), group_id);
331 // fill in the real data to be saved
332 if (cell.inMathed()) {
333 // simply use the whole cell
334 MathData & ar = cell.cell();
335 undo.array = new MathData(ar.buffer(), ar.begin(), ar.end());
337 // some more effort needed here as 'the whole cell' of the
338 // main Text _is_ the whole document.
339 // record the relevant paragraphs
340 Text const * text = cell.text();
342 ParagraphList const & plist = text->paragraphs();
343 ParagraphList::const_iterator first = plist.begin();
344 advance(first, first_pit);
345 ParagraphList::const_iterator last = plist.begin();
346 advance(last, last_pit + 1);
347 undo.pars = new ParagraphList(first, last);
350 // push the undo entry to undo stack
352 //lyxerr << "undo record: " << stack.top() << endl;
356 void Undo::Private::recordUndo(UndoKind kind,
357 DocIterator const & cell,
358 pit_type first_pit, pit_type last_pit,
359 DocIterator const & cur,
362 LASSERT(first_pit <= cell.lastpit(), /**/);
363 LASSERT(last_pit <= cell.lastpit(), /**/);
365 doRecordUndo(kind, cell, first_pit, last_pit, cur,
366 isFullBuffer, undostack_);
368 // next time we'll try again to combine entries if possible
369 undo_finished_ = false;
371 // If we ran recordUndo, it means that we plan to change the buffer
375 //lyxerr << "undostack:\n";
376 //for (size_t i = 0, n = buf.undostack().size(); i != n && i < 6; ++i)
377 // lyxerr << " " << i << ": " << buf.undostack()[i] << endl;
381 void Undo::Private::doTextUndoOrRedo(DocIterator & cur, UndoElementStack & stack, UndoElementStack & otherstack)
383 // Adjust undo stack and get hold of current undo data.
384 UndoElement & undo = stack.top();
385 LYXERR(Debug::UNDO, "Undo element of group " << undo.group_id);
386 // We'll pop the stack only when we're done with this element. So do NOT
387 // try to return early.
389 // We will store in otherstack the part of the document under 'undo'
390 DocIterator cell_dit = undo.cell.asDocIterator(&buffer_);
392 doRecordUndo(ATOMIC_UNDO, cell_dit,
393 undo.from, cell_dit.lastpit() - undo.end, undo.cur_after,
394 undo.isFullBuffer, otherstack);
395 otherstack.top().cur_after = undo.cur_before;
397 // This does the actual undo/redo.
398 //LYXERR0("undo, performing: " << undo);
399 DocIterator dit = undo.cell.asDocIterator(&buffer_);
400 if (undo.isFullBuffer) {
401 LASSERT(undo.pars, /**/);
402 // This is a full document
403 delete otherstack.top().bparams;
404 otherstack.top().bparams = new BufferParams(buffer_.params());
405 buffer_.params() = *undo.bparams;
406 swap(buffer_.paragraphs(), *undo.pars);
409 } else if (dit.inMathed()) {
410 // We stored the full cell here as there is not much to be
411 // gained by storing just 'a few' paragraphs (most if not
412 // all math inset cells have just one paragraph!)
413 //LYXERR0("undo.array: " << *undo.array);
414 LASSERT(undo.array, /**/);
415 dit.cell().swap(*undo.array);
419 // Some finer machinery is needed here.
420 Text * text = dit.text();
422 LASSERT(undo.pars, /**/);
423 ParagraphList & plist = text->paragraphs();
425 // remove new stuff between first and last
426 ParagraphList::iterator first = plist.begin();
427 advance(first, undo.from);
428 ParagraphList::iterator last = plist.begin();
429 advance(last, plist.size() - undo.end);
430 plist.erase(first, last);
432 // re-insert old stuff instead
433 first = plist.begin();
434 advance(first, undo.from);
436 // this ugly stuff is needed until we get rid of the
437 // inset_owner backpointer
438 ParagraphList::iterator pit = undo.pars->begin();
439 ParagraphList::iterator const end = undo.pars->end();
440 for (; pit != end; ++pit)
441 pit->setInsetOwner(dit.realInset());
442 plist.insert(first, undo.pars->begin(), undo.pars->end());
446 LASSERT(undo.pars == 0, /**/);
447 LASSERT(undo.array == 0, /**/);
449 if (undo.cur_before.size())
450 cur = undo.cur_before.asDocIterator(&buffer_);
455 // Now that we're done with undo, we pop it off the stack.
460 bool Undo::Private::textUndoOrRedo(DocIterator & cur, bool isUndoOperation)
462 undo_finished_ = true;
464 UndoElementStack & stack = isUndoOperation ? undostack_ : redostack_;
470 UndoElementStack & otherstack = isUndoOperation ? redostack_ : undostack_;
472 const size_t gid = stack.top().group_id;
473 while (!stack.empty() && stack.top().group_id == gid)
474 doTextUndoOrRedo(cur, stack, otherstack);
476 // Adapt the new material to current buffer.
477 buffer_.setBuffersForInsets(); // FIXME This shouldn't be here.
482 void Undo::finishUndo()
484 // Make sure the next operation will be stored.
485 d->undo_finished_ = true;
489 bool Undo::textUndo(DocIterator & cur)
491 return d->textUndoOrRedo(cur, true);
495 bool Undo::textRedo(DocIterator & cur)
497 return d->textUndoOrRedo(cur, false);
501 void Undo::beginUndoGroup()
503 if (d->group_level == 0) {
504 // create a new group
506 LYXERR(Debug::UNDO, "+++++++Creating new group " << d->group_id);
512 void Undo::endUndoGroup()
514 if (d->group_level == 0)
515 LYXERR0("There is no undo group to end here");
517 if (d->group_level == 0) {
518 // real end of the group
519 LYXERR(Debug::UNDO, "-------End of group " << d->group_id);
524 void Undo::endUndoGroup(DocIterator const & cur)
527 if (!d->undostack_.empty() && !d->undostack_.top().cur_after.size())
528 d->undostack_.top().cur_after = cur;
532 // FIXME: remove these convenience functions and make
533 // Private::recordUndo public as sole interface. The code in the
534 // convenience functions can move to Cursor.cpp.
536 void Undo::recordUndo(DocIterator const & cur, UndoKind kind)
538 d->recordUndo(kind, cur, cur.pit(), cur.pit(), cur, false);
542 void Undo::recordUndoInset(DocIterator const & cur, UndoKind kind,
545 if (!inset || inset == &cur.inset()) {
548 d->recordUndo(kind, c, c.pit(), c.pit(), cur, false);
549 } else if (inset == cur.nextInset())
550 recordUndo(cur, kind);
552 LYXERR0("Inset not found, no undo stack added.");
556 void Undo::recordUndo(DocIterator const & cur, UndoKind kind, pit_type from)
558 d->recordUndo(kind, cur, cur.pit(), from, cur, false);
562 void Undo::recordUndo(DocIterator const & cur, UndoKind kind,
563 pit_type from, pit_type to)
565 d->recordUndo(kind, cur, from, to, cur, false);
569 void Undo::recordUndoFullDocument(DocIterator const & cur)
571 // This one may happen outside of the main undo group, so we
572 // put it in its own subgroup to avoid complaints.
574 d->recordUndo(ATOMIC_UNDO, doc_iterator_begin(&d->buffer_),
575 0, d->buffer_.paragraphs().size() - 1, cur, true);