]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
simpler logic to create 'end' from 'behind'
[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 = 0; // nothing to replace yet (undo of delete maybe)
325
326         Paragraph * start = first;
327         Paragraph * end = &*boost::prior(itbehind);
328
329         if (start && end && (start != end->next()) &&
330             ((before_number != behind_number) ||
331                  ((before_number < 0) && (behind_number < 0))))
332         {
333                 Paragraph * tmppar = start;
334                 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
335
336                 // a memory optimization: Just store the layout information
337                 // when only edit
338                 if (kind == Undo::EDIT) {
339                         tmppar2->clearContents();
340                 }
341
342                 undopar = tmppar2;
343
344                 while (tmppar != end && tmppar->next()) {
345                         tmppar = tmppar->next();
346                         tmppar2->next(new Paragraph(*tmppar, true));
347                         // a memory optimization: Just store the layout
348                         // information when only edit
349                         if (kind == Undo::EDIT) {
350                                 tmppar2->clearContents();
351                         }
352                         tmppar2->next()->previous(tmppar2);
353
354                         tmppar2 = tmppar2->next();
355                 }
356                 tmppar2->next(0);
357         }
358
359         int cursor_par = undoCursor(bv).par()->id();
360         int cursor_pos = undoCursor(bv).pos();
361
362         u.reset(new Undo(kind, inset_id,
363                 before_number, behind_number,
364                 cursor_par, cursor_pos, undopar));
365
366         undo_finished = false;
367         return true;
368 }
369
370 } // namespace anon
371
372 void finishUndo()
373 {
374         // makes sure the next operation will be stored
375         undo_finished = true;
376 }
377
378
379 void freezeUndo()
380 {
381         // this is dangerous and for internal use only
382         undo_frozen = true;
383 }
384
385
386 void unFreezeUndo()
387 {
388         // this is dangerous and for internal use only
389         undo_frozen = false;
390 }
391
392
393 // returns false if no undo possible
394 bool textUndo(BufferView * bv)
395 {
396         Buffer * b = bv->buffer();
397
398         if (b->undostack.empty()) {
399                 finishNoUndo(bv);
400                 return false;
401         }
402
403         shared_ptr<Undo> undo = b->undostack.top();
404         b->undostack.pop();
405         finishUndo();
406
407         if (!undo_frozen) {
408                 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
409                 if (first && first->next())
410                         first = first->next();
411                 else if (!first)
412                         first = firstUndoParagraph(bv, undo->number_of_inset_id);
413                 if (first) {
414                         shared_ptr<Undo> u;
415                         if (createUndo(bv, undo->kind, first,
416                                 b->getParFromID(undo->number_of_behind_par), u))
417                                 b->redostack.push(u);
418                 }
419         }
420
421         // now we can unlock the inset for saftey because the inset pointer could
422         // be changed during the undo-function. Anyway if needed we have to lock
423         // the right inset/position if this is requested.
424         freezeUndo();
425         bv->unlockInset(bv->theLockingInset());
426         bool const ret = textHandleUndo(bv, *undo.get());
427         unFreezeUndo();
428         return ret;
429 }
430
431
432 // returns false if no redo possible
433 bool textRedo(BufferView * bv)
434 {
435         Buffer * b = bv->buffer();
436
437         if (b->redostack.empty()) {
438                 finishNoUndo(bv);
439                 return false;
440         }
441
442         shared_ptr<Undo> undo = b->redostack.top();
443         b->redostack.pop();
444         finishUndo();
445
446         if (!undo_frozen) {
447         Paragraph * first = &*bv->buffer()->getParFromID(undo->number_of_before_par);
448                 if (first && first->next())
449                         first = first->next();
450                 else if (!first)
451                         first = firstUndoParagraph(bv, undo->number_of_inset_id);
452                 if (first) {
453                         shared_ptr<Undo> u;
454                         if (createUndo(bv, undo->kind, first,
455                                 &*bv->buffer()->getParFromID(undo->number_of_behind_par), u))
456                                 bv->buffer()->undostack.push(u);
457                 }
458         }
459
460         // now we can unlock the inset for saftey because the inset pointer could
461         // be changed during the undo-function. Anyway if needed we have to lock
462         // the right inset/position if this is requested.
463         freezeUndo();
464         bv->unlockInset(bv->theLockingInset());
465         bool ret = textHandleUndo(bv, *undo.get());
466         unFreezeUndo();
467         return ret;
468 }
469
470
471 void setUndo(BufferView * bv, Undo::undo_kind kind,
472              ParagraphList::iterator first, ParagraphList::iterator behind)
473 {
474         if (!undo_frozen) {
475                 shared_ptr<Undo> u;
476                 if (createUndo(bv, kind, first, behind, u))
477                         bv->buffer()->undostack.push(u);
478                 bv->buffer()->redostack.clear();
479         }
480 }
481
482
483 void setRedo(BufferView * bv, Undo::undo_kind kind,
484              ParagraphList::iterator first, ParagraphList::iterator behind)
485 {
486         shared_ptr<Undo> u;
487         if (createUndo(bv, kind, first, behind, u))
488                 bv->buffer()->redostack.push(u);
489 }
490
491
492 void setCursorParUndo(BufferView * bv)
493 {
494         setUndo(bv, Undo::FINISH, &*bv->text->cursor.par(),
495                 bv->text->cursor.par()->next());
496 }