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 ParIterator const before = b->getParFromID(undo.number_of_before_par);
82 ParIterator const behind = b->getParFromID(undo.number_of_behind_par);
83 ParIterator const end = b->par_iterator_end();
85 // If there's no before take the beginning
86 // of the document for redoing.
88 LyXText * t = bv->text;
89 int num = undo.number_of_inset_id;
90 if (undo.number_of_inset_id >= 0) {
91 Inset * in = bv->buffer()->getInsetFromID(num);
93 t = in->getLyXText(bv);
98 t->setCursorIntern(undoParagraphs(bv, num).begin(), 0);
101 // Set the right(new) inset-owner of the paragraph if there is any.
102 if (!undo.pars.empty()) {
105 in = before->inInset();
106 else if (undo.number_of_inset_id >= 0)
107 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
108 for (size_t i = 0, n = undo.pars.size(); i < n; ++i)
109 undo.pars[i]->setInsetOwner(in);
112 // Replace the paragraphs with the undo informations.
113 vector<Paragraph *> deletelist;
115 // Now add old paragraphs to be deleted.
116 if (before != behind || (behind == end && before == end)) {
117 ParagraphList::iterator deletepar;
122 deletepar = undoParagraphs(bv, undo.number_of_inset_id).begin();
124 // this surprisingly fills the undo! (Andre')
126 //while (deletepar && deletepar != *behind)
127 while (deletepar != *behind) {
128 deletelist.push_back(&*deletepar);
131 // A memory optimization for edit:
132 // Only layout information
133 // is stored in the undo. So restore
134 // the text informations.
135 if (undo.kind == Undo::EDIT) {
136 undo.pars[par]->setContentsFromPar(*deletelist.back());
142 // The order here is VERY IMPORTANT. We have to set the right
143 // next/prev pointer in the paragraphs so that a rebuild of
144 // the LyXText works!!!
146 // Thread the end of the undo onto the par in front if any.
147 if (!undo.pars.empty()) {
148 undo.pars.back()->next(&**behind);
150 (&**behind)->previous(undo.pars.back());
153 // Put the new stuff in the list if there is one.
154 Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front();
155 if (!undo.pars.empty()) {
156 undo.pars.front()->previous(&**before);
158 (&**before)->next(undopar);
160 int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
161 ParIterator op = bv->buffer()->getParFromID(id);
162 if (op != end && op->inInset()) {
163 #warning FIXME reimplementaion needed here
164 //static_cast<InsetText*>(op->inInset())->paragraph(undopar);
166 #warning FIXME reimplementation needed here
167 //bv->buffer()->paragraphs.set(undopar);
171 // We enter here on DELETE undo operations where we
172 // have to substitue the second paragraph with the
173 // first if the removed one is the first.
174 if (before == end && behind != end) {
175 int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
176 ParIterator op = bv->buffer()->getParFromID(id);
177 if (op != end && op->inInset()) {
178 #warning FIXME reimplementation needed here
179 //static_cast<InsetText*>(op->inInset())->paragraph(&**behind);
181 #warning FIXME reimplementation needed here
182 //bv->buffer()->paragraphs.set(&**behind);
189 // Set the cursor for redoing.
190 // If we have a par before the undopar.
192 Inset * it = before->inInset();
194 it->getLyXText(bv)->setCursorIntern(*before, 0);
196 bv->text->setCursorIntern(*before, 0);
199 // we are not ready for this we cannot set the cursor for a paragraph
200 // which is not already in a row of LyXText!!!
202 else { // otherwise this is the first one and we start here
203 Inset * it = undopar->inInset();
205 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
207 bv->text->setCursorIntern(bv, undopar, 0);
211 UpdatableInset * it = 0;
213 it = static_cast<UpdatableInset*>(undopar->inInset());
215 LyXText * text = it ? it->getLyXText(bv) : bv->text;
217 ParagraphList::iterator endpar = text->ownerParagraphs().end();
219 // Calculate the endpar for redoing the paragraphs.
225 text->redoParagraphs(text->cursor, endpar);
227 bv->buffer()->getParFromID(undo.number_of_cursor_par);
231 Inset * it = tmppar->inInset();
233 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
234 it->localDispatch(cmd);
235 t = it->getLyXText(bv);
239 t->setCursorIntern(*tmppar, undo.cursor_pos);
240 // Clear any selection and set the selection
241 // cursor for an evt. new selection.
243 t->selection.cursor = t->cursor;
250 bv->text->setCursorIntern(bv->text->cursor.par(),
251 bv->text->cursor.pos());
254 // And here it's safe enough to delete all removed paragraphs.
255 vector<Paragraph *>::iterator pit = deletelist.begin();
256 for(; pit != deletelist.end(); ++pit) {
262 // Otherwise the undo destructor would delete the paragraphs
266 bv->text->postPaint(0);
271 bool createUndo(BufferView * bv, Undo::undo_kind kind,
272 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
273 shared_ptr<Undo> & u)
275 Paragraph * const first = &*itfirst;
276 Paragraph * const behind = &*itbehind;
279 int before_number = -1;
280 int behind_number = -1;
283 if (first->previous())
284 before_number = first->previous()->id();
286 behind_number = behind->id();
287 if (first->inInset())
288 inset_id = first->inInset()->id();
290 Buffer * b = bv->buffer();
292 // Undo::EDIT and Undo::FINISH are
293 // always finished. (no overlapping there)
294 // overlapping only with insert and delete inside one paragraph:
295 // Nobody wants all removed character
296 // appear one by one when undoing.
297 // EDIT is special since only layout information, not the
298 // contents of a paragaph are stored.
299 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
300 // Check whether storing is needed.
301 if (!b->undostack.empty() &&
302 b->undostack.top()->kind == kind &&
303 b->undostack.top()->number_of_before_par == before_number &&
304 b->undostack.top()->number_of_behind_par == behind_number) {
310 // Create a new Undo.
311 std::vector<Paragraph *> undo_pars;
313 Paragraph const * end = 0;
316 end = behind->previous();
323 if (first && end && (first != end->next()) &&
324 ((before_number != behind_number) ||
325 ((before_number < 0) && (behind_number < 0))))
327 undo_pars.push_back(new Paragraph(*first, true));
328 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
329 tmppar = tmppar->next();
330 undo_pars.push_back(new Paragraph(*tmppar, true));
331 size_t const n = undo_pars.size();
332 undo_pars[n - 2]->next(undo_pars[n - 1]);
333 undo_pars[n - 1]->previous(undo_pars[n - 2]);
335 undo_pars.back()->next(0);
338 // A memory optimization: Just store the layout
339 // information when only edit.
340 if (kind == Undo::EDIT) {
341 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
342 undo_pars[i]->clearContents();
345 int cursor_par = undoCursor(bv).par()->id();
346 int cursor_pos = undoCursor(bv).pos();
348 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
349 // << before_number << " behind_number: " << behind_number << "\n";
350 u.reset(new Undo(kind, inset_id,
351 before_number, behind_number,
352 cursor_par, cursor_pos, undo_pars));
354 undo_finished = false;
359 // Returns false if no undo possible.
360 bool textUndoOrRedo(BufferView * bv,
361 limited_stack<boost::shared_ptr<Undo> > & stack,
362 limited_stack<boost::shared_ptr<Undo> > & otherstack)
364 Buffer * b = bv->buffer();
371 shared_ptr<Undo> undo = stack.top();
377 ParIterator p = b->getParFromID(undo->number_of_before_par);
379 ParagraphList::iterator first;
380 // default constructed?
381 ParIterator const end = b->par_iterator_end();
385 first = first->next();
387 first = undoParagraphs(bv, undo->number_of_inset_id).begin();
390 ParIterator behind = b->getParFromID(undo->number_of_behind_par);
391 if (createUndo(bv, undo->kind, first, 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()));