]> git.lyx.org Git - lyx.git/blob - src/undo.C
the spellcheck cleanup
[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 "debug.h"
21 #include "BufferView.h"
22 #include "iterators.h"
23 #include "lyxtext.h"
24 #include "paragraph.h"
25
26 #include "insets/updatableinset.h" // for dynamic_cast<UpdatableInset *>
27
28 using lyx::paroffset_type;
29
30
31 /// The flag used by finishUndo().
32 bool undo_finished;
33
34 /// Whether actions are not added to the undo stacks.
35 bool undo_frozen;
36
37 Undo::Undo(undo_kind kind_, int text_, int index_,
38            int first_par_, int end_par_, int cursor_par_, int cursor_pos_)
39         :
40                 kind(kind_),
41                 text(text_),
42                 index(index_),
43                 first_par(first_par_),
44                 end_par(end_par_),
45                 cursor_par(cursor_par_),
46                 cursor_pos(cursor_pos_)
47 {}
48
49
50 namespace {
51
52 std::ostream & operator<<(std::ostream & os, Undo const & undo)
53 {
54         return os << " text: " << undo.text
55                 << " index: " << undo.index
56                 << " first: " << undo.first_par
57                 << " from end: " << undo.end_par
58                 << " cursor: " << undo.cursor_par
59                 << "/" << undo.cursor_pos;
60 }
61
62
63 // translates LyXText pointer into offset count from document begin
64 ParIterator text2pit(LyXText * text, int & tcount)
65 {
66         tcount = 0;
67         Buffer * buf = text->bv()->buffer();
68         ParIterator pit = buf->par_iterator_begin();
69         ParIterator end = buf->par_iterator_end();
70
71         // it.text() returns 0 for outermost text.
72         if (text == text->bv()->text)
73                 return pit;
74
75         for ( ; pit != end; ++pit, ++tcount)
76                 if (pit.text() == text)
77                         return pit;
78         lyxerr << "undo: should not happen" << std::endl;
79         return end;
80 }
81
82
83 // translates offset from buffer begin to ParIterator
84 ParIterator num2pit(BufferView * bv, int num)
85 {
86         Buffer * buf = bv->buffer();
87         ParIterator pit = buf->par_iterator_begin();
88         ParIterator end = buf->par_iterator_end();
89
90         for ( ; num && pit != end; ++pit, --num)
91                 ;
92
93         if (pit != end)
94                 return pit;
95
96         // don't crash early...
97         lyxerr << "undo: num2pit: num: " << num << std::endl;
98         BOOST_ASSERT(false);
99         return buf->par_iterator_begin();
100 }
101
102
103 // translates offset from buffer begin to LyXText
104 LyXText * pit2text(BufferView * bv, ParIterator const & pit)
105 {
106         LyXText * text = pit.text();
107         return text ? text : bv->text;
108 }
109
110
111 void recordUndo(Undo::undo_kind kind,
112         LyXText * text, paroffset_type first_par, paroffset_type last_par,
113         limited_stack<Undo> & stack)
114 {
115         Buffer * buf = text->bv()->buffer();
116
117         int const end_par = text->ownerParagraphs().size() - last_par;
118
119         // Undo::ATOMIC are always recorded (no overlapping there).
120         // overlapping only with insert and delete inside one paragraph:
121         // nobody wants all removed character appear one by one when undoing.
122         if (!undo_finished && kind != Undo::ATOMIC) {
123                 // Check whether storing is needed.
124                 if (!buf->undostack().empty()
125                     && buf->undostack().top().kind == kind
126                     && buf->undostack().top().first_par == first_par
127                     && buf->undostack().top().end_par == end_par) {
128                         // No additonal undo recording needed -
129                         // effectively, we combine undo recordings to one.
130                         return;
131                 }
132         }
133
134         // make and push the Undo entry
135         int textnum;
136         ParIterator pit = text2pit(text, textnum);
137         stack.push(Undo(kind, textnum, pit.index(),
138                 first_par, end_par, text->cursor.par(), text->cursor.pos()));
139         lyxerr << "undo record: " << stack.top() << std::endl;
140
141         // record the relevant paragraphs
142         ParagraphList & undo_pars = stack.top().pars;
143
144         ParagraphList & plist = text->ownerParagraphs();
145         ParagraphList::iterator first = plist.begin();
146         advance(first, first_par);
147         ParagraphList::iterator last = plist.begin();
148         advance(last, last_par);
149
150         for (ParagraphList::iterator it = first; it != last; ++it)
151                 undo_pars.push_back(*it);
152         undo_pars.push_back(*last);
153
154         // and make sure that next time, we should be combining if possible
155         undo_finished = false;
156 }
157
158
159 // returns false if no undo possible
160 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
161 {
162         lyxerr << "undo, performing: " << undo << std::endl;
163         ParIterator pit = num2pit(bv, undo.text);
164         LyXText * text = pit2text(bv, pit);
165         ParagraphList & plist = text->ownerParagraphs();
166
167         // remove new stuff between first and last
168         {
169                 ParagraphList::iterator first = plist.begin();
170                 advance(first, undo.first_par);
171                 ParagraphList::iterator last = plist.begin();
172                 advance(last, plist.size() - undo.end_par);
173                 plist.erase(first, ++last);
174         }
175
176         // re-insert old stuff instead
177         if (plist.empty()) {
178                 plist.assign(undo.pars.begin(), undo.pars.end());
179         } else {
180                 ParagraphList::iterator first = plist.begin();
181                 advance(first, undo.first_par);
182                 plist.insert(first, undo.pars.begin(), undo.pars.end());
183         }
184
185         // set cursor
186         lyxerr <<   "undo, text: " << undo.text
187                << " inset: " << pit.inset()
188                << " index: " << undo.index
189                << " par: " << undo.cursor_par
190                << " pos: " << undo.cursor_pos
191                << std::endl;
192
193         // set cursor again to force the position to be the right one
194         text->cursor.par(undo.cursor_par);
195         text->cursor.pos(undo.cursor_pos);
196
197         // clear any selection
198         text->clearSelection();
199         text->selection.cursor = text->cursor;
200         text->updateCounters();
201
202         // rebreak the entire lyxtext
203         bv->text->fullRebreak();
204
205         pit.lockPath(bv);
206         
207         finishUndo();
208         return true;
209 }
210
211
212 // returns false if no undo possible
213 bool textUndoOrRedo(BufferView * bv,
214         limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
215 {
216         if (stack.empty()) {
217                 // nothing to do
218                 freezeUndo();
219                 bv->unlockInset(bv->theLockingInset());
220                 finishUndo();
221                 unFreezeUndo();
222                 return false;
223         }
224
225         Undo undo = stack.top();
226         stack.pop();
227         finishUndo();
228
229         if (!undo_frozen) {
230                 otherstack.push(undo);
231                 otherstack.top().pars.clear();
232                 ParIterator pit = num2pit(bv, undo.text);
233                 ParagraphList & plist = pit.plist();
234                 if (undo.first_par + undo.end_par <= int(plist.size())) {
235                         ParagraphList::iterator first = plist.begin();
236                         advance(first, undo.first_par);
237                         ParagraphList::iterator last = plist.begin();
238                         advance(last, plist.size() - undo.end_par + 1);
239                         otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
240                 }
241                 LyXText * text = pit2text(bv, pit);
242                 otherstack.top().cursor_pos = text->cursor.pos();
243                 otherstack.top().cursor_par = text->cursor.par();
244                 lyxerr << " undo other: " << otherstack.top() << std::endl;
245         }
246
247         // Now we can unlock the inset for safety because the inset
248         // pointer could be changed during the undo-function. Anyway
249         // if needed we have to lock the right inset/position if this
250         // is requested.
251         freezeUndo();
252         bv->unlockInset(bv->theLockingInset());
253         bool const ret = performUndoOrRedo(bv, undo);
254         unFreezeUndo();
255         return ret;
256 }
257
258 } // namespace anon
259
260
261 void freezeUndo()
262 {
263         // this is dangerous and for internal use only
264         undo_frozen = true;
265 }
266
267
268 void unFreezeUndo()
269 {
270         // this is dangerous and for internal use only
271         undo_frozen = false;
272 }
273
274
275 void finishUndo()
276 {
277         // makes sure the next operation will be stored
278         undo_finished = true;
279 }
280
281
282 bool textUndo(BufferView * bv)
283 {
284         return textUndoOrRedo(bv, bv->buffer()->undostack(),
285                               bv->buffer()->redostack());
286 }
287
288
289 bool textRedo(BufferView * bv)
290 {
291         return textUndoOrRedo(bv, bv->buffer()->redostack(),
292                               bv->buffer()->undostack());
293 }
294
295
296 void recordUndo(Undo::undo_kind kind,
297         LyXText const * text, paroffset_type first, paroffset_type last)
298 {
299         if (undo_frozen)
300                 return;
301         Buffer * buf = text->bv()->buffer();
302         recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
303         buf->redostack().clear();
304 }
305
306
307 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
308 {
309         recordUndo(kind, text, par, par);
310 }
311
312
313 void recordUndo(BufferView * bv, Undo::undo_kind kind)
314 {
315         recordUndo(kind, bv->text, bv->text->cursor.par());
316 }