1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
12 #include "undo_funcs.h"
14 #include "BufferView.h"
16 #include "insets/updatableinset.h"
17 #include "insets/insettext.h"
19 #include "support/LAssert.h"
20 #include "iterators.h"
25 using boost::shared_ptr;
28 /// The flag used by FinishUndo().
30 /// Whether actions are not added to the undo stacks.
35 /// Utility to return the cursor.
36 LyXCursor const & undoCursor(BufferView * bv)
38 if (bv->theLockingInset())
39 return bv->theLockingInset()->cursor(bv);
40 return bv->text->cursor;
45 * Returns a pointer to the very first Paragraph depending of where
46 * we are so it will return the first paragraph of the buffer or the
47 * first paragraph of the textinset we're in.
49 ParagraphList undoParagraphs(BufferView * bv, int inset_id)
51 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
53 ParagraphList * result = inset->getParagraphs(0);
54 if (result && !result->empty())
57 return bv->text->ownerParagraphs();
62 * Finish the undo operation in the case there was no entry
63 * on the stack to perform.
65 void finishNoUndo(BufferView * bv)
68 bv->unlockInset(bv->theLockingInset());
70 bv->text->postPaint(0);
75 // Returns false if no undo possible.
76 bool textHandleUndo(BufferView * bv, Undo & undo)
78 Buffer * b = bv->buffer();
80 Paragraph * const before = &*b->getParFromID(undo.number_of_before_par);
81 Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
83 // If there's no before take the beginning
84 // of the document for redoing.
86 LyXText * t = bv->text;
87 int num = undo.number_of_inset_id;
88 if (undo.number_of_inset_id >= 0) {
89 Inset * in = bv->buffer()->getInsetFromID(num);
91 t = in->getLyXText(bv);
96 t->setCursorIntern(undoParagraphs(bv, num).begin(), 0);
99 // Set the right(new) inset-owner of the paragraph if there is any.
100 if (!undo.pars.empty()) {
103 in = before->inInset();
104 else if (undo.number_of_inset_id >= 0)
105 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
106 for (size_t i = 0, n = undo.pars.size(); i < n; ++i)
107 undo.pars[i]->setInsetOwner(in);
110 // Replace the paragraphs with the undo informations.
111 vector<Paragraph *> deletelist;
113 // Now add old paragraphs to be deleted.
114 if (before != behind || (!behind && !before)) {
115 Paragraph * deletepar;
117 deletepar = before->next();
119 deletepar = &undoParagraphs(bv, undo.number_of_inset_id).front();
120 // this surprisingly fills the undo! (Andre')
122 while (deletepar && deletepar != behind) {
123 deletelist.push_back(deletepar);
124 deletepar = deletepar->next();
126 // A memory optimization for edit:
127 // Only layout information
128 // is stored in the undo. So restore
129 // the text informations.
130 if (undo.kind == Undo::EDIT) {
131 undo.pars[par]->setContentsFromPar(*deletelist.back());
137 // The order here is VERY IMPORTANT. We have to set the right
138 // next/prev pointer in the paragraphs so that a rebuild of
139 // the LyXText works!!!
141 // Thread the end of the undo onto the par in front if any.
142 if (!undo.pars.empty()) {
143 undo.pars.back()->next(behind);
145 behind->previous(undo.pars.back());
148 // Put the new stuff in the list if there is one.
149 Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front();
150 if (!undo.pars.empty()) {
151 undo.pars.front()->previous(before);
153 before->next(undopar);
155 int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
156 Paragraph * op = &*bv->buffer()->getParFromID(id);
157 if (op && op->inInset()) {
158 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
160 bv->buffer()->paragraphs.set(undopar);
164 // We enter here on DELETE undo operations where we
165 // have to substitue the second paragraph with the
166 // first if the removed one is the first.
167 if (!before && behind) {
168 int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
169 Paragraph * op = &*bv->buffer()->getParFromID(id);
170 if (op && op->inInset()) {
171 static_cast<InsetText*>(op->inInset())->paragraph(behind);
173 bv->buffer()->paragraphs.set(behind);
180 // Set the cursor for redoing.
181 // If we have a par before the undopar.
183 Inset * it = before->inInset();
185 it->getLyXText(bv)->setCursorIntern(before, 0);
187 bv->text->setCursorIntern(before, 0);
190 // we are not ready for this we cannot set the cursor for a paragraph
191 // which is not already in a row of LyXText!!!
193 else { // otherwise this is the first one and we start here
194 Inset * it = undopar->inInset();
196 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
198 bv->text->setCursorIntern(bv, undopar, 0);
202 Paragraph * endpar = 0;
204 // Calculate the endpar for redoing the paragraphs.
206 endpar = behind->next();
208 UpdatableInset * it = 0;
210 it = static_cast<UpdatableInset*>(undopar->inInset());
212 it->getLyXText(bv)->redoParagraphs(
213 it->getLyXText(bv)->cursor,
216 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
218 it = static_cast<UpdatableInset*>(tmppar->inInset());
222 t = it->getLyXText(bv);
226 t->setCursorIntern(tmppar, undo.cursor_pos);
227 // Clear any selection and set the selection
228 // cursor for an evt. new selection.
230 t->selection.cursor = t->cursor;
235 bv->text->setCursorIntern(bv->text->cursor.par(),
236 bv->text->cursor.pos());
238 bv->text->redoParagraphs(bv->text->cursor, endpar);
240 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
243 Inset * it = tmppar->inInset();
246 t = it->getLyXText(bv);
250 t->setCursorIntern(tmppar, undo.cursor_pos);
251 // Clear any selection and set the selection
252 // cursor for an evt. new selection.
254 t->selection.cursor = t->cursor;
259 // And here it's safe enough to delete all removed paragraphs.
260 vector<Paragraph *>::iterator pit = deletelist.begin();
261 for(; pit != deletelist.end(); ++pit) {
267 // Otherwise the undo destructor would delete the paragraphs
271 bv->text->postPaint(0);
276 bool createUndo(BufferView * bv, Undo::undo_kind kind,
277 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
278 shared_ptr<Undo> & u)
280 Paragraph * const first = &*itfirst;
281 Paragraph * const behind = &*itbehind;
284 int before_number = -1;
285 int behind_number = -1;
288 if (first->previous())
289 before_number = first->previous()->id();
291 behind_number = behind->id();
292 if (first->inInset())
293 inset_id = first->inInset()->id();
295 Buffer * b = bv->buffer();
297 // Undo::EDIT and Undo::FINISH are
298 // always finished. (no overlapping there)
299 // overlapping only with insert and delete inside one paragraph:
300 // Nobody wants all removed character
301 // appear one by one when undoing.
302 // EDIT is special since only layout information, not the
303 // contents of a paragaph are stored.
304 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
305 // Check whether storing is needed.
306 if (!b->undostack.empty() &&
307 b->undostack.top()->kind == kind &&
308 b->undostack.top()->number_of_before_par == before_number &&
309 b->undostack.top()->number_of_behind_par == behind_number) {
315 // Create a new Undo.
316 std::vector<Paragraph *> undo_pars;
318 Paragraph const * end = 0;
321 end = behind->previous();
328 if (first && end && (first != end->next()) &&
329 ((before_number != behind_number) ||
330 ((before_number < 0) && (behind_number < 0))))
332 undo_pars.push_back(new Paragraph(*first, true));
333 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
334 tmppar = tmppar->next();
335 undo_pars.push_back(new Paragraph(*tmppar, true));
336 size_t const n = undo_pars.size();
337 undo_pars[n - 2]->next(undo_pars[n - 1]);
338 undo_pars[n - 1]->previous(undo_pars[n - 2]);
340 undo_pars.back()->next(0);
343 // A memory optimization: Just store the layout
344 // information when only edit.
345 if (kind == Undo::EDIT) {
346 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
347 undo_pars[i]->clearContents();
350 int cursor_par = undoCursor(bv).par()->id();
351 int cursor_pos = undoCursor(bv).pos();
353 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
354 // << before_number << " behind_number: " << behind_number << "\n";
355 u.reset(new Undo(kind, inset_id,
356 before_number, behind_number,
357 cursor_par, cursor_pos, undo_pars));
359 undo_finished = false;
364 // Returns false if no undo possible.
365 bool textUndoOrRedo(BufferView * bv,
366 limited_stack<boost::shared_ptr<Undo> > & stack,
367 limited_stack<boost::shared_ptr<Undo> > & otherstack)
369 Buffer * b = bv->buffer();
376 shared_ptr<Undo> undo = stack.top();
381 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
382 if (first && first->next())
383 first = first->next();
385 first = &*undoParagraphs(bv, undo->number_of_inset_id).begin();
388 if (createUndo(bv, undo->kind, first,
389 b->getParFromID(undo->number_of_behind_par), u))
394 // Now we can unlock the inset for saftey because the inset
395 // pointer could be changed during the undo-function. Anyway
396 // if needed we have to lock the right inset/position if this
399 bv->unlockInset(bv->theLockingInset());
400 bool const ret = textHandleUndo(bv, *undo.get());
410 // Makes sure the next operation will be stored.
411 undo_finished = true;
417 // This is dangerous and for internal use only.
424 // This is dangerous and for internal use only.
429 bool textUndo(BufferView * bv)
431 return textUndoOrRedo(bv, bv->buffer()->undostack,
432 bv->buffer()->redostack);
436 bool textRedo(BufferView * bv)
438 return textUndoOrRedo(bv, bv->buffer()->redostack,
439 bv->buffer()->undostack);
443 void setUndo(BufferView * bv, Undo::undo_kind kind,
444 ParagraphList::iterator first, ParagraphList::iterator behind)
448 if (createUndo(bv, kind, first, behind, u))
449 bv->buffer()->undostack.push(u);
450 bv->buffer()->redostack.clear();
455 void setRedo(BufferView * bv, Undo::undo_kind kind,
456 ParagraphList::iterator first, ParagraphList::iterator behind)
459 if (createUndo(bv, kind, first, behind, u))
460 bv->buffer()->redostack.push(u);
464 void setCursorParUndo(BufferView * bv)
466 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
467 boost::next(bv->text->cursor.par()));