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