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"
24 /// The flag used by FinishUndo().
26 /// Whether actions are not added to the undo stacks.
33 * Finish the undo operation in the case there was no entry
34 * on the stack to perform.
36 void finishNoUndo(BufferView * bv)
39 bv->unlockInset(bv->theLockingInset());
41 bv->text->postPaint(0);
46 // Returns false if no undo possible.
47 bool textHandleUndo(BufferView * bv, Undo & undo)
49 Buffer * buf = bv->buffer();
51 ParagraphList * plist = &buf->paragraphs;
53 ParIterator null = buf->par_iterator_end();
55 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it)
56 if (it.plist().id() == undo.plist_id) {
62 // Set the right(new) inset-owner of the paragraph if there is any.
63 UpdatableInset * inset = 0;
64 if (undo.inset_id >= 0) {
65 Inset * in = bv->buffer()->getInsetFromID(undo.inset_id);
66 inset = static_cast<UpdatableInset *>(in);
68 ParagraphList::iterator pit = undo.pars.begin();
69 ParagraphList::iterator end = undo.pars.end();
70 for ( ; pit != end; ++pit)
71 pit->setInsetOwner(inset);
73 lyxerr << "\nhandle: inset_id: " << undo.inset_id << "\n";
74 lyxerr << "handle: inset: " << inset << "\n";
75 lyxerr << "handle: plist_id: " << undo.plist_id << "\n";
76 lyxerr << "handle: undo.pars.size(): " << undo.pars.size() << "\n";
78 // remove stuff between first and behind
80 ParagraphList::iterator first = plist->begin();
81 advance(first, undo.first_par_offset);
82 ParagraphList::iterator last = plist->begin();
83 advance(last, plist->size() - undo.last_par_offset);
84 lyxerr << "handle: first_id: " << first->id() << "\n";
85 lyxerr << "handle: last_id: " << last->id() << "\n";
86 plist->erase(first, ++last);
87 lyxerr << "after remove\n";
90 // re-insert old stuff
92 ParagraphList::iterator first = plist->begin();
93 advance(first, undo.first_par_offset);
94 plist->insert(first, undo.pars.begin(), undo.pars.end());
95 lyxerr << "after insert\n";
99 // A memory optimization for edit:
100 // Only layout information
101 // is stored in the undo. So restore
102 // the text informations.
103 if (undo.kind == Undo::EDIT) {
104 undo.pars[par]->setContentsFromPar(*deletelist.back());
109 // redo Paragraphs (should be handled outside undo...)
110 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
112 lyxerr << "text: " << text << "\n";
113 if (undo.first_par_offset) {
114 ParagraphList::iterator redo = plist->begin();
115 advance(redo, undo.first_par_offset);
116 text->setCursorIntern(plist->begin(), 0);
118 text->redoParagraphs(text->cursor, plist->end());
119 lyxerr << "after redo\n";
123 Inset * = bv->buffer()->getInsetFromID(inset_id);
124 lyxerr << "tmppar: " << tmppar->id() << "\n";
127 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
128 it->localDispatch(cmd);
129 t = it->getLyXText(bv);
133 t->setCursorIntern(*tmppar, undo.cursor_pos);
134 // Clear any selection and set the selection
135 // cursor for an evt. new selection.
137 t->selection.cursor = t->cursor;
142 lyxerr << "fit cursor...\n";
144 bv->updateInset(inset);
149 ParagraphList::iterator cursor = plist->begin();
150 advance(cursor, undo.cursor_par_offset);
151 bv->text->setCursorIntern(cursor, undo.cursor_pos);
152 lyxerr << "after setCursor\n";
157 bv->text->postPaint(0);
159 lyxerr << "finished textHandleUndo...\n";
164 void createUndo(BufferView * bv, Undo::undo_kind kind,
165 ParagraphList::iterator first, ParagraphList::iterator last,
166 limited_stack<Undo> & stack)
168 Buffer * buf = bv->buffer();
170 ParagraphList * plist = 0;
171 ParIterator null = buf->par_iterator_end();
176 // this is what we'd like to have in the end for small grained undo
177 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
178 if (it->id() == first->id()) {
186 // and that's the big stick we wield now
187 lyxerr << "create: first_id orig: " << first->id() << "\n";
188 lyxerr << "create: last_id orig: " << last->id() << "\n";
189 plist = &buf->paragraphs;
190 // this is what we'd like to have in the end for small grained undo
191 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
192 if (it->id() == first->id()) {
193 first = it.outerPar();
194 last = it.outerPar();
201 int const inset_id = first->inInset() ? first->inInset()->id() : -1;
203 int const first_offset = std::distance(plist->begin(), first);
204 int const last_offset = std::distance(last, plist->end());
206 if (last == plist->end()) {
207 lyxerr << "*** createUndo: last == end schould not happen\n";
210 //lyxerr << "create: plist_id: " << plist->id() << "\n";
211 lyxerr << "create: first_id: " << first->id() << "\n";
212 lyxerr << "create: last_id: " << last->id() << "\n";
213 lyxerr << "create: first_offset: " << first_offset << "\n";
214 lyxerr << "create: last_offset: " << last_offset << "\n";
215 lyxerr << "create: inset_id: " << inset_id << "\n";
216 lyxerr << "create: kind: " << kind << "\n";
219 // Undo::EDIT and Undo::FINISH are
220 // always finished. (no overlapping there)
221 // overlapping only with insert and delete inside one paragraph:
222 // Nobody wants all removed character
223 // appear one by one when undoing.
224 // EDIT is special since only layout information, not the
225 // contents of a paragaph are stored.
226 if (!undo_finished && kind != Undo::EDIT && kind != Undo::FINISH) {
227 // Check whether storing is needed.
228 if (!buf->undostack.empty() &&
229 buf->undostack.top().kind == kind &&
230 buf->undostack.top().first_par_offset == first_offset &&
231 buf->undostack.top().last_par_offset == last_offset) {
237 // Create a new Undo.
239 // this should be re-activated once we are back at fine-grained undo
240 LyXCursor const & cur = bv->theLockingInset() ?
241 bv->theLockingInset()->cursor(bv) : bv->text->cursor;
242 int const cursor_offset = std::distance(plist->begin(), cur.par());
244 stack.push(Undo(kind, inset_id, plist->id(),
245 first_offset, last_offset, cursor_offset, cur.pos(), ParagraphList()));
247 stack.push(Undo(kind, inset_id, 0, //plist->id(),
248 first_offset, last_offset, first_offset, 0, ParagraphList()));
250 ParagraphList & undo_pars = stack.top().pars;
252 for (ParagraphList::iterator it = first; it != last; ++it) {
253 undo_pars.push_back(*it);
254 undo_pars.back().id(it->id());
256 undo_pars.push_back(*last);
257 undo_pars.back().id(last->id());
259 // A memory optimization: Just store the layout
260 // information when only edit.
262 //if (kind == Undo::EDIT)
263 // for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
264 // undo_pars[i]->clearContents();
266 undo_finished = false;
270 // Returns false if no undo possible.
271 bool textUndoOrRedo(BufferView * bv,
272 limited_stack<Undo> & stack,
273 limited_stack<Undo> & otherstack)
280 Undo undo = stack.top();
285 otherstack.push(undo);
286 otherstack.top().pars.clear();
287 Buffer * buf = bv->buffer();
288 ParagraphList & plist = buf->paragraphs;
289 lyxerr << "\nredo: first: " << undo.first_par_offset << "\n";
290 lyxerr << "redo: last: " << undo.last_par_offset << "\n";
291 lyxerr << "redo: size: " << plist.size() << "\n";
292 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
293 ParagraphList::iterator first = plist.begin();
294 advance(first, undo.first_par_offset);
295 ParagraphList::iterator last = plist.begin();
296 advance(last, plist.size() - undo.last_par_offset + 1);
297 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
301 // Now we can unlock the inset for safety because the inset
302 // pointer could be changed during the undo-function. Anyway
303 // if needed we have to lock the right inset/position if this
306 bv->unlockInset(bv->theLockingInset());
307 bool const ret = textHandleUndo(bv, undo);
317 // Makes sure the next operation will be stored.
318 undo_finished = true;
324 // This is dangerous and for internal use only.
331 // This is dangerous and for internal use only.
336 bool textUndo(BufferView * bv)
338 return textUndoOrRedo(bv, bv->buffer()->undostack,
339 bv->buffer()->redostack);
343 bool textRedo(BufferView * bv)
345 return textUndoOrRedo(bv, bv->buffer()->redostack,
346 bv->buffer()->undostack);
350 void setUndo(BufferView * bv, Undo::undo_kind kind,
351 ParagraphList::iterator first)
353 setUndo(bv, kind, first, first);
357 void setUndo(BufferView * bv, Undo::undo_kind kind,
358 ParagraphList::iterator first, ParagraphList::iterator last)
361 createUndo(bv, kind, first, last, bv->buffer()->undostack);
362 bv->buffer()->redostack.clear();
367 void setCursorParUndo(BufferView * bv)
369 setUndo(bv, Undo::FINISH, bv->text->cursor.par());