1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
12 #include "undo_funcs.h"
14 #include "funcrequest.h"
15 #include "BufferView.h"
17 #include "insets/updatableinset.h"
18 #include "insets/insettext.h"
20 #include "support/LAssert.h"
21 #include "iterators.h"
24 /// The flag used by FinishUndo().
27 /// Whether actions are not added to the undo stacks.
33 void recordUndo(BufferView * bv, Undo::undo_kind kind,
34 ParagraphList::iterator first, ParagraphList::iterator last,
35 limited_stack<Undo> & stack)
37 Buffer * buf = bv->buffer();
39 // First, record inset id, if cursor is in one
40 UpdatableInset * inset = first->inInset();
41 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
42 int const inset_id = inset ? inset->id() : -1;
44 // We simply record the entire outer paragraphs
45 ParagraphList * plist = &buf->paragraphs;
46 ParIterator null = buf->par_iterator_end();
48 // First, identify the outer paragraphs
49 for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
50 if (it->id() == first->id())
51 first = it.outerPar();
52 if (it->id() == last->id())
56 // And calculate a stable reference to them
57 int const first_offset = std::distance(plist->begin(), first);
58 int const last_offset = std::distance(last, plist->end());
60 // Undo::ATOMIC are always recorded (no overlapping there).
62 // Overlapping only with insert and delete inside one paragraph:
63 // Nobody wants all removed character appear one by one when undoing.
64 if (! undo_finished && kind != Undo::ATOMIC) {
65 // Check whether storing is needed.
66 if (! buf->undostack.empty()
67 && buf->undostack.top().kind == kind
68 && buf->undostack.top().first_par_offset == first_offset
69 && buf->undostack.top().last_par_offset == last_offset) {
70 // No additonal undo recording needed -
71 // effectively, we combine undo recordings to one.
76 // Record the cursor position in a stable way.
77 int const cursor_offset = std::distance
78 (text->ownerParagraphs().begin(), text->cursor.par());
80 // Make and push the Undo entry
81 stack.push(Undo(kind, inset_id,
82 first_offset, last_offset,
83 cursor_offset, text->cursor.pos(),
86 // Record the relevant paragraphs
87 ParagraphList & undo_pars = stack.top().pars;
89 for (ParagraphList::iterator it = first; it != last; ++it) {
90 undo_pars.push_back(*it);
91 undo_pars.back().id(it->id());
93 undo_pars.push_back(*last);
94 undo_pars.back().id(last->id());
96 // And make sure that next time, we should be combining if possible
97 undo_finished = false;
101 // Returns false if no undo possible.
102 bool performUndoOrRedo(BufferView * bv, Undo & undo)
104 Buffer * buf = bv->buffer();
105 ParagraphList * plist = &buf->paragraphs;
107 // Remove new stuff between first and last
109 ParagraphList::iterator first = plist->begin();
110 advance(first, undo.first_par_offset);
111 ParagraphList::iterator last = plist->begin();
112 advance(last, plist->size() - undo.last_par_offset);
113 plist->erase(first, ++last);
116 // Re-insert old stuff instead
118 if (plist->empty()) {
119 plist->assign(undo.pars.begin(), undo.pars.end());
121 ParagraphList::iterator first = plist->begin();
122 advance(first, undo.first_par_offset);
123 plist->insert(first, undo.pars.begin(), undo.pars.end());
127 // Rebreak the entire document
128 bv->text->fullRebreak();
132 // Get a hold of the inset for the cursor, if relevant
133 UpdatableInset * inset =
134 static_cast<UpdatableInset *>(
135 buf->getInsetFromID(undo.inset_id));
137 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
138 ParagraphList::iterator cursor = text->ownerParagraphs().begin();
139 advance(cursor, undo.cursor_par_offset);
140 text->setCursorIntern(cursor, undo.cursor_pos);
143 // Magic needed to update inset internal state
144 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
145 inset->localDispatch(cmd);
148 // set cursor again to force the position to be the right one
149 text->setCursorIntern(cursor, undo.cursor_pos);
151 // Clear any selection and set the selection
152 // cursor for any new selection.
153 text->clearSelection();
154 text->selection.cursor = text->cursor;
155 text->updateCounters();
160 // And repaint the lot
161 bv->text->postPaint();
167 // Returns false if no undo possible.
168 bool textUndoOrRedo(BufferView * bv,
169 limited_stack<Undo> & stack,
170 limited_stack<Undo> & otherstack)
174 * Finish the undo operation in the case there was no entry
175 * on the stack to perform.
178 bv->unlockInset(bv->theLockingInset());
180 bv->text->postPaint();
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());