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