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()) {
149 //undo.pars.back()->next(&**behind);
151 //(&**behind)->previous(undo.pars.back());
154 // Put the new stuff in the list if there is one.
155 Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front();
156 if (!undo.pars.empty()) {
158 //undo.pars.front()->previous(&**before);
161 //(&**before)->next(undopar);
163 int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
164 ParIterator op = bv->buffer()->getParFromID(id);
165 if (op != end && op->inInset()) {
166 #warning FIXME reimplementaion needed here
167 //static_cast<InsetText*>(op->inInset())->paragraph(undopar);
169 #warning FIXME reimplementation needed here
170 //bv->buffer()->paragraphs.set(undopar);
174 // We enter here on DELETE undo operations where we
175 // have to substitue the second paragraph with the
176 // first if the removed one is the first.
177 if (before == end && behind != end) {
178 int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
179 ParIterator op = bv->buffer()->getParFromID(id);
180 if (op != end && op->inInset()) {
181 #warning FIXME reimplementation needed here
182 //static_cast<InsetText*>(op->inInset())->paragraph(&**behind);
184 #warning FIXME reimplementation needed here
185 //bv->buffer()->paragraphs.set(&**behind);
192 // Set the cursor for redoing.
193 // If we have a par before the undopar.
195 Inset * it = before->inInset();
197 it->getLyXText(bv)->setCursorIntern(*before, 0);
199 bv->text->setCursorIntern(*before, 0);
202 // we are not ready for this we cannot set the cursor for a paragraph
203 // which is not already in a row of LyXText!!!
205 else { // otherwise this is the first one and we start here
206 Inset * it = undopar->inInset();
208 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
210 bv->text->setCursorIntern(bv, undopar, 0);
214 UpdatableInset * it = 0;
216 it = static_cast<UpdatableInset*>(undopar->inInset());
218 LyXText * text = it ? it->getLyXText(bv) : bv->text;
220 ParagraphList::iterator endpar = text->ownerParagraphs().end();
222 // Calculate the endpar for redoing the paragraphs.
228 text->redoParagraphs(text->cursor, endpar);
230 bv->buffer()->getParFromID(undo.number_of_cursor_par);
234 Inset * it = tmppar->inInset();
236 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
237 it->localDispatch(cmd);
238 t = it->getLyXText(bv);
242 t->setCursorIntern(*tmppar, undo.cursor_pos);
243 // Clear any selection and set the selection
244 // cursor for an evt. new selection.
246 t->selection.cursor = t->cursor;
253 bv->text->setCursorIntern(bv->text->cursor.par(),
254 bv->text->cursor.pos());
257 // And here it's safe enough to delete all removed paragraphs.
258 vector<Paragraph *>::iterator pit = deletelist.begin();
259 for(; pit != deletelist.end(); ++pit) {
261 //(*pit)->previous(0);
266 // Otherwise the undo destructor would delete the paragraphs
270 bv->text->postPaint(0);
275 bool createUndo(BufferView * bv, Undo::undo_kind kind,
276 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
277 shared_ptr<Undo> & u)
279 Paragraph * const first = &*itfirst;
280 Paragraph * const behind = &*itbehind;
283 int before_number = -1;
284 int behind_number = -1;
288 //if (first->previous())
289 // before_number = first->previous()->id();
291 behind_number = behind->id();
292 if (first->inInset())
293 inset_id = first->inInset()->id();
295 Buffer * b = bv->buffer();
297 // Undo::EDIT and Undo::FINISH are
298 // always finished. (no overlapping there)
299 // overlapping only with insert and delete inside one paragraph:
300 // Nobody wants all removed character
301 // appear one by one when undoing.
302 // EDIT is special since only layout information, not the
303 // contents of a paragaph are stored.
304 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
305 // Check whether storing is needed.
306 if (!b->undostack.empty() &&
307 b->undostack.top()->kind == kind &&
308 b->undostack.top()->number_of_before_par == before_number &&
309 b->undostack.top()->number_of_behind_par == behind_number) {
315 // Create a new Undo.
316 std::vector<Paragraph *> undo_pars;
318 Paragraph const * end = 0;
322 //end = behind->previous();
326 //while (end->next())
327 // end = end->next();
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();
385 ParIterator p = b->getParFromID(undo->number_of_before_par);
387 ParagraphList::iterator first;
388 // default constructed?
389 ParIterator const end = b->par_iterator_end();
393 first = first->next();
395 first = undoParagraphs(bv, undo->number_of_inset_id).begin();
398 ParIterator behind = b->getParFromID(undo->number_of_behind_par);
399 if (createUndo(bv, undo->kind, first, behind.par(), u))
405 // Now we can unlock the inset for saftey because the inset
406 // pointer could be changed during the undo-function. Anyway
407 // if needed we have to lock the right inset/position if this
410 bv->unlockInset(bv->theLockingInset());
411 bool const ret = textHandleUndo(bv, *undo.get());
421 // Makes sure the next operation will be stored.
422 undo_finished = true;
428 // This is dangerous and for internal use only.
435 // This is dangerous and for internal use only.
440 bool textUndo(BufferView * bv)
442 return textUndoOrRedo(bv, bv->buffer()->undostack,
443 bv->buffer()->redostack);
447 bool textRedo(BufferView * bv)
449 return textUndoOrRedo(bv, bv->buffer()->redostack,
450 bv->buffer()->undostack);
454 void setUndo(BufferView * bv, Undo::undo_kind kind,
455 ParagraphList::iterator first, ParagraphList::iterator behind)
459 if (createUndo(bv, kind, first, behind, u))
460 bv->buffer()->undostack.push(u);
461 bv->buffer()->redostack.clear();
466 void setRedo(BufferView * bv, Undo::undo_kind kind,
467 ParagraphList::iterator first, ParagraphList::iterator behind)
470 if (createUndo(bv, kind, first, behind, u))
471 bv->buffer()->redostack.push(u);
475 void setCursorParUndo(BufferView * bv)
477 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
478 boost::next(bv->text->cursor.par()));