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 "funcrequest.h"
15 #include "BufferView.h"
17 #include "insets/updatableinset.h"
18 #include "insets/insettext.h"
20 #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 ParagraphList undoParagraphs(BufferView * bv, int inset_id)
52 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
54 ParagraphList * result = inset->getParagraphs(0);
55 if (result && !result->empty())
58 return bv->text->ownerParagraphs();
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(undoParagraphs(bv, num).begin(), 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 = &undoParagraphs(bv, undo.number_of_inset_id).front();
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 = undoParagraphs(bv, undo.number_of_inset_id).front().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 = undoParagraphs(bv, undo.number_of_inset_id).front().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());
222 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
223 it->localDispatch(cmd);
224 t = it->getLyXText(bv);
228 t->setCursorIntern(tmppar, undo.cursor_pos);
229 // Clear any selection and set the selection
230 // cursor for an evt. new selection.
232 t->selection.cursor = t->cursor;
237 bv->text->setCursorIntern(bv->text->cursor.par(),
238 bv->text->cursor.pos());
240 bv->text->redoParagraphs(bv->text->cursor, endpar);
242 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
245 Inset * it = tmppar->inInset();
247 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
248 it->localDispatch(cmd);
249 t = it->getLyXText(bv);
253 t->setCursorIntern(tmppar, undo.cursor_pos);
254 // Clear any selection and set the selection
255 // cursor for an evt. new selection.
257 t->selection.cursor = t->cursor;
262 // And here it's safe enough to delete all removed paragraphs.
263 vector<Paragraph *>::iterator pit = deletelist.begin();
264 for(; pit != deletelist.end(); ++pit) {
270 // Otherwise the undo destructor would delete the paragraphs
274 bv->text->postPaint(0);
279 bool createUndo(BufferView * bv, Undo::undo_kind kind,
280 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
281 shared_ptr<Undo> & u)
283 Paragraph * const first = &*itfirst;
284 Paragraph * const behind = &*itbehind;
287 int before_number = -1;
288 int behind_number = -1;
291 if (first->previous())
292 before_number = first->previous()->id();
294 behind_number = behind->id();
295 if (first->inInset())
296 inset_id = first->inInset()->id();
298 Buffer * b = bv->buffer();
300 // Undo::EDIT and Undo::FINISH are
301 // always finished. (no overlapping there)
302 // overlapping only with insert and delete inside one paragraph:
303 // Nobody wants all removed character
304 // appear one by one when undoing.
305 // EDIT is special since only layout information, not the
306 // contents of a paragaph are stored.
307 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
308 // Check whether storing is needed.
309 if (!b->undostack.empty() &&
310 b->undostack.top()->kind == kind &&
311 b->undostack.top()->number_of_before_par == before_number &&
312 b->undostack.top()->number_of_behind_par == behind_number) {
318 // Create a new Undo.
319 std::vector<Paragraph *> undo_pars;
321 Paragraph const * end = 0;
324 end = behind->previous();
331 if (first && end && (first != end->next()) &&
332 ((before_number != behind_number) ||
333 ((before_number < 0) && (behind_number < 0))))
335 undo_pars.push_back(new Paragraph(*first, true));
336 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
337 tmppar = tmppar->next();
338 undo_pars.push_back(new Paragraph(*tmppar, true));
339 size_t const n = undo_pars.size();
340 undo_pars[n - 2]->next(undo_pars[n - 1]);
341 undo_pars[n - 1]->previous(undo_pars[n - 2]);
343 undo_pars.back()->next(0);
346 // A memory optimization: Just store the layout
347 // information when only edit.
348 if (kind == Undo::EDIT) {
349 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
350 undo_pars[i]->clearContents();
353 int cursor_par = undoCursor(bv).par()->id();
354 int cursor_pos = undoCursor(bv).pos();
356 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
357 // << before_number << " behind_number: " << behind_number << "\n";
358 u.reset(new Undo(kind, inset_id,
359 before_number, behind_number,
360 cursor_par, cursor_pos, undo_pars));
362 undo_finished = false;
367 // Returns false if no undo possible.
368 bool textUndoOrRedo(BufferView * bv,
369 limited_stack<boost::shared_ptr<Undo> > & stack,
370 limited_stack<boost::shared_ptr<Undo> > & otherstack)
372 Buffer * b = bv->buffer();
379 shared_ptr<Undo> undo = stack.top();
384 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
385 if (first && first->next())
386 first = first->next();
388 first = &*undoParagraphs(bv, undo->number_of_inset_id).begin();
391 if (createUndo(bv, undo->kind, first,
392 b->getParFromID(undo->number_of_behind_par), u))
397 // Now we can unlock the inset for saftey because the inset
398 // pointer could be changed during the undo-function. Anyway
399 // if needed we have to lock the right inset/position if this
402 bv->unlockInset(bv->theLockingInset());
403 bool const ret = textHandleUndo(bv, *undo.get());
413 // Makes sure the next operation will be stored.
414 undo_finished = true;
420 // This is dangerous and for internal use only.
427 // This is dangerous and for internal use only.
432 bool textUndo(BufferView * bv)
434 return textUndoOrRedo(bv, bv->buffer()->undostack,
435 bv->buffer()->redostack);
439 bool textRedo(BufferView * bv)
441 return textUndoOrRedo(bv, bv->buffer()->redostack,
442 bv->buffer()->undostack);
446 void setUndo(BufferView * bv, Undo::undo_kind kind,
447 ParagraphList::iterator first, ParagraphList::iterator behind)
451 if (createUndo(bv, kind, first, behind, u))
452 bv->buffer()->undostack.push(u);
453 bv->buffer()->redostack.clear();
458 void setRedo(BufferView * bv, Undo::undo_kind kind,
459 ParagraphList::iterator first, ParagraphList::iterator behind)
462 if (createUndo(bv, kind, first, behind, u))
463 bv->buffer()->redostack.push(u);
467 void setCursorParUndo(BufferView * bv)
469 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
470 boost::next(bv->text->cursor.par()));