]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
ddda43f5799f3dbc6734bd323f99a8f4fa1d1fa0
[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 "BufferView.h"
15 #include "buffer.h"
16 #include "insets/updatableinset.h"
17 #include "insets/insettext.h"
18 #include "debug.h"
19 #include "support/LAssert.h"
20
21 #include "iterators.h"
22
23 #include <vector>
24
25 using std::vector;
26 using boost::shared_ptr;
27
28
29 /// the flag used by FinishUndo();
30 bool undo_finished;
31 /// whether actions are not added to the undo stacks
32 bool undo_frozen;
33
34 namespace {
35
36 /// utility to return the cursor
37 LyXCursor const & undoCursor(BufferView * bv)
38 {
39         if (bv->theLockingInset())
40                 return bv->theLockingInset()->cursor(bv);
41         return bv->text->cursor;
42 }
43
44 /**
45  * returns a pointer to the very first Paragraph depending of where we are
46  * so it will return the first paragraph of the buffer or the first paragraph
47  * of the textinset we're in.
48  */
49 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
50 {
51         Inset * inset = bv->buffer()->getInsetFromID(inset_id);
52         if (inset) {
53                 Paragraph * result = inset->getFirstParagraph(0);
54                 if (result)
55                         return result;
56         }
57         return &*bv->text->ownerParagraphs().begin();
58 }
59
60
61 /**
62  * Finish the undo operation in the case there was no entry
63  * on the stack to perform.
64  */
65 void finishNoUndo(BufferView * bv)
66 {
67         freezeUndo();
68         bv->unlockInset(bv->theLockingInset());
69         finishUndo();
70         bv->text->postPaint(0);
71         unFreezeUndo();
72 }
73
74
75 // returns false if no undo possible
76 bool textHandleUndo(BufferView * bv, Undo & undo)
77 {
78         Buffer * b = bv->buffer();
79
80         Paragraph * before = &*b->getParFromID(undo.number_of_before_par);
81         Paragraph * behind = &*b->getParFromID(undo.number_of_behind_par);
82         Paragraph * tmppar;
83         Paragraph * tmppar2;
84
85         // if there's no before take the beginning
86         // of the document for redoing
87         if (!before) {
88                 LyXText * t = bv->text;
89                 int num = undo.number_of_inset_id;
90                 if (undo.number_of_inset_id >= 0) {
91                         Inset * in = bv->buffer()->getInsetFromID(num);
92                         if (in) {
93                                 t = in->getLyXText(bv);
94                         } else {
95                                 num = -1;
96                         }
97                 }
98                 t->setCursorIntern(firstUndoParagraph(bv, num), 0);
99         }
100
101         // replace the paragraphs with the undo informations
102
103         Paragraph * undopar = undo.par;
104         undo.par = 0;   /* otherwise the undo destructor would
105                            delete the paragraph */
106
107         // get last undo par and set the right(new) inset-owner of the
108         // paragraph if there is any. This is not needed if we don't have
109         // a paragraph before because then in is automatically done in the
110         // function which assigns the first paragraph to an InsetText. (Jug)
111         Paragraph * lastundopar = undopar;
112         if (lastundopar) {
113                 Inset * in = 0;
114                 if (before)
115                         in = before->inInset();
116                 else if (undo.number_of_inset_id >= 0)
117                         in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
118                 lastundopar->setInsetOwner(in);
119                 while (lastundopar->next()) {
120                         lastundopar = lastundopar->next();
121                         lastundopar->setInsetOwner(in);
122                 }
123         }
124
125         vector<Paragraph *> deletelist;
126
127         Paragraph * deletepar;
128
129         // now add old paragraphs to be deleted
130         if (before != behind || (!behind && !before)) {
131                 if (before)
132                         deletepar = before->next();
133                 else
134                         deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
135                 tmppar2 = undopar;
136                 while (deletepar && deletepar != behind) {
137                         deletelist.push_back(deletepar);
138                         tmppar = deletepar;
139                         deletepar = deletepar->next();
140
141                         // a memory optimization for edit:
142                         // Only layout information
143                         // is stored in the undo. So restore
144                         // the text informations.
145                         if (undo.kind == Undo::EDIT) {
146                                 tmppar2->setContentsFromPar(*tmppar);
147                                 tmppar2 = tmppar2->next();
148                         }
149                 }
150         }
151
152         // The order here is VERY IMPORTANT. We have to set the right
153         // next/prev pointer in the paragraphs so that a rebuild of
154         // the LyXText works!!!
155
156         // thread the end of the undo onto the par in front if any
157         if (lastundopar) {
158                 lastundopar->next(behind);
159                 if (behind)
160                         behind->previous(lastundopar);
161         }
162
163         // put the new stuff in the list if there is one
164         if (undopar) {
165                 undopar->previous(before);
166                 if (before)
167                         before->next(undopar);
168                 else {
169                         int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
170                         Paragraph * op = &*bv->buffer()->getParFromID(id);
171                         if (op && op->inInset()) {
172                                 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
173                         } else {
174                                 bv->buffer()->paragraphs.set(undopar);
175                         }
176                 }
177         } else {
178                 // We enter here on DELETE undo operations where we have to
179                 // substitue the second paragraph with the first if the removed
180                 // one is the first!
181                 if (!before && behind) {
182                         int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
183                         Paragraph * op = &*bv->buffer()->getParFromID(id);
184                         if (op && op->inInset()) {
185                                 static_cast<InsetText*>(op->inInset())->paragraph(behind);
186                         } else {
187                                 bv->buffer()->paragraphs.set(behind);
188                         }
189
190                         undopar = behind;
191                 }
192         }
193
194
195         // Set the cursor for redoing
196         if (before) { // if we have a par before the undopar
197                 Inset * it = before->inInset();
198                 if (it)
199                         it->getLyXText(bv)->setCursorIntern(before, 0);
200                 else
201                         bv->text->setCursorIntern(before, 0);
202         }
203 // we are not ready for this we cannot set the cursor for a paragraph
204 // which is not already in a row of LyXText!!!
205 #if 0
206         else { // otherwise this is the first one and we start here
207                 Inset * it = undopar->inInset();
208                 if (it)
209                         it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
210                 else
211                         bv->text->setCursorIntern(bv, undopar, 0);
212         }
213 #endif
214
215         Paragraph * endpar = 0;
216         // calculate the endpar for redoing the paragraphs.
217         if (behind)
218                 endpar = behind->next();
219
220         tmppar = &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
221         UpdatableInset* it = 0;
222         if (undopar)
223                 it = static_cast<UpdatableInset*>(undopar->inInset());
224         if (it) {
225                 it->getLyXText(bv)->redoParagraphs(
226                                                    it->getLyXText(bv)->cursor,
227                                                    endpar);
228                 if (tmppar) {
229                         it = static_cast<UpdatableInset*>(tmppar->inInset());
230                         LyXText * t;
231                         if (it) {
232                                 it->edit(bv);
233                                 t = it->getLyXText(bv);
234                         } else {
235                                 t = bv->text;
236                         }
237                         t->setCursorIntern(tmppar, undo.cursor_pos);
238                         // clear any selection and set the selection cursor
239                         // for an evt. new selection.
240                         t->clearSelection();
241                         t->selection.cursor = t->cursor;
242                         t->updateCounters();
243                         bv->fitCursor();
244                 }
245                 bv->updateInset(it);
246                 bv->text->setCursorIntern(bv->text->cursor.par(),
247                                           bv->text->cursor.pos());
248         } else {
249                 bv->text->redoParagraphs(bv->text->cursor, endpar);
250                 if (tmppar) {
251                         LyXText * t;
252                         Inset * it = tmppar->inInset();
253                         if (it) {
254                                 it->edit(bv);
255                                 t = it->getLyXText(bv);
256                         } else {
257                                 t = bv->text;
258                         }
259                         t->setCursorIntern(tmppar, undo.cursor_pos);
260                         // clear any selection and set the selection cursor
261                         // for an evt. new selection.
262                         t->clearSelection();
263                         t->selection.cursor = t->cursor;
264                         t->updateCounters();
265                 }
266         }
267
268         // And here it's safe enough to delete all removed paragraphs
269         vector<Paragraph *>::iterator pit = deletelist.begin();
270         if (pit != deletelist.end()) {
271                         for(;pit != deletelist.end(); ++pit) {
272                                 (*pit)->previous(0);
273                                 (*pit)->next(0);
274                                 delete (*pit);
275                         }
276                 }
277
278         finishUndo();
279         bv->text->postPaint(0);
280         return true;
281 }
282
283
284 bool createUndo(BufferView * bv, Undo::undo_kind kind,
285         ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
286         shared_ptr<Undo> & u)
287 {
288         Paragraph * first = &*itfirst;
289         Paragraph * behind = &*itbehind;
290         lyx::Assert(first);
291
292         int before_number = -1;
293         int behind_number = -1;
294         int inset_id = -1;
295
296         if (first->previous())
297                 before_number = first->previous()->id();
298         if (behind)
299                 behind_number = behind->id();
300         if (first->inInset())
301                 inset_id = first->inInset()->id();
302
303         Buffer * b = bv->buffer();
304
305         // Undo::EDIT  and Undo::FINISH are
306         // always finished. (no overlapping there)
307         // overlapping only with insert and delete inside one paragraph:
308         // Nobody wants all removed  character
309         // appear one by one when undoing.
310         // EDIT is special since only layout information, not the
311         // contents of a paragaph are stored.
312         if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
313                 // check whether storing is needed
314                 if (!b->undostack.empty() &&
315                     b->undostack.top()->kind == kind &&
316                     b->undostack.top()->number_of_before_par == before_number &&
317                     b->undostack.top()->number_of_behind_par == behind_number) {
318                         // no undo needed
319                         return false;
320                 }
321         }
322
323         // create a new Undo
324         Paragraph * undopar;
325
326         Paragraph * start = first;
327         Paragraph * end = 0;
328
329         if (behind)
330                 end = const_cast<Paragraph*>(behind->previous());
331         else {
332                 end = start;
333                 while (end->next())
334                         end = end->next();
335         }
336         if (start && end && (start != end->next()) &&
337             ((before_number != behind_number) ||
338                  ((before_number < 0) && (behind_number < 0))))
339         {
340                 Paragraph * tmppar = start;
341                 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
342
343                 // a memory optimization: Just store the layout information
344                 // when only edit
345                 if (kind == Undo::EDIT) {
346                         tmppar2->clearContents();
347                 }
348
349                 undopar = tmppar2;
350
351                 while (tmppar != end && tmppar->next()) {
352                         tmppar = tmppar->next();
353                         tmppar2->next(new Paragraph(*tmppar, true));
354                         // a memory optimization: Just store the layout
355                         // information when only edit
356                         if (kind == Undo::EDIT) {
357                                 tmppar2->clearContents();
358                         }
359                         tmppar2->next()->previous(tmppar2);
360
361                         tmppar2 = tmppar2->next();
362                 }
363                 tmppar2->next(0);
364         } else
365                 undopar = 0; // nothing to replace (undo of delete maybe)
366
367         int cursor_par = undoCursor(bv).par()->id();
368         int cursor_pos =  undoCursor(bv).pos();
369
370         u.reset(new Undo(kind, inset_id,
371                 before_number, behind_number,
372                 cursor_par, cursor_pos, undopar));
373
374         undo_finished = false;
375         return true;
376 }
377
378 } // namespace anon
379
380 void finishUndo()
381 {
382         // makes sure the next operation will be stored
383         undo_finished = true;
384 }
385
386
387 void freezeUndo()
388 {
389         // this is dangerous and for internal use only
390         undo_frozen = true;
391 }
392
393
394 void unFreezeUndo()
395 {
396         // this is dangerous and for internal use only
397         undo_frozen = false;
398 }
399
400
401 // returns false if no undo possible
402 bool textUndo(BufferView * bv)
403 {
404         Buffer * b = bv->buffer();
405
406         if (b->undostack.empty()) {
407                 finishNoUndo(bv);
408                 return false;
409         }
410
411         shared_ptr<Undo> undo = b->undostack.top();
412         b->undostack.pop();
413         finishUndo();
414
415         if (!undo_frozen) {
416                 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
417                 if (first && first->next())
418                         first = first->next();
419                 else if (!first)
420                         first = firstUndoParagraph(bv, undo->number_of_inset_id);
421                 if (first) {
422                         shared_ptr<Undo> u;
423                         if (createUndo(bv, undo->kind, first,
424                                 b->getParFromID(undo->number_of_behind_par), u))
425                                 b->redostack.push(u);
426                 }
427         }
428
429         // now we can unlock the inset for saftey because the inset pointer could
430         // be changed during the undo-function. Anyway if needed we have to lock
431         // the right inset/position if this is requested.
432         freezeUndo();
433         bv->unlockInset(bv->theLockingInset());
434         bool const ret = textHandleUndo(bv, *undo.get());
435         unFreezeUndo();
436         return ret;
437 }
438
439
440 // returns false if no redo possible
441 bool textRedo(BufferView * bv)
442 {
443         Buffer * b = bv->buffer();
444
445         if (b->redostack.empty()) {
446                 finishNoUndo(bv);
447                 return false;
448         }
449
450         shared_ptr<Undo> undo = b->redostack.top();
451         b->redostack.pop();
452         finishUndo();
453
454         if (!undo_frozen) {
455         Paragraph * first = &*bv->buffer()->getParFromID(undo->number_of_before_par);
456                 if (first && first->next())
457                         first = first->next();
458                 else if (!first)
459                         first = firstUndoParagraph(bv, undo->number_of_inset_id);
460                 if (first) {
461                         shared_ptr<Undo> u;
462                         if (createUndo(bv, undo->kind, first,
463                                 &*bv->buffer()->getParFromID(undo->number_of_behind_par), u))
464                                 bv->buffer()->undostack.push(u);
465                 }
466         }
467
468         // now we can unlock the inset for saftey because the inset pointer could
469         // be changed during the undo-function. Anyway if needed we have to lock
470         // the right inset/position if this is requested.
471         freezeUndo();
472         bv->unlockInset(bv->theLockingInset());
473         bool ret = textHandleUndo(bv, *undo.get());
474         unFreezeUndo();
475         return ret;
476 }
477
478
479 void setUndo(BufferView * bv, Undo::undo_kind kind,
480              ParagraphList::iterator first, ParagraphList::iterator behind)
481 {
482         if (!undo_frozen) {
483                 shared_ptr<Undo> u;
484                 if (createUndo(bv, kind, first, behind, u))
485                         bv->buffer()->undostack.push(u);
486                 bv->buffer()->redostack.clear();
487         }
488 }
489
490
491 void setRedo(BufferView * bv, Undo::undo_kind kind,
492              ParagraphList::iterator first, ParagraphList::iterator behind)
493 {
494         shared_ptr<Undo> u;
495         if (createUndo(bv, kind, first, behind, u))
496                 bv->buffer()->redostack.push(u);
497 }
498
499
500 void setCursorParUndo(BufferView * bv)
501 {
502         setUndo(bv, Undo::FINISH, &*bv->text->cursor.par(),
503                 bv->text->cursor.par()->next());
504 }