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"
27 #include "insets/inset.h"
39 /// The flag used by finishUndo().
43 std::ostream & operator<<(std::ostream & os, Undo const & undo)
45 return os << " from: " << undo.from << " end: " << undo.end
46 << " cell:\n" << undo.cell
47 << " cursor:\n" << undo.cursor;
51 bool samePar(StableDocIterator const & i1, StableDocIterator const & i2)
53 StableDocIterator tmpi2 = i2;
54 tmpi2.pos() = i1.pos();
59 void doRecordUndo(Undo::undo_kind kind,
60 DocIterator const & cell,
61 pit_type first_pit, pit_type last_pit,
62 DocIterator const & cur,
63 BufferParams const & bparams,
65 limited_stack<Undo> & stack)
67 if (first_pit > last_pit)
68 std::swap(first_pit, last_pit);
69 // create the position information of the Undo entry
74 undo.bparams = bparams ;
75 undo.isFullBuffer = isFullBuffer;
76 //lyxerr << "recordUndo: cur: " << cur << endl;
77 //lyxerr << "recordUndo: pos: " << cur.pos() << endl;
78 //lyxerr << "recordUndo: cell: " << cell << endl;
79 undo.from = first_pit;
80 undo.end = cell.lastpit() - last_pit;
82 // Undo::ATOMIC are always recorded (no overlapping there).
83 // As nobody wants all removed character appear one by one when undoing,
84 // we want combine 'similar' non-ATOMIC undo recordings to one.
86 && kind != Undo::ATOMIC
88 && samePar(stack.top().cell, undo.cell)
89 && stack.top().kind == undo.kind
90 && stack.top().from == undo.from
91 && stack.top().end == undo.end)
94 // fill in the real data to be saved
95 if (cell.inMathed()) {
96 // simply use the whole cell
97 undo.array = asString(cell.cell());
99 // some more effort needed here as 'the whole cell' of the
100 // main LyXText _is_ the whole document.
101 // record the relevant paragraphs
102 LyXText const * text = cell.text();
104 ParagraphList const & plist = text->paragraphs();
105 ParagraphList::const_iterator first = plist.begin();
106 advance(first, first_pit);
107 ParagraphList::const_iterator last = plist.begin();
108 advance(last, last_pit + 1);
109 undo.pars = ParagraphList(first, last);
112 // push the undo entry to undo stack
113 //lyxerr << "undo record: " << stack.top() << std::endl;
116 // next time we'll try again to combine entries if possible
117 undo_finished = false;
121 void recordUndo(Undo::undo_kind kind,
122 LCursor & cur, pit_type first_pit, pit_type last_pit,
123 limited_stack<Undo> & stack)
125 BOOST_ASSERT(first_pit <= cur.lastpit());
126 BOOST_ASSERT(last_pit <= cur.lastpit());
128 doRecordUndo(kind, cur, first_pit, last_pit, cur,
129 cur.bv().buffer()->params(), false, stack);
134 // Returns false if no undo possible.
135 bool textUndoOrRedo(BufferView & bv,
136 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
145 // Adjust undo stack and get hold of current undo data.
146 Undo undo = stack.top();
149 // We will store in otherstack the part of the document under 'undo'
150 Buffer * buf = bv.buffer();
151 DocIterator cell_dit = undo.cell.asDocIterator(&buf->inset());
153 doRecordUndo(Undo::ATOMIC, cell_dit,
154 undo.from, cell_dit.lastpit() - undo.end, bv.cursor(),
155 undo.bparams, undo.isFullBuffer,
158 // This does the actual undo/redo.
159 //lyxerr << "undo, performing: " << undo << std::endl;
160 DocIterator dit = undo.cell.asDocIterator(&buf->inset());
161 if (undo.isFullBuffer) {
162 // This is a full document
163 otherstack.top().bparams = buf->params();
164 buf->params() = undo.bparams;
165 buf->paragraphs() = undo.pars;
166 } else if (dit.inMathed()) {
167 // We stored the full cell here as there is not much to be
168 // gained by storing just 'a few' paragraphs (most if not
169 // all math inset cells have just one paragraph!)
170 asArray(undo.array, dit.cell());
172 // Some finer machinery is needed here.
173 LyXText * text = dit.text();
175 ParagraphList & plist = text->paragraphs();
177 // remove new stuff between first and last
178 ParagraphList::iterator first = plist.begin();
179 advance(first, undo.from);
180 ParagraphList::iterator last = plist.begin();
181 advance(last, plist.size() - undo.end);
182 plist.erase(first, last);
184 // re-insert old stuff instead
185 first = plist.begin();
186 advance(first, undo.from);
188 // this ugly stuff is needed until we get rid of the
189 // inset_owner backpointer
190 ParagraphList::iterator pit = undo.pars.begin();
191 ParagraphList::iterator const end = undo.pars.end();
192 for (; pit != end; ++pit)
193 pit->setInsetOwner(dit.realInset());
194 plist.insert(first, undo.pars.begin(), undo.pars.end());
198 LCursor & cur = bv.cursor();
199 cur.setCursor(undo.cursor.asDocIterator(&buf->inset()));
200 cur.selection() = false;
212 // Make sure the next operation will be stored.
213 undo_finished = true;
217 bool textUndo(BufferView & bv)
219 return textUndoOrRedo(bv, bv.buffer()->undostack(),
220 bv.buffer()->redostack());
224 bool textRedo(BufferView & bv)
226 return textUndoOrRedo(bv, bv.buffer()->redostack(),
227 bv.buffer()->undostack());
231 void recordUndo(Undo::undo_kind kind,
232 LCursor & cur, pit_type first, pit_type last)
234 Buffer * buf = cur.bv().buffer();
235 recordUndo(kind, cur, first, last, buf->undostack());
236 buf->redostack().clear();
237 //lyxerr << "undostack:\n";
238 //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
239 // lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
243 void recordUndo(LCursor & cur, Undo::undo_kind kind)
245 recordUndo(kind, cur, cur.pit(), cur.pit());
249 void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
257 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
259 recordUndo(kind, cur, cur.selBegin().pit(), cur.selEnd().pit());
263 void recordUndo(LCursor & cur, Undo::undo_kind kind, pit_type from)
265 recordUndo(kind, cur, cur.pit(), from);
269 void recordUndo(LCursor & cur, Undo::undo_kind kind,
270 pit_type from, pit_type to)
272 recordUndo(kind, cur, from, to);
276 void recordUndoFullDocument(BufferView * bv)
278 Buffer * buf = bv->buffer();
281 doc_iterator_begin(buf->inset()),
282 0, buf->paragraphs().size() - 1,
288 undo_finished = false;