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