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"
25 #include "ParagraphList.h"
27 #include "mathed/MathSupport.h"
28 #include "insets/inset.h"
40 /// The flag used by finishUndo().
44 std::ostream & operator<<(std::ostream & os, Undo const & undo)
46 return os << " from: " << undo.from << " end: " << undo.end
47 << " cell:\n" << undo.cell
48 << " cursor:\n" << undo.cursor;
52 bool samePar(StableDocIterator const & i1, StableDocIterator const & i2)
54 StableDocIterator tmpi2 = i2;
55 tmpi2.pos() = i1.pos();
60 void doRecordUndo(Undo::undo_kind kind,
61 DocIterator const & cell,
62 pit_type first_pit, pit_type last_pit,
63 DocIterator const & cur,
64 BufferParams const & bparams,
66 limited_stack<Undo> & stack)
68 if (first_pit > last_pit)
69 std::swap(first_pit, last_pit);
70 // create the position information of the Undo entry
75 undo.bparams = bparams ;
76 undo.isFullBuffer = isFullBuffer;
77 //lyxerr << "recordUndo: cur: " << cur << endl;
78 //lyxerr << "recordUndo: pos: " << cur.pos() << endl;
79 //lyxerr << "recordUndo: cell: " << cell << endl;
80 undo.from = first_pit;
81 undo.end = cell.lastpit() - last_pit;
83 // Undo::ATOMIC are always recorded (no overlapping there).
84 // As nobody wants all removed character appear one by one when undoing,
85 // we want combine 'similar' non-ATOMIC undo recordings to one.
87 && kind != Undo::ATOMIC
89 && samePar(stack.top().cell, undo.cell)
90 && stack.top().kind == undo.kind
91 && stack.top().from == undo.from
92 && stack.top().end == undo.end)
95 // fill in the real data to be saved
96 if (cell.inMathed()) {
97 // simply use the whole cell
98 undo.array = asString(cell.cell());
100 // some more effort needed here as 'the whole cell' of the
101 // main LyXText _is_ the whole document.
102 // record the relevant paragraphs
103 LyXText const * text = cell.text();
105 ParagraphList const & plist = text->paragraphs();
106 ParagraphList::const_iterator first = plist.begin();
107 advance(first, first_pit);
108 ParagraphList::const_iterator last = plist.begin();
109 advance(last, last_pit + 1);
110 undo.pars = ParagraphList(first, last);
113 // push the undo entry to undo stack
114 //lyxerr << "undo record: " << stack.top() << std::endl;
117 // next time we'll try again to combine entries if possible
118 undo_finished = false;
122 void recordUndo(Undo::undo_kind kind,
123 LCursor & cur, pit_type first_pit, pit_type last_pit,
124 limited_stack<Undo> & stack)
126 BOOST_ASSERT(first_pit <= cur.lastpit());
127 BOOST_ASSERT(last_pit <= cur.lastpit());
129 doRecordUndo(kind, cur, first_pit, last_pit, cur,
130 cur.bv().buffer()->params(), false, stack);
135 // Returns false if no undo possible.
136 bool textUndoOrRedo(BufferView & bv,
137 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
146 // Adjust undo stack and get hold of current undo data.
147 Undo undo = stack.top();
150 // We will store in otherstack the part of the document under 'undo'
151 Buffer * buf = bv.buffer();
152 DocIterator cell_dit = undo.cell.asDocIterator(&buf->inset());
154 doRecordUndo(Undo::ATOMIC, cell_dit,
155 undo.from, cell_dit.lastpit() - undo.end, bv.cursor(),
156 undo.bparams, undo.isFullBuffer,
159 // This does the actual undo/redo.
160 //lyxerr << "undo, performing: " << undo << std::endl;
161 DocIterator dit = undo.cell.asDocIterator(&buf->inset());
162 if (undo.isFullBuffer) {
163 // This is a full document
164 otherstack.top().bparams = buf->params();
165 buf->params() = undo.bparams;
166 buf->paragraphs() = undo.pars;
167 } else if (dit.inMathed()) {
168 // We stored the full cell here as there is not much to be
169 // gained by storing just 'a few' paragraphs (most if not
170 // all math inset cells have just one paragraph!)
171 //lyxerr << "undo.array=" << undo.array <<endl;
173 asArray(lyx::from_utf8(undo.array), dit.cell());
175 // Some finer machinery is needed here.
176 LyXText * text = dit.text();
178 ParagraphList & plist = text->paragraphs();
180 // remove new stuff between first and last
181 ParagraphList::iterator first = plist.begin();
182 advance(first, undo.from);
183 ParagraphList::iterator last = plist.begin();
184 advance(last, plist.size() - undo.end);
185 plist.erase(first, last);
187 // re-insert old stuff instead
188 first = plist.begin();
189 advance(first, undo.from);
191 // this ugly stuff is needed until we get rid of the
192 // inset_owner backpointer
193 ParagraphList::iterator pit = undo.pars.begin();
194 ParagraphList::iterator const end = undo.pars.end();
195 for (; pit != end; ++pit)
196 pit->setInsetOwner(dit.realInset());
197 plist.insert(first, undo.pars.begin(), undo.pars.end());
201 LCursor & cur = bv.cursor();
202 cur.setCursor(undo.cursor.asDocIterator(&buf->inset()));
203 cur.selection() = false;
215 // Make sure the next operation will be stored.
216 undo_finished = true;
220 bool textUndo(BufferView & bv)
222 return textUndoOrRedo(bv, bv.buffer()->undostack(),
223 bv.buffer()->redostack());
227 bool textRedo(BufferView & bv)
229 return textUndoOrRedo(bv, bv.buffer()->redostack(),
230 bv.buffer()->undostack());
234 void recordUndo(Undo::undo_kind kind,
235 LCursor & cur, pit_type first, pit_type last)
237 Buffer * buf = cur.bv().buffer();
238 recordUndo(kind, cur, first, last, buf->undostack());
239 buf->redostack().clear();
240 //lyxerr << "undostack:\n";
241 //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
242 // lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
246 void recordUndo(LCursor & cur, Undo::undo_kind kind)
248 recordUndo(kind, cur, cur.pit(), cur.pit());
252 void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
256 Buffer * buf = cur.bv().buffer();
257 doRecordUndo(kind, c, c.pit(), c.pit(), cur,
258 buf->params(), false, buf->undostack());
262 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
264 recordUndo(kind, cur, cur.selBegin().pit(), cur.selEnd().pit());
268 void recordUndo(LCursor & cur, Undo::undo_kind kind, pit_type from)
270 recordUndo(kind, cur, cur.pit(), from);
274 void recordUndo(LCursor & cur, Undo::undo_kind kind,
275 pit_type from, pit_type to)
277 recordUndo(kind, cur, from, to);
281 void recordUndoFullDocument(BufferView * bv)
283 Buffer * buf = bv->buffer();
286 doc_iterator_begin(buf->inset()),
287 0, buf->paragraphs().size() - 1,
293 undo_finished = false;