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);
228 Inset * it = tmppar->inInset();
230 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
231 it->localDispatch(cmd);
232 t = it->getLyXText(bv);
236 t->setCursorIntern(*tmppar, undo.cursor_pos);
237 // Clear any selection and set the selection
238 // cursor for an evt. new selection.
240 t->selection.cursor = t->cursor;
245 bv->text->setCursorIntern(bv->text->cursor.par(),
246 bv->text->cursor.pos());
250 Inset * it = tmppar->inInset();
252 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
253 it->localDispatch(cmd);
254 t = it->getLyXText(bv);
258 t->setCursorIntern(*tmppar, undo.cursor_pos);
259 // Clear any selection and set the selection
260 // cursor for an evt. new selection.
262 t->selection.cursor = t->cursor;
267 // And here it's safe enough to delete all removed paragraphs.
268 vector<Paragraph *>::iterator pit = deletelist.begin();
269 for(; pit != deletelist.end(); ++pit) {
275 // Otherwise the undo destructor would delete the paragraphs
279 bv->text->postPaint(0);
284 bool createUndo(BufferView * bv, Undo::undo_kind kind,
285 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
286 shared_ptr<Undo> & u)
288 Paragraph * const first = &*itfirst;
289 Paragraph * const behind = &*itbehind;
292 int before_number = -1;
293 int behind_number = -1;
296 if (first->previous())
297 before_number = first->previous()->id();
299 behind_number = behind->id();
300 if (first->inInset())
301 inset_id = first->inInset()->id();
303 Buffer * b = bv->buffer();
305 // Undo::EDIT and Undo::FINISH are
306 // always finished. (no overlapping there)
307 // overlapping only with insert and delete inside one paragraph:
308 // Nobody wants all removed character
309 // appear one by one when undoing.
310 // EDIT is special since only layout information, not the
311 // contents of a paragaph are stored.
312 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
313 // Check whether storing is needed.
314 if (!b->undostack.empty() &&
315 b->undostack.top()->kind == kind &&
316 b->undostack.top()->number_of_before_par == before_number &&
317 b->undostack.top()->number_of_behind_par == behind_number) {
323 // Create a new Undo.
324 std::vector<Paragraph *> undo_pars;
326 Paragraph const * end = 0;
329 end = behind->previous();
336 if (first && end && (first != end->next()) &&
337 ((before_number != behind_number) ||
338 ((before_number < 0) && (behind_number < 0))))
340 undo_pars.push_back(new Paragraph(*first, true));
341 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
342 tmppar = tmppar->next();
343 undo_pars.push_back(new Paragraph(*tmppar, true));
344 size_t const n = undo_pars.size();
345 undo_pars[n - 2]->next(undo_pars[n - 1]);
346 undo_pars[n - 1]->previous(undo_pars[n - 2]);
348 undo_pars.back()->next(0);
351 // A memory optimization: Just store the layout
352 // information when only edit.
353 if (kind == Undo::EDIT) {
354 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
355 undo_pars[i]->clearContents();
358 int cursor_par = undoCursor(bv).par()->id();
359 int cursor_pos = undoCursor(bv).pos();
361 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
362 // << before_number << " behind_number: " << behind_number << "\n";
363 u.reset(new Undo(kind, inset_id,
364 before_number, behind_number,
365 cursor_par, cursor_pos, undo_pars));
367 undo_finished = false;
372 // Returns false if no undo possible.
373 bool textUndoOrRedo(BufferView * bv,
374 limited_stack<boost::shared_ptr<Undo> > & stack,
375 limited_stack<boost::shared_ptr<Undo> > & otherstack)
377 Buffer * b = bv->buffer();
384 shared_ptr<Undo> undo = stack.top();
390 ParIterator p = b->getParFromID(undo->number_of_before_par);
392 ParagraphList::iterator first;
393 // default constructed?
394 ParIterator const end = b->par_iterator_end();
398 first = first->next();
400 first = undoParagraphs(bv, undo->number_of_inset_id).begin();
403 ParIterator behind = b->getParFromID(undo->number_of_behind_par);
404 if (createUndo(bv, undo->kind, first, behind.par(), u))
410 // Now we can unlock the inset for saftey because the inset
411 // pointer could be changed during the undo-function. Anyway
412 // if needed we have to lock the right inset/position if this
415 bv->unlockInset(bv->theLockingInset());
416 bool const ret = textHandleUndo(bv, *undo.get());
426 // Makes sure the next operation will be stored.
427 undo_finished = true;
433 // This is dangerous and for internal use only.
440 // This is dangerous and for internal use only.
445 bool textUndo(BufferView * bv)
447 return textUndoOrRedo(bv, bv->buffer()->undostack,
448 bv->buffer()->redostack);
452 bool textRedo(BufferView * bv)
454 return textUndoOrRedo(bv, bv->buffer()->redostack,
455 bv->buffer()->undostack);
459 void setUndo(BufferView * bv, Undo::undo_kind kind,
460 ParagraphList::iterator first, ParagraphList::iterator behind)
464 if (createUndo(bv, kind, first, behind, u))
465 bv->buffer()->undostack.push(u);
466 bv->buffer()->redostack.clear();
471 void setRedo(BufferView * bv, Undo::undo_kind kind,
472 ParagraphList::iterator first, ParagraphList::iterator behind)
475 if (createUndo(bv, kind, first, behind, u))
476 bv->buffer()->redostack.push(u);
480 void setCursorParUndo(BufferView * bv)
482 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
483 boost::next(bv->text->cursor.par()));