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"
24 #include "paragraph.h"
26 #include "mathed/math_support.h"
30 using lyx::paroffset_type;
35 /// The flag used by finishUndo().
39 std::ostream & operator<<(std::ostream & os, Undo const & undo)
41 return os << " from: " << undo.from
42 << " end: " << undo.end
43 << " cursor:\n" << undo.cursor;
47 void recordUndo(Undo::undo_kind kind,
48 LCursor & cur, paroffset_type first_par, paroffset_type last_par,
49 limited_stack<Undo> & stack)
51 BOOST_ASSERT(first_par <= cur.lastpar());
52 BOOST_ASSERT(last_par <= cur.lastpar());
54 if (first_par > last_par)
55 std::swap(first_par, last_par);
57 // create the position information of the Undo entry
60 undo.cursor = StableDocumentIterator(cur);
61 undo.from = first_par;
62 undo.end = cur.lastpar() - last_par;
64 // Undo::ATOMIC are always recorded (no overlapping there).
65 // As nobody wants all removed character appear one by one when undoing,
66 // we want combine 'similar' non-ATOMIC undo recordings to one.
68 && kind != Undo::ATOMIC
70 && stack.top().cursor.size() == undo.cursor.size()
71 && stack.top().kind == undo.kind
72 && stack.top().from == undo.from
73 && stack.top().end == undo.end)
76 // fill in the real data to be saved
78 // simply use the whole cell
79 undo.array = asString(cur.cell());
81 // some more effort needed here as 'the whole cell' of the
82 // main LyXText _is_ the whole document.
83 // record the relevant paragraphs
84 LyXText * text = cur.text();
86 ParagraphList & plist = text->paragraphs();
87 ParagraphList::iterator first = plist.begin();
88 advance(first, first_par);
89 ParagraphList::iterator last = plist.begin();
90 advance(last, last_par + 1);
91 undo.pars = ParagraphList(first, last);
94 // push the undo entry to undo stack
95 //lyxerr << "undo record: " << stack.top() << std::endl;
98 // next time we'll try again to combine entries if possible
99 undo_finished = false;
103 void performUndoOrRedo(BufferView & bv, Undo const & undo)
105 LCursor & cur = bv.cursor();
106 lyxerr << "undo, performing: " << undo << std::endl;
107 cur.setCursor(undo.cursor.asDocumentIterator(&bv.buffer()->inset()), false);
109 if (cur.inMathed()) {
110 // We stored the full cell here as there is not much to be
111 // gained by storing just 'a few' paragraphs (most if not
112 // all math inset cells have just one paragraph!)
113 asArray(undo.array, cur.cell());
115 // Some finer machinery is needed here.
116 LyXText * text = cur.text();
118 ParagraphList & plist = text->paragraphs();
120 // remove new stuff between first and last
121 ParagraphList::iterator first = plist.begin();
122 advance(first, undo.from);
123 ParagraphList::iterator last = plist.begin();
124 advance(last, plist.size() - undo.end);
125 plist.erase(first, last);
127 // re-insert old stuff instead
128 first = plist.begin();
129 advance(first, undo.from);
130 plist.insert(first, undo.pars.begin(), undo.pars.end());
138 // returns false if no undo possible
139 bool textUndoOrRedo(BufferView & bv,
140 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
148 Undo undo = stack.top();
152 // this implements redo
153 otherstack.push(undo);
154 DocumentIterator dit =
155 undo.cursor.asDocumentIterator(&bv.buffer()->inset());
156 if (dit.inMathed()) {
157 // not much to be done
159 otherstack.top().pars.clear();
160 LyXText * text = dit.text();
162 ParagraphList & plist = text->paragraphs();
163 if (undo.from + undo.end <= int(plist.size())) {
164 ParagraphList::iterator first = plist.begin();
165 advance(first, undo.from);
166 ParagraphList::iterator last = plist.begin();
167 advance(last, plist.size() - undo.end);
168 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
171 otherstack.top().cursor = bv.cursor();
172 //lyxerr << " undo other: " << otherstack.top() << std::endl;
174 performUndoOrRedo(bv, undo);
183 // makes sure the next operation will be stored
184 undo_finished = true;
188 bool textUndo(BufferView & bv)
190 return textUndoOrRedo(bv, bv.buffer()->undostack(),
191 bv.buffer()->redostack());
195 bool textRedo(BufferView & bv)
197 return textUndoOrRedo(bv, bv.buffer()->redostack(),
198 bv.buffer()->undostack());
202 void recordUndo(Undo::undo_kind kind,
203 LCursor & cur, paroffset_type first, paroffset_type last)
205 Buffer * buf = cur.bv().buffer();
206 recordUndo(kind, cur, first, last, buf->undostack());
207 buf->redostack().clear();
208 lyxerr << "undostack:\n";
209 for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
210 lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
214 void recordUndo(LCursor & cur, Undo::undo_kind kind)
216 recordUndo(kind, cur, cur.par(), cur.par());
220 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
222 recordUndo(kind, cur, cur.selBegin().par(), cur.selEnd().par());
226 void recordUndo(LCursor & cur, Undo::undo_kind kind, paroffset_type from)
228 recordUndo(kind, cur, cur.par(), from);
232 void recordUndo(LCursor & cur, Undo::undo_kind kind,
233 paroffset_type from, paroffset_type to)
235 recordUndo(kind, cur, from, to);
239 void recordUndoFullDocument(LCursor &)
241 //recordUndo(Undo::ATOMIC,
242 // cur, 0, cur.bv().text()->paragraphs().size() - 1);