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"
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/debug.h"
34 #include "support/gettext.h"
35 #include "support/lassert.h"
41 using namespace lyx::support;
47 These are the elements put on the undo stack. Each object contains
48 complete paragraphs from some cell and sufficient information to
49 restore the cursor state.
51 The cell is given by a DocIterator pointing to this cell, the
52 'interesting' range of paragraphs by counting them from begin and end
53 of cell, respectively.
55 The cursor is also given as DocIterator and should point to some place
56 in the stored paragraph range. In case of math, we simply store the
57 whole cell, as there usually is just a simple paragraph in a cell.
59 The idea is to store the contents of 'interesting' paragraphs in some
60 structure ('Undo') _before_ it is changed in some edit operation.
61 Obviously, the stored range should be as small as possible. However,
62 there is a lower limit: The StableDocIterator stored in the undo class
63 must be valid after the changes, too, as it will used as a pointer
64 where to insert the stored bits when performining undo.
69 UndoElement(UndoKind kin, CursorData const & cb,
70 StableDocIterator const & cel,
71 pit_type fro, pit_type en, ParagraphList * pl,
72 MathData * ar, BufferParams const & bp,
73 bool ifb, bool lc, size_t gid) :
74 kind(kin), cur_before(cb), cell(cel), from(fro), end(en),
75 pars(pl), array(ar), bparams(0), isFullBuffer(ifb),
76 lyx_clean(lc), group_id(gid)
79 bparams = new BufferParams(bp);
82 UndoElement(UndoElement const & ue)
85 cur_before = ue.cur_before;
86 cur_after = ue.cur_after;
92 bparams = ue.isFullBuffer
93 ? new BufferParams(*ue.bparams) : ue.bparams;
94 isFullBuffer = ue.isFullBuffer;
95 lyx_clean = ue.lyx_clean;
96 group_id = ue.group_id;
104 /// Which kind of operation are we recording for?
106 /// the position of the cursor before recordUndo
107 CursorData cur_before;
108 /// the position of the cursor at the end of the undo group
109 CursorData cur_after;
110 /// the position of the cell described
111 StableDocIterator cell;
112 /// counted from begin of cell
114 /// complement to end of this cell
116 /// the contents of the saved Paragraphs (for texted)
117 ParagraphList * pars;
118 /// the contents of the saved MathData (for mathed)
120 /// Only used in case of full backups
121 BufferParams const * bparams;
122 /// Only used in case of full backups
124 /// Was the buffer clean at this point?
126 /// the element's group id
129 /// Protect construction
134 class UndoElementStack
137 /// limit is the maximum size of the stack
138 UndoElementStack(size_t limit = 100) { limit_ = limit; }
139 /// limit is the maximum size of the stack
140 ~UndoElementStack() { clear(); }
142 /// Return the top element.
143 UndoElement & top() { return c_.front(); }
145 /// Pop and throw away the top element.
146 void pop() { c_.pop_front(); }
148 /// Return true if the stack is empty.
149 bool empty() const { return c_.empty(); }
151 /// Clear all elements, deleting them.
153 for (size_t i = 0; i != c_.size(); ++i) {
160 /// Push an item on to the stack, deleting the bottom group on
162 void push(UndoElement const & v) {
164 if (c_.size() > limit_) {
165 // remove a whole group at once.
166 const size_t gid = c_.back().group_id;
167 while (!c_.empty() && c_.back().group_id == gid)
172 /// Mark all the elements of the stack as dirty
174 for (size_t i = 0; i != c_.size(); ++i)
175 c_[i].lyx_clean = false;
179 /// Internal contents.
180 std::deque<UndoElement> c_;
181 /// The maximum number elements stored.
188 Private(Buffer & buffer) : buffer_(buffer), undo_finished_(true),
189 group_id(0), group_level(0) {}
191 // Do one undo/redo step
192 void doTextUndoOrRedo(CursorData & cur, UndoElementStack & stack,
193 UndoElementStack & otherStack);
194 // Apply one undo/redo group. Returns false if no undo possible.
195 bool textUndoOrRedo(CursorData & cur, bool isUndoOperation);
198 void doRecordUndo(UndoKind kind,
199 DocIterator const & cell,
202 CursorData const & cur,
204 UndoElementStack & stack);
206 void recordUndo(UndoKind kind,
207 DocIterator const & cell,
210 CursorData const & cur,
216 UndoElementStack undostack_;
218 UndoElementStack redostack_;
220 /// The flag used by Undo::finishUndo().
223 /// Current group Id.
225 /// Current group nesting nevel.
230 /////////////////////////////////////////////////////////////////////
234 /////////////////////////////////////////////////////////////////////
237 Undo::Undo(Buffer & buffer)
238 : d(new Undo::Private(buffer))
250 d->undostack_.clear();
251 d->redostack_.clear();
252 d->undo_finished_ = true;
253 // We used to do that, but I believe it is better to keep
254 // groups (only used in Buffer::reload for now (JMarc)
256 //d->group_level = 0;
260 bool Undo::hasUndoStack() const
262 return !d->undostack_.empty();
266 bool Undo::hasRedoStack() const
268 return !d->redostack_.empty();
272 void Undo::markDirty()
274 d->undo_finished_ = true;
275 d->undostack_.markDirty();
276 d->redostack_.markDirty();
280 /////////////////////////////////////////////////////////////////////
284 ///////////////////////////////////////////////////////////////////////
286 static bool samePar(StableDocIterator const & i1, StableDocIterator const & i2)
288 StableDocIterator tmpi2 = i2;
289 tmpi2.pos() = i1.pos();
294 void Undo::Private::doRecordUndo(UndoKind kind,
295 DocIterator const & cell,
296 pit_type first_pit, pit_type last_pit,
297 CursorData const & cur_before,
299 UndoElementStack & stack)
302 LYXERR0("There is no group open (creating one)");
306 if (first_pit > last_pit)
307 swap(first_pit, last_pit);
309 // Undo::ATOMIC are always recorded (no overlapping there).
310 // As nobody wants all removed character appear one by one when undoing,
311 // we want combine 'similar' non-ATOMIC undo recordings to one.
312 pit_type from = first_pit;
313 pit_type end = cell.lastpit() - last_pit;
315 && kind != ATOMIC_UNDO
317 && samePar(stack.top().cell, cell)
318 && stack.top().kind == kind
319 && stack.top().from == from
320 && stack.top().end == end) {
321 // reset cur_after; it will be filled correctly by endUndoGroup.
322 stack.top().cur_after = CursorData();
327 LYXERR(Debug::UNDO, "Create full buffer undo element of group " << group_id);
329 LYXERR(Debug::UNDO, "Create undo element of group " << group_id);
330 // create the position information of the Undo entry
331 UndoElement undo(kind, cur_before, cell, from, end, 0, 0,
332 buffer_.params(), isFullBuffer, buffer_.isClean(), group_id);
334 // fill in the real data to be saved
335 if (cell.inMathed()) {
336 // simply use the whole cell
337 MathData & ar = cell.cell();
338 undo.array = new MathData(ar.buffer(), ar.begin(), ar.end());
340 // some more effort needed here as 'the whole cell' of the
341 // main Text _is_ the whole document.
342 // record the relevant paragraphs
343 Text const * text = cell.text();
345 ParagraphList const & plist = text->paragraphs();
346 ParagraphList::const_iterator first = plist.begin();
347 advance(first, first_pit);
348 ParagraphList::const_iterator last = plist.begin();
349 advance(last, last_pit + 1);
350 undo.pars = new ParagraphList(first, last);
353 // push the undo entry to undo stack
355 //lyxerr << "undo record: " << stack.top() << endl;
359 void Undo::Private::recordUndo(UndoKind kind,
360 DocIterator const & cell,
361 pit_type first_pit, pit_type last_pit,
362 CursorData const & cur,
365 LASSERT(first_pit <= cell.lastpit(), return);
366 LASSERT(last_pit <= cell.lastpit(), return);
368 doRecordUndo(kind, cell, first_pit, last_pit, cur,
369 isFullBuffer, undostack_);
371 // next time we'll try again to combine entries if possible
372 undo_finished_ = false;
374 // If we ran recordUndo, it means that we plan to change the buffer
378 //lyxerr << "undostack:\n";
379 //for (size_t i = 0, n = buf.undostack().size(); i != n && i < 6; ++i)
380 // lyxerr << " " << i << ": " << buf.undostack()[i] << endl;
384 void Undo::Private::doTextUndoOrRedo(CursorData & cur, UndoElementStack & stack, UndoElementStack & otherstack)
386 // Adjust undo stack and get hold of current undo data.
387 UndoElement & undo = stack.top();
388 LYXERR(Debug::UNDO, "Undo element of group " << undo.group_id);
389 // We'll pop the stack only when we're done with this element. So do NOT
390 // try to return early.
392 // We will store in otherstack the part of the document under 'undo'
393 DocIterator cell_dit = undo.cell.asDocIterator(&buffer_);
395 doRecordUndo(ATOMIC_UNDO, cell_dit,
396 undo.from, cell_dit.lastpit() - undo.end, undo.cur_after,
397 undo.isFullBuffer, otherstack);
398 otherstack.top().cur_after = undo.cur_before;
400 // This does the actual undo/redo.
401 //LYXERR0("undo, performing: " << undo);
402 DocIterator dit = undo.cell.asDocIterator(&buffer_);
403 if (undo.isFullBuffer) {
405 // This is a full document
406 delete otherstack.top().bparams;
407 otherstack.top().bparams = new BufferParams(buffer_.params());
408 buffer_.params() = *undo.bparams;
409 swap(buffer_.paragraphs(), *undo.pars);
412 } else if (dit.inMathed()) {
413 // We stored the full cell here as there is not much to be
414 // gained by storing just 'a few' paragraphs (most if not
415 // all math inset cells have just one paragraph!)
416 //LYXERR0("undo.array: " << *undo.array);
418 dit.cell().swap(*undo.array);
422 // Some finer machinery is needed here.
423 Text * text = dit.text();
426 ParagraphList & plist = text->paragraphs();
428 // remove new stuff between first and last
429 ParagraphList::iterator first = plist.begin();
430 advance(first, undo.from);
431 ParagraphList::iterator last = plist.begin();
432 advance(last, plist.size() - undo.end);
433 plist.erase(first, last);
435 // re-insert old stuff instead
436 first = plist.begin();
437 advance(first, undo.from);
439 // this ugly stuff is needed until we get rid of the
440 // inset_owner backpointer
441 ParagraphList::iterator pit = undo.pars->begin();
442 ParagraphList::iterator const end = undo.pars->end();
443 for (; pit != end; ++pit)
444 pit->setInsetOwner(dit.realInset());
445 plist.insert(first, undo.pars->begin(), undo.pars->end());
450 // We'll clean up in release mode.
451 LASSERT(undo.pars == 0, undo.pars = 0);
452 LASSERT(undo.array == 0, undo.array = 0);
454 if (!undo.cur_before.empty())
455 cur = undo.cur_before;
460 // Now that we're done with undo, we pop it off the stack.
465 bool Undo::Private::textUndoOrRedo(CursorData & cur, bool isUndoOperation)
467 undo_finished_ = true;
469 UndoElementStack & stack = isUndoOperation ? undostack_ : redostack_;
475 UndoElementStack & otherstack = isUndoOperation ? redostack_ : undostack_;
477 const size_t gid = stack.top().group_id;
478 while (!stack.empty() && stack.top().group_id == gid)
479 doTextUndoOrRedo(cur, stack, otherstack);
481 // Adapt the new material to current buffer.
482 buffer_.setBuffersForInsets(); // FIXME This shouldn't be here.
487 void Undo::finishUndo()
489 // Make sure the next operation will be stored.
490 d->undo_finished_ = true;
494 bool Undo::textUndo(CursorData & cur)
496 return d->textUndoOrRedo(cur, true);
500 bool Undo::textRedo(CursorData & cur)
502 return d->textUndoOrRedo(cur, false);
506 void Undo::beginUndoGroup()
508 if (d->group_level == 0) {
509 // create a new group
511 LYXERR(Debug::UNDO, "+++++++Creating new group " << d->group_id);
517 void Undo::endUndoGroup()
519 if (d->group_level == 0) {
520 LYXERR0("There is no undo group to end here");
524 if (d->group_level == 0) {
525 // real end of the group
526 LYXERR(Debug::UNDO, "-------End of group " << d->group_id);
531 void Undo::endUndoGroup(CursorData const & cur)
534 if (!d->undostack_.empty() && d->undostack_.top().cur_after.empty())
535 d->undostack_.top().cur_after = cur;
539 // FIXME: remove these convenience functions and make
540 // Private::recordUndo public as sole interface. The code in the
541 // convenience functions can move to Cursor.cpp.
543 void Undo::recordUndo(CursorData const & cur, UndoKind kind)
545 d->recordUndo(kind, cur, cur.pit(), cur.pit(), cur, false);
549 void Undo::recordUndoInset(CursorData const & cur, UndoKind kind,
552 if (!inset || inset == &cur.inset()) {
555 d->recordUndo(kind, c, c.pit(), c.pit(), cur, false);
556 } else if (inset == cur.nextInset())
557 recordUndo(cur, kind);
559 LYXERR0("Inset not found, no undo stack added.");
563 void Undo::recordUndo(CursorData const & cur, UndoKind kind, pit_type from)
565 d->recordUndo(kind, cur, cur.pit(), from, cur, false);
569 void Undo::recordUndo(CursorData const & cur, UndoKind kind,
570 pit_type from, pit_type to)
572 d->recordUndo(kind, cur, from, to, cur, false);
576 void Undo::recordUndoFullDocument(CursorData const & cur)
578 // This one may happen outside of the main undo group, so we
579 // put it in its own subgroup to avoid complaints.
581 d->recordUndo(ATOMIC_UNDO, doc_iterator_begin(&d->buffer_),
582 0, d->buffer_.paragraphs().size() - 1, cur, true);