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"
20 #include "BufferView.h"
21 #include "funcrequest.h"
22 #include "iterators.h"
24 #include "paragraph.h"
26 #include "insets/updatableinset.h"
29 /// The flag used by FinishUndo().
32 /// Whether actions are not added to the undo stacks.
38 void recordUndo(BufferView * bv, Undo::undo_kind kind,
39 ParagraphList::iterator first, ParagraphList::iterator last,
40 limited_stack<Undo> & stack)
42 Buffer * buf = bv->buffer();
44 // First, record inset id, if cursor is in one
45 UpdatableInset * inset = first->inInset();
46 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
47 int const inset_id = inset ? inset->id() : -1;
49 // We simply record the entire outer paragraphs
50 ParagraphList & plist = buf->paragraphs();
51 ParIterator null = buf->par_iterator_end();
53 // First, identify the outer paragraphs
54 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
55 if (it->id() == first->id())
56 first = it.outerPar();
57 if (it->id() == last->id())
61 // And calculate a stable reference to them
62 int const first_offset = std::distance(plist.begin(), first);
63 int const last_offset = std::distance(last, plist.end());
65 // Undo::ATOMIC are always recorded (no overlapping there).
67 // Overlapping only with insert and delete inside one paragraph:
68 // Nobody wants all removed character appear one by one when undoing.
69 if (! undo_finished && kind != Undo::ATOMIC) {
70 // Check whether storing is needed.
71 if (! buf->undostack().empty()
72 && buf->undostack().top().kind == kind
73 && buf->undostack().top().first_par_offset == first_offset
74 && buf->undostack().top().last_par_offset == last_offset) {
75 // No additonal undo recording needed -
76 // effectively, we combine undo recordings to one.
81 // Record the cursor position in a stable way.
82 int const cursor_offset = text->cursor.par();
84 // Make and push the Undo entry
85 stack.push(Undo(kind, inset_id,
86 first_offset, last_offset,
87 cursor_offset, text->cursor.pos(),
90 // Record the relevant paragraphs
91 ParagraphList & undo_pars = stack.top().pars;
93 for (ParagraphList::iterator it = first; it != last; ++it) {
94 undo_pars.push_back(*it);
95 undo_pars.back().id(it->id());
97 undo_pars.push_back(*last);
98 undo_pars.back().id(last->id());
100 // And make sure that next time, we should be combining if possible
101 undo_finished = false;
105 // Returns false if no undo possible.
106 bool performUndoOrRedo(BufferView * bv, Undo & undo)
108 Buffer * buf = bv->buffer();
109 ParagraphList & plist = buf->paragraphs();
111 // Remove new stuff between first and last
113 ParagraphList::iterator first = plist.begin();
114 advance(first, undo.first_par_offset);
115 ParagraphList::iterator last = plist.begin();
116 advance(last, plist.size() - undo.last_par_offset);
117 plist.erase(first, ++last);
120 // Re-insert old stuff instead
123 plist.assign(undo.pars.begin(), undo.pars.end());
125 ParagraphList::iterator first = plist.begin();
126 advance(first, undo.first_par_offset);
127 plist.insert(first, undo.pars.begin(), undo.pars.end());
131 // Rebreak the entire document
132 bv->text->fullRebreak();
136 // Get a hold of the inset for the cursor, if relevant
137 UpdatableInset * inset =
138 static_cast<UpdatableInset *>(
139 buf->getInsetFromID(undo.inset_id));
141 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
142 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
145 // Magic needed to update inset internal state
146 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
147 inset->localDispatch(cmd);
150 // set cursor again to force the position to be the right one
151 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
153 // Clear any selection and set the selection
154 // cursor for any new selection.
155 text->clearSelection();
156 text->selection.cursor = text->cursor;
157 text->updateCounters();
165 // Returns false if no undo possible.
166 bool textUndoOrRedo(BufferView * bv,
167 limited_stack<Undo> & stack,
168 limited_stack<Undo> & otherstack)
172 * Finish the undo operation in the case there was no entry
173 * on the stack to perform.
176 bv->unlockInset(bv->theLockingInset());
182 Undo undo = stack.top();
187 otherstack.push(undo);
188 otherstack.top().pars.clear();
189 Buffer * buf = bv->buffer();
190 ParagraphList & plist = buf->paragraphs();
191 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
192 ParagraphList::iterator first = plist.begin();
193 advance(first, undo.first_par_offset);
194 ParagraphList::iterator last = plist.begin();
195 advance(last, plist.size() - undo.last_par_offset + 1);
196 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
200 // Now we can unlock the inset for safety because the inset
201 // pointer could be changed during the undo-function. Anyway
202 // if needed we have to lock the right inset/position if this
205 bv->unlockInset(bv->theLockingInset());
206 bool const ret = performUndoOrRedo(bv, undo);
216 // This is dangerous and for internal use only.
223 // This is dangerous and for internal use only.
230 // Makes sure the next operation will be stored.
231 undo_finished = true;
235 bool textUndo(BufferView * bv)
237 return textUndoOrRedo(bv, bv->buffer()->undostack(),
238 bv->buffer()->redostack());
242 bool textRedo(BufferView * bv)
244 return textUndoOrRedo(bv, bv->buffer()->redostack(),
245 bv->buffer()->undostack());
249 void recordUndo(BufferView * bv, Undo::undo_kind kind,
250 ParagraphList::iterator first, ParagraphList::iterator last)
253 recordUndo(bv, kind, first, last, bv->buffer()->undostack());
254 bv->buffer()->redostack().clear();
259 void recordUndo(BufferView * bv, Undo::undo_kind kind,
260 ParagraphList::iterator first)
262 recordUndo(bv, kind, first, first);
266 void recordUndo(BufferView * bv, Undo::undo_kind kind)
268 recordUndo(bv, kind, bv->text->cursorPar());