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 using lyx::paroffset_type;
29 /// The flag used by finishUndo().
32 /// Whether actions are not added to the undo stacks.
35 Undo::Undo(undo_kind kind_, int text_, int index_,
36 int first_par_, int end_par_, int cursor_par_, int cursor_pos_)
41 first_par(first_par_),
43 cursor_par(cursor_par_),
44 cursor_pos(cursor_pos_)
50 std::ostream & operator<<(std::ostream & os, Undo const & undo)
52 return os << " text: " << undo.text
53 << " index: " << undo.index
54 << " first: " << undo.first_par
55 << " from end: " << undo.end_par
56 << " cursor: " << undo.cursor_par
57 << "/" << undo.cursor_pos;
61 // translates LyXText pointer into offset count from document begin
62 ParIterator text2pit(LyXText * text, int & tcount)
65 Buffer * buf = text->bv()->buffer();
66 ParIterator pit = buf->par_iterator_begin();
67 ParIterator end = buf->par_iterator_end();
69 // it.text() returns 0 for outermost text.
70 if (text == text->bv()->text)
73 for ( ; pit != end; ++pit, ++tcount)
74 if (pit.text() == text)
76 lyxerr << "undo: should not happen" << std::endl;
81 // translates offset from buffer begin to ParIterator
82 ParIterator num2pit(BufferView * bv, int num)
84 Buffer * buf = bv->buffer();
85 ParIterator pit = buf->par_iterator_begin();
86 ParIterator end = buf->par_iterator_end();
88 for ( ; num && pit != end; ++pit, --num)
94 // don't crash early...
95 lyxerr << "undo: num2pit: num: " << num << std::endl;
97 return buf->par_iterator_begin();
101 // translates offset from buffer begin to LyXText
102 LyXText * pit2text(BufferView * bv, ParIterator const & pit)
104 LyXText * text = pit.text();
105 return text ? text : bv->text;
109 void recordUndo(Undo::undo_kind kind,
110 LyXText * text, paroffset_type first_par, paroffset_type last_par,
111 limited_stack<Undo> & stack)
113 Buffer * buf = text->bv()->buffer();
115 int const end_par = text->ownerParagraphs().size() - last_par;
117 // Undo::ATOMIC are always recorded (no overlapping there).
118 // overlapping only with insert and delete inside one paragraph:
119 // nobody wants all removed character appear one by one when undoing.
120 if (!undo_finished && kind != Undo::ATOMIC) {
121 // Check whether storing is needed.
122 if (!buf->undostack().empty()
123 && buf->undostack().top().kind == kind
124 && buf->undostack().top().first_par == first_par
125 && buf->undostack().top().end_par == end_par) {
126 // No additonal undo recording needed -
127 // effectively, we combine undo recordings to one.
132 // make and push the Undo entry
134 ParIterator pit = text2pit(text, textnum);
135 stack.push(Undo(kind, textnum, pit.index(),
136 first_par, end_par, text->cursor.par(), text->cursor.pos()));
137 lyxerr << "undo record: " << stack.top() << std::endl;
139 // record the relevant paragraphs
140 ParagraphList & undo_pars = stack.top().pars;
142 ParagraphList & plist = text->ownerParagraphs();
143 ParagraphList::iterator first = plist.begin();
144 advance(first, first_par);
145 ParagraphList::iterator last = plist.begin();
146 advance(last, last_par);
148 for (ParagraphList::iterator it = first; it != last; ++it) {
149 undo_pars.push_back(*it);
150 undo_pars.back().id(it->id());
152 undo_pars.push_back(*last);
153 undo_pars.back().id(last->id());
155 // and make sure that next time, we should be combining if possible
156 undo_finished = false;
160 // returns false if no undo possible
161 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
163 lyxerr << "undo, performing: " << undo << std::endl;
164 ParIterator pit = num2pit(bv, undo.text);
165 LyXText * text = pit2text(bv, pit);
166 ParagraphList & plist = text->ownerParagraphs();
168 // remove new stuff between first and last
170 ParagraphList::iterator first = plist.begin();
171 advance(first, undo.first_par);
172 ParagraphList::iterator last = plist.begin();
173 advance(last, plist.size() - undo.end_par);
174 plist.erase(first, ++last);
177 // re-insert old stuff instead
179 plist.assign(undo.pars.begin(), undo.pars.end());
181 ParagraphList::iterator first = plist.begin();
182 advance(first, undo.first_par);
183 plist.insert(first, undo.pars.begin(), undo.pars.end());
187 lyxerr << "undo, text: " << undo.text
188 << " inset: " << pit.inset()
189 << " index: " << undo.index
191 InsetOld * inset = pit.inset();
193 // magic needed to cope with inset locking
194 inset->edit(bv, undo.index);
197 // set cursor again to force the position to be the right one
198 text->setCursorIntern(undo.cursor_par, undo.cursor_pos);
200 // clear any selection
201 text->clearSelection();
202 text->selection.cursor = text->cursor;
203 text->updateCounters();
205 // rebreak the entire document
206 bv->text->fullRebreak();
213 // returns false if no undo possible
214 bool textUndoOrRedo(BufferView * bv,
215 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
220 bv->unlockInset(bv->theLockingInset());
226 Undo undo = stack.top();
231 otherstack.push(undo);
232 otherstack.top().pars.clear();
233 ParIterator pit = num2pit(bv, undo.text);
234 ParagraphList & plist = pit.plist();
235 if (undo.first_par + undo.end_par <= int(plist.size())) {
236 ParagraphList::iterator first = plist.begin();
237 advance(first, undo.first_par);
238 ParagraphList::iterator last = plist.begin();
239 advance(last, plist.size() - undo.end_par + 1);
240 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
242 LyXText * text = pit2text(bv, pit);
243 otherstack.top().cursor_pos = text->cursor.pos();
244 otherstack.top().cursor_par = text->cursor.par();
245 lyxerr << " undo other: " << otherstack.top() << std::endl;
248 // Now we can unlock the inset for safety because the inset
249 // pointer could be changed during the undo-function. Anyway
250 // if needed we have to lock the right inset/position if this
253 bv->unlockInset(bv->theLockingInset());
254 bool const ret = performUndoOrRedo(bv, undo);
264 // this is dangerous and for internal use only
271 // this is dangerous and for internal use only
278 // makes sure the next operation will be stored
279 undo_finished = true;
283 bool textUndo(BufferView * bv)
285 return textUndoOrRedo(bv, bv->buffer()->undostack(),
286 bv->buffer()->redostack());
290 bool textRedo(BufferView * bv)
292 return textUndoOrRedo(bv, bv->buffer()->redostack(),
293 bv->buffer()->undostack());
297 void recordUndo(Undo::undo_kind kind,
298 LyXText const * text, paroffset_type first, paroffset_type last)
302 Buffer * buf = text->bv()->buffer();
303 recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
304 buf->redostack().clear();
308 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
310 recordUndo(kind, text, par, par);
314 void recordUndo(BufferView * bv, Undo::undo_kind kind)
316 recordUndo(kind, bv->text, bv->text->cursor.par());