1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
13 #pragma implementation
16 #include "undo_funcs.h"
18 #include "BufferView.h"
20 #include "insets/inset.h"
22 #include "support/LAssert.h"
24 #include "iterators.h"
26 //#define DELETE_UNUSED_PARAGRAPHS 1
27 #ifdef DELETE_UNUSED_PARAGRAPHS
31 /// the flag used by FinishUndo();
37 bool textUndo(BufferView * bv)
39 // returns false if no undo possible
40 Undo * undo = bv->buffer()->undostack.top();
41 bv->buffer()->undostack.pop();
45 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
46 if (first && first->next())
47 first = first->next();
49 first = firstUndoParagraph(bv, undo->number_of_inset_id);
51 bv->buffer()->redostack.push(
52 createUndo(bv, undo->kind, first,
53 bv->buffer()->getParFromID(undo->number_of_behind_par)));
57 // now we can unlock the inset for saftey because the inset pointer could
58 // be changed during the undo-function. Anyway if needed we have to lock
59 // the right inset/position if this is requested.
61 bv->unlockInset(bv->theLockingInset());
62 bool ret = textHandleUndo(bv, undo);
68 bool textRedo(BufferView * bv)
70 // returns false if no redo possible
71 Undo * undo = bv->buffer()->redostack.top();
72 bv->buffer()->redostack.pop();
76 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
77 if (first && first->next())
78 first = first->next();
80 first = firstUndoParagraph(bv, undo->number_of_inset_id);
82 bv->buffer()->undostack.push(
83 createUndo(bv, undo->kind, first,
84 bv->buffer()->getParFromID(undo->number_of_behind_par)));
88 // now we can unlock the inset for saftey because the inset pointer could
89 // be changed during the undo-function. Anyway if needed we have to lock
90 // the right inset/position if this is requested.
92 bv->unlockInset(bv->theLockingInset());
93 bool ret = textHandleUndo(bv, undo);
99 bool textHandleUndo(BufferView * bv, Undo * undo)
101 // returns false if no undo possible
105 bv->buffer()->getParFromID(undo->number_of_before_par);
107 bv->buffer()->getParFromID(undo->number_of_behind_par);
112 // if there's no before take the beginning
113 // of the document for redoing
115 LyXText * t = bv->text;
116 int num = undo->number_of_inset_id;
117 if (undo->number_of_inset_id >= 0) {
118 Inset * in = bv->buffer()->getInsetFromID(num);
120 t = in->getLyXText(bv);
125 t->setCursorIntern(bv, firstUndoParagraph(bv, num), 0);
128 // replace the paragraphs with the undo informations
130 Paragraph * tmppar3 = undo->par;
131 undo->par = 0; /* otherwise the undo destructor would
132 delete the paragraph */
134 // get last undo par and set the right(new) inset-owner of the
135 // paragraph if there is any. This is not needed if we don't have
136 // a paragraph before because then in is automatically done in the
137 // function which assigns the first paragraph to an InsetText. (Jug)
138 Paragraph * tmppar4 = tmppar3;
142 in = before->inInset();
143 tmppar4->setInsetOwner(in);
144 while (tmppar4->next()) {
145 tmppar4 = tmppar4->next();
146 tmppar4->setInsetOwner(in);
150 // now remove the old text if there is any
151 #ifdef DELETE_UNUSED_PARAGRAPHS
152 std::vector<Paragraph *> vvpar;
154 if (before != behind || (!behind && !before)) {
156 tmppar5 = before->next();
158 tmppar5 = firstUndoParagraph(bv, undo->number_of_inset_id);
160 while (tmppar5 && tmppar5 != behind) {
161 #ifdef DELETE_UNUSED_PARAGRAPHS
162 vvpar.push_back(tmppar5);
165 tmppar5 = tmppar5->next();
166 // a memory optimization for edit:
167 // Only layout information
168 // is stored in the undo. So restore
169 // the text informations.
170 if (undo->kind == Undo::EDIT) {
171 tmppar2->setContentsFromPar(tmppar);
172 #ifndef DELETE_UNUSED_PARAGRAPHS
173 tmppar->clearContents();
175 tmppar2 = tmppar2->next();
180 // put the new stuff in the list if there is one
183 before->next(tmppar3);
185 bv->text->ownerParagraph(tmppar3->id(),
188 tmppar3->previous(before);
190 // Do we really enter here ??? (Jug)
191 if (!before && behind) {
192 bv->text->ownerParagraph(behind);
197 tmppar4->next(behind);
199 behind->previous(tmppar4);
203 // Set the cursor for redoing
205 Inset * it = before->inInset();
207 it->getLyXText(bv)->setCursorIntern(bv, before, 0);
209 bv->text->setCursorIntern(bv, before, 0);
212 Paragraph * endpar = 0;
213 // calculate the endpar for redoing the paragraphs.
215 endpar = behind->next();
217 tmppar = bv->buffer()->getParFromID(undo->number_of_cursor_par);
219 UpdatableInset* it = static_cast<UpdatableInset*>(tmppar3->inInset());
221 it->getLyXText(bv)->redoParagraphs(bv,
222 it->getLyXText(bv)->cursor,
225 it->update(bv, font, false);
226 // we now would have to rebreak the whole paragraph the
227 // undo-par was in. How we do it here is not really true.
228 // We would have to save this information in the undo-struct
229 // and then we could do the right rebreak. Here we only
230 // handle the case where this was in the actual paragraph,
231 // which not always is true.
232 bv->text->redoParagraphs(bv, bv->text->cursor,
233 bv->text->cursor.par());
235 it = static_cast<UpdatableInset*>(tmppar->inInset());
239 t = it->getLyXText(bv);
243 t->setCursorIntern(bv, tmppar, undo->cursor_pos);
244 t->updateCounters(bv, t->cursor.row());
246 bv->text->setCursorIntern(bv, bv->text->cursor.par(),
247 bv->text->cursor.pos());
250 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
253 Inset * it = tmppar->inInset();
256 t = it->getLyXText(bv);
260 t->setCursorIntern(bv, tmppar, undo->cursor_pos);
261 t->updateCounters(bv, t->cursor.row());
266 #ifdef DELETE_UNUSED_PARAGRAPHS
267 // And here it's save enough to delete all removed paragraphs
268 std::vector<Paragraph *>::iterator pit = vvpar.begin();
269 if (pit != vvpar.end()) {
271 for(;pit != vvpar.end(); ++pit) {
272 lyxerr << *pit << " ";
275 lyxerr << endl << "PARS:";
276 ParIterator end = bv->buffer()->par_iterator_end();
277 ParIterator it = bv->buffer()->par_iterator_begin();
278 for (; it != end; ++it)
279 lyxerr << *it << " ";
285 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
292 // makes sure the next operation will be stored
293 undo_finished = true;
299 // this is dangerous and for internal use only
306 // this is dangerous and for internal use only
311 void setUndo(BufferView * bv, Undo::undo_kind kind,
312 Paragraph const * first, Paragraph const * behind)
315 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
316 bv->buffer()->redostack.clear();
321 void setRedo(BufferView * bv, Undo::undo_kind kind,
322 Paragraph const * first, Paragraph const * behind)
324 bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
328 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
329 Paragraph const * first, Paragraph const * behind)
333 int before_number = -1;
334 int behind_number = -1;
337 if (first->previous())
338 before_number = first->previous()->id();
340 behind_number = behind->id();
341 if (first->inInset())
342 inset_id = first->inInset()->id();
344 // Undo::EDIT and Undo::FINISH are
345 // always finished. (no overlapping there)
346 // overlapping only with insert and delete inside one paragraph:
347 // Nobody wants all removed character
348 // appear one by one when undoing.
349 // EDIT is special since only layout information, not the
350 // contents of a paragaph are stored.
351 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)){
352 // check wether storing is needed
353 if (!bv->buffer()->undostack.empty() &&
354 bv->buffer()->undostack.top()->kind == kind &&
355 bv->buffer()->undostack.top()->number_of_before_par == before_number &&
356 bv->buffer()->undostack.top()->number_of_behind_par == behind_number) {
364 Paragraph * start = const_cast<Paragraph *>(first);
368 end = const_cast<Paragraph*>(behind->previous());
374 if (start && end && (start != end->next()) &&
375 ((before_number != behind_number) ||
376 ((before_number < 0) && (behind_number < 0))))
378 Paragraph * tmppar = start;
379 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
381 // a memory optimization: Just store the layout information
383 if (kind == Undo::EDIT) {
384 tmppar2->clearContents();
389 while (tmppar != end && tmppar->next()) {
390 tmppar = tmppar->next();
391 tmppar2->next(new Paragraph(*tmppar, true));
392 // a memory optimization: Just store the layout
393 // information when only edit
394 if (kind == Undo::EDIT) {
395 tmppar2->clearContents();
397 tmppar2->next()->previous(tmppar2);
398 tmppar2 = tmppar2->next();
402 undopar = 0; // nothing to replace (undo of delete maybe)
404 int cursor_par = undoCursor(bv).par()->id();
405 int cursor_pos = undoCursor(bv).pos();
407 Undo * undo = new Undo(kind, inset_id,
408 before_number, behind_number,
409 cursor_par, cursor_pos, undopar);
411 undo_finished = false;
416 void setCursorParUndo(BufferView * bv)
418 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
419 bv->text->cursor.par()->next());
423 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
425 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
427 Paragraph * result = inset->getFirstParagraph(0);
431 return bv->text->ownerParagraph();
435 LyXCursor const & undoCursor(BufferView * bv)
437 if (bv->theLockingInset())
438 return bv->theLockingInset()->cursor(bv);
439 return bv->text->cursor;