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