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 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
165 bv->buffer()->paragraphs.set(undopar);
169 // We enter here on DELETE undo operations where we
170 // have to substitue the second paragraph with the
171 // first if the removed one is the first.
172 if (before == end && behind != end) {
173 int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
174 ParIterator op = bv->buffer()->getParFromID(id);
175 if (op != end && op->inInset()) {
176 static_cast<InsetText*>(op->inInset())->paragraph(&**behind);
178 bv->buffer()->paragraphs.set(&**behind);
185 // Set the cursor for redoing.
186 // If we have a par before the undopar.
188 Inset * it = before->inInset();
190 it->getLyXText(bv)->setCursorIntern(*before, 0);
192 bv->text->setCursorIntern(*before, 0);
195 // we are not ready for this we cannot set the cursor for a paragraph
196 // which is not already in a row of LyXText!!!
198 else { // otherwise this is the first one and we start here
199 Inset * it = undopar->inInset();
201 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
203 bv->text->setCursorIntern(bv, undopar, 0);
207 UpdatableInset * it = 0;
209 it = static_cast<UpdatableInset*>(undopar->inInset());
211 LyXText * text = it ? it->getLyXText(bv) : bv->text;
213 ParagraphList::iterator endpar = text->ownerParagraphs().end();
215 // Calculate the endpar for redoing the paragraphs.
221 text->redoParagraphs(text->cursor, endpar);
223 bv->buffer()->getParFromID(undo.number_of_cursor_par);
227 Inset * it = tmppar->inInset();
229 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
230 it->localDispatch(cmd);
231 t = it->getLyXText(bv);
235 t->setCursorIntern(*tmppar, undo.cursor_pos);
236 // Clear any selection and set the selection
237 // cursor for an evt. new selection.
239 t->selection.cursor = t->cursor;
246 bv->text->setCursorIntern(bv->text->cursor.par(),
247 bv->text->cursor.pos());
250 // And here it's safe enough to delete all removed paragraphs.
251 vector<Paragraph *>::iterator pit = deletelist.begin();
252 for(; pit != deletelist.end(); ++pit) {
258 // Otherwise the undo destructor would delete the paragraphs
262 bv->text->postPaint(0);
267 bool createUndo(BufferView * bv, Undo::undo_kind kind,
268 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
269 shared_ptr<Undo> & u)
271 Paragraph * const first = &*itfirst;
272 Paragraph * const behind = &*itbehind;
275 int before_number = -1;
276 int behind_number = -1;
279 if (first->previous())
280 before_number = first->previous()->id();
282 behind_number = behind->id();
283 if (first->inInset())
284 inset_id = first->inInset()->id();
286 Buffer * b = bv->buffer();
288 // Undo::EDIT and Undo::FINISH are
289 // always finished. (no overlapping there)
290 // overlapping only with insert and delete inside one paragraph:
291 // Nobody wants all removed character
292 // appear one by one when undoing.
293 // EDIT is special since only layout information, not the
294 // contents of a paragaph are stored.
295 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
296 // Check whether storing is needed.
297 if (!b->undostack.empty() &&
298 b->undostack.top()->kind == kind &&
299 b->undostack.top()->number_of_before_par == before_number &&
300 b->undostack.top()->number_of_behind_par == behind_number) {
306 // Create a new Undo.
307 std::vector<Paragraph *> undo_pars;
309 Paragraph const * end = 0;
312 end = behind->previous();
319 if (first && end && (first != end->next()) &&
320 ((before_number != behind_number) ||
321 ((before_number < 0) && (behind_number < 0))))
323 undo_pars.push_back(new Paragraph(*first, true));
324 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
325 tmppar = tmppar->next();
326 undo_pars.push_back(new Paragraph(*tmppar, true));
327 size_t const n = undo_pars.size();
328 undo_pars[n - 2]->next(undo_pars[n - 1]);
329 undo_pars[n - 1]->previous(undo_pars[n - 2]);
331 undo_pars.back()->next(0);
334 // A memory optimization: Just store the layout
335 // information when only edit.
336 if (kind == Undo::EDIT) {
337 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
338 undo_pars[i]->clearContents();
341 int cursor_par = undoCursor(bv).par()->id();
342 int cursor_pos = undoCursor(bv).pos();
344 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
345 // << before_number << " behind_number: " << behind_number << "\n";
346 u.reset(new Undo(kind, inset_id,
347 before_number, behind_number,
348 cursor_par, cursor_pos, undo_pars));
350 undo_finished = false;
355 // Returns false if no undo possible.
356 bool textUndoOrRedo(BufferView * bv,
357 limited_stack<boost::shared_ptr<Undo> > & stack,
358 limited_stack<boost::shared_ptr<Undo> > & otherstack)
360 Buffer * b = bv->buffer();
367 shared_ptr<Undo> undo = stack.top();
373 ParIterator p = b->getParFromID(undo->number_of_before_par);
375 ParagraphList::iterator first;
376 // default constructed?
377 ParIterator const end = b->par_iterator_end();
381 first = first->next();
383 first = undoParagraphs(bv, undo->number_of_inset_id).begin();
386 ParIterator behind = b->getParFromID(undo->number_of_behind_par);
387 if (createUndo(bv, undo->kind, first, behind.par(), u))
393 // Now we can unlock the inset for saftey because the inset
394 // pointer could be changed during the undo-function. Anyway
395 // if needed we have to lock the right inset/position if this
398 bv->unlockInset(bv->theLockingInset());
399 bool const ret = textHandleUndo(bv, *undo.get());
409 // Makes sure the next operation will be stored.
410 undo_finished = true;
416 // This is dangerous and for internal use only.
423 // This is dangerous and for internal use only.
428 bool textUndo(BufferView * bv)
430 return textUndoOrRedo(bv, bv->buffer()->undostack,
431 bv->buffer()->redostack);
435 bool textRedo(BufferView * bv)
437 return textUndoOrRedo(bv, bv->buffer()->redostack,
438 bv->buffer()->undostack);
442 void setUndo(BufferView * bv, Undo::undo_kind kind,
443 ParagraphList::iterator first, ParagraphList::iterator behind)
447 if (createUndo(bv, kind, first, behind, u))
448 bv->buffer()->undostack.push(u);
449 bv->buffer()->redostack.clear();
454 void setRedo(BufferView * bv, Undo::undo_kind kind,
455 ParagraphList::iterator first, ParagraphList::iterator behind)
458 if (createUndo(bv, kind, first, behind, u))
459 bv->buffer()->redostack.push(u);
463 void setCursorParUndo(BufferView * bv)
465 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
466 boost::next(bv->text->cursor.par()));