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 /// Whether actions are not added to the undo stacks.
38 /// The flag used by finishUndo().
42 std::ostream & operator<<(std::ostream & os, Undo const & undo)
44 return os << " from: " << undo.from
45 << " end: " << undo.end
46 << " cursor:\n" << undo.cursor;
50 void recordUndo(Undo::undo_kind kind,
51 LCursor & cur, paroffset_type first_par, paroffset_type last_par,
52 limited_stack<Undo> & stack)
54 BOOST_ASSERT(first_par <= cur.lastpar());
55 BOOST_ASSERT(last_par <= cur.lastpar());
57 if (first_par > last_par)
58 std::swap(first_par, last_par);
60 // create the position information of the Undo entry
63 undo.cursor = StableDocumentIterator(cur);
64 undo.from = first_par;
65 undo.end = cur.lastpar() - last_par;
67 // Undo::ATOMIC are always recorded (no overlapping there).
68 // As nobody wants all removed character appear one by one when undoing,
69 // we want combine 'similar' non-ATOMIC undo recordings to one.
71 && kind != Undo::ATOMIC
73 && stack.top().cursor.size() == undo.cursor.size()
74 && stack.top().kind == undo.kind
75 && stack.top().from == undo.from
76 && stack.top().end == undo.end)
79 // fill in the real data to be saved
81 // simply use the whole cell
82 undo.array = asString(cur.cell());
84 // some more effort needed here as 'the whole cell' of the
85 // main LyXText _is_ the whole document.
86 // record the relevant paragraphs
87 LyXText * text = cur.text();
89 ParagraphList & plist = text->paragraphs();
90 ParagraphList::iterator first = plist.begin();
91 advance(first, first_par);
92 ParagraphList::iterator last = plist.begin();
93 advance(last, last_par + 1);
94 undo.pars = ParagraphList(first, last);
97 // push the undo entry to undo stack
98 //lyxerr << "undo record: " << stack.top() << std::endl;
101 // next time we'll try again to combine entries if possible
102 undo_finished = false;
106 void performUndoOrRedo(BufferView & bv, Undo const & undo)
108 LCursor & cur = bv.cursor();
109 lyxerr << "undo, performing: " << undo << std::endl;
110 cur.setCursor(undo.cursor.asDocumentIterator(&bv.buffer()->inset()), false);
112 if (cur.inMathed()) {
113 // We stored the full cell here as there is not much to be
114 // gained by storing just 'a few' paragraphs (most if not
115 // all math inset cells have just one paragraph!)
116 asArray(undo.array, cur.cell());
118 // Some finer machinery is needed here.
119 LyXText * text = cur.text();
121 ParagraphList & plist = text->paragraphs();
123 // remove new stuff between first and last
124 ParagraphList::iterator first = plist.begin();
125 advance(first, undo.from);
126 ParagraphList::iterator last = plist.begin();
127 advance(last, plist.size() - undo.end);
128 plist.erase(first, last);
130 // re-insert old stuff instead
131 first = plist.begin();
132 advance(first, undo.from);
133 plist.insert(first, undo.pars.begin(), undo.pars.end());
141 // returns false if no undo possible
142 bool textUndoOrRedo(BufferView & bv,
143 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
151 Undo undo = stack.top();
155 // this implements redo
157 otherstack.push(undo);
158 DocumentIterator dit =
159 undo.cursor.asDocumentIterator(&bv.buffer()->inset());
160 if (dit.inMathed()) {
161 // not much to be done
163 otherstack.top().pars.clear();
164 LyXText * text = dit.text();
166 ParagraphList & plist = text->paragraphs();
167 if (undo.from + undo.end <= int(plist.size())) {
168 ParagraphList::iterator first = plist.begin();
169 advance(first, undo.from);
170 ParagraphList::iterator last = plist.begin();
171 advance(last, plist.size() - undo.end);
172 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
175 otherstack.top().cursor = bv.cursor();
176 //lyxerr << " undo other: " << otherstack.top() << std::endl;
180 performUndoOrRedo(bv, undo);
190 // makes sure the next operation will be stored
191 undo_finished = true;
195 bool textUndo(BufferView & bv)
197 return textUndoOrRedo(bv, bv.buffer()->undostack(),
198 bv.buffer()->redostack());
202 bool textRedo(BufferView & bv)
204 return textUndoOrRedo(bv, bv.buffer()->redostack(),
205 bv.buffer()->undostack());
209 void recordUndo(Undo::undo_kind kind,
210 LCursor & cur, paroffset_type first, paroffset_type last)
214 Buffer * buf = cur.bv().buffer();
215 recordUndo(kind, cur, first, last, buf->undostack());
216 buf->redostack().clear();
217 lyxerr << "undostack:\n";
218 for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
219 lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
223 void recordUndo(LCursor & cur, Undo::undo_kind kind)
225 recordUndo(kind, cur, cur.par(), cur.par());
229 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
231 recordUndo(kind, cur, cur.selBegin().par(), cur.selEnd().par());
235 void recordUndo(LCursor & cur, Undo::undo_kind kind, paroffset_type from)
237 recordUndo(kind, cur, cur.par(), from);
241 void recordUndo(LCursor & cur, Undo::undo_kind kind,
242 paroffset_type from, paroffset_type to)
244 recordUndo(kind, cur, from, to);
248 void recordUndoFullDocument(LCursor &)
250 //recordUndo(Undo::ATOMIC,
251 // cur, 0, cur.bv().text()->paragraphs().size() - 1);