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"
37 /// The flag used by finishUndo().
41 std::ostream & operator<<(std::ostream & os, Undo const & undo)
43 return os << " from: " << undo.from
44 << " end: " << undo.end
45 << " cursor:\n" << undo.cursor;
49 void recordUndo(Undo::undo_kind kind,
50 LCursor & cur, par_type first_par, par_type last_par,
51 limited_stack<Undo> & stack)
53 BOOST_ASSERT(first_par <= cur.lastpar());
54 BOOST_ASSERT(last_par <= cur.lastpar());
56 if (first_par > last_par)
57 std::swap(first_par, last_par);
59 // create the position information of the Undo entry
62 undo.cursor = StableDocumentIterator(cur);
63 undo.from = first_par;
64 undo.end = cur.lastpar() - last_par;
66 // Undo::ATOMIC are always recorded (no overlapping there).
67 // As nobody wants all removed character appear one by one when undoing,
68 // we want combine 'similar' non-ATOMIC undo recordings to one.
70 && kind != Undo::ATOMIC
72 && stack.top().cursor.size() == undo.cursor.size()
73 && stack.top().kind == undo.kind
74 && stack.top().from == undo.from
75 && stack.top().end == undo.end)
78 // fill in the real data to be saved
80 // simply use the whole cell
81 undo.array = asString(cur.cell());
83 // some more effort needed here as 'the whole cell' of the
84 // main LyXText _is_ the whole document.
85 // record the relevant paragraphs
86 LyXText * text = cur.text();
88 ParagraphList & plist = text->paragraphs();
89 ParagraphList::iterator first = plist.begin();
90 advance(first, first_par);
91 ParagraphList::iterator last = plist.begin();
92 advance(last, last_par + 1);
93 undo.pars = ParagraphList(first, last);
96 // push the undo entry to undo stack
97 //lyxerr << "undo record: " << stack.top() << std::endl;
100 // next time we'll try again to combine entries if possible
101 undo_finished = false;
105 void performUndoOrRedo(BufferView & bv, Undo const & undo)
107 LCursor & cur = bv.cursor();
108 lyxerr << "undo, performing: " << undo << std::endl;
109 cur.setCursor(undo.cursor.asDocumentIterator(&bv.buffer()->inset()), false);
111 if (cur.inMathed()) {
112 // We stored the full cell here as there is not much to be
113 // gained by storing just 'a few' paragraphs (most if not
114 // all math inset cells have just one paragraph!)
115 asArray(undo.array, cur.cell());
117 // Some finer machinery is needed here.
118 LyXText * text = cur.text();
120 ParagraphList & plist = text->paragraphs();
122 // remove new stuff between first and last
123 ParagraphList::iterator first = plist.begin();
124 advance(first, undo.from);
125 ParagraphList::iterator last = plist.begin();
126 advance(last, plist.size() - undo.end);
127 plist.erase(first, last);
129 // re-insert old stuff instead
130 first = plist.begin();
131 advance(first, undo.from);
132 plist.insert(first, undo.pars.begin(), undo.pars.end());
140 // returns false if no undo possible
141 bool textUndoOrRedo(BufferView & bv,
142 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
150 Undo undo = stack.top();
154 // this implements redo
155 otherstack.push(undo);
156 DocumentIterator dit =
157 undo.cursor.asDocumentIterator(&bv.buffer()->inset());
158 if (dit.inMathed()) {
159 // not much to be done
161 otherstack.top().pars.clear();
162 LyXText * text = dit.text();
164 ParagraphList & plist = text->paragraphs();
165 if (undo.from + undo.end <= int(plist.size())) {
166 ParagraphList::iterator first = plist.begin();
167 advance(first, undo.from);
168 ParagraphList::iterator last = plist.begin();
169 advance(last, plist.size() - undo.end);
170 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
173 otherstack.top().cursor = bv.cursor();
174 //lyxerr << " undo other: " << otherstack.top() << std::endl;
176 performUndoOrRedo(bv, undo);
185 // makes sure the next operation will be stored
186 undo_finished = true;
190 bool textUndo(BufferView & bv)
192 return textUndoOrRedo(bv, bv.buffer()->undostack(),
193 bv.buffer()->redostack());
197 bool textRedo(BufferView & bv)
199 return textUndoOrRedo(bv, bv.buffer()->redostack(),
200 bv.buffer()->undostack());
204 void recordUndo(Undo::undo_kind kind,
205 LCursor & cur, par_type first, par_type last)
207 Buffer * buf = cur.bv().buffer();
208 recordUndo(kind, cur, first, last, buf->undostack());
209 buf->redostack().clear();
210 //lyxerr << "undostack:\n";
211 //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
212 // lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
216 void recordUndo(LCursor & cur, Undo::undo_kind kind)
218 recordUndo(kind, cur, cur.par(), cur.par());
222 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
224 recordUndo(kind, cur, cur.selBegin().par(), cur.selEnd().par());
228 void recordUndo(LCursor & cur, Undo::undo_kind kind, par_type from)
230 recordUndo(kind, cur, cur.par(), from);
234 void recordUndo(LCursor & cur, Undo::undo_kind kind,
235 par_type from, par_type to)
237 recordUndo(kind, cur, from, to);
241 void recordUndoFullDocument(LCursor &)
243 //recordUndo(Undo::ATOMIC,
244 // cur, 0, cur.bv().text()->paragraphs().size() - 1);