]> git.lyx.org Git - lyx.git/blob - src/undo.C
partial fix for bug 622, cosmetic rest remains open
[lyx.git] / src / undo.C
1 /**
2  * \file undo.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author André Pönitz
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "undo.h"
18
19 #include "buffer.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "BufferView.h"
23 #include "lyxtext.h"
24 #include "paragraph.h"
25
26 #include "mathed/math_support.h"
27 #include "insets/updatableinset.h"
28
29 #include <algorithm>
30
31 using lyx::pit_type;
32
33 using std::advance;
34 using std::endl;
35
36
37 namespace {
38
39 /// The flag used by finishUndo().
40 bool undo_finished;
41
42
43 std::ostream & operator<<(std::ostream & os, Undo const & undo)
44 {
45         return os << " from: " << undo.from << " end: " << undo.end
46                 << " cell:\n" << undo.cell
47                 << " cursor:\n" << undo.cursor;
48 }
49
50
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,
56         bool isFullBuffer,
57         limited_stack<Undo> & stack)
58 {
59         if (first_pit > last_pit)
60                 std::swap(first_pit, last_pit);
61         // create the position information of the Undo entry
62         Undo undo;
63         undo.kind = kind;
64         undo.cell = cell;
65         undo.cursor = cur;
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;
73
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.
77         if (!undo_finished
78             && kind != Undo::ATOMIC
79             && !stack.empty()
80             && stack.top().cell == undo.cell
81                   && stack.top().kind == undo.kind
82                   && stack.top().from == undo.from
83                   && stack.top().end == undo.end)
84                 return;
85
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());
90         } else {
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();
95                 BOOST_ASSERT(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);
102         }
103
104         // push the undo entry to undo stack
105         //lyxerr << "undo record: " << stack.top() << std::endl;
106         stack.push(undo);
107
108         // next time we'll try again to combine entries if possible
109         undo_finished = false;
110 }
111
112
113 void recordUndo(Undo::undo_kind kind,
114         LCursor & cur, pit_type first_pit, pit_type last_pit,
115         limited_stack<Undo> & stack)
116 {
117         BOOST_ASSERT(first_pit <= cur.lastpit());
118         BOOST_ASSERT(last_pit <= cur.lastpit());
119
120         doRecordUndo(kind, cur, first_pit, last_pit, cur,
121                 cur.bv().buffer()->params(), false, stack);
122 }
123
124
125
126 // Returns false if no undo possible.
127 bool textUndoOrRedo(BufferView & bv,
128         limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
129 {
130         finishUndo();
131
132         if (stack.empty()) {
133                 // Nothing to do.
134                 return false;
135         }
136
137         // Adjust undo stack and get hold of current undo data.
138         Undo undo = stack.top();
139         stack.pop();
140
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());
144
145         doRecordUndo(Undo::ATOMIC, cell_dit,
146                    undo.from, cell_dit.lastpit() - undo.end, bv.cursor(),
147                          undo.bparams, undo.isFullBuffer,
148                    otherstack);
149
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());
163         } else {
164                 // Some finer machinery is needed here.
165                 LyXText * text = dit.text();
166                 BOOST_ASSERT(text);
167                 ParagraphList & plist = text->paragraphs();
168
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);
175
176                 // re-insert old stuff instead
177                 first = plist.begin();
178                 advance(first, undo.from);
179
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(
186                                 dynamic_cast<UpdatableInset *>(&dit.inset()));
187                 plist.insert(first, undo.pars.begin(), undo.pars.end());
188         }
189
190         // Set cursor
191         LCursor & cur = bv.cursor();
192         cur.setCursor(undo.cursor.asDocIterator(&buf->inset()));
193         cur.selection() = false;
194         cur.resetAnchor();
195         finishUndo();
196
197         return true;
198 }
199
200 } // namespace anon
201
202
203 void finishUndo()
204 {
205         // Make sure the next operation will be stored.
206         undo_finished = true;
207 }
208
209
210 bool textUndo(BufferView & bv)
211 {
212         return textUndoOrRedo(bv, bv.buffer()->undostack(),
213                               bv.buffer()->redostack());
214 }
215
216
217 bool textRedo(BufferView & bv)
218 {
219         return textUndoOrRedo(bv, bv.buffer()->redostack(),
220                               bv.buffer()->undostack());
221 }
222
223
224 void recordUndo(Undo::undo_kind kind,
225         LCursor & cur, pit_type first, pit_type last)
226 {
227         Buffer * buf = cur.bv().buffer();
228         recordUndo(kind, cur, first, last, buf->undostack());
229         buf->redostack().clear();
230         //lyxerr << "undostack:\n";
231         //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
232         //      lyxerr << "  " << i << ": " << buf->undostack()[i] << std::endl;
233 }
234
235
236 void recordUndo(LCursor & cur, Undo::undo_kind kind)
237 {
238         recordUndo(kind, cur, cur.pit(), cur.pit());
239 }
240
241
242 void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
243 {
244         LCursor c = cur;
245         c.pop();
246         recordUndo(c, kind);
247 }
248
249
250 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
251 {
252         recordUndo(kind, cur, cur.selBegin().pit(), cur.selEnd().pit());
253 }
254
255
256 void recordUndo(LCursor & cur, Undo::undo_kind kind, pit_type from)
257 {
258         recordUndo(kind, cur, cur.pit(), from);
259 }
260
261
262 void recordUndo(LCursor & cur, Undo::undo_kind kind,
263         pit_type from, pit_type to)
264 {
265         recordUndo(kind, cur, from, to);
266 }
267
268
269 void recordUndoFullDocument(BufferView * bv)
270 {
271         Buffer * buf = bv->buffer();
272         doRecordUndo(
273                 Undo::ATOMIC,
274                 doc_iterator_begin(buf->inset()),
275                 0, buf->paragraphs().size() - 1,
276                 bv->cursor(),
277                 buf->params(),
278                 true,
279                 buf->undostack()
280         );
281         undo_finished = false;
282 }