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