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)
68 // And calculate a stable reference to them
69 int const first_offset = firstpar;
70 int const last_offset = plist.size() - lastpar;
72 // Undo::ATOMIC are always recorded (no overlapping there).
73 // Overlapping only with insert and delete inside one paragraph:
74 // Nobody wants all removed character appear one by one when undoing.
75 if (! undo_finished && kind != Undo::ATOMIC) {
76 // Check whether storing is needed.
77 if (! buf->undostack().empty()
78 && buf->undostack().top().kind == kind
79 && buf->undostack().top().first_par_offset == first_offset
80 && buf->undostack().top().last_par_offset == last_offset) {
81 // No additonal undo recording needed -
82 // effectively, we combine undo recordings to one.
87 // Record the cursor position in a stable way.
88 int const cursor_offset = text->cursor.par();
90 // Make and push the Undo entry
91 stack.push(Undo(kind, inset_id,
93 first_offset, last_offset,
94 cursor_offset, text->cursor.pos(),
97 // Record the relevant paragraphs
98 ParagraphList & undo_pars = stack.top().pars;
100 for (ParagraphList::iterator it = first; it != last; ++it) {
101 undo_pars.push_back(*it);
102 undo_pars.back().id(it->id());
104 undo_pars.push_back(*last);
105 undo_pars.back().id(last->id());
107 // And make sure that next time, we should be combining if possible
108 undo_finished = false;
112 // Returns false if no undo possible.
113 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
115 Buffer * buf = bv->buffer();
117 ParIterator null = buf->par_iterator_end();
118 ParIterator plit = buf->par_iterator_begin();
120 for ( ; n && plit != null; ++plit, --n)
122 BOOST_ASSERT(plit != null);
123 ParagraphList & plist = plit.plist();
125 // Remove new stuff between first and last
127 ParagraphList::iterator first = plist.begin();
128 advance(first, undo.first_par_offset);
129 ParagraphList::iterator last = plist.begin();
130 advance(last, plist.size() - undo.last_par_offset);
131 plist.erase(first, ++last);
134 // Re-insert old stuff instead
137 plist.assign(undo.pars.begin(), undo.pars.end());
139 ParagraphList::iterator first = plist.begin();
140 advance(first, undo.first_par_offset);
141 plist.insert(first, undo.pars.begin(), undo.pars.end());
145 // Rebreak the entire document
146 bv->text->fullRebreak();
150 // Get a hold of the inset for the cursor, if relevant
151 UpdatableInset * inset =
152 static_cast<UpdatableInset *>(
153 buf->getInsetFromID(undo.inset_id));
155 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
156 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
158 //lyxerr << "undo, inset: " << inset << endl;
161 //lyxerr << "undo, inset owner: " << inset->owner() << endl;
163 // Magic needed to update inset internal state
164 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
166 inset->owner()->localDispatch(cmd);
168 inset->localDispatch(cmd);
171 // set cursor again to force the position to be the right one
172 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
174 // Clear any selection and set the selection
175 // cursor for any new selection.
176 text->clearSelection();
177 text->selection.cursor = text->cursor;
178 text->updateCounters();
186 // Returns false if no undo possible.
187 bool textUndoOrRedo(BufferView * bv,
188 limited_stack<Undo> & stack,
189 limited_stack<Undo> & otherstack)
193 * Finish the undo operation in the case there was no entry
194 * on the stack to perform.
197 bv->unlockInset(bv->theLockingInset());
203 Undo undo = stack.top();
208 otherstack.push(undo);
209 otherstack.top().pars.clear();
210 Buffer * buf = bv->buffer();
211 ParagraphList & plist = buf->paragraphs();
212 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
213 ParagraphList::iterator first = plist.begin();
214 advance(first, undo.first_par_offset);
215 ParagraphList::iterator last = plist.begin();
216 advance(last, plist.size() - undo.last_par_offset + 1);
217 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
221 // Now we can unlock the inset for safety because the inset
222 // pointer could be changed during the undo-function. Anyway
223 // if needed we have to lock the right inset/position if this
226 bv->unlockInset(bv->theLockingInset());
227 bool const ret = performUndoOrRedo(bv, undo);
237 // This is dangerous and for internal use only.
244 // This is dangerous and for internal use only.
251 // Makes sure the next operation will be stored.
252 undo_finished = true;
256 bool textUndo(BufferView * bv)
258 return textUndoOrRedo(bv, bv->buffer()->undostack(),
259 bv->buffer()->redostack());
263 bool textRedo(BufferView * bv)
265 return textUndoOrRedo(bv, bv->buffer()->redostack(),
266 bv->buffer()->undostack());
270 void recordUndo(BufferView * bv, Undo::undo_kind kind,
271 ParagraphList & plist, paroffset_type first, paroffset_type last)
274 recordUndo(bv, kind, plist, first, last, bv->buffer()->undostack());
275 bv->buffer()->redostack().clear();
280 void recordUndo(BufferView * bv, Undo::undo_kind kind,
281 ParagraphList & plist, paroffset_type par)
283 recordUndo(bv, kind, plist, par, par);
287 void recordUndo(BufferView * bv, Undo::undo_kind kind)
289 recordUndo(bv, kind, bv->text->ownerParagraphs(), bv->text->cursor.par());