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 t->updateCounters(bv, t->cursor.row());
246 bv->updateInset(it, false);
247 bv->text->setCursorIntern(bv, bv->text->cursor.par(),
248 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 vector<Paragraph *>::iterator pit = vvpar.begin();
269 if (pit != vvpar.end()) {
271 lyxerr << endl << "PARS BEFORE:";
272 ParIterator end = bv->buffer()->par_iterator_end();
273 ParIterator it = bv->buffer()->par_iterator_begin();
274 for (; it != end; ++it)
275 lyxerr << (*it)->previous() << "<- " << (*it) << " ->" << (*it)->next() << endl;
278 for(;pit != vvpar.end(); ++pit) {
279 // lyxerr << *pit << " ";
285 lyxerr << endl << "PARS AFTER:";
286 end = bv->buffer()->par_iterator_end();
287 it = bv->buffer()->par_iterator_begin();
288 for (; it != end; ++it)
289 lyxerr << (*it)->previous() << "<- " << (*it) << " ->" << (*it)->next() << endl;
295 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
302 // makes sure the next operation will be stored
303 undo_finished = true;
309 // this is dangerous and for internal use only
316 // this is dangerous and for internal use only
321 void setUndo(BufferView * bv, Undo::undo_kind kind,
322 Paragraph const * first, Paragraph const * behind)
325 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
326 bv->buffer()->redostack.clear();
331 void setRedo(BufferView * bv, Undo::undo_kind kind,
332 Paragraph const * first, Paragraph const * behind)
334 bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
338 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
339 Paragraph const * first, Paragraph const * behind)
343 int before_number = -1;
344 int behind_number = -1;
347 if (first->previous())
348 before_number = first->previous()->id();
350 behind_number = behind->id();
351 if (first->inInset())
352 inset_id = first->inInset()->id();
354 // Undo::EDIT and Undo::FINISH are
355 // always finished. (no overlapping there)
356 // overlapping only with insert and delete inside one paragraph:
357 // Nobody wants all removed character
358 // appear one by one when undoing.
359 // EDIT is special since only layout information, not the
360 // contents of a paragaph are stored.
361 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
362 // check wether storing is needed
363 if (!bv->buffer()->undostack.empty() &&
364 bv->buffer()->undostack.top()->kind == kind &&
365 bv->buffer()->undostack.top()->number_of_before_par == before_number &&
366 bv->buffer()->undostack.top()->number_of_behind_par == behind_number) {
374 Paragraph * start = const_cast<Paragraph *>(first);
378 end = const_cast<Paragraph*>(behind->previous());
384 if (start && end && (start != end->next()) &&
385 ((before_number != behind_number) ||
386 ((before_number < 0) && (behind_number < 0))))
388 Paragraph * tmppar = start;
389 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
391 // a memory optimization: Just store the layout information
393 if (kind == Undo::EDIT) {
394 tmppar2->clearContents();
399 while (tmppar != end && tmppar->next()) {
400 tmppar = tmppar->next();
402 tmppar2->next(new Paragraph(*tmppar, true));
404 Paragraph * ptmp = new Paragraph(*tmppar, true);
407 // a memory optimization: Just store the layout
408 // information when only edit
409 if (kind == Undo::EDIT) {
410 tmppar2->clearContents();
412 tmppar2->next()->previous(tmppar2);
414 tmppar2 = tmppar2->next();
418 undopar = 0; // nothing to replace (undo of delete maybe)
420 int cursor_par = undoCursor(bv).par()->id();
421 int cursor_pos = undoCursor(bv).pos();
423 Undo * undo = new Undo(kind, inset_id,
424 before_number, behind_number,
425 cursor_par, cursor_pos, undopar);
427 undo_finished = false;
432 void setCursorParUndo(BufferView * bv)
434 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
435 bv->text->cursor.par()->next());
439 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
441 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
443 Paragraph * result = inset->getFirstParagraph(0);
447 return bv->text->ownerParagraph();
451 LyXCursor const & undoCursor(BufferView * bv)
453 if (bv->theLockingInset())
454 return bv->theLockingInset()->cursor(bv);
455 return bv->text->cursor;