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
34 /// the flag used by FinishUndo();
40 bool textUndo(BufferView * bv)
42 // returns false if no undo possible
43 Undo * undo = bv->buffer()->undostack.top();
44 bv->buffer()->undostack.pop();
48 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
49 if (first && first->next())
50 first = first->next();
52 first = firstUndoParagraph(bv, undo->number_of_inset_id);
54 bv->buffer()->redostack.push(
55 createUndo(bv, undo->kind, first,
56 bv->buffer()->getParFromID(undo->number_of_behind_par)));
60 // now we can unlock the inset for saftey because the inset pointer could
61 // be changed during the undo-function. Anyway if needed we have to lock
62 // the right inset/position if this is requested.
64 bv->unlockInset(bv->theLockingInset());
65 bool ret = textHandleUndo(bv, undo);
71 bool textRedo(BufferView * bv)
73 // returns false if no redo possible
74 Undo * undo = bv->buffer()->redostack.top();
75 bv->buffer()->redostack.pop();
79 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
80 if (first && first->next())
81 first = first->next();
83 first = firstUndoParagraph(bv, undo->number_of_inset_id);
85 bv->buffer()->undostack.push(
86 createUndo(bv, undo->kind, first,
87 bv->buffer()->getParFromID(undo->number_of_behind_par)));
91 // now we can unlock the inset for saftey because the inset pointer could
92 // be changed during the undo-function. Anyway if needed we have to lock
93 // the right inset/position if this is requested.
95 bv->unlockInset(bv->theLockingInset());
96 bool ret = textHandleUndo(bv, undo);
102 bool textHandleUndo(BufferView * bv, Undo * undo)
104 // returns false if no undo possible
108 bv->buffer()->getParFromID(undo->number_of_before_par);
110 bv->buffer()->getParFromID(undo->number_of_behind_par);
115 // if there's no before take the beginning
116 // of the document for redoing
118 LyXText * t = bv->text;
119 int num = undo->number_of_inset_id;
120 if (undo->number_of_inset_id >= 0) {
121 Inset * in = bv->buffer()->getInsetFromID(num);
123 t = in->getLyXText(bv);
128 t->setCursorIntern(bv, firstUndoParagraph(bv, num), 0);
131 // replace the paragraphs with the undo informations
133 Paragraph * tmppar3 = undo->par;
134 undo->par = 0; /* otherwise the undo destructor would
135 delete the paragraph */
137 // get last undo par and set the right(new) inset-owner of the
138 // paragraph if there is any. This is not needed if we don't have
139 // a paragraph before because then in is automatically done in the
140 // function which assigns the first paragraph to an InsetText. (Jug)
141 Paragraph * tmppar4 = tmppar3;
145 in = before->inInset();
146 else if (undo->number_of_inset_id >= 0)
147 in = bv->buffer()->getInsetFromID(undo->number_of_inset_id);
148 tmppar4->setInsetOwner(in);
149 while (tmppar4->next()) {
150 tmppar4 = tmppar4->next();
151 tmppar4->setInsetOwner(in);
155 // now remove the old text if there is any
156 #ifdef DELETE_UNUSED_PARAGRAPHS
157 vector<Paragraph *> vvpar;
159 if (before != behind || (!behind && !before)) {
161 tmppar5 = before->next();
163 tmppar5 = firstUndoParagraph(bv, undo->number_of_inset_id);
165 while (tmppar5 && tmppar5 != behind) {
166 #ifdef DELETE_UNUSED_PARAGRAPHS
167 vvpar.push_back(tmppar5);
170 tmppar5 = tmppar5->next();
171 // a memory optimization for edit:
172 // Only layout information
173 // is stored in the undo. So restore
174 // the text informations.
175 if (undo->kind == Undo::EDIT) {
176 tmppar2->setContentsFromPar(tmppar);
177 #ifndef DELETE_UNUSED_PARAGRAPHS
178 tmppar->clearContents();
180 tmppar2 = tmppar2->next();
185 // put the new stuff in the list if there is one
188 before->next(tmppar3);
190 bv->text->ownerParagraph(firstUndoParagraph(bv, undo->number_of_inset_id)->id(),
193 tmppar3->previous(before);
195 // We enter here on DELETE undo operations where we have to
196 // substitue the second paragraph with the first if the removed
198 if (!before && behind) {
199 bv->text->ownerParagraph(firstUndoParagraph(bv, undo->number_of_inset_id)->id(),
205 tmppar4->next(behind);
207 behind->previous(tmppar4);
211 // Set the cursor for redoing
213 Inset * it = before->inInset();
215 it->getLyXText(bv)->setCursorIntern(bv, before, 0);
217 bv->text->setCursorIntern(bv, before, 0);
220 Paragraph * endpar = 0;
221 // calculate the endpar for redoing the paragraphs.
223 endpar = behind->next();
225 tmppar = bv->buffer()->getParFromID(undo->number_of_cursor_par);
226 UpdatableInset* it = 0;
228 it = static_cast<UpdatableInset*>(tmppar3->inInset());
230 it->getLyXText(bv)->redoParagraphs(bv,
231 it->getLyXText(bv)->cursor,
234 it = static_cast<UpdatableInset*>(tmppar->inInset());
238 t = it->getLyXText(bv);
242 t->setCursorIntern(bv, tmppar, undo->cursor_pos);
243 // clear any selection and set the selection cursor
244 // for an evt. new selection.
246 t->selection.cursor = t->cursor;
247 t->updateCounters(bv, t->cursor.row());
250 bv->updateInset(it, false);
251 bv->text->setCursorIntern(bv, bv->text->cursor.par(),
252 bv->text->cursor.pos());
254 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
257 Inset * it = tmppar->inInset();
260 t = it->getLyXText(bv);
264 t->setCursorIntern(bv, tmppar, undo->cursor_pos);
265 // clear any selection and set the selection cursor
266 // for an evt. new selection.
268 t->selection.cursor = t->cursor;
269 t->updateCounters(bv, t->cursor.row());
274 #ifdef DELETE_UNUSED_PARAGRAPHS
275 // And here it's save enough to delete all removed paragraphs
276 vector<Paragraph *>::iterator pit = vvpar.begin();
277 if (pit != vvpar.end()) {
279 lyxerr << endl << "PARS BEFORE:";
280 ParIterator end = bv->buffer()->par_iterator_end();
281 ParIterator it = bv->buffer()->par_iterator_begin();
282 for (; it != end; ++it)
283 lyxerr << (*it)->previous() << "<- " << (*it) << " ->" << (*it)->next() << endl;
286 for(;pit != vvpar.end(); ++pit) {
287 // lyxerr << *pit << " ";
293 lyxerr << endl << "PARS AFTER:";
294 end = bv->buffer()->par_iterator_end();
295 it = bv->buffer()->par_iterator_begin();
296 for (; it != end; ++it)
297 lyxerr << (*it)->previous() << "<- " << (*it) << " ->" << (*it)->next() << endl;
303 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
310 // makes sure the next operation will be stored
311 undo_finished = true;
317 // this is dangerous and for internal use only
324 // this is dangerous and for internal use only
329 void setUndo(BufferView * bv, Undo::undo_kind kind,
330 Paragraph const * first, Paragraph const * behind)
333 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
334 bv->buffer()->redostack.clear();
339 void setRedo(BufferView * bv, Undo::undo_kind kind,
340 Paragraph const * first, Paragraph const * behind)
342 bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
346 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
347 Paragraph const * first, Paragraph const * behind)
351 int before_number = -1;
352 int behind_number = -1;
355 if (first->previous())
356 before_number = first->previous()->id();
358 behind_number = behind->id();
359 if (first->inInset())
360 inset_id = first->inInset()->id();
362 // Undo::EDIT and Undo::FINISH are
363 // always finished. (no overlapping there)
364 // overlapping only with insert and delete inside one paragraph:
365 // Nobody wants all removed character
366 // appear one by one when undoing.
367 // EDIT is special since only layout information, not the
368 // contents of a paragaph are stored.
369 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
370 // check wether storing is needed
371 if (!bv->buffer()->undostack.empty() &&
372 bv->buffer()->undostack.top()->kind == kind &&
373 bv->buffer()->undostack.top()->number_of_before_par == before_number &&
374 bv->buffer()->undostack.top()->number_of_behind_par == behind_number) {
382 Paragraph * start = const_cast<Paragraph *>(first);
386 end = const_cast<Paragraph*>(behind->previous());
392 if (start && end && (start != end->next()) &&
393 ((before_number != behind_number) ||
394 ((before_number < 0) && (behind_number < 0))))
396 Paragraph * tmppar = start;
397 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
399 // a memory optimization: Just store the layout information
401 if (kind == Undo::EDIT) {
402 tmppar2->clearContents();
407 while (tmppar != end && tmppar->next()) {
408 tmppar = tmppar->next();
410 tmppar2->next(new Paragraph(*tmppar, true));
412 Paragraph * ptmp = new Paragraph(*tmppar, true);
415 // a memory optimization: Just store the layout
416 // information when only edit
417 if (kind == Undo::EDIT) {
418 tmppar2->clearContents();
420 tmppar2->next()->previous(tmppar2);
422 tmppar2 = tmppar2->next();
426 undopar = 0; // nothing to replace (undo of delete maybe)
428 int cursor_par = undoCursor(bv).par()->id();
429 int cursor_pos = undoCursor(bv).pos();
431 Undo * undo = new Undo(kind, inset_id,
432 before_number, behind_number,
433 cursor_par, cursor_pos, undopar);
435 undo_finished = false;
440 void setCursorParUndo(BufferView * bv)
442 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
443 bv->text->cursor.par()->next());
447 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
449 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
451 Paragraph * result = inset->getFirstParagraph(0);
455 return bv->text->ownerParagraph();
459 LyXCursor const & undoCursor(BufferView * bv)
461 if (bv->theLockingInset())
462 return bv->theLockingInset()->cursor(bv);
463 return bv->text->cursor;