]> git.lyx.org Git - lyx.git/blob - src/undo.C
08431d9ee2c395eeec516278d054e5eb0ed015ee
[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
28 #include <algorithm>
29
30 using lyx::par_type;
31
32 using std::advance;
33 using std::endl;
34
35
36 namespace {
37
38 /// The flag used by finishUndo().
39 bool undo_finished;
40
41
42 std::ostream & operator<<(std::ostream & os, Undo const & undo)
43 {
44         return os << " from: " << undo.from << " end: " << undo.end
45                 << " cursor:\n" << undo.cursor;
46 }
47
48
49 void recordUndo(Undo::undo_kind kind,
50         LCursor & cur, par_type first_par, par_type last_par,
51         limited_stack<Undo> & stack)
52 {
53         BOOST_ASSERT(first_par <= cur.lastpar());
54         BOOST_ASSERT(last_par <= cur.lastpar());
55
56         if (first_par > last_par)
57                 std::swap(first_par, last_par);
58
59         // create the position information of the Undo entry
60         Undo undo;
61         undo.kind = kind;
62         undo.cursor = StableDocIterator(cur);
63         lyxerr << "recordUndo: cur: " << cur << endl;
64         lyxerr << "recordUndo: undo.cursor: " << undo.cursor << endl;
65         undo.from = first_par;
66         undo.end = cur.lastpar() - last_par;
67
68         // Undo::ATOMIC are always recorded (no overlapping there).
69         // As nobody wants all removed character appear one by one when undoing,
70         // we want combine 'similar' non-ATOMIC undo recordings to one.
71         if (!undo_finished
72             && kind != Undo::ATOMIC
73             && !stack.empty()
74             && stack.top().cursor.size() == undo.cursor.size()
75                   && stack.top().kind == undo.kind
76                   && stack.top().from == undo.from
77                   && stack.top().end == undo.end)
78                 return;
79
80         // fill in the real data to be saved
81         if (cur.inMathed()) {
82                 // simply use the whole cell
83                 undo.array = asString(cur.cell());
84         } else {
85                 // some more effort needed here as 'the whole cell' of the
86                 // main LyXText _is_ the whole document.
87                 // record the relevant paragraphs
88                 LyXText * text = cur.text();
89                 BOOST_ASSERT(text);
90                 ParagraphList & plist = text->paragraphs();
91                 ParagraphList::iterator first = plist.begin();
92                 advance(first, first_par);
93                 ParagraphList::iterator last = plist.begin();
94                 advance(last, last_par + 1);
95                 undo.pars = ParagraphList(first, last);
96         }
97
98         // push the undo entry to undo stack
99         //lyxerr << "undo record: " << stack.top() << std::endl;
100         stack.push(undo);
101
102         // next time we'll try again to combine entries if possible
103         undo_finished = false;
104 }
105
106
107 void performUndoOrRedo(BufferView & bv, Undo const & undo)
108 {
109         LCursor & cur = bv.cursor();
110         lyxerr << "undo, performing: " << undo << std::endl;
111         cur.setCursor(undo.cursor.asDocIterator(&bv.buffer()->inset()));
112         cur.selection() = false;
113
114         if (cur.inMathed()) {
115                 // We stored the full cell here as there is not much to be
116                 // gained by storing just 'a few' paragraphs (most if not
117                 // all math inset cells have just one paragraph!)
118                 asArray(undo.array, cur.cell());
119         } else {
120                 // Some finer machinery is needed here.
121                 LyXText * text = cur.text();
122                 BOOST_ASSERT(text);
123                 ParagraphList & plist = text->paragraphs();
124
125                 // remove new stuff between first and last
126                 ParagraphList::iterator first = plist.begin();
127                 advance(first, undo.from);
128                 ParagraphList::iterator last = plist.begin();
129                 advance(last, plist.size() - undo.end);
130                 plist.erase(first, last);
131
132                 // re-insert old stuff instead
133                 first = plist.begin();
134                 advance(first, undo.from);
135                 plist.insert(first, undo.pars.begin(), undo.pars.end());
136         }
137
138         cur.resetAnchor();
139         finishUndo();
140 }
141
142
143 // Returns false if no undo possible.
144 bool textUndoOrRedo(BufferView & bv,
145         limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
146 {
147         if (stack.empty()) {
148                 // Nothing to do.
149                 finishUndo();
150                 return false;
151         }
152
153         Undo undo = stack.top();
154         stack.pop();
155         finishUndo();
156
157         // This implements redo
158         otherstack.push(undo);
159         // FIXME: This triggers the error in dociterator.C +454
160         // could the reason be that we rebuild the dit _before_ the
161         // contents is actually 'undone'?
162         // I.e. state now is 'A', state on undo stack is 'B'.
163         // dit is created according to positions from undo.cursor, i.e. B
164         // but current buffer contents is more likely 'A'.
165         DocIterator dit = undo.cursor.asDocIterator(&bv.buffer()->inset());
166         if (dit.inMathed()) {
167                 // Easy way out: store a full cell.
168                 otherstack.top().array = asString(dit.cell());
169         } else {
170                 // As cells might be too large in texted, store just a part
171                 // of the paragraph list.
172                 otherstack.top().pars.clear();
173                 LyXText * text = dit.text();
174                 BOOST_ASSERT(text);
175                 ParagraphList & plist = text->paragraphs();
176                 if (undo.from + undo.end <= int(plist.size())) {
177                         ParagraphList::iterator first = plist.begin();
178                         advance(first, undo.from);
179                         ParagraphList::iterator last = plist.begin();
180                         advance(last, plist.size() - undo.end);
181                         otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
182                 }
183         }
184         otherstack.top().cursor = bv.cursor();
185         //lyxerr << " undo other: " << otherstack.top() << std::endl;
186
187         performUndoOrRedo(bv, undo);
188         return true;
189 }
190
191 } // namespace anon
192
193
194 void finishUndo()
195 {
196         // Make sure the next operation will be stored.
197         undo_finished = true;
198 }
199
200
201 bool textUndo(BufferView & bv)
202 {
203         return textUndoOrRedo(bv, bv.buffer()->undostack(),
204                               bv.buffer()->redostack());
205 }
206
207
208 bool textRedo(BufferView & bv)
209 {
210         return textUndoOrRedo(bv, bv.buffer()->redostack(),
211                               bv.buffer()->undostack());
212 }
213
214
215 void recordUndo(Undo::undo_kind kind,
216         LCursor & cur, par_type first, par_type last)
217 {
218         Buffer * buf = cur.bv().buffer();
219         recordUndo(kind, cur, first, last, buf->undostack());
220         buf->redostack().clear();
221         lyxerr << "undostack:\n";
222         for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
223                 lyxerr << "  " << i << ": " << buf->undostack()[i] << std::endl;
224 }
225
226
227 void recordUndo(LCursor & cur, Undo::undo_kind kind)
228 {
229         recordUndo(kind, cur, cur.par(), cur.par());
230 }
231
232
233 void recordUndoInset(LCursor & cur, Undo::undo_kind kind)
234 {
235         LCursor c = cur;
236         c.pop();
237         recordUndo(c, kind);
238 }
239
240
241 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
242 {
243         recordUndo(kind, cur, cur.selBegin().par(), cur.selEnd().par());
244 }
245
246
247 void recordUndo(LCursor & cur, Undo::undo_kind kind, par_type from)
248 {
249         recordUndo(kind, cur, cur.par(), from);
250 }
251
252
253 void recordUndo(LCursor & cur, Undo::undo_kind kind,
254         par_type from, par_type to)
255 {
256         recordUndo(kind, cur, from, to);
257 }
258
259
260 void recordUndoFullDocument(LCursor &)
261 {
262         //recordUndo(Undo::ATOMIC,
263         //      cur, 0, cur.bv().text()->paragraphs().size() - 1);
264 }