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(Buffer & buf, LyXText * text, int & tcount)
67 ParIterator pit = buf.par_iterator_begin();
68 ParIterator end = buf.par_iterator_end();
70 for ( ; pit != end; ++pit, ++tcount)
71 if (pit.text(buf) == text)
73 lyxerr << "undo: should not happen" << std::endl;
78 // translates offset from buffer begin to ParIterator
79 ParIterator num2pit(Buffer & buf, int num)
81 ParIterator pit = buf.par_iterator_begin();
82 ParIterator end = buf.par_iterator_end();
84 for ( ; num && pit != end; ++pit, --num)
90 // don't crash early...
91 lyxerr << "undo: num2pit: num: " << num << std::endl;
93 return buf.par_iterator_begin();
97 void recordUndo(Undo::undo_kind kind,
98 LyXText * text, paroffset_type first_par, paroffset_type last_par,
99 limited_stack<Undo> & stack)
101 Buffer & buf = *text->bv()->buffer();
103 int const end_par = text->paragraphs().size() - last_par;
105 // Undo::ATOMIC are always recorded (no overlapping there).
106 // overlapping only with insert and delete inside one paragraph:
107 // nobody wants all removed character appear one by one when undoing.
108 if (!undo_finished && kind != Undo::ATOMIC) {
109 // Check whether storing is needed.
110 if (!buf.undostack().empty()
111 && buf.undostack().top().kind == kind
112 && buf.undostack().top().first_par == first_par
113 && buf.undostack().top().end_par == end_par) {
114 // No additonal undo recording needed -
115 // effectively, we combine undo recordings to one.
120 // make and push the Undo entry
122 ParIterator pit = text2pit(buf, text, textnum);
123 stack.push(Undo(kind, textnum, pit.index(),
124 first_par, end_par, text->cursor.par(), text->cursor.pos()));
125 lyxerr << "undo record: " << stack.top() << std::endl;
127 // record the relevant paragraphs
128 ParagraphList & undo_pars = stack.top().pars;
130 ParagraphList & plist = text->paragraphs();
131 ParagraphList::iterator first = plist.begin();
132 advance(first, first_par);
133 ParagraphList::iterator last = plist.begin();
134 advance(last, last_par);
136 for (ParagraphList::iterator it = first; it != last; ++it)
137 undo_pars.push_back(*it);
138 undo_pars.push_back(*last);
140 // and make sure that next time, we should be combining if possible
141 undo_finished = false;
145 // returns false if no undo possible
146 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
148 Buffer & buf = *bv->buffer();
149 lyxerr << "undo, performing: " << undo << std::endl;
150 ParIterator pit = num2pit(buf, undo.text);
151 LyXText * text = pit.text(buf);
152 ParagraphList & plist = text->paragraphs();
154 // remove new stuff between first and last
156 ParagraphList::iterator first = plist.begin();
157 advance(first, undo.first_par);
158 ParagraphList::iterator last = plist.begin();
159 advance(last, plist.size() - undo.end_par);
160 plist.erase(first, ++last);
163 // re-insert old stuff instead
165 plist.assign(undo.pars.begin(), undo.pars.end());
167 ParagraphList::iterator first = plist.begin();
168 advance(first, undo.first_par);
169 plist.insert(first, undo.pars.begin(), undo.pars.end());
173 lyxerr << "undo, text: " << undo.text
174 << " inset: " << pit.inset()
175 << " index: " << undo.index
176 << " par: " << undo.cursor_par
177 << " pos: " << undo.cursor_pos
180 // set cursor again to force the position to be the right one
181 text->cursor.par(undo.cursor_par);
182 text->cursor.pos(undo.cursor_pos);
184 // clear any selection
185 text->clearSelection();
186 text->selection.cursor = text->cursor;
187 text->updateCounters();
189 // rebreak the entire lyxtext
190 buf.text().fullRebreak();
193 text->setCursor(undo.cursor_par, undo.cursor_pos);
200 // returns false if no undo possible
201 bool textUndoOrRedo(BufferView * bv,
202 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
204 Buffer & buf = *bv->buffer();
211 Undo undo = stack.top();
216 otherstack.push(undo);
217 otherstack.top().pars.clear();
218 ParIterator pit = num2pit(buf, undo.text);
219 ParagraphList & plist = pit.plist();
220 if (undo.first_par + undo.end_par <= int(plist.size())) {
221 ParagraphList::iterator first = plist.begin();
222 advance(first, undo.first_par);
223 ParagraphList::iterator last = plist.begin();
224 advance(last, plist.size() - undo.end_par + 1);
225 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
227 LyXText * text = pit.text(buf);
228 otherstack.top().cursor_pos = text->cursor.pos();
229 otherstack.top().cursor_par = text->cursor.par();
230 lyxerr << " undo other: " << otherstack.top() << std::endl;
234 bool const ret = performUndoOrRedo(bv, undo);
244 // this is dangerous and for internal use only
251 // this is dangerous and for internal use only
258 // makes sure the next operation will be stored
259 undo_finished = true;
263 bool textUndo(BufferView * bv)
265 return textUndoOrRedo(bv, bv->buffer()->undostack(),
266 bv->buffer()->redostack());
270 bool textRedo(BufferView * bv)
272 return textUndoOrRedo(bv, bv->buffer()->redostack(),
273 bv->buffer()->undostack());
277 void recordUndo(Undo::undo_kind kind,
278 LyXText const * text, paroffset_type first, paroffset_type last)
282 Buffer * buf = text->bv()->buffer();
283 recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
284 buf->redostack().clear();
288 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
290 recordUndo(kind, text, par, par);
294 void recordUndo(BufferView * bv, Undo::undo_kind kind)
296 recordUndo(kind, bv->text(), bv->text()->cursor.par());