]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
undo-4.diff ("Another next() ...")
[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 /**
46  * Returns a pointer to the very first Paragraph depending of where
47  * we are so it will return the first paragraph of the buffer or the
48  * first paragraph of the textinset we're in.
49  */
50 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
51 {
52         Inset * inset = bv->buffer()->getInsetFromID(inset_id);
53         if (inset) {
54                 Paragraph * result = inset->getFirstParagraph(0);
55                 if (result)
56                         return result;
57         }
58         return &*bv->text->ownerParagraphs().begin();
59 }
60
61
62 /**
63  * Finish the undo operation in the case there was no entry
64  * on the stack to perform.
65  */
66 void finishNoUndo(BufferView * bv)
67 {
68         freezeUndo();
69         bv->unlockInset(bv->theLockingInset());
70         finishUndo();
71         bv->text->postPaint(0);
72         unFreezeUndo();
73 }
74
75
76 // Returns false if no undo possible.
77 bool textHandleUndo(BufferView * bv, Undo & undo)
78 {
79         Buffer * b = bv->buffer();
80
81         Paragraph * const before = &*b->getParFromID(undo.number_of_before_par);
82         Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
83
84         // If there's no before take the beginning
85         // of the document for redoing.
86         if (!before) {
87                 LyXText * t = bv->text;
88                 int num = undo.number_of_inset_id;
89                 if (undo.number_of_inset_id >= 0) {
90                         Inset * in = bv->buffer()->getInsetFromID(num);
91                         if (in) {
92                                 t = in->getLyXText(bv);
93                         } else {
94                                 num = -1;
95                         }
96                 }
97                 t->setCursorIntern(firstUndoParagraph(bv, num), 0);
98         }
99
100         // Replace the paragraphs with the undo informations.
101         Paragraph * undopar     = undo.pars.empty() ? 0 : undo.pars.front();
102         Paragraph * lastundopar = undo.pars.empty() ? 0 : undo.pars.back();
103
104         // Get last undo par and set the right(new) inset-owner of the
105         // paragraph if there is any. This is not needed if we don't
106         // have a paragraph before because then in is automatically
107         // done in the function which assigns the first paragraph to
108         // an InsetText. (Jug)
109         if (!undo.pars.empty()) {
110                 Inset * in = 0;
111                 if (before)
112                         in = before->inInset();
113                 else if (undo.number_of_inset_id >= 0)
114                         in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
115                 for (size_t i = 0, n = undo.pars.size(); i < n; ++i)
116                         undo.pars[i]->setInsetOwner(in);
117         }
118
119         // Otherwise the undo destructor would
120         // delete the paragraph.
121         undo.pars.resize(0);
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
176                 // have to substitue the second paragraph with the
177                 // first if the removed 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 we have a par before the undopar.
194         if (before) {
195                 Inset * it = before->inInset();
196                 if (it)
197                         it->getLyXText(bv)->setCursorIntern(before, 0);
198                 else
199                         bv->text->setCursorIntern(before, 0);
200         }
201
202 // we are not ready for this we cannot set the cursor for a paragraph
203 // which is not already in a row of LyXText!!!
204 #if 0
205         else { // otherwise this is the first one and we start here
206                 Inset * it = undopar->inInset();
207                 if (it)
208                         it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
209                 else
210                         bv->text->setCursorIntern(bv, undopar, 0);
211         }
212 #endif
213
214         Paragraph * endpar = 0;
215
216         // Calculate the endpar for redoing the paragraphs.
217         if (behind)
218                 endpar = behind->next();
219
220         UpdatableInset * it = 0;
221         if (undopar)
222                 it = static_cast<UpdatableInset*>(undopar->inInset());
223         if (it) {
224                 it->getLyXText(bv)->redoParagraphs(
225                                                    it->getLyXText(bv)->cursor,
226                                                    endpar);
227                 Paragraph * tmppar =
228                         &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
229                 if (tmppar) {
230                         it = static_cast<UpdatableInset*>(tmppar->inInset());
231                         LyXText * t;
232                         if (it) {
233                                 it->edit(bv);
234                                 t = it->getLyXText(bv);
235                         } else {
236                                 t = bv->text;
237                         }
238                         t->setCursorIntern(tmppar, undo.cursor_pos);
239                         // Clear any selection and set the selection
240                         // cursor for an evt. new selection.
241                         t->clearSelection();
242                         t->selection.cursor = t->cursor;
243                         t->updateCounters();
244                         bv->fitCursor();
245                 }
246                 bv->updateInset(it);
247                 bv->text->setCursorIntern(bv->text->cursor.par(),
248                                           bv->text->cursor.pos());
249         } else {
250                 bv->text->redoParagraphs(bv->text->cursor, endpar);
251                 Paragraph * tmppar =
252                         &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
253                 if (tmppar) {
254                         LyXText * t;
255                         Inset * it = tmppar->inInset();
256                         if (it) {
257                                 it->edit(bv);
258                                 t = it->getLyXText(bv);
259                         } else {
260                                 t = bv->text;
261                         }
262                         t->setCursorIntern(tmppar, undo.cursor_pos);
263                         // Clear any selection and set the selection
264                         // cursor for an evt. new selection.
265                         t->clearSelection();
266                         t->selection.cursor = t->cursor;
267                         t->updateCounters();
268                 }
269         }
270
271         // And here it's safe enough to delete all removed paragraphs.
272         vector<Paragraph *>::iterator pit = deletelist.begin();
273         for(; pit != deletelist.end(); ++pit) {
274                 (*pit)->previous(0);
275                 (*pit)->next(0);
276                 delete (*pit);
277         }
278
279         finishUndo();
280         bv->text->postPaint(0);
281         return true;
282 }
283
284
285 bool createUndo(BufferView * bv, Undo::undo_kind kind,
286         ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
287         shared_ptr<Undo> & u)
288 {
289         Paragraph * const first = &*itfirst;
290         Paragraph * const behind = &*itbehind;
291         lyx::Assert(first);
292
293         int before_number = -1;
294         int behind_number = -1;
295         int inset_id = -1;
296
297         if (first->previous())
298                 before_number = first->previous()->id();
299         if (behind)
300                 behind_number = behind->id();
301         if (first->inInset())
302                 inset_id = first->inInset()->id();
303
304         Buffer * b = bv->buffer();
305
306         // Undo::EDIT  and Undo::FINISH are
307         // always finished. (no overlapping there)
308         // overlapping only with insert and delete inside one paragraph:
309         // Nobody wants all removed  character
310         // appear one by one when undoing.
311         // EDIT is special since only layout information, not the
312         // contents of a paragaph are stored.
313         if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
314                 // Check whether storing is needed.
315                 if (!b->undostack.empty() &&
316                     b->undostack.top()->kind == kind &&
317                     b->undostack.top()->number_of_before_par == before_number &&
318                     b->undostack.top()->number_of_behind_par == behind_number) {
319                         // No undo needed.
320                         return false;
321                 }
322         }
323
324         // Create a new Undo.
325         std::vector<Paragraph *> undo_pars;
326
327         Paragraph const * end = 0;
328
329         if (behind)
330                 end = behind->previous();
331         else {
332                 end = first;
333                 while (end->next())
334                         end = end->next();
335         }
336
337         if (first && end && (first != end->next()) &&
338             ((before_number != behind_number) ||
339                  ((before_number < 0) && (behind_number < 0))))
340         {
341                 undo_pars.push_back(new Paragraph(*first, true));
342                 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
343                         tmppar = tmppar->next();
344                         undo_pars.push_back(new Paragraph(*tmppar, true));
345                         size_t const n = undo_pars.size();
346                         undo_pars[n - 2]->next(undo_pars[n - 1]);
347                         undo_pars[n - 1]->previous(undo_pars[n - 2]);
348                 }
349                 undo_pars.back()->next(0);
350         }
351
352         // A memory optimization: Just store the layout
353         // information when only edit.
354         if (kind == Undo::EDIT) {
355                 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
356                         undo_pars[i]->clearContents();
357         }               
358
359         int cursor_par = undoCursor(bv).par()->id();
360         int cursor_pos = undoCursor(bv).pos();
361
362         //lyxerr << "createUndo: inset_id: " << inset_id << "  before_number: " 
363         //      << before_number << "  behind_number: " << behind_number << "\n";
364         u.reset(new Undo(kind, inset_id,
365                 before_number, behind_number,
366                 cursor_par, cursor_pos, undo_pars));
367
368         undo_finished = false;
369         return true;
370 }
371
372
373 // Returns false if no undo possible.
374 bool textUndoOrRedo(BufferView * bv,
375         limited_stack<boost::shared_ptr<Undo> > & stack,
376         limited_stack<boost::shared_ptr<Undo> > & otherstack)
377 {
378         Buffer * b = bv->buffer();
379
380         if (stack.empty()) {
381                 finishNoUndo(bv);
382                 return false;
383         }
384
385         shared_ptr<Undo> undo = stack.top();
386         stack.pop();
387         finishUndo();
388
389         if (!undo_frozen) {
390                 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
391                 if (first && first->next())
392                         first = first->next();
393                 else if (!first)
394                         first = firstUndoParagraph(bv, undo->number_of_inset_id);
395                 if (first) {
396                         shared_ptr<Undo> u;
397                         if (createUndo(bv, undo->kind, first,
398                                              b->getParFromID(undo->number_of_behind_par), u))
399                                 otherstack.push(u);
400                 }
401         }
402
403         // Now we can unlock the inset for saftey because the inset
404         // pointer could be changed during the undo-function. Anyway
405         // if needed we have to lock the right inset/position if this
406         // is requested.
407         freezeUndo();
408         bv->unlockInset(bv->theLockingInset());
409         bool const ret = textHandleUndo(bv, *undo.get());
410         unFreezeUndo();
411         return ret;
412 }
413
414 } // namespace anon
415
416
417 void finishUndo()
418 {
419         // Makes sure the next operation will be stored.
420         undo_finished = true;
421 }
422
423
424 void freezeUndo()
425 {
426         // This is dangerous and for internal use only.
427         undo_frozen = true;
428 }
429
430
431 void unFreezeUndo()
432 {
433         // This is dangerous and for internal use only.
434         undo_frozen = false;
435 }
436
437
438 bool textUndo(BufferView * bv)
439 {
440         return textUndoOrRedo(bv, bv->buffer()->undostack,
441                               bv->buffer()->redostack);
442 }
443
444
445 bool textRedo(BufferView * bv)
446 {
447         return textUndoOrRedo(bv, bv->buffer()->redostack,
448                               bv->buffer()->undostack);
449 }
450
451
452 void setUndo(BufferView * bv, Undo::undo_kind kind,
453              ParagraphList::iterator first, ParagraphList::iterator behind)
454 {
455         if (!undo_frozen) {
456                 shared_ptr<Undo> u;
457                 if (createUndo(bv, kind, first, behind, u))
458                         bv->buffer()->undostack.push(u);
459                 bv->buffer()->redostack.clear();
460         }
461 }
462
463
464 void setRedo(BufferView * bv, Undo::undo_kind kind,
465              ParagraphList::iterator first, ParagraphList::iterator behind)
466 {
467         shared_ptr<Undo> u;
468         if (createUndo(bv, kind, first, behind, u))
469                 bv->buffer()->redostack.push(u);
470 }
471
472
473 void setCursorParUndo(BufferView * bv)
474 {
475         setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
476                 boost::next(bv->text->cursor.par()));
477 }