]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
Alfredo's second patch
[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 * const before = &*b->getParFromID(undo.number_of_before_par);
81         Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
82
83         // if there's no before take the beginning
84         // of the document for redoing
85         if (!before) {
86                 LyXText * t = bv->text;
87                 int num = undo.number_of_inset_id;
88                 if (undo.number_of_inset_id >= 0) {
89                         Inset * in = bv->buffer()->getInsetFromID(num);
90                         if (in) {
91                                 t = in->getLyXText(bv);
92                         } else {
93                                 num = -1;
94                         }
95                 }
96                 t->setCursorIntern(firstUndoParagraph(bv, num), 0);
97         }
98
99         // replace the paragraphs with the undo informations
100
101         Paragraph * undopar = undo.par;
102         undo.par = 0;   /* otherwise the undo destructor would
103                          delete the paragraph */
104
105         // get last undo par and set the right(new) inset-owner of the
106         // paragraph if there is any. This is not needed if we don't have
107         // a paragraph before because then in is automatically done in the
108         // function which assigns the first paragraph to an InsetText. (Jug)
109         Paragraph * lastundopar = undopar;
110         if (lastundopar) {
111                 Inset * in = 0;
112                 if (before)
113                         in = before->inInset();
114                 else if (undo.number_of_inset_id >= 0)
115                         in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
116                 lastundopar->setInsetOwner(in);
117                 while (lastundopar->next()) {
118                         lastundopar = lastundopar->next();
119                         lastundopar->setInsetOwner(in);
120                 }
121         }
122
123         vector<Paragraph *> deletelist;
124
125         // now add old paragraphs to be deleted
126         if (before != behind || (!behind && !before)) {
127                 Paragraph * deletepar;
128                 if (before)
129                         deletepar = before->next();
130                 else
131                         deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
132                 Paragraph * tmppar2 = undopar;
133                 while (deletepar && deletepar != behind) {
134                         deletelist.push_back(deletepar);
135                         Paragraph * tmppar = deletepar;
136                         deletepar = deletepar->next();
137
138                         // a memory optimization for edit:
139                         // Only layout information
140                         // is stored in the undo. So restore
141                         // the text informations.
142                         if (undo.kind == Undo::EDIT) {
143                                 tmppar2->setContentsFromPar(*tmppar);
144                                 tmppar2 = tmppar2->next();
145                         }
146                 }
147         }
148
149         // The order here is VERY IMPORTANT. We have to set the right
150         // next/prev pointer in the paragraphs so that a rebuild of
151         // the LyXText works!!!
152
153         // thread the end of the undo onto the par in front if any
154         if (lastundopar) {
155                 lastundopar->next(behind);
156                 if (behind)
157                         behind->previous(lastundopar);
158         }
159
160         // put the new stuff in the list if there is one
161         if (undopar) {
162                 undopar->previous(before);
163                 if (before)
164                         before->next(undopar);
165                 else {
166                         int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
167                         Paragraph * op = &*bv->buffer()->getParFromID(id);
168                         if (op && op->inInset()) {
169                                 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
170                         } else {
171                                 bv->buffer()->paragraphs.set(undopar);
172                         }
173                 }
174         } else {
175                 // We enter here on DELETE undo operations where we have to
176                 // substitue the second paragraph with the first if the removed
177                 // one is the first!
178                 if (!before && behind) {
179                         int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
180                         Paragraph * op = &*bv->buffer()->getParFromID(id);
181                         if (op && op->inInset()) {
182                                 static_cast<InsetText*>(op->inInset())->paragraph(behind);
183                         } else {
184                                 bv->buffer()->paragraphs.set(behind);
185                         }
186
187                         undopar = behind;
188                 }
189         }
190
191
192         // Set the cursor for redoing
193         if (before) { // if we have a par before the undopar
194                 Inset * it = before->inInset();
195                 if (it)
196                         it->getLyXText(bv)->setCursorIntern(before, 0);
197                 else
198                         bv->text->setCursorIntern(before, 0);
199         }
200 // we are not ready for this we cannot set the cursor for a paragraph
201 // which is not already in a row of LyXText!!!
202 #if 0
203         else { // otherwise this is the first one and we start here
204                 Inset * it = undopar->inInset();
205                 if (it)
206                         it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
207                 else
208                         bv->text->setCursorIntern(bv, undopar, 0);
209         }
210 #endif
211
212         Paragraph * endpar = 0;
213         // calculate the endpar for redoing the paragraphs.
214         if (behind)
215                 endpar = behind->next();
216
217         UpdatableInset * it = 0;
218         if (undopar)
219                 it = static_cast<UpdatableInset*>(undopar->inInset());
220         if (it) {
221                 it->getLyXText(bv)->redoParagraphs(
222                                                    it->getLyXText(bv)->cursor,
223                                                    endpar);
224                 Paragraph * tmppar =
225                         &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
226                 if (tmppar) {
227                         it = static_cast<UpdatableInset*>(tmppar->inInset());
228                         LyXText * t;
229                         if (it) {
230                                 it->edit(bv);
231                                 t = it->getLyXText(bv);
232                         } else {
233                                 t = bv->text;
234                         }
235                         t->setCursorIntern(tmppar, undo.cursor_pos);
236                         // clear any selection and set the selection cursor
237                         // for an evt. new selection.
238                         t->clearSelection();
239                         t->selection.cursor = t->cursor;
240                         t->updateCounters();
241                         bv->fitCursor();
242                 }
243                 bv->updateInset(it);
244                 bv->text->setCursorIntern(bv->text->cursor.par(),
245                                           bv->text->cursor.pos());
246         } else {
247                 bv->text->redoParagraphs(bv->text->cursor, endpar);
248                 Paragraph * tmppar =
249                         &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
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 (undo of delete maybe)
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         }
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
378 // returns false if no undo possible
379 bool textUndoOrRedo(BufferView * bv,
380         limited_stack<boost::shared_ptr<Undo> > & stack,
381         limited_stack<boost::shared_ptr<Undo> > & otherstack)
382 {
383         Buffer * b = bv->buffer();
384
385         if (stack.empty()) {
386                 finishNoUndo(bv);
387                 return false;
388         }
389
390         shared_ptr<Undo> undo = stack.top();
391         stack.pop();
392         finishUndo();
393
394         if (!undo_frozen) {
395                 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
396                 if (first && first->next())
397                         first = first->next();
398                 else if (!first)
399                         first = firstUndoParagraph(bv, undo->number_of_inset_id);
400                 if (first) {
401                         shared_ptr<Undo> u;
402                         if (createUndo(bv, undo->kind, first,
403                                              b->getParFromID(undo->number_of_behind_par), u))
404                                 otherstack.push(u);
405                 }
406         }
407
408         // now we can unlock the inset for saftey because the inset pointer could
409         // be changed during the undo-function. Anyway if needed we have to lock
410         // the right inset/position if this is requested.
411         freezeUndo();
412         bv->unlockInset(bv->theLockingInset());
413         bool const ret = textHandleUndo(bv, *undo.get());
414         unFreezeUndo();
415         return ret;
416 }
417
418 } // namespace anon
419
420 void finishUndo()
421 {
422         // makes sure the next operation will be stored
423         undo_finished = true;
424 }
425
426
427 void freezeUndo()
428 {
429         // this is dangerous and for internal use only
430         undo_frozen = true;
431 }
432
433
434 void unFreezeUndo()
435 {
436         // this is dangerous and for internal use only
437         undo_frozen = false;
438 }
439
440
441 bool textUndo(BufferView * bv)
442 {
443         return textUndoOrRedo(bv, bv->buffer()->undostack, bv->buffer()->redostack);
444 }
445
446
447 bool textRedo(BufferView * bv)
448 {
449         return textUndoOrRedo(bv, bv->buffer()->redostack, bv->buffer()->undostack);
450 }
451
452
453 void setUndo(BufferView * bv, Undo::undo_kind kind,
454              ParagraphList::iterator first, ParagraphList::iterator behind)
455 {
456         if (!undo_frozen) {
457                 shared_ptr<Undo> u;
458                 if (createUndo(bv, kind, first, behind, u))
459                         bv->buffer()->undostack.push(u);
460                 bv->buffer()->redostack.clear();
461         }
462 }
463
464
465 void setRedo(BufferView * bv, Undo::undo_kind kind,
466              ParagraphList::iterator first, ParagraphList::iterator behind)
467 {
468         shared_ptr<Undo> u;
469         if (createUndo(bv, kind, first, behind, u))
470                 bv->buffer()->redostack.push(u);
471 }
472
473
474 void setCursorParUndo(BufferView * bv)
475 {
476         setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
477                 boost::next(bv->text->cursor.par()));
478 }