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/updatableinset.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 recordUndo(Undo::undo_kind kind,
53 pit_type first_pit, pit_type last_pit,
55 limited_stack<Undo> & stack)
57 if (first_pit > last_pit)
58 std::swap(first_pit, last_pit);
59 // create the position information of the Undo entry
64 lyxerr << "recordUndo: cur: " << cur << endl;
65 lyxerr << "recordUndo: pos: " << cur.pos() << endl;
66 //lyxerr << "recordUndo: cell: " << cell << endl;
67 undo.from = first_pit;
68 undo.end = cell.lastpit() - last_pit;
70 // Undo::ATOMIC are always recorded (no overlapping there).
71 // As nobody wants all removed character appear one by one when undoing,
72 // we want combine 'similar' non-ATOMIC undo recordings to one.
74 && kind != Undo::ATOMIC
76 && stack.top().cell.size() == undo.cell.size()
77 && stack.top().kind == undo.kind
78 && stack.top().from == undo.from
79 && stack.top().end == undo.end)
82 // fill in the real data to be saved
83 if (cell.inMathed()) {
84 // simply use the whole cell
85 undo.array = asString(cell.cell());
87 // some more effort needed here as 'the whole cell' of the
88 // main LyXText _is_ the whole document.
89 // record the relevant paragraphs
90 LyXText * text = cell.text();
92 ParagraphList & plist = text->paragraphs();
93 ParagraphList::iterator first = plist.begin();
94 advance(first, first_pit);
95 ParagraphList::iterator last = plist.begin();
96 advance(last, last_pit + 1);
97 undo.pars = ParagraphList(first, last);
100 // push the undo entry to undo stack
101 //lyxerr << "undo record: " << stack.top() << std::endl;
104 // next time we'll try again to combine entries if possible
105 undo_finished = false;
109 void recordUndo(Undo::undo_kind kind,
110 LCursor & cur, pit_type first_pit, pit_type last_pit,
111 limited_stack<Undo> & stack)
113 BOOST_ASSERT(first_pit <= cur.lastpit());
114 BOOST_ASSERT(last_pit <= cur.lastpit());
116 recordUndo(kind, cur, first_pit, last_pit, cur, stack);
120 void performUndoOrRedo(BufferView & bv, Undo const & undo)
122 //lyxerr << "undo, performing: " << undo << std::endl;
123 DocIterator dit = undo.cell.asDocIterator(&bv.buffer()->inset());
124 if (dit.inMathed()) {
125 // We stored the full cell here as there is not much to be
126 // gained by storing just 'a few' paragraphs (most if not
127 // all math inset cells have just one paragraph!)
128 asArray(undo.array, dit.cell());
130 // Some finer machinery is needed here.
131 LyXText * text = dit.text();
133 ParagraphList & plist = text->paragraphs();
135 // remove new stuff between first and last
136 ParagraphList::iterator first = plist.begin();
137 advance(first, undo.from);
138 ParagraphList::iterator last = plist.begin();
139 advance(last, plist.size() - undo.end);
140 plist.erase(first, last);
142 // re-insert old stuff instead
143 first = plist.begin();
144 advance(first, undo.from);
146 // this ugly stuff is needed until we get rid of the
147 // inset_owner backpointer
148 ParagraphList::const_iterator pit = undo.pars.begin();
149 ParagraphList::const_iterator end = undo.pars.end();
150 for (; pit != end; ++pit)
151 const_cast<Paragraph &>(*pit).setInsetOwner(dynamic_cast<UpdatableInset *>(&dit.inset()));
152 plist.insert(first, undo.pars.begin(), undo.pars.end());
155 LCursor & cur = bv.cursor();
156 cur.setCursor(undo.cursor.asDocIterator(&bv.buffer()->inset()));
157 cur.selection() = false;
163 // Returns false if no undo possible.
164 bool textUndoOrRedo(BufferView & bv,
165 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
174 // Adjust undo stack and get hold of current undo data.
175 Undo undo = stack.top();
178 // We will store in otherstack the part of the document under 'undo'
179 DocIterator cell_dit = undo.cell.asDocIterator(&bv.buffer()->inset());
181 recordUndo(Undo::ATOMIC, cell_dit,
182 undo.from, cell_dit.lastpit() - undo.end,
183 bv.cursor(), otherstack);
185 // This does the actual undo/redo.
186 performUndoOrRedo(bv, undo);
195 // Make sure the next operation will be stored.
196 undo_finished = true;
200 bool textUndo(BufferView & bv)
202 return textUndoOrRedo(bv, bv.buffer()->undostack(),
203 bv.buffer()->redostack());
207 bool textRedo(BufferView & bv)
209 return textUndoOrRedo(bv, bv.buffer()->redostack(),
210 bv.buffer()->undostack());
214 void recordUndo(Undo::undo_kind kind,
215 LCursor & cur, pit_type first, pit_type last)
217 Buffer * buf = cur.bv().buffer();
218 recordUndo(kind, cur, first, last, buf->undostack());
219 buf->redostack().clear();
220 //lyxerr << "undostack:\n";
221 //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
222 // lyxerr << " " << i << ": " << buf->undostack()[i] << std::endl;
226 void recordUndo(LCursor & cur, Undo::undo_kind kind)
228 recordUndo(kind, cur, cur.pit(), cur.pit());
232 void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
240 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
242 recordUndo(kind, cur, cur.selBegin().pit(), cur.selEnd().pit());
246 void recordUndo(LCursor & cur, Undo::undo_kind kind, pit_type from)
248 recordUndo(kind, cur, cur.pit(), from);
252 void recordUndo(LCursor & cur, Undo::undo_kind kind,
253 pit_type from, pit_type to)
255 recordUndo(kind, cur, from, to);
259 void recordUndoFullDocument(LCursor & cur)
261 recordUndo(Undo::ATOMIC, cur, 0,
262 cur.bv().text()->paragraphs().size() - 1);