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.push_back(*last);
154 // and make sure that next time, we should be combining if possible
155 undo_finished = false;
159 // returns false if no undo possible
160 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
162 lyxerr << "undo, performing: " << undo << std::endl;
163 ParIterator pit = num2pit(bv, undo.text);
164 LyXText * text = pit2text(bv, pit);
165 ParagraphList & plist = text->ownerParagraphs();
167 // remove new stuff between first and last
169 ParagraphList::iterator first = plist.begin();
170 advance(first, undo.first_par);
171 ParagraphList::iterator last = plist.begin();
172 advance(last, plist.size() - undo.end_par);
173 plist.erase(first, ++last);
176 // re-insert old stuff instead
178 plist.assign(undo.pars.begin(), undo.pars.end());
180 ParagraphList::iterator first = plist.begin();
181 advance(first, undo.first_par);
182 plist.insert(first, undo.pars.begin(), undo.pars.end());
186 lyxerr << "undo, text: " << undo.text
187 << " inset: " << pit.inset()
188 << " index: " << undo.index
191 // set cursor again to force the position to be the right one
192 text->cursor.par(undo.cursor_par);
193 text->cursor.pos(undo.cursor_pos);
195 // clear any selection
196 text->clearSelection();
197 text->selection.cursor = text->cursor;
198 text->updateCounters();
200 // rebreak the entire lyxtext
201 bv->text->fullRebreak();
203 InsetOld * inset = pit.inset();
205 // magic needed to cope with inset locking
206 bv->lockInset(dynamic_cast<UpdatableInset *>(inset));
214 // returns false if no undo possible
215 bool textUndoOrRedo(BufferView * bv,
216 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
221 bv->unlockInset(bv->theLockingInset());
227 Undo undo = stack.top();
232 otherstack.push(undo);
233 otherstack.top().pars.clear();
234 ParIterator pit = num2pit(bv, undo.text);
235 ParagraphList & plist = pit.plist();
236 if (undo.first_par + undo.end_par <= int(plist.size())) {
237 ParagraphList::iterator first = plist.begin();
238 advance(first, undo.first_par);
239 ParagraphList::iterator last = plist.begin();
240 advance(last, plist.size() - undo.end_par + 1);
241 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
243 LyXText * text = pit2text(bv, pit);
244 otherstack.top().cursor_pos = text->cursor.pos();
245 otherstack.top().cursor_par = text->cursor.par();
246 lyxerr << " undo other: " << otherstack.top() << std::endl;
249 // Now we can unlock the inset for safety because the inset
250 // pointer could be changed during the undo-function. Anyway
251 // if needed we have to lock the right inset/position if this
254 bv->unlockInset(bv->theLockingInset());
255 bool const ret = performUndoOrRedo(bv, undo);
265 // this is dangerous and for internal use only
272 // this is dangerous and for internal use only
279 // makes sure the next operation will be stored
280 undo_finished = true;
284 bool textUndo(BufferView * bv)
286 return textUndoOrRedo(bv, bv->buffer()->undostack(),
287 bv->buffer()->redostack());
291 bool textRedo(BufferView * bv)
293 return textUndoOrRedo(bv, bv->buffer()->redostack(),
294 bv->buffer()->undostack());
298 void recordUndo(Undo::undo_kind kind,
299 LyXText const * text, paroffset_type first, paroffset_type last)
303 Buffer * buf = text->bv()->buffer();
304 recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
305 buf->redostack().clear();
309 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
311 recordUndo(kind, text, par, par);
315 void recordUndo(BufferView * bv, Undo::undo_kind kind)
317 recordUndo(kind, bv->text, bv->text->cursor.par());