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 void doRecordUndo(Undo::undo_kind kind,
52 DocIterator const & cell,
53 pit_type first_pit, pit_type last_pit,
54 DocIterator const & cur,
55 BufferParams const & bparams,
57 limited_stack<Undo> & stack)
59 if (first_pit > last_pit)
60 std::swap(first_pit, last_pit);
61 // create the position information of the Undo entry
66 undo.bparams = bparams ;
67 undo.isFullBuffer = isFullBuffer;
68 //lyxerr << "recordUndo: cur: " << cur << endl;
69 //lyxerr << "recordUndo: pos: " << cur.pos() << endl;
70 //lyxerr << "recordUndo: cell: " << cell << endl;
71 undo.from = first_pit;
72 undo.end = cell.lastpit() - last_pit;
74 // Undo::ATOMIC are always recorded (no overlapping there).
75 // As nobody wants all removed character appear one by one when undoing,
76 // we want combine 'similar' non-ATOMIC undo recordings to one.
78 && kind != Undo::ATOMIC
80 && stack.top().cell == undo.cell
81 && stack.top().kind == undo.kind
82 && stack.top().from == undo.from
83 && stack.top().end == undo.end)
86 // fill in the real data to be saved
87 if (cell.inMathed()) {
88 // simply use the whole cell
89 undo.array = asString(cell.cell());
91 // some more effort needed here as 'the whole cell' of the
92 // main LyXText _is_ the whole document.
93 // record the relevant paragraphs
94 LyXText const * text = cell.text();
96 ParagraphList & plist = text->paragraphs();
97 ParagraphList::iterator first = plist.begin();
98 advance(first, first_pit);
99 ParagraphList::iterator last = plist.begin();
100 advance(last, last_pit + 1);
101 undo.pars = ParagraphList(first, last);
104 // push the undo entry to undo stack
105 //lyxerr << "undo record: " << stack.top() << std::endl;
108 // next time we'll try again to combine entries if possible
109 undo_finished = false;
113 void recordUndo(Undo::undo_kind kind,
114 LCursor & cur, pit_type first_pit, pit_type last_pit,
115 limited_stack<Undo> & stack)
117 BOOST_ASSERT(first_pit <= cur.lastpit());
118 BOOST_ASSERT(last_pit <= cur.lastpit());
120 doRecordUndo(kind, cur, first_pit, last_pit, cur,
121 cur.bv().buffer()->params(), false, stack);
126 // Returns false if no undo possible.
127 bool textUndoOrRedo(BufferView & bv,
128 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
137 // Adjust undo stack and get hold of current undo data.
138 Undo undo = stack.top();
141 // We will store in otherstack the part of the document under 'undo'
142 Buffer * buf = bv.buffer();
143 DocIterator cell_dit = undo.cell.asDocIterator(&buf->inset());
145 doRecordUndo(Undo::ATOMIC, cell_dit,
146 undo.from, cell_dit.lastpit() - undo.end, bv.cursor(),
147 undo.bparams, undo.isFullBuffer,
150 // This does the actual undo/redo.
151 //lyxerr << "undo, performing: " << undo << std::endl;
152 DocIterator dit = undo.cell.asDocIterator(&buf->inset());
153 if (undo.isFullBuffer) {
154 // This is a full document
155 otherstack.top().bparams = buf->params();
156 buf->params() = undo.bparams;
157 buf->paragraphs() = undo.pars;
158 } else if (dit.inMathed()) {
159 // We stored the full cell here as there is not much to be
160 // gained by storing just 'a few' paragraphs (most if not
161 // all math inset cells have just one paragraph!)
162 asArray(undo.array, dit.cell());
164 // Some finer machinery is needed here.
165 LyXText * text = dit.text();
167 ParagraphList & plist = text->paragraphs();
169 // remove new stuff between first and last
170 ParagraphList::iterator first = plist.begin();
171 advance(first, undo.from);
172 ParagraphList::iterator last = plist.begin();
173 advance(last, plist.size() - undo.end);
174 plist.erase(first, last);
176 // re-insert old stuff instead
177 first = plist.begin();
178 advance(first, undo.from);
180 // this ugly stuff is needed until we get rid of the
181 // inset_owner backpointer
182 ParagraphList::const_iterator pit = undo.pars.begin();
183 ParagraphList::const_iterator end = undo.pars.end();
184 for (; pit != end; ++pit)
185 const_cast<Paragraph &>(*pit).setInsetOwner(&dit.inset());
186 plist.insert(first, undo.pars.begin(), undo.pars.end());
190 LCursor & cur = bv.cursor();
191 cur.setCursor(undo.cursor.asDocIterator(&buf->inset()));
192 cur.selection() = false;
204 // Make sure the next operation will be stored.
205 undo_finished = true;
209 bool textUndo(BufferView & bv)
211 return textUndoOrRedo(bv, bv.buffer()->undostack(),
212 bv.buffer()->redostack());
216 bool textRedo(BufferView & bv)
218 return textUndoOrRedo(bv, bv.buffer()->redostack(),
219 bv.buffer()->undostack());
223 void recordUndo(Undo::undo_kind kind,
224 LCursor & cur, pit_type first, pit_type last)
226 Buffer * buf = cur.bv().buffer();
227 recordUndo(kind, cur, first, last, buf->undostack());
228 buf->redostack().clear();
229 //lyxerr << "undostack:\n";
230 //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
231 // lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
235 void recordUndo(LCursor & cur, Undo::undo_kind kind)
237 recordUndo(kind, cur, cur.pit(), cur.pit());
241 void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
249 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
251 recordUndo(kind, cur, cur.selBegin().pit(), cur.selEnd().pit());
255 void recordUndo(LCursor & cur, Undo::undo_kind kind, pit_type from)
257 recordUndo(kind, cur, cur.pit(), from);
261 void recordUndo(LCursor & cur, Undo::undo_kind kind,
262 pit_type from, pit_type to)
264 recordUndo(kind, cur, from, to);
268 void recordUndoFullDocument(BufferView * bv)
270 Buffer * buf = bv->buffer();
273 doc_iterator_begin(buf->inset()),
274 0, buf->paragraphs().size() - 1,
280 undo_finished = false;