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