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 = std::distance
83 (text->ownerParagraphs().begin(), text->cursor.par());
85 // Make and push the Undo entry
86 stack.push(Undo(kind, inset_id,
87 first_offset, last_offset,
88 cursor_offset, text->cursor.pos(),
91 // Record the relevant paragraphs
92 ParagraphList & undo_pars = stack.top().pars;
94 for (ParagraphList::iterator it = first; it != last; ++it) {
95 undo_pars.push_back(*it);
96 undo_pars.back().id(it->id());
98 undo_pars.push_back(*last);
99 undo_pars.back().id(last->id());
101 // And make sure that next time, we should be combining if possible
102 undo_finished = false;
106 // Returns false if no undo possible.
107 bool performUndoOrRedo(BufferView * bv, Undo & undo)
109 Buffer * buf = bv->buffer();
110 ParagraphList & plist = buf->paragraphs();
112 // Remove new stuff between first and last
114 ParagraphList::iterator first = plist.begin();
115 advance(first, undo.first_par_offset);
116 ParagraphList::iterator last = plist.begin();
117 advance(last, plist.size() - undo.last_par_offset);
118 plist.erase(first, ++last);
121 // Re-insert old stuff instead
124 plist.assign(undo.pars.begin(), undo.pars.end());
126 ParagraphList::iterator first = plist.begin();
127 advance(first, undo.first_par_offset);
128 plist.insert(first, undo.pars.begin(), undo.pars.end());
132 // Rebreak the entire document
133 bv->text->fullRebreak();
137 // Get a hold of the inset for the cursor, if relevant
138 UpdatableInset * inset =
139 static_cast<UpdatableInset *>(
140 buf->getInsetFromID(undo.inset_id));
142 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
143 ParagraphList::iterator cursor = text->ownerParagraphs().begin();
144 advance(cursor, undo.cursor_par_offset);
145 text->setCursorIntern(cursor, undo.cursor_pos);
148 // Magic needed to update inset internal state
149 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
150 inset->localDispatch(cmd);
153 // set cursor again to force the position to be the right one
154 text->setCursorIntern(cursor, undo.cursor_pos);
156 // Clear any selection and set the selection
157 // cursor for any new selection.
158 text->clearSelection();
159 text->selection.cursor = text->cursor;
160 text->updateCounters();
168 // Returns false if no undo possible.
169 bool textUndoOrRedo(BufferView * bv,
170 limited_stack<Undo> & stack,
171 limited_stack<Undo> & otherstack)
175 * Finish the undo operation in the case there was no entry
176 * on the stack to perform.
179 bv->unlockInset(bv->theLockingInset());
185 Undo undo = stack.top();
190 otherstack.push(undo);
191 otherstack.top().pars.clear();
192 Buffer * buf = bv->buffer();
193 ParagraphList & plist = buf->paragraphs();
194 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
195 ParagraphList::iterator first = plist.begin();
196 advance(first, undo.first_par_offset);
197 ParagraphList::iterator last = plist.begin();
198 advance(last, plist.size() - undo.last_par_offset + 1);
199 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
203 // Now we can unlock the inset for safety because the inset
204 // pointer could be changed during the undo-function. Anyway
205 // if needed we have to lock the right inset/position if this
208 bv->unlockInset(bv->theLockingInset());
209 bool const ret = performUndoOrRedo(bv, undo);
219 // This is dangerous and for internal use only.
226 // This is dangerous and for internal use only.
233 // Makes sure the next operation will be stored.
234 undo_finished = true;
238 bool textUndo(BufferView * bv)
240 return textUndoOrRedo(bv, bv->buffer()->undostack(),
241 bv->buffer()->redostack());
245 bool textRedo(BufferView * bv)
247 return textUndoOrRedo(bv, bv->buffer()->redostack(),
248 bv->buffer()->undostack());
252 void recordUndo(BufferView * bv, Undo::undo_kind kind,
253 ParagraphList::iterator first, ParagraphList::iterator last)
256 recordUndo(bv, kind, first, last, bv->buffer()->undostack());
257 bv->buffer()->redostack().clear();
262 void recordUndo(BufferView * bv, Undo::undo_kind kind,
263 ParagraphList::iterator first)
265 recordUndo(bv, kind, first, first);
269 void recordUndo(BufferView * bv, Undo::undo_kind kind)
271 recordUndo(bv, kind, bv->text->cursor.par());