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.
22 #include "BufferView.h"
23 #include "iterators.h"
25 #include "paragraph.h"
27 using lyx::paroffset_type;
30 /// The flag used by finishUndo().
33 /// Whether actions are not added to the undo stacks.
36 Undo::Undo(undo_kind kind_, int text_, int index_,
37 int first_par_, int end_par_, int cursor_par_, int cursor_pos_)
42 first_par(first_par_),
44 cursor_par(cursor_par_),
45 cursor_pos(cursor_pos_)
51 std::ostream & operator<<(std::ostream & os, Undo const & undo)
53 return os << " text: " << undo.text
54 << " index: " << undo.index
55 << " first: " << undo.first_par
56 << " from end: " << undo.end_par
57 << " cursor: " << undo.cursor_par
58 << "/" << undo.cursor_pos;
62 // translates LyXText pointer into offset count from document begin
63 ParIterator text2pit(Buffer & buf, LyXText * text, int & tcount)
66 ParIterator pit = buf.par_iterator_begin();
67 ParIterator end = buf.par_iterator_end();
69 for ( ; pit != end; ++pit, ++tcount)
70 if (pit.text(buf) == text)
72 lyxerr << "undo: should not happen" << std::endl;
77 // translates offset from buffer begin to ParIterator
78 ParIterator num2pit(Buffer & buf, int num)
80 ParIterator pit = buf.par_iterator_begin();
81 ParIterator end = buf.par_iterator_end();
83 for ( ; num && pit != end; ++pit, --num)
89 // don't crash early...
90 lyxerr << "undo: num2pit: num: " << num << std::endl;
92 return buf.par_iterator_begin();
96 void recordUndo(Undo::undo_kind kind,
97 LCursor & cur, paroffset_type first_par, paroffset_type last_par,
98 limited_stack<Undo> & stack)
100 if (first_par > last_par) {
101 paroffset_type t = first_par;
102 first_par = last_par;
106 Buffer & buf = *cur.bv().buffer();
107 int const end_par = cur.lastpar() + 1 - last_par;
109 // Undo::ATOMIC are always recorded (no overlapping there).
110 // overlapping only with insert and delete inside one paragraph:
111 // nobody wants all removed character appear one by one when undoing.
112 if (!undo_finished && kind != Undo::ATOMIC) {
113 // Check whether storing is needed.
114 if (!buf.undostack().empty()
115 && buf.undostack().top().kind == kind
116 && buf.undostack().top().first_par == first_par
117 && buf.undostack().top().end_par == end_par) {
118 // No additonal undo recording needed -
119 // effectively, we combine undo recordings to one.
124 // make and push the Undo entry
126 LyXText * text = cur.text();
127 BOOST_ASSERT(text); // not in mathed (yet)
128 ParIterator pit = text2pit(buf, text, textnum);
129 stack.push(Undo(kind, textnum, pit.index(),
130 first_par, end_par, cur.par(), cur.pos()));
131 //lyxerr << "undo record: " << stack.top() << std::endl;
133 // record the relevant paragraphs
134 ParagraphList & undo_pars = stack.top().pars;
136 ParagraphList & plist = text->paragraphs();
137 ParagraphList::iterator first = plist.begin();
138 advance(first, first_par);
139 ParagraphList::iterator last = plist.begin();
140 advance(last, last_par);
142 for (ParagraphList::iterator it = first; it != last; ++it)
143 undo_pars.push_back(*it);
144 undo_pars.push_back(*last);
146 // and make sure that next time, we should be combining if possible
147 undo_finished = false;
151 // returns false if no undo possible
152 bool performUndoOrRedo(BufferView & bv, Undo const & undo)
154 Buffer & buf = *bv.buffer();
155 lyxerr << "undo, performing: " << undo << std::endl;
156 ParIterator pit = num2pit(buf, undo.text);
157 LyXText * text = pit.text(buf);
158 ParagraphList & plist = text->paragraphs();
160 // remove new stuff between first and last
162 ParagraphList::iterator first = plist.begin();
163 advance(first, undo.first_par);
164 ParagraphList::iterator last = plist.begin();
165 advance(last, plist.size() - undo.end_par);
166 plist.erase(first, ++last);
169 // re-insert old stuff instead
171 plist.assign(undo.pars.begin(), undo.pars.end());
173 ParagraphList::iterator first = plist.begin();
174 advance(first, undo.first_par);
175 plist.insert(first, undo.pars.begin(), undo.pars.end());
179 lyxerr << "undo, text: " << undo.text
180 << " inset: " << pit.inset()
181 << " index: " << undo.index
182 << " par: " << undo.cursor_par
183 << " pos: " << undo.cursor_pos
186 text->updateCounters();
188 // rebreak the entire lyxtext
190 text->redoParagraphs(buf.paragraphs().begin(), buf.paragraphs().end());
191 bv.cursor().resetAnchor();
193 ParIterator pit2 = num2pit(buf, undo.text);
194 advance(pit2, undo.cursor_par);
195 bv.setCursor(pit2, undo.cursor_pos);
202 // returns false if no undo possible
203 bool textUndoOrRedo(BufferView & bv,
204 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
206 Buffer & buf = *bv.buffer();
213 Undo undo = stack.top();
218 otherstack.push(undo);
219 otherstack.top().pars.clear();
220 ParIterator pit = num2pit(buf, undo.text);
221 ParagraphList & plist = pit.plist();
222 if (undo.first_par + undo.end_par <= int(plist.size())) {
223 ParagraphList::iterator first = plist.begin();
224 advance(first, undo.first_par);
225 ParagraphList::iterator last = plist.begin();
226 advance(last, plist.size() - undo.end_par + 1);
227 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
229 otherstack.top().cursor_pos = bv.cursor().pos();
230 otherstack.top().cursor_par = bv.cursor().par();
231 lyxerr << " undo other: " << otherstack.top() << std::endl;
235 bool const ret = performUndoOrRedo(bv, undo);
245 // this is dangerous and for internal use only
252 // this is dangerous and for internal use only
259 // makes sure the next operation will be stored
260 undo_finished = true;
264 bool textUndo(BufferView & bv)
266 return textUndoOrRedo(bv, bv.buffer()->undostack(),
267 bv.buffer()->redostack());
271 bool textRedo(BufferView & bv)
273 return textUndoOrRedo(bv, bv.buffer()->redostack(),
274 bv.buffer()->undostack());
278 void recordUndo(Undo::undo_kind kind,
279 LCursor & cur, paroffset_type first, paroffset_type last)
283 Buffer * buf = cur.bv().buffer();
284 recordUndo(kind, cur, first, last, buf->undostack());
285 buf->redostack().clear();
289 void recordUndo(LCursor & cur, Undo::undo_kind kind)
291 recordUndo(kind, cur, cur.par(), cur.par());
295 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
297 recordUndo(kind, cur, cur.selBegin().par(), cur.selEnd().par());
301 void recordUndo(LCursor & cur, Undo::undo_kind kind, paroffset_type from)
303 recordUndo(kind, cur, cur.par(), from);
307 void recordUndo(LCursor & cur, Undo::undo_kind kind,
308 paroffset_type from, paroffset_type to)
310 recordUndo(kind, cur, from, to);
314 void recordUndoFullDocument(LCursor &)
316 //recordUndo(Undo::ATOMIC,
317 // cur, 0, cur.bv().text()->paragraphs().size() - 1);