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"
39 /// The flag used by finishUndo().
43 std::ostream & operator<<(std::ostream & os, Undo const & undo)
45 return os << " from: " << undo.from
46 << " end: " << undo.end
47 << " cursor:\n" << undo.cursor;
51 void recordUndo(Undo::undo_kind kind,
52 LCursor & cur, par_type first_par, par_type last_par,
53 limited_stack<Undo> & stack)
55 BOOST_ASSERT(first_par <= cur.lastpar());
56 BOOST_ASSERT(last_par <= cur.lastpar());
58 if (first_par > last_par)
59 std::swap(first_par, last_par);
61 // create the position information of the Undo entry
64 undo.cursor = StableDocIterator(cur);
65 undo.from = first_par;
66 undo.end = cur.lastpar() - last_par;
68 // Undo::ATOMIC are always recorded (no overlapping there).
69 // As nobody wants all removed character appear one by one when undoing,
70 // we want combine 'similar' non-ATOMIC undo recordings to one.
72 && kind != Undo::ATOMIC
74 && stack.top().cursor.size() == undo.cursor.size()
75 && stack.top().kind == undo.kind
76 && stack.top().from == undo.from
77 && stack.top().end == undo.end)
80 // fill in the real data to be saved
82 // simply use the whole cell
83 undo.array = asString(cur.cell());
85 // some more effort needed here as 'the whole cell' of the
86 // main LyXText _is_ the whole document.
87 // record the relevant paragraphs
88 LyXText * text = cur.text();
90 ParagraphList & plist = text->paragraphs();
91 ParagraphList::iterator first = plist.begin();
92 advance(first, first_par);
93 ParagraphList::iterator last = plist.begin();
94 advance(last, last_par + 1);
95 undo.pars = ParagraphList(first, last);
98 // push the undo entry to undo stack
99 //lyxerr << "undo record: " << stack.top() << std::endl;
102 // next time we'll try again to combine entries if possible
103 undo_finished = false;
107 void performUndoOrRedo(BufferView & bv, Undo const & undo)
109 LCursor & cur = bv.cursor();
110 lyxerr << "undo, performing: " << undo << std::endl;
111 cur.setCursor(undo.cursor.asDocIterator(&bv.buffer()->inset()));
112 cur.selection() = false;
114 if (cur.inMathed()) {
115 // We stored the full cell here as there is not much to be
116 // gained by storing just 'a few' paragraphs (most if not
117 // all math inset cells have just one paragraph!)
118 asArray(undo.array, cur.cell());
120 // Some finer machinery is needed here.
121 LyXText * text = cur.text();
123 ParagraphList & plist = text->paragraphs();
125 // remove new stuff between first and last
126 ParagraphList::iterator first = plist.begin();
127 advance(first, undo.from);
128 ParagraphList::iterator last = plist.begin();
129 advance(last, plist.size() - undo.end);
130 plist.erase(first, last);
132 // re-insert old stuff instead
133 first = plist.begin();
134 advance(first, undo.from);
135 plist.insert(first, undo.pars.begin(), undo.pars.end());
143 // returns false if no undo possible
144 bool textUndoOrRedo(BufferView & bv,
145 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
153 Undo undo = stack.top();
157 // this implements redo
158 otherstack.push(undo);
160 undo.cursor.asDocIterator(&bv.buffer()->inset());
161 if (dit.inMathed()) {
162 // Easy way out: store a full cell.
163 otherstack.top().array = asString(dit.cell());
165 // As cells might be too large in texted, store just a part
166 // of the paragraph list.
167 otherstack.top().pars.clear();
168 LyXText * text = dit.text();
170 ParagraphList & plist = text->paragraphs();
171 if (undo.from + undo.end <= int(plist.size())) {
172 ParagraphList::iterator first = plist.begin();
173 advance(first, undo.from);
174 ParagraphList::iterator last = plist.begin();
175 advance(last, plist.size() - undo.end);
176 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
179 otherstack.top().cursor = bv.cursor();
180 //lyxerr << " undo other: " << otherstack.top() << std::endl;
182 performUndoOrRedo(bv, undo);
191 // makes sure the next operation will be stored
192 undo_finished = true;
196 bool textUndo(BufferView & bv)
198 return textUndoOrRedo(bv, bv.buffer()->undostack(),
199 bv.buffer()->redostack());
203 bool textRedo(BufferView & bv)
205 return textUndoOrRedo(bv, bv.buffer()->redostack(),
206 bv.buffer()->undostack());
210 void recordUndo(Undo::undo_kind kind,
211 LCursor & cur, par_type first, par_type last)
213 Buffer * buf = cur.bv().buffer();
214 recordUndo(kind, cur, first, last, buf->undostack());
215 buf->redostack().clear();
216 lyxerr << "undostack:\n";
217 for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
218 lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
222 void recordUndo(LCursor & cur, Undo::undo_kind kind)
224 recordUndo(kind, cur, cur.par(), cur.par());
228 void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
236 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
238 recordUndo(kind, cur, cur.selBegin().par(), cur.selEnd().par());
242 void recordUndo(LCursor & cur, Undo::undo_kind kind, par_type from)
244 recordUndo(kind, cur, cur.par(), from);
248 void recordUndo(LCursor & cur, Undo::undo_kind kind,
249 par_type from, par_type to)
251 recordUndo(kind, cur, from, to);
255 void recordUndoFullDocument(LCursor &)
257 //recordUndo(Undo::ATOMIC,
258 // cur, 0, cur.bv().text()->paragraphs().size() - 1);