3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
17 #include "undo_funcs.h"
21 #include "BufferView.h"
22 #include "funcrequest.h"
23 #include "iterators.h"
25 #include "paragraph.h"
27 #include "insets/updatableinset.h"
32 using lyx::paroffset_type;
35 /// The flag used by finishUndo().
38 /// Whether actions are not added to the undo stacks.
44 void recordUndo(BufferView * bv, Undo::undo_kind kind,
45 ParagraphList & plist, paroffset_type firstpar, paroffset_type lastpar,
46 limited_stack<Undo> & stack)
48 Buffer * buf = bv->buffer();
50 ParagraphList::iterator first = plist.begin();
51 advance(first, firstpar);
52 ParagraphList::iterator last = plist.begin();
53 advance(last, lastpar);
55 // First, record inset id, if cursor is in one
56 UpdatableInset * inset = first->inInset();
57 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
58 int const inset_id = inset ? inset->id() : -1;
60 // Try to find the appropriate list by counting the,
61 ParIterator null = buf->par_iterator_end();
64 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it, ++pcount)
65 if (&it.plist() == &plist)
67 //lyxerr << "This is plist #" << pcount << ' ' << &plist << endl;
69 // We simply record the entire outer paragraphs
70 // First, identify the outer paragraphs
71 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
72 if (it->id() == first->id())
73 first = it.outerPar();
74 if (it->id() == last->id())
78 // And calculate a stable reference to them
79 int const first_offset = firstpar;
80 int const last_offset = plist.size() - lastpar;
82 // Undo::ATOMIC are always recorded (no overlapping there).
84 // Overlapping only with insert and delete inside one paragraph:
85 // Nobody wants all removed character appear one by one when undoing.
86 if (! undo_finished && kind != Undo::ATOMIC) {
87 // Check whether storing is needed.
88 if (! buf->undostack().empty()
89 && buf->undostack().top().kind == kind
90 && buf->undostack().top().first_par_offset == first_offset
91 && buf->undostack().top().last_par_offset == last_offset) {
92 // No additonal undo recording needed -
93 // effectively, we combine undo recordings to one.
98 // Record the cursor position in a stable way.
99 int const cursor_offset = text->cursor.par();
101 // Make and push the Undo entry
102 stack.push(Undo(kind, inset_id,
104 first_offset, last_offset,
105 cursor_offset, text->cursor.pos(),
108 // Record the relevant paragraphs
109 ParagraphList & undo_pars = stack.top().pars;
111 for (ParagraphList::iterator it = first; it != last; ++it) {
112 undo_pars.push_back(*it);
113 undo_pars.back().id(it->id());
115 undo_pars.push_back(*last);
116 undo_pars.back().id(last->id());
118 // And make sure that next time, we should be combining if possible
119 undo_finished = false;
123 // Returns false if no undo possible.
124 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
126 Buffer * buf = bv->buffer();
127 ParagraphList & plist = buf->paragraphs();
129 ParIterator null = buf->par_iterator_end();
130 ParIterator plit = buf->par_iterator_begin();
132 for ( ; n && plit != null; ++plit, --n)
134 //lyxerr << "undo: using plist #" << undo.plist << " " << &plit.plist() << endl;
136 // Remove new stuff between first and last
138 ParagraphList::iterator first = plist.begin();
139 advance(first, undo.first_par_offset);
140 ParagraphList::iterator last = plist.begin();
141 advance(last, plist.size() - undo.last_par_offset);
142 plist.erase(first, ++last);
145 // Re-insert old stuff instead
148 plist.assign(undo.pars.begin(), undo.pars.end());
150 ParagraphList::iterator first = plist.begin();
151 advance(first, undo.first_par_offset);
152 plist.insert(first, undo.pars.begin(), undo.pars.end());
156 // Rebreak the entire document
157 bv->text->fullRebreak();
161 // Get a hold of the inset for the cursor, if relevant
162 UpdatableInset * inset =
163 static_cast<UpdatableInset *>(
164 buf->getInsetFromID(undo.inset_id));
166 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
167 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
169 //lyxerr << "undo, inset: " << inset << endl;
172 //lyxerr << "undo, inset owner: " << inset->owner() << endl;
174 // Magic needed to update inset internal state
175 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
177 inset->owner()->localDispatch(cmd);
179 inset->localDispatch(cmd);
182 // set cursor again to force the position to be the right one
183 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
185 // Clear any selection and set the selection
186 // cursor for any new selection.
187 text->clearSelection();
188 text->selection.cursor = text->cursor;
189 text->updateCounters();
197 // Returns false if no undo possible.
198 bool textUndoOrRedo(BufferView * bv,
199 limited_stack<Undo> & stack,
200 limited_stack<Undo> & otherstack)
204 * Finish the undo operation in the case there was no entry
205 * on the stack to perform.
208 bv->unlockInset(bv->theLockingInset());
214 Undo undo = stack.top();
219 otherstack.push(undo);
220 otherstack.top().pars.clear();
221 Buffer * buf = bv->buffer();
222 ParagraphList & plist = buf->paragraphs();
223 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
224 ParagraphList::iterator first = plist.begin();
225 advance(first, undo.first_par_offset);
226 ParagraphList::iterator last = plist.begin();
227 advance(last, plist.size() - undo.last_par_offset + 1);
228 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
232 // Now we can unlock the inset for safety because the inset
233 // pointer could be changed during the undo-function. Anyway
234 // if needed we have to lock the right inset/position if this
237 bv->unlockInset(bv->theLockingInset());
238 bool const ret = performUndoOrRedo(bv, undo);
248 // This is dangerous and for internal use only.
255 // This is dangerous and for internal use only.
262 // Makes sure the next operation will be stored.
263 undo_finished = true;
267 bool textUndo(BufferView * bv)
269 return textUndoOrRedo(bv, bv->buffer()->undostack(),
270 bv->buffer()->redostack());
274 bool textRedo(BufferView * bv)
276 return textUndoOrRedo(bv, bv->buffer()->redostack(),
277 bv->buffer()->undostack());
281 void recordUndo(BufferView * bv, Undo::undo_kind kind,
282 ParagraphList & plist, paroffset_type first, paroffset_type last)
285 recordUndo(bv, kind, plist, first, last, bv->buffer()->undostack());
286 bv->buffer()->redostack().clear();
291 void recordUndo(BufferView * bv, Undo::undo_kind kind,
292 ParagraphList & plist, paroffset_type par)
294 recordUndo(bv, kind, plist, par, par);
298 void recordUndo(BufferView * bv, Undo::undo_kind kind)
300 recordUndo(bv, kind, bv->text->ownerParagraphs(), bv->text->cursor.par());