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