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