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"
21 #include "iterators.h"
26 using boost::shared_ptr;
29 /// The flag used by FinishUndo().
31 /// Whether actions are not added to the undo stacks.
36 /// Utility to return the cursor.
37 LyXCursor const & undoCursor(BufferView * bv)
39 if (bv->theLockingInset())
40 return bv->theLockingInset()->cursor(bv);
41 return bv->text->cursor;
46 * Returns a pointer to the very first Paragraph depending of where
47 * we are so it will return the first paragraph of the buffer or the
48 * first paragraph of the textinset we're in.
50 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
52 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
54 Paragraph * result = inset->getFirstParagraph(0);
58 return &*bv->text->ownerParagraphs().begin();
63 * Finish the undo operation in the case there was no entry
64 * on the stack to perform.
66 void finishNoUndo(BufferView * bv)
69 bv->unlockInset(bv->theLockingInset());
71 bv->text->postPaint(0);
76 // Returns false if no undo possible.
77 bool textHandleUndo(BufferView * bv, Undo & undo)
79 Buffer * b = bv->buffer();
81 Paragraph * const before = &*b->getParFromID(undo.number_of_before_par);
82 Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
84 // If there's no before take the beginning
85 // of the document for redoing.
87 LyXText * t = bv->text;
88 int num = undo.number_of_inset_id;
89 if (undo.number_of_inset_id >= 0) {
90 Inset * in = bv->buffer()->getInsetFromID(num);
92 t = in->getLyXText(bv);
97 t->setCursorIntern(firstUndoParagraph(bv, num), 0);
100 // Set the right(new) inset-owner of the paragraph if there is any.
101 if (!undo.pars.empty()) {
104 in = before->inInset();
105 else if (undo.number_of_inset_id >= 0)
106 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
107 for (size_t i = 0, n = undo.pars.size(); i < n; ++i)
108 undo.pars[i]->setInsetOwner(in);
111 // Replace the paragraphs with the undo informations.
112 vector<Paragraph *> deletelist;
114 // Now add old paragraphs to be deleted.
115 if (before != behind || (!behind && !before)) {
116 Paragraph * deletepar;
118 deletepar = before->next();
120 deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
121 // this surprisingly fills the undo! (Andre')
123 while (deletepar && deletepar != behind) {
124 deletelist.push_back(deletepar);
125 deletepar = deletepar->next();
127 // A memory optimization for edit:
128 // Only layout information
129 // is stored in the undo. So restore
130 // the text informations.
131 if (undo.kind == Undo::EDIT) {
132 undo.pars[par]->setContentsFromPar(*deletelist.back());
138 // The order here is VERY IMPORTANT. We have to set the right
139 // next/prev pointer in the paragraphs so that a rebuild of
140 // the LyXText works!!!
142 // Thread the end of the undo onto the par in front if any.
143 if (!undo.pars.empty()) {
144 undo.pars.back()->next(behind);
146 behind->previous(undo.pars.back());
149 // Put the new stuff in the list if there is one.
150 Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front();
151 if (!undo.pars.empty()) {
152 undo.pars.front()->previous(before);
154 before->next(undopar);
156 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
157 Paragraph * op = &*bv->buffer()->getParFromID(id);
158 if (op && op->inInset()) {
159 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
161 bv->buffer()->paragraphs.set(undopar);
165 // We enter here on DELETE undo operations where we
166 // have to substitue the second paragraph with the
167 // first if the removed one is the first.
168 if (!before && behind) {
169 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
170 Paragraph * op = &*bv->buffer()->getParFromID(id);
171 if (op && op->inInset()) {
172 static_cast<InsetText*>(op->inInset())->paragraph(behind);
174 bv->buffer()->paragraphs.set(behind);
181 // Set the cursor for redoing.
182 // If we have a par before the undopar.
184 Inset * it = before->inInset();
186 it->getLyXText(bv)->setCursorIntern(before, 0);
188 bv->text->setCursorIntern(before, 0);
191 // we are not ready for this we cannot set the cursor for a paragraph
192 // which is not already in a row of LyXText!!!
194 else { // otherwise this is the first one and we start here
195 Inset * it = undopar->inInset();
197 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
199 bv->text->setCursorIntern(bv, undopar, 0);
203 Paragraph * endpar = 0;
205 // Calculate the endpar for redoing the paragraphs.
207 endpar = behind->next();
209 UpdatableInset * it = 0;
211 it = static_cast<UpdatableInset*>(undopar->inInset());
213 it->getLyXText(bv)->redoParagraphs(
214 it->getLyXText(bv)->cursor,
217 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
219 it = static_cast<UpdatableInset*>(tmppar->inInset());
223 t = it->getLyXText(bv);
227 t->setCursorIntern(tmppar, undo.cursor_pos);
228 // Clear any selection and set the selection
229 // cursor for an evt. new selection.
231 t->selection.cursor = t->cursor;
236 bv->text->setCursorIntern(bv->text->cursor.par(),
237 bv->text->cursor.pos());
239 bv->text->redoParagraphs(bv->text->cursor, endpar);
241 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
244 Inset * it = tmppar->inInset();
247 t = it->getLyXText(bv);
251 t->setCursorIntern(tmppar, undo.cursor_pos);
252 // Clear any selection and set the selection
253 // cursor for an evt. new selection.
255 t->selection.cursor = t->cursor;
260 // And here it's safe enough to delete all removed paragraphs.
261 vector<Paragraph *>::iterator pit = deletelist.begin();
262 for(; pit != deletelist.end(); ++pit) {
268 // Otherwise the undo destructor would delete the paragraphs
272 bv->text->postPaint(0);
277 bool createUndo(BufferView * bv, Undo::undo_kind kind,
278 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
279 shared_ptr<Undo> & u)
281 Paragraph * const first = &*itfirst;
282 Paragraph * const behind = &*itbehind;
285 int before_number = -1;
286 int behind_number = -1;
289 if (first->previous())
290 before_number = first->previous()->id();
292 behind_number = behind->id();
293 if (first->inInset())
294 inset_id = first->inInset()->id();
296 Buffer * b = bv->buffer();
298 // Undo::EDIT and Undo::FINISH are
299 // always finished. (no overlapping there)
300 // overlapping only with insert and delete inside one paragraph:
301 // Nobody wants all removed character
302 // appear one by one when undoing.
303 // EDIT is special since only layout information, not the
304 // contents of a paragaph are stored.
305 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
306 // Check whether storing is needed.
307 if (!b->undostack.empty() &&
308 b->undostack.top()->kind == kind &&
309 b->undostack.top()->number_of_before_par == before_number &&
310 b->undostack.top()->number_of_behind_par == behind_number) {
316 // Create a new Undo.
317 std::vector<Paragraph *> undo_pars;
319 Paragraph const * end = 0;
322 end = behind->previous();
329 if (first && end && (first != end->next()) &&
330 ((before_number != behind_number) ||
331 ((before_number < 0) && (behind_number < 0))))
333 undo_pars.push_back(new Paragraph(*first, true));
334 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
335 tmppar = tmppar->next();
336 undo_pars.push_back(new Paragraph(*tmppar, true));
337 size_t const n = undo_pars.size();
338 undo_pars[n - 2]->next(undo_pars[n - 1]);
339 undo_pars[n - 1]->previous(undo_pars[n - 2]);
341 undo_pars.back()->next(0);
344 // A memory optimization: Just store the layout
345 // information when only edit.
346 if (kind == Undo::EDIT) {
347 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
348 undo_pars[i]->clearContents();
351 int cursor_par = undoCursor(bv).par()->id();
352 int cursor_pos = undoCursor(bv).pos();
354 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
355 // << before_number << " behind_number: " << behind_number << "\n";
356 u.reset(new Undo(kind, inset_id,
357 before_number, behind_number,
358 cursor_par, cursor_pos, undo_pars));
360 undo_finished = false;
365 // Returns false if no undo possible.
366 bool textUndoOrRedo(BufferView * bv,
367 limited_stack<boost::shared_ptr<Undo> > & stack,
368 limited_stack<boost::shared_ptr<Undo> > & otherstack)
370 Buffer * b = bv->buffer();
377 shared_ptr<Undo> undo = stack.top();
382 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
383 if (first && first->next())
384 first = first->next();
386 first = firstUndoParagraph(bv, undo->number_of_inset_id);
389 if (createUndo(bv, undo->kind, first,
390 b->getParFromID(undo->number_of_behind_par), u))
395 // Now we can unlock the inset for saftey because the inset
396 // pointer could be changed during the undo-function. Anyway
397 // if needed we have to lock the right inset/position if this
400 bv->unlockInset(bv->theLockingInset());
401 bool const ret = textHandleUndo(bv, *undo.get());
411 // Makes sure the next operation will be stored.
412 undo_finished = true;
418 // This is dangerous and for internal use only.
425 // This is dangerous and for internal use only.
430 bool textUndo(BufferView * bv)
432 return textUndoOrRedo(bv, bv->buffer()->undostack,
433 bv->buffer()->redostack);
437 bool textRedo(BufferView * bv)
439 return textUndoOrRedo(bv, bv->buffer()->redostack,
440 bv->buffer()->undostack);
444 void setUndo(BufferView * bv, Undo::undo_kind kind,
445 ParagraphList::iterator first, ParagraphList::iterator behind)
449 if (createUndo(bv, kind, first, behind, u))
450 bv->buffer()->undostack.push(u);
451 bv->buffer()->redostack.clear();
456 void setRedo(BufferView * bv, Undo::undo_kind kind,
457 ParagraphList::iterator first, ParagraphList::iterator behind)
460 if (createUndo(bv, kind, first, behind, u))
461 bv->buffer()->redostack.push(u);
465 void setCursorParUndo(BufferView * bv)
467 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
468 boost::next(bv->text->cursor.par()));