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 ParIterator const before = buf->getParFromID(undo.number_of_before_par);
52 ParIterator const behind = buf->getParFromID(undo.number_of_behind_par);
53 ParIterator const null = buf->par_iterator_end();
55 int const before_id = (before == null) ? -1 : before->id();
56 int const behind_id = (behind == null) ? -1 : behind->id();
57 int const inset_id = undo.number_of_inset_id;
59 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
60 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
62 ParagraphList * plist = &bv->text->ownerParagraphs();
64 ParagraphList * tmp = inset->getParagraphs(0);
65 if (tmp && !tmp->empty())
69 ParagraphList::iterator first;
71 // if there's no before take the beginning of parlist.
72 first = plist->begin();
73 text->setCursorIntern(plist->begin(), 0);
78 int const first_id = first->id();
80 lyxerr << "\nhandle: before_id: " << before_id << "\n";
81 lyxerr << "handle: first_id: " << first_id << "\n";
82 lyxerr << "handle: behind_id: " << behind_id << "\n";
83 lyxerr << "handle: inset_id: " << inset_id << "\n";
85 // Set the right(new) inset-owner of the paragraph if there is any.
86 UpdatableInset * in = 0;
88 in = before->inInset();
89 else if (inset_id >= 0) {
90 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
91 in = static_cast<UpdatableInset *>(inset);
93 ParagraphList::iterator pit = undo.pars.begin();
94 ParagraphList::iterator end = undo.pars.end();
95 for ( ; pit != end; ++pit)
96 pit->setInsetOwner(in);
97 lyxerr << "in: " << in << "\n";
98 lyxerr << "undo.pars.size(): " << undo.pars.size() << "\n";
100 // remove stuff between first and behind
102 plist->erase(first, plist->end());
104 plist->erase(first, *behind);
105 lyxerr << "after erase\n";
108 if (before == null) {
109 // if there's no before take the beginning of parlist.
110 lyxerr << "no 'before'\n";
111 first = plist->begin();
113 lyxerr << "have 'before'\n";
119 // inset saved paragraphs
120 lyxerr << "undo.pars.size(): " << undo.pars.size() << "\n";
121 plist->insert(first, undo.pars.begin(), undo.pars.end());
122 lyxerr << "after insert\n";
124 // A memory optimization for edit:
125 // Only layout information
126 // is stored in the undo. So restore
127 // the text informations.
128 if (undo.kind == Undo::EDIT) {
129 undo.pars[par]->setContentsFromPar(*deletelist.back());
134 // Set the cursor for redoing
135 // if we have a par before the first.
136 if (before != null) {
137 Inset * it = before->inInset();
138 LyXText * text = it ? it->getLyXText(bv) : bv->text;
139 text->setCursorIntern(*before, 0);
142 UpdatableInset * it = 0;
143 if (first != plist->end())
144 it = first->inInset();
145 lyxerr << "it: " << it << "\n";
148 text->redoParagraphs(text->cursor, plist->end());
150 ParIterator tmppar = bv->buffer()->getParFromID(inset_id);
152 if (tmppar != null) {
153 lyxerr << "tmppar: " << tmppar->id() << "\n";
155 Inset * it = tmppar->inInset();
157 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
158 it->localDispatch(cmd);
159 t = it->getLyXText(bv);
163 t->setCursorIntern(*tmppar, undo.cursor_pos);
164 // Clear any selection and set the selection
165 // cursor for an evt. new selection.
167 t->selection.cursor = t->cursor;
170 lyxerr << "tmppar == null \n";
174 lyxerr << "fit cursor...\n";
177 bv->text->setCursorIntern(bv->text->cursor.par(),
178 bv->text->cursor.pos());
182 bv->text->postPaint(0);
184 lyxerr << "finished textHandleUndo...\n";
189 void createUndo(BufferView * bv, Undo::undo_kind kind,
190 int first_id, int last_id,
191 limited_stack<Undo> & stack)
193 Buffer * buf = bv->buffer();
195 ParIterator null = buf->par_iterator_end();
196 ParIterator prev = null;
197 ParIterator before = null;
198 ParIterator first = null;
199 ParIterator last = null;
200 ParIterator behind = null;
202 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
203 if (it->id() == first_id) {
207 if (it->id() == last_id) {
218 int const before_id = (before == null) ? -1 : before->id();
219 int const behind_id = (behind == null) ? -1 : behind->id();
220 int inset_id = (first->inInset()) ? first->inInset()->id() : -1;
222 lyxerr << "\ncreate: before_id: " << before_id << "\n";
223 lyxerr << "create: first_id: " << first_id << "\n";
224 lyxerr << "create: last_id: " << last_id << "\n";
225 lyxerr << "create: behind_id: " << behind_id << "\n";
226 lyxerr << "create: inset_id: " << inset_id << "\n";
227 lyxerr << "create: kind: " << kind << "\n";
229 ParagraphList * plist = 0;
231 plist = &first.plist();
232 else if (behind != null)
233 plist = &behind.plist();
235 lyxerr << "plist from buffer (should this happen?)\n";
236 plist = &buf->paragraphs;
239 // Undo::EDIT and Undo::FINISH are
240 // always finished. (no overlapping there)
241 // overlapping only with insert and delete inside one paragraph:
242 // Nobody wants all removed character
243 // appear one by one when undoing.
244 // EDIT is special since only layout information, not the
245 // contents of a paragaph are stored.
246 if (!undo_finished && kind != Undo::EDIT && kind != Undo::FINISH) {
247 // Check whether storing is needed.
248 if (!buf->undostack.empty() &&
249 buf->undostack.top().kind == kind &&
250 buf->undostack.top().number_of_before_par == before_id &&
251 buf->undostack.top().number_of_behind_par == behind_id) {
257 // Create a new Undo.
258 LyXCursor const & cur = bv->theLockingInset() ?
259 bv->theLockingInset()->cursor(bv) : bv->text->cursor;
261 stack.push(Undo(kind, inset_id,
262 before_id, behind_id, cur.par()->id(), cur.pos(), ParagraphList()));
264 ParagraphList & undo_pars = stack.top().pars;
266 for (ParagraphList::iterator it = *first; it != *last; ++it) {
267 undo_pars.push_back(*it);
268 undo_pars.back().id(it->id());
270 undo_pars.push_back(**last);
271 undo_pars.back().id(last->id());
273 // A memory optimization: Just store the layout
274 // information when only edit.
276 //if (kind == Undo::EDIT)
277 // for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
278 // undo_pars[i]->clearContents();
280 undo_finished = false;
284 // Returns false if no undo possible.
285 bool textUndoOrRedo(BufferView * bv,
286 limited_stack<Undo> & stack,
287 limited_stack<Undo> & /*otherstack*/)
294 Undo undo = stack.top();
300 Buffer * buf = bv->buffer();
301 ParIterator p = buf->getParFromID(undo->number_of_before_par);
302 ParIterator const end = buf->par_iterator_end();
304 ParagraphList::iterator first;
305 // default constructed?
309 first = first->next();
311 // Set first to the very first Paragraph depending of where
312 // we are so it will return the first paragraph of the buffer or the
313 // first paragraph of the textinset we're in.
314 first = bv->text->ownerParagraphs()->begin();
315 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
317 ParagraphList * result = inset->getParagraphs(0);
318 if (result && !result->empty())
319 first = result->begin();
323 ParIterator behind = buf->getParFromID(undo.number_of_behind_par);
324 createUndo(bv, undo.kind, first, behind.par(), otherstack);
329 // Now we can unlock the inset for saftey because the inset
330 // pointer could be changed during the undo-function. Anyway
331 // if needed we have to lock the right inset/position if this
334 bv->unlockInset(bv->theLockingInset());
335 bool const ret = textHandleUndo(bv, undo);
345 // Makes sure the next operation will be stored.
346 undo_finished = true;
352 // This is dangerous and for internal use only.
359 // This is dangerous and for internal use only.
364 bool textUndo(BufferView * bv)
366 return textUndoOrRedo(bv, bv->buffer()->undostack,
367 bv->buffer()->redostack);
371 bool textRedo(BufferView * bv)
373 return textUndoOrRedo(bv, bv->buffer()->redostack,
374 bv->buffer()->undostack);
378 void setUndo(BufferView * bv, Undo::undo_kind kind,
379 ParagraphList::iterator first)
381 setUndo(bv, kind, first, first);
385 void setUndo(BufferView * bv, Undo::undo_kind kind,
386 ParagraphList::iterator first, ParagraphList::iterator last)
389 createUndo(bv, kind, first->id(), last->id(), bv->buffer()->undostack);
390 bv->buffer()->redostack.clear();
395 void setCursorParUndo(BufferView * bv)
397 setUndo(bv, Undo::FINISH, bv->text->cursor.par());