]> git.lyx.org Git - lyx.git/blob - src/undo.C
9d028f03c7b18bf0d0764d5d2b23f7c44e481c8f
[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), 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 = undo.cursor.asDocumentIterator(bv);
159                 if (dit.inMathed()) {
160                         // not much to be done
161                 } else {
162                         otherstack.top().pars.clear();
163                         LyXText * text = dit.text();
164                         BOOST_ASSERT(text);
165                         ParagraphList & plist = text->paragraphs();
166                         if (undo.from + undo.end <= int(plist.size())) {
167                                 ParagraphList::iterator first = plist.begin();
168                                 advance(first, undo.from);
169                                 ParagraphList::iterator last = plist.begin();
170                                 advance(last, plist.size() - undo.end);
171                                 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
172                         }
173                 }
174                 otherstack.top().cursor = bv.cursor();
175                 //lyxerr << " undo other: " << otherstack.top() << std::endl;
176         }
177
178         undo_frozen = true;
179         performUndoOrRedo(bv, undo);
180         undo_frozen = false;
181         return true;
182 }
183
184 } // namespace anon
185
186
187 void finishUndo()
188 {
189         // makes sure the next operation will be stored
190         undo_finished = true;
191 }
192
193
194 bool textUndo(BufferView & bv)
195 {
196         return textUndoOrRedo(bv, bv.buffer()->undostack(),
197                               bv.buffer()->redostack());
198 }
199
200
201 bool textRedo(BufferView & bv)
202 {
203         return textUndoOrRedo(bv, bv.buffer()->redostack(),
204                               bv.buffer()->undostack());
205 }
206
207
208 void recordUndo(Undo::undo_kind kind,
209         LCursor & cur, paroffset_type first, paroffset_type last)
210 {
211         if (undo_frozen)
212                 return;
213         Buffer * buf = cur.bv().buffer();
214         recordUndo(kind, cur, first, last, buf->undostack());
215         buf->redostack().clear();
216         //lyxerr << "undostack:\n";
217         //for (size_t i = 0, n = buf->undostack().size(); i != n && i < 6; ++i)
218         //      lyxerr << "  " << i << ": " << buf->undostack()[i] << std::endl;
219 }
220
221
222 void recordUndo(LCursor & cur, Undo::undo_kind kind)
223 {
224         recordUndo(kind, cur, cur.par(), cur.par());
225 }
226
227
228 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind)
229 {
230         recordUndo(kind, cur, cur.selBegin().par(), cur.selEnd().par());
231 }
232
233
234 void recordUndo(LCursor & cur, Undo::undo_kind kind, paroffset_type from)
235 {
236         recordUndo(kind, cur, cur.par(), from);
237 }
238
239
240 void recordUndo(LCursor & cur, Undo::undo_kind kind,
241         paroffset_type from, paroffset_type to)
242 {
243         recordUndo(kind, cur, from, to);
244 }
245
246
247 void recordUndoFullDocument(LCursor &)
248 {
249         //recordUndo(Undo::ATOMIC,
250         //      cur, 0, cur.bv().text()->paragraphs().size() - 1);
251 }