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