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"
19 #include "funcrequest.h"
20 #include "BufferView.h"
22 #include "insets/updatableinset.h"
23 #include "iterators.h"
24 #include "paragraph.h"
27 /// The flag used by FinishUndo().
30 /// Whether actions are not added to the undo stacks.
36 void recordUndo(BufferView * bv, Undo::undo_kind kind,
37 ParagraphList::iterator first, ParagraphList::iterator last,
38 limited_stack<Undo> & stack)
40 Buffer * buf = bv->buffer();
42 // First, record inset id, if cursor is in one
43 UpdatableInset * inset = first->inInset();
44 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
45 int const inset_id = inset ? inset->id() : -1;
47 // We simply record the entire outer paragraphs
48 ParagraphList * plist = &buf->paragraphs;
49 ParIterator null = buf->par_iterator_end();
51 // First, identify the outer paragraphs
52 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
53 if (it->id() == first->id())
54 first = it.outerPar();
55 if (it->id() == last->id())
59 // And calculate a stable reference to them
60 int const first_offset = std::distance(plist->begin(), first);
61 int const last_offset = std::distance(last, plist->end());
63 // Undo::ATOMIC are always recorded (no overlapping there).
65 // Overlapping only with insert and delete inside one paragraph:
66 // Nobody wants all removed character appear one by one when undoing.
67 if (! undo_finished && kind != Undo::ATOMIC) {
68 // Check whether storing is needed.
69 if (! buf->undostack.empty()
70 && buf->undostack.top().kind == kind
71 && buf->undostack.top().first_par_offset == first_offset
72 && buf->undostack.top().last_par_offset == last_offset) {
73 // No additonal undo recording needed -
74 // effectively, we combine undo recordings to one.
79 // Record the cursor position in a stable way.
80 int const cursor_offset = std::distance
81 (text->ownerParagraphs().begin(), text->cursor.par());
83 // Make and push the Undo entry
84 stack.push(Undo(kind, inset_id,
85 first_offset, last_offset,
86 cursor_offset, text->cursor.pos(),
89 // Record the relevant paragraphs
90 ParagraphList & undo_pars = stack.top().pars;
92 for (ParagraphList::iterator it = first; it != last; ++it) {
93 undo_pars.push_back(*it);
94 undo_pars.back().id(it->id());
96 undo_pars.push_back(*last);
97 undo_pars.back().id(last->id());
99 // And make sure that next time, we should be combining if possible
100 undo_finished = false;
104 // Returns false if no undo possible.
105 bool performUndoOrRedo(BufferView * bv, Undo & undo)
107 Buffer * buf = bv->buffer();
108 ParagraphList * plist = &buf->paragraphs;
110 // Remove new stuff between first and last
112 ParagraphList::iterator first = plist->begin();
113 advance(first, undo.first_par_offset);
114 ParagraphList::iterator last = plist->begin();
115 advance(last, plist->size() - undo.last_par_offset);
116 plist->erase(first, ++last);
119 // Re-insert old stuff instead
121 if (plist->empty()) {
122 plist->assign(undo.pars.begin(), undo.pars.end());
124 ParagraphList::iterator first = plist->begin();
125 advance(first, undo.first_par_offset);
126 plist->insert(first, undo.pars.begin(), undo.pars.end());
130 // Rebreak the entire document
131 bv->text->fullRebreak();
135 // Get a hold of the inset for the cursor, if relevant
136 UpdatableInset * inset =
137 static_cast<UpdatableInset *>(
138 buf->getInsetFromID(undo.inset_id));
140 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
141 ParagraphList::iterator cursor = text->ownerParagraphs().begin();
142 advance(cursor, undo.cursor_par_offset);
143 text->setCursorIntern(cursor, undo.cursor_pos);
146 // Magic needed to update inset internal state
147 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
148 inset->localDispatch(cmd);
151 // set cursor again to force the position to be the right one
152 text->setCursorIntern(cursor, undo.cursor_pos);
154 // Clear any selection and set the selection
155 // cursor for any new selection.
156 text->clearSelection();
157 text->selection.cursor = text->cursor;
158 text->updateCounters();
166 // Returns false if no undo possible.
167 bool textUndoOrRedo(BufferView * bv,
168 limited_stack<Undo> & stack,
169 limited_stack<Undo> & otherstack)
173 * Finish the undo operation in the case there was no entry
174 * on the stack to perform.
177 bv->unlockInset(bv->theLockingInset());
183 Undo undo = stack.top();
188 otherstack.push(undo);
189 otherstack.top().pars.clear();
190 Buffer * buf = bv->buffer();
191 ParagraphList & plist = buf->paragraphs;
192 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
193 ParagraphList::iterator first = plist.begin();
194 advance(first, undo.first_par_offset);
195 ParagraphList::iterator last = plist.begin();
196 advance(last, plist.size() - undo.last_par_offset + 1);
197 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
201 // Now we can unlock the inset for safety because the inset
202 // pointer could be changed during the undo-function. Anyway
203 // if needed we have to lock the right inset/position if this
206 bv->unlockInset(bv->theLockingInset());
207 bool const ret = performUndoOrRedo(bv, undo);
217 // This is dangerous and for internal use only.
224 // This is dangerous and for internal use only.
231 // Makes sure the next operation will be stored.
232 undo_finished = true;
236 bool textUndo(BufferView * bv)
238 return textUndoOrRedo(bv, bv->buffer()->undostack,
239 bv->buffer()->redostack);
243 bool textRedo(BufferView * bv)
245 return textUndoOrRedo(bv, bv->buffer()->redostack,
246 bv->buffer()->undostack);
250 void recordUndo(BufferView * bv, Undo::undo_kind kind,
251 ParagraphList::iterator first, ParagraphList::iterator last)
254 recordUndo(bv, kind, first, last, bv->buffer()->undostack);
255 bv->buffer()->redostack.clear();
260 void recordUndo(BufferView * bv, Undo::undo_kind kind,
261 ParagraphList::iterator first)
263 recordUndo(bv, kind, first, first);
267 void recordUndo(BufferView * bv, Undo::undo_kind kind)
269 recordUndo(bv, kind, bv->text->cursor.par());