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 // We simply record the entire outer paragraphs
61 ParIterator null = buf->par_iterator_end();
63 // First, identify the outer paragraphs
64 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
65 if (it->id() == first->id())
66 first = it.outerPar();
67 if (it->id() == last->id())
71 // And calculate a stable reference to them
72 int const first_offset = firstpar;
73 int const last_offset = plist.size() - lastpar;
75 // Undo::ATOMIC are always recorded (no overlapping there).
77 // Overlapping only with insert and delete inside one paragraph:
78 // Nobody wants all removed character appear one by one when undoing.
79 if (! undo_finished && kind != Undo::ATOMIC) {
80 // Check whether storing is needed.
81 if (! buf->undostack().empty()
82 && buf->undostack().top().kind == kind
83 && buf->undostack().top().first_par_offset == first_offset
84 && buf->undostack().top().last_par_offset == last_offset) {
85 // No additonal undo recording needed -
86 // effectively, we combine undo recordings to one.
91 // Record the cursor position in a stable way.
92 int const cursor_offset = text->cursor.par();
94 // Make and push the Undo entry
95 stack.push(Undo(kind, inset_id,
96 first_offset, last_offset,
97 cursor_offset, text->cursor.pos(),
100 lyxerr << "G" << endl;
101 // Record the relevant paragraphs
102 ParagraphList & undo_pars = stack.top().pars;
104 for (ParagraphList::iterator it = first; it != last; ++it) {
105 undo_pars.push_back(*it);
106 undo_pars.back().id(it->id());
108 undo_pars.push_back(*last);
109 undo_pars.back().id(last->id());
111 // And make sure that next time, we should be combining if possible
112 undo_finished = false;
116 // Returns false if no undo possible.
117 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
119 Buffer * buf = bv->buffer();
120 ParagraphList & plist = buf->paragraphs();
122 // Remove new stuff between first and last
124 ParagraphList::iterator first = plist.begin();
125 advance(first, undo.first_par_offset);
126 ParagraphList::iterator last = plist.begin();
127 advance(last, plist.size() - undo.last_par_offset);
128 plist.erase(first, ++last);
131 // Re-insert old stuff instead
134 plist.assign(undo.pars.begin(), undo.pars.end());
136 ParagraphList::iterator first = plist.begin();
137 advance(first, undo.first_par_offset);
138 plist.insert(first, undo.pars.begin(), undo.pars.end());
142 // Rebreak the entire document
143 bv->text->fullRebreak();
147 // Get a hold of the inset for the cursor, if relevant
148 UpdatableInset * inset =
149 static_cast<UpdatableInset *>(
150 buf->getInsetFromID(undo.inset_id));
152 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
153 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
155 lyxerr << "undo, inset: " << inset << endl;
158 lyxerr << "undo, inset owner: " << inset->owner() << endl;
160 // Magic needed to update inset internal state
161 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
163 inset->owner()->localDispatch(cmd);
165 inset->localDispatch(cmd);
168 // set cursor again to force the position to be the right one
169 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
171 // Clear any selection and set the selection
172 // cursor for any new selection.
173 text->clearSelection();
174 text->selection.cursor = text->cursor;
175 text->updateCounters();
183 // Returns false if no undo possible.
184 bool textUndoOrRedo(BufferView * bv,
185 limited_stack<Undo> & stack,
186 limited_stack<Undo> & otherstack)
190 * Finish the undo operation in the case there was no entry
191 * on the stack to perform.
194 bv->unlockInset(bv->theLockingInset());
200 Undo undo = stack.top();
205 otherstack.push(undo);
206 otherstack.top().pars.clear();
207 Buffer * buf = bv->buffer();
208 ParagraphList & plist = buf->paragraphs();
209 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
210 ParagraphList::iterator first = plist.begin();
211 advance(first, undo.first_par_offset);
212 ParagraphList::iterator last = plist.begin();
213 advance(last, plist.size() - undo.last_par_offset + 1);
214 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
218 // Now we can unlock the inset for safety because the inset
219 // pointer could be changed during the undo-function. Anyway
220 // if needed we have to lock the right inset/position if this
223 bv->unlockInset(bv->theLockingInset());
224 bool const ret = performUndoOrRedo(bv, undo);
234 // This is dangerous and for internal use only.
241 // This is dangerous and for internal use only.
248 // Makes sure the next operation will be stored.
249 undo_finished = true;
253 bool textUndo(BufferView * bv)
255 return textUndoOrRedo(bv, bv->buffer()->undostack(),
256 bv->buffer()->redostack());
260 bool textRedo(BufferView * bv)
262 return textUndoOrRedo(bv, bv->buffer()->redostack(),
263 bv->buffer()->undostack());
267 void recordUndo(BufferView * bv, Undo::undo_kind kind,
268 ParagraphList & plist, paroffset_type first, paroffset_type last)
271 recordUndo(bv, kind, plist, first, last, bv->buffer()->undostack());
272 bv->buffer()->redostack().clear();
277 void recordUndo(BufferView * bv, Undo::undo_kind kind,
278 ParagraphList & plist, paroffset_type par)
280 recordUndo(bv, kind, plist, par, par);
284 void recordUndo(BufferView * bv, Undo::undo_kind kind)
286 recordUndo(bv, kind, bv->text->ownerParagraphs(), bv->text->cursor.par());