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.
21 #include "BufferView.h"
22 #include "iterators.h"
24 #include "paragraph.h"
26 #include "insets/updatableinset.h" // for dynamic_cast<UpdatableInset *>
28 using lyx::paroffset_type;
31 /// The flag used by finishUndo().
34 /// Whether actions are not added to the undo stacks.
37 Undo::Undo(undo_kind kind_, int text_, int index_,
38 int first_par_, int end_par_, int cursor_par_, int cursor_pos_)
43 first_par(first_par_),
45 cursor_par(cursor_par_),
46 cursor_pos(cursor_pos_)
52 std::ostream & operator<<(std::ostream & os, Undo const & undo)
54 return os << " text: " << undo.text
55 << " index: " << undo.index
56 << " first: " << undo.first_par
57 << " from end: " << undo.end_par
58 << " cursor: " << undo.cursor_par
59 << "/" << undo.cursor_pos;
63 // translates LyXText pointer into offset count from document begin
64 ParIterator text2pit(LyXText * text, int & tcount)
67 Buffer * buf = text->bv()->buffer();
68 ParIterator pit = buf->par_iterator_begin();
69 ParIterator end = buf->par_iterator_end();
71 // it.text() returns 0 for outermost text.
72 if (text == text->bv()->text)
75 for ( ; pit != end; ++pit, ++tcount)
76 if (pit.text() == text)
78 lyxerr << "undo: should not happen" << std::endl;
83 // translates offset from buffer begin to ParIterator
84 ParIterator num2pit(BufferView * bv, int num)
86 Buffer * buf = bv->buffer();
87 ParIterator pit = buf->par_iterator_begin();
88 ParIterator end = buf->par_iterator_end();
90 for ( ; num && pit != end; ++pit, --num)
96 // don't crash early...
97 lyxerr << "undo: num2pit: num: " << num << std::endl;
99 return buf->par_iterator_begin();
103 // translates offset from buffer begin to LyXText
104 LyXText * pit2text(BufferView * bv, ParIterator const & pit)
106 LyXText * text = pit.text();
107 return text ? text : bv->text;
111 void recordUndo(Undo::undo_kind kind,
112 LyXText * text, paroffset_type first_par, paroffset_type last_par,
113 limited_stack<Undo> & stack)
115 Buffer * buf = text->bv()->buffer();
117 int const end_par = text->ownerParagraphs().size() - last_par;
119 // Undo::ATOMIC are always recorded (no overlapping there).
120 // overlapping only with insert and delete inside one paragraph:
121 // nobody wants all removed character appear one by one when undoing.
122 if (!undo_finished && kind != Undo::ATOMIC) {
123 // Check whether storing is needed.
124 if (!buf->undostack().empty()
125 && buf->undostack().top().kind == kind
126 && buf->undostack().top().first_par == first_par
127 && buf->undostack().top().end_par == end_par) {
128 // No additonal undo recording needed -
129 // effectively, we combine undo recordings to one.
134 // make and push the Undo entry
136 ParIterator pit = text2pit(text, textnum);
137 stack.push(Undo(kind, textnum, pit.index(),
138 first_par, end_par, text->cursor.par(), text->cursor.pos()));
139 lyxerr << "undo record: " << stack.top() << std::endl;
141 // record the relevant paragraphs
142 ParagraphList & undo_pars = stack.top().pars;
144 ParagraphList & plist = text->ownerParagraphs();
145 ParagraphList::iterator first = plist.begin();
146 advance(first, first_par);
147 ParagraphList::iterator last = plist.begin();
148 advance(last, last_par);
150 for (ParagraphList::iterator it = first; it != last; ++it) {
151 undo_pars.push_back(*it);
152 undo_pars.back().id(it->id());
154 undo_pars.push_back(*last);
155 undo_pars.back().id(last->id());
157 // and make sure that next time, we should be combining if possible
158 undo_finished = false;
162 // returns false if no undo possible
163 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
165 lyxerr << "undo, performing: " << undo << std::endl;
166 ParIterator pit = num2pit(bv, undo.text);
167 LyXText * text = pit2text(bv, pit);
168 ParagraphList & plist = text->ownerParagraphs();
170 // remove new stuff between first and last
172 ParagraphList::iterator first = plist.begin();
173 advance(first, undo.first_par);
174 ParagraphList::iterator last = plist.begin();
175 advance(last, plist.size() - undo.end_par);
176 plist.erase(first, ++last);
179 // re-insert old stuff instead
181 plist.assign(undo.pars.begin(), undo.pars.end());
183 ParagraphList::iterator first = plist.begin();
184 advance(first, undo.first_par);
185 plist.insert(first, undo.pars.begin(), undo.pars.end());
189 lyxerr << "undo, text: " << undo.text
190 << " inset: " << pit.inset()
191 << " index: " << undo.index
194 // set cursor again to force the position to be the right one
195 text->cursor.par(undo.cursor_par);
196 text->cursor.pos(undo.cursor_pos);
198 // clear any selection
199 text->clearSelection();
200 text->selection.cursor = text->cursor;
201 text->updateCounters();
203 // rebreak the entire lyxtext
204 bv->text->fullRebreak();
206 InsetOld * inset = pit.inset();
208 // magic needed to cope with inset locking
209 bv->lockInset(dynamic_cast<UpdatableInset *>(inset));
217 // returns false if no undo possible
218 bool textUndoOrRedo(BufferView * bv,
219 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
224 bv->unlockInset(bv->theLockingInset());
230 Undo undo = stack.top();
235 otherstack.push(undo);
236 otherstack.top().pars.clear();
237 ParIterator pit = num2pit(bv, undo.text);
238 ParagraphList & plist = pit.plist();
239 if (undo.first_par + undo.end_par <= int(plist.size())) {
240 ParagraphList::iterator first = plist.begin();
241 advance(first, undo.first_par);
242 ParagraphList::iterator last = plist.begin();
243 advance(last, plist.size() - undo.end_par + 1);
244 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
246 LyXText * text = pit2text(bv, pit);
247 otherstack.top().cursor_pos = text->cursor.pos();
248 otherstack.top().cursor_par = text->cursor.par();
249 lyxerr << " undo other: " << otherstack.top() << std::endl;
252 // Now we can unlock the inset for safety because the inset
253 // pointer could be changed during the undo-function. Anyway
254 // if needed we have to lock the right inset/position if this
257 bv->unlockInset(bv->theLockingInset());
258 bool const ret = performUndoOrRedo(bv, undo);
268 // this is dangerous and for internal use only
275 // this is dangerous and for internal use only
282 // makes sure the next operation will be stored
283 undo_finished = true;
287 bool textUndo(BufferView * bv)
289 return textUndoOrRedo(bv, bv->buffer()->undostack(),
290 bv->buffer()->redostack());
294 bool textRedo(BufferView * bv)
296 return textUndoOrRedo(bv, bv->buffer()->redostack(),
297 bv->buffer()->undostack());
301 void recordUndo(Undo::undo_kind kind,
302 LyXText const * text, paroffset_type first, paroffset_type last)
306 Buffer * buf = text->bv()->buffer();
307 recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
308 buf->redostack().clear();
312 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
314 recordUndo(kind, text, par, par);
318 void recordUndo(BufferView * bv, Undo::undo_kind kind)
320 recordUndo(kind, bv->text, bv->text->cursor.par());