]> git.lyx.org Git - lyx.git/blob - src/undo.C
cleanup after svn hang-up, #undef CursorShape. Should be compilable ganin now.
[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 #include "ParagraphList.h"
26
27 #include "mathed/MathSupport.h"
28 #include "insets/inset.h"
29
30 #include <algorithm>
31
32 using lyx::pit_type;
33
34 using std::advance;
35 using std::endl;
36
37
38 namespace {
39
40 /// The flag used by finishUndo().
41 bool undo_finished;
42
43
44 std::ostream & operator<<(std::ostream & os, Undo const & undo)
45 {
46         return os << " from: " << undo.from << " end: " << undo.end
47                 << " cell:\n" << undo.cell
48                 << " cursor:\n" << undo.cursor;
49 }
50
51
52 bool samePar(StableDocIterator const & i1, StableDocIterator const & i2)
53 {
54         StableDocIterator tmpi2 = i2;
55         tmpi2.pos() = i1.pos();
56         return i1 == tmpi2;
57 }
58
59
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,
65         bool isFullBuffer,
66         limited_stack<Undo> & stack)
67 {
68         if (first_pit > last_pit)
69                 std::swap(first_pit, last_pit);
70         // create the position information of the Undo entry
71         Undo undo;
72         undo.kind = kind;
73         undo.cell = cell;
74         undo.cursor = cur;
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;
82
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.
86         if (!undo_finished
87             && kind != Undo::ATOMIC
88             && !stack.empty()
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)
93                 return;
94
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());
99         } else {
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();
104                 BOOST_ASSERT(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);
111         }
112
113         // push the undo entry to undo stack
114         //lyxerr << "undo record: " << stack.top() << std::endl;
115         stack.push(undo);
116
117         // next time we'll try again to combine entries if possible
118         undo_finished = false;
119 }
120
121
122 void recordUndo(Undo::undo_kind kind,
123         LCursor & cur, pit_type first_pit, pit_type last_pit,
124         limited_stack<Undo> & stack)
125 {
126         BOOST_ASSERT(first_pit <= cur.lastpit());
127         BOOST_ASSERT(last_pit <= cur.lastpit());
128
129         doRecordUndo(kind, cur, first_pit, last_pit, cur,
130                 cur.bv().buffer()->params(), false, stack);
131 }
132
133
134
135 // Returns false if no undo possible.
136 bool textUndoOrRedo(BufferView & bv,
137         limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
138 {
139         finishUndo();
140
141         if (stack.empty()) {
142                 // Nothing to do.
143                 return false;
144         }
145
146         // Adjust undo stack and get hold of current undo data.
147         Undo undo = stack.top();
148         stack.pop();
149
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());
153
154         doRecordUndo(Undo::ATOMIC, cell_dit,
155                    undo.from, cell_dit.lastpit() - undo.end, bv.cursor(),
156                          undo.bparams, undo.isFullBuffer,
157                    otherstack);
158
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;
172                 asArray(undo.array, dit.cell());
173         } else {
174                 // Some finer machinery is needed here.
175                 LyXText * text = dit.text();
176                 BOOST_ASSERT(text);
177                 ParagraphList & plist = text->paragraphs();
178
179                 // remove new stuff between first and last
180                 ParagraphList::iterator first = plist.begin();
181                 advance(first, undo.from);
182                 ParagraphList::iterator last = plist.begin();
183                 advance(last, plist.size() - undo.end);
184                 plist.erase(first, last);
185
186                 // re-insert old stuff instead
187                 first = plist.begin();
188                 advance(first, undo.from);
189
190                 // this ugly stuff is needed until we get rid of the
191                 // inset_owner backpointer
192                 ParagraphList::iterator pit = undo.pars.begin();
193                 ParagraphList::iterator const end = undo.pars.end();
194                 for (; pit != end; ++pit)
195                         pit->setInsetOwner(dit.realInset());
196                 plist.insert(first, undo.pars.begin(), undo.pars.end());
197         }
198
199         // Set cursor
200         LCursor & cur = bv.cursor();
201         cur.setCursor(undo.cursor.asDocIterator(&buf->inset()));
202         cur.selection() = false;
203         cur.resetAnchor();
204         finishUndo();
205
206         return true;
207 }
208
209 } // namespace anon
210
211
212 void finishUndo()
213 {
214         // Make sure the next operation will be stored.
215         undo_finished = true;
216 }
217
218
219 bool textUndo(BufferView & bv)
220 {
221         return textUndoOrRedo(bv, bv.buffer()->undostack(),
222                               bv.buffer()->redostack());
223 }
224
225
226 bool textRedo(BufferView & bv)
227 {
228         return textUndoOrRedo(bv, bv.buffer()->redostack(),
229                               bv.buffer()->undostack());
230 }
231
232
233 void recordUndo(Undo::undo_kind kind,
234         LCursor & cur, pit_type first, pit_type last)
235 {
236         Buffer * buf = cur.bv().buffer();
237         recordUndo(kind, cur, first, last, buf->undostack());
238         buf->redostack().clear();
239         //lyxerr << "undostack:\n";
240         //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
241         //      lyxerr << "  " << i << ": " << buf->undostack()[i] << std::endl;
242 }
243
244
245 void recordUndo(LCursor & cur, Undo::undo_kind kind)
246 {
247         recordUndo(kind, cur, cur.pit(), cur.pit());
248 }
249
250
251 void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
252 {
253         LCursor c = cur;
254         c.pop();
255         Buffer * buf = cur.bv().buffer();
256         doRecordUndo(kind, c, c.pit(), c.pit(), cur,
257                      buf->params(), false, buf->undostack());
258 }
259
260
261 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
262 {
263         recordUndo(kind, cur, cur.selBegin().pit(), cur.selEnd().pit());
264 }
265
266
267 void recordUndo(LCursor & cur, Undo::undo_kind kind, pit_type from)
268 {
269         recordUndo(kind, cur, cur.pit(), from);
270 }
271
272
273 void recordUndo(LCursor & cur, Undo::undo_kind kind,
274         pit_type from, pit_type to)
275 {
276         recordUndo(kind, cur, from, to);
277 }
278
279
280 void recordUndoFullDocument(BufferView * bv)
281 {
282         Buffer * buf = bv->buffer();
283         doRecordUndo(
284                 Undo::ATOMIC,
285                 doc_iterator_begin(buf->inset()),
286                 0, buf->paragraphs().size() - 1,
287                 bv->cursor(),
288                 buf->params(),
289                 true,
290                 buf->undostack()
291         );
292         undo_finished = false;
293 }