]> git.lyx.org Git - lyx.git/blob - src/undo.C
7f38077479d3fcf52dac7d318173c2a9ba7ef951
[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.back().id(it->id());
153         }
154         undo_pars.push_back(*last);
155         undo_pars.back().id(last->id());
156
157         // and make sure that next time, we should be combining if possible
158         undo_finished = false;
159 }
160
161
162 // returns false if no undo possible
163 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
164 {
165         lyxerr << "undo, performing: " << undo << std::endl;
166         ParIterator pit = num2pit(bv, undo.text);
167         LyXText * text = pit2text(bv, pit);
168         ParagraphList & plist = text->ownerParagraphs();
169
170         // remove new stuff between first and last
171         {
172                 ParagraphList::iterator first = plist.begin();
173                 advance(first, undo.first_par);
174                 ParagraphList::iterator last = plist.begin();
175                 advance(last, plist.size() - undo.end_par);
176                 plist.erase(first, ++last);
177         }
178
179         // re-insert old stuff instead
180         if (plist.empty()) {
181                 plist.assign(undo.pars.begin(), undo.pars.end());
182         } else {
183                 ParagraphList::iterator first = plist.begin();
184                 advance(first, undo.first_par);
185                 plist.insert(first, undo.pars.begin(), undo.pars.end());
186         }
187
188         // set cursor
189         lyxerr <<   "undo, text: " << undo.text
190                 << " inset: " << pit.inset()
191                 << " index: " << undo.index
192                 << std::endl;
193
194         // set cursor again to force the position to be the right one
195         text->cursor.par(undo.cursor_par);
196         text->cursor.pos(undo.cursor_pos);
197
198         // clear any selection
199         text->clearSelection();
200         text->selection.cursor = text->cursor;
201         text->updateCounters();
202
203         // rebreak the entire lyxtext
204         bv->text->fullRebreak();
205
206         InsetOld * inset = pit.inset();
207         if (inset) {
208                 // magic needed to cope with inset locking
209                 bv->lockInset(dynamic_cast<UpdatableInset *>(inset));
210         }
211
212         finishUndo();
213         return true;
214 }
215
216
217 // returns false if no undo possible
218 bool textUndoOrRedo(BufferView * bv,
219         limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
220 {
221         if (stack.empty()) {
222                 // nothing to do
223                 freezeUndo();
224                 bv->unlockInset(bv->theLockingInset());
225                 finishUndo();
226                 unFreezeUndo();
227                 return false;
228         }
229
230         Undo undo = stack.top();
231         stack.pop();
232         finishUndo();
233
234         if (!undo_frozen) {
235                 otherstack.push(undo);
236                 otherstack.top().pars.clear();
237                 ParIterator pit = num2pit(bv, undo.text);
238                 ParagraphList & plist = pit.plist();
239                 if (undo.first_par + undo.end_par <= int(plist.size())) {
240                         ParagraphList::iterator first = plist.begin();
241                         advance(first, undo.first_par);
242                         ParagraphList::iterator last = plist.begin();
243                         advance(last, plist.size() - undo.end_par + 1);
244                         otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
245                 }
246                 LyXText * text = pit2text(bv, pit);
247                 otherstack.top().cursor_pos = text->cursor.pos();
248                 otherstack.top().cursor_par = text->cursor.par();
249                 lyxerr << " undo other: " << otherstack.top() << std::endl;
250         }
251
252         // Now we can unlock the inset for safety because the inset
253         // pointer could be changed during the undo-function. Anyway
254         // if needed we have to lock the right inset/position if this
255         // is requested.
256         freezeUndo();
257         bv->unlockInset(bv->theLockingInset());
258         bool const ret = performUndoOrRedo(bv, undo);
259         unFreezeUndo();
260         return ret;
261 }
262
263 } // namespace anon
264
265
266 void freezeUndo()
267 {
268         // this is dangerous and for internal use only
269         undo_frozen = true;
270 }
271
272
273 void unFreezeUndo()
274 {
275         // this is dangerous and for internal use only
276         undo_frozen = false;
277 }
278
279
280 void finishUndo()
281 {
282         // makes sure the next operation will be stored
283         undo_finished = true;
284 }
285
286
287 bool textUndo(BufferView * bv)
288 {
289         return textUndoOrRedo(bv, bv->buffer()->undostack(),
290                               bv->buffer()->redostack());
291 }
292
293
294 bool textRedo(BufferView * bv)
295 {
296         return textUndoOrRedo(bv, bv->buffer()->redostack(),
297                               bv->buffer()->undostack());
298 }
299
300
301 void recordUndo(Undo::undo_kind kind,
302         LyXText const * text, paroffset_type first, paroffset_type last)
303 {
304         if (undo_frozen)
305                 return;
306         Buffer * buf = text->bv()->buffer();
307         recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
308         buf->redostack().clear();
309 }
310
311
312 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
313 {
314         recordUndo(kind, text, par, par);
315 }
316
317
318 void recordUndo(BufferView * bv, Undo::undo_kind kind)
319 {
320         recordUndo(kind, bv->text, bv->text->cursor.par());
321 }