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