]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
the latest undo patch without the paragraphlist stuff
[lyx.git] / src / undo_funcs.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995-2001 The LyX Team.
7  *
8  * ====================================================== */
9
10 #include <config.h>
11
12 #include "undo_funcs.h"
13 #include "lyxtext.h"
14 #include "funcrequest.h"
15 #include "BufferView.h"
16 #include "buffer.h"
17 #include "insets/updatableinset.h"
18 #include "insets/insettext.h"
19 #include "debug.h"
20 #include "support/LAssert.h"
21 #include "iterators.h"
22
23
24 /// The flag used by FinishUndo().
25 bool undo_finished;
26 /// Whether actions are not added to the undo stacks.
27 bool undo_frozen;
28
29 namespace {
30
31
32 /**
33  * Finish the undo operation in the case there was no entry
34  * on the stack to perform.
35  */
36 void finishNoUndo(BufferView * bv)
37 {
38         freezeUndo();
39         bv->unlockInset(bv->theLockingInset());
40         finishUndo();
41         bv->text->postPaint(0);
42         unFreezeUndo();
43 }
44
45
46 // Returns false if no undo possible.
47 bool textHandleUndo(BufferView * bv, Undo & undo)
48 {
49         Buffer * buf = bv->buffer();
50
51         ParagraphList * plist = &buf->paragraphs;
52 /*
53         ParIterator null = buf->par_iterator_end();
54
55         for (ParIterator it = buf->par_iterator_begin(); it != null; ++it)
56                 if (it.plist().id() == undo.plist_id) {
57                         plist = &it.plist();
58                         break;
59                 }
60 */
61
62         // Set the right(new) inset-owner of the paragraph if there is any.
63         UpdatableInset * inset = 0;
64         if (undo.inset_id >= 0) {
65                 Inset * in = bv->buffer()->getInsetFromID(undo.inset_id);
66                 inset = static_cast<UpdatableInset *>(in);
67         }
68         ParagraphList::iterator pit = undo.pars.begin();
69         ParagraphList::iterator end = undo.pars.end();
70         for ( ; pit != end; ++pit)
71                 pit->setInsetOwner(inset);
72
73         lyxerr << "\nhandle: inset_id: " << undo.inset_id << "\n";
74         lyxerr << "handle: inset: " << inset << "\n";
75         lyxerr << "handle: plist_id: " << undo.plist_id << "\n";
76         lyxerr << "handle: undo.pars.size(): " << undo.pars.size() << "\n";
77
78         // remove stuff between first and behind
79         {
80                 ParagraphList::iterator first = plist->begin();
81                 advance(first, undo.first_par_offset);
82                 ParagraphList::iterator last = plist->begin();
83                 advance(last, plist->size() - undo.last_par_offset);
84                 lyxerr << "handle: first_id:  " << first->id()  << "\n";
85                 lyxerr << "handle: last_id: " << last->id() << "\n";
86                 plist->erase(first, ++last);
87                 lyxerr << "after remove\n";
88         }
89
90         // re-insert old stuff
91         {
92                 ParagraphList::iterator first = plist->begin();
93                 advance(first, undo.first_par_offset);
94                 plist->insert(first, undo.pars.begin(), undo.pars.end());
95                 lyxerr << "after insert\n";
96         }
97
98         /*
99                 // A memory optimization for edit:
100                 // Only layout information
101                 // is stored in the undo. So restore
102                 // the text informations.
103                 if (undo.kind == Undo::EDIT) {
104                         undo.pars[par]->setContentsFromPar(*deletelist.back());
105                         ++par;
106                 }
107         */
108
109         // redo Paragraphs  (should be handled outside undo...)
110         LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
111         {
112                 lyxerr << "text: " << text << "\n";
113                 if (undo.first_par_offset) {
114                         ParagraphList::iterator redo = plist->begin();
115                         advance(redo, undo.first_par_offset);
116                         text->setCursorIntern(plist->begin(), 0);
117                 }
118                 text->redoParagraphs(text->cursor, plist->end());
119                 lyxerr << "after redo\n";
120         }
121
122 /*
123         Inset * = bv->buffer()->getInsetFromID(inset_id);
124         lyxerr << "tmppar: " << tmppar->id() << "\n";
125         LyXText * t;
126         if (it) {
127                 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
128                 it->localDispatch(cmd);
129                 t = it->getLyXText(bv);
130         } else {
131                 t = bv->text;
132         }
133         t->setCursorIntern(*tmppar, undo.cursor_pos);
134         // Clear any selection and set the selection
135         // cursor for an evt. new selection.
136         t->clearSelection();
137         t->selection.cursor = t->cursor;
138         t->updateCounters();
139 */
140
141         if (inset) {
142                 lyxerr << "fit cursor...\n";
143                 bv->fitCursor();
144                 bv->updateInset(inset);
145         }
146
147         // set cursor
148         {
149                 ParagraphList::iterator cursor = plist->begin();
150                 advance(cursor, undo.cursor_par_offset);
151                 bv->text->setCursorIntern(cursor, undo.cursor_pos);
152                 lyxerr << "after setCursor\n";
153         }
154
155
156         finishUndo();
157         bv->text->postPaint(0);
158
159         lyxerr << "finished  textHandleUndo...\n";
160         return true;
161 }
162
163
164 void createUndo(BufferView * bv, Undo::undo_kind kind,
165         ParagraphList::iterator first, ParagraphList::iterator last,
166         limited_stack<Undo> & stack)
167 {
168         Buffer * buf = bv->buffer();
169
170         ParagraphList * plist = 0;
171         ParIterator null = buf->par_iterator_end();
172
173         lyxerr << "\n";
174
175 #if 0
176         // this is what we'd like to have in the end for small grained undo
177         for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
178                 if (it->id() == first->id()) {
179                         plist = &it.plist();
180                         break;
181                 }
182         }
183
184 #else
185
186         // and that's the big stick we wield now
187         lyxerr << "create: first_id orig:   " << first->id()  << "\n";
188         lyxerr << "create: last_id orig:    " << last->id()   << "\n";
189         plist = &buf->paragraphs;
190         // this is what we'd like to have in the end for small grained undo
191         for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
192                 if (it->id() == first->id()) {
193                         first = it.outerPar(); 
194                         last = it.outerPar(); 
195                         break;
196                 }
197         }
198
199 #endif
200
201         int const inset_id = first->inInset() ? first->inInset()->id() : -1;
202
203         int const first_offset  = std::distance(plist->begin(), first);
204         int const last_offset   = std::distance(last, plist->end());
205
206         if (last == plist->end()) {
207                 lyxerr << "*** createUndo: last == end  schould not happen\n";
208         }
209
210         //lyxerr << "create: plist_id:     " << plist->id()  << "\n";
211         lyxerr << "create: first_id:     " << first->id()  << "\n";
212         lyxerr << "create: last_id:      " << last->id()   << "\n";
213         lyxerr << "create: first_offset: " << first_offset  << "\n";
214         lyxerr << "create: last_offset:  " << last_offset   << "\n";
215         lyxerr << "create: inset_id:     " << inset_id  << "\n";
216         lyxerr << "create: kind:         " << kind  << "\n";
217
218
219         // Undo::EDIT and Undo::FINISH are
220         // always finished. (no overlapping there)
221         // overlapping only with insert and delete inside one paragraph:
222         // Nobody wants all removed  character
223         // appear one by one when undoing.
224         // EDIT is special since only layout information, not the
225         // contents of a paragaph are stored.
226         if (!undo_finished && kind != Undo::EDIT && kind != Undo::FINISH) {
227                 // Check whether storing is needed.
228                 if (!buf->undostack.empty() &&
229                     buf->undostack.top().kind == kind &&
230                     buf->undostack.top().first_par_offset == first_offset &&
231                     buf->undostack.top().last_par_offset == last_offset) {
232                         // No undo needed.
233                         return;
234                 }
235         }
236
237         // Create a new Undo.
238 /*
239         // this should be re-activated once we are back at fine-grained undo
240         LyXCursor const & cur = bv->theLockingInset() ?
241                         bv->theLockingInset()->cursor(bv) : bv->text->cursor;
242         int const cursor_offset = std::distance(plist->begin(), cur.par());
243
244         stack.push(Undo(kind, inset_id, plist->id(),
245                 first_offset, last_offset, cursor_offset, cur.pos(), ParagraphList()));
246 */
247         stack.push(Undo(kind, inset_id, 0, //plist->id(),
248                 first_offset, last_offset, first_offset, 0, ParagraphList()));
249
250         ParagraphList & undo_pars = stack.top().pars;
251
252         for (ParagraphList::iterator it = first; it != last; ++it) {
253                 undo_pars.push_back(*it);
254                 undo_pars.back().id(it->id());
255         }
256         undo_pars.push_back(*last);
257         undo_pars.back().id(last->id());
258
259         // A memory optimization: Just store the layout
260         // information when only edit.
261 #warning Waste...
262         //if (kind == Undo::EDIT)
263         //      for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
264         //              undo_pars[i]->clearContents();
265
266         undo_finished = false;
267 }
268
269
270 // Returns false if no undo possible.
271 bool textUndoOrRedo(BufferView * bv,
272         limited_stack<Undo> & stack,
273         limited_stack<Undo> & otherstack)
274 {
275         if (stack.empty()) {
276                 finishNoUndo(bv);
277                 return false;
278         }
279
280         Undo undo = stack.top();
281         stack.pop();
282         finishUndo();
283
284         if (!undo_frozen) {
285                 otherstack.push(undo);
286                 otherstack.top().pars.clear();
287                 Buffer * buf = bv->buffer();
288                 ParagraphList & plist = buf->paragraphs;
289                 lyxerr << "\nredo: first: " << undo.first_par_offset << "\n";
290                 lyxerr << "redo: last:  " << undo.last_par_offset << "\n";
291                 lyxerr << "redo: size:  " << plist.size() << "\n";
292                 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
293                         ParagraphList::iterator first = plist.begin();
294                         advance(first, undo.first_par_offset);
295                         ParagraphList::iterator last = plist.begin();
296                         advance(last, plist.size() - undo.last_par_offset + 1);
297                         otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
298                 }
299         }
300
301         // Now we can unlock the inset for safety because the inset
302         // pointer could be changed during the undo-function. Anyway
303         // if needed we have to lock the right inset/position if this
304         // is requested.
305         freezeUndo();
306         bv->unlockInset(bv->theLockingInset());
307         bool const ret = textHandleUndo(bv, undo);
308         unFreezeUndo();
309         return ret;
310 }
311
312 } // namespace anon
313
314
315 void finishUndo()
316 {
317         // Makes sure the next operation will be stored.
318         undo_finished = true;
319 }
320
321
322 void freezeUndo()
323 {
324         // This is dangerous and for internal use only.
325         undo_frozen = true;
326 }
327
328
329 void unFreezeUndo()
330 {
331         // This is dangerous and for internal use only.
332         undo_frozen = false;
333 }
334
335
336 bool textUndo(BufferView * bv)
337 {
338         return textUndoOrRedo(bv, bv->buffer()->undostack,
339                               bv->buffer()->redostack);
340 }
341
342
343 bool textRedo(BufferView * bv)
344 {
345         return textUndoOrRedo(bv, bv->buffer()->redostack,
346                               bv->buffer()->undostack);
347 }
348
349
350 void setUndo(BufferView * bv, Undo::undo_kind kind,
351              ParagraphList::iterator first)
352 {
353         setUndo(bv, kind, first, first);
354 }
355
356
357 void setUndo(BufferView * bv, Undo::undo_kind kind,
358              ParagraphList::iterator first, ParagraphList::iterator last)
359 {
360         if (!undo_frozen) {
361                 createUndo(bv, kind, first, last, bv->buffer()->undostack);
362                 bv->buffer()->redostack.clear();
363         }
364 }
365
366
367 void setCursorParUndo(BufferView * bv)
368 {
369         setUndo(bv, Undo::FINISH, bv->text->cursor.par());
370 }