]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
setUndo2 interface change
[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)->begin()->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)->begin()->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 itlast,
277         shared_ptr<Undo> & u)
278 {
279         Buffer * b = bv->buffer();
280
281         ParIterator it     = b->par_iterator_begin();
282         ParIterator null   = b->par_iterator_end();
283         ParIterator prev   = null;
284         ParIterator before = null;
285         ParIterator first  = null;
286         ParIterator last   = null;
287         ParIterator behind = null;
288
289         for (; it != null; ++it) {
290                 if ((*it)->id() == itfirst->id()) {
291                         first = it;
292                         before = prev;
293                 } else if ((*it)->id() == itlast->id()) {
294                         last = it;
295                         behind = last;
296                         ++behind;
297                 }
298                 prev = it;
299         }
300
301         if (last == null)
302                 last = first;
303
304         int const before_id = (before == null) ? -1 : (*before)->id();
305         int const first_id  = (first == null)  ? -1 : (*first)->id();
306         int const last_id   = (last == null)   ? -1 : (*last)->id();
307         int const behind_id = (behind == null) ? -1 : (*behind)->id();
308         int inset_id        = (first->inInset()) ? first->inInset()->id() : -1;
309
310         lyxerr << "\nbefore_id: " << before_id << "\n";
311         lyxerr << "first_id:  " << first_id  << "\n";
312         lyxerr << "last_id:   " << last_id   << "\n";
313         lyxerr << "behind_id: " << behind_id << "\n";
314         lyxerr << "inset_id:  " << inset_id  << "\n";
315
316         ParagraphList * plist = 0;
317         if (first != null)
318                 plist = &first.plist();
319         else if (behind != null)
320                 plist = &behind.plist();
321         else if (!plist) {
322                 lyxerr << "plist from buffer (should this happen?)\n";
323                 plist = &b->paragraphs;
324         }
325
326         // Undo::EDIT and Undo::FINISH are
327         // always finished. (no overlapping there)
328         // overlapping only with insert and delete inside one paragraph:
329         // Nobody wants all removed  character
330         // appear one by one when undoing.
331         // EDIT is special since only layout information, not the
332         // contents of a paragaph are stored.
333         if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
334                 // Check whether storing is needed.
335                 if (!b->undostack.empty() &&
336                     b->undostack.top()->kind == kind &&
337                     b->undostack.top()->number_of_before_par == before_id &&
338                     b->undostack.top()->number_of_behind_par == behind_id) {
339                         // No undo needed.
340                         return false;
341                 }
342         }
343
344         // Create a new Undo.
345         std::vector<Paragraph *> undo_pars;
346
347         for (ParagraphList::iterator it = *first; it != *last; ++it) 
348                 undo_pars.push_back(new Paragraph(*it, true));
349         undo_pars.push_back(new Paragraph(**last, true));
350
351         // A memory optimization: Just store the layout
352         // information when only edit.
353         if (kind == Undo::EDIT)
354                 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
355                         undo_pars[i]->clearContents();
356
357         int cursor_par = undoCursor(bv).par()->id();
358         int cursor_pos = undoCursor(bv).pos();
359
360         lyxerr << "createUndo: inset_id: " << inset_id << "  before_id: "
361                 << before_id << "  behind_id: " << behind_id << "\n";
362         u.reset(new Undo(kind, inset_id,
363                 before_id, behind_id, cursor_par, cursor_pos, undo_pars));
364
365         undo_finished = false;
366         return true;
367 }
368
369
370 // Returns false if no undo possible.
371 bool textUndoOrRedo(BufferView * bv,
372         limited_stack<boost::shared_ptr<Undo> > & stack,
373                     limited_stack<boost::shared_ptr<Undo> > & /*otherstack*/)
374 {
375         //Buffer * b = bv->buffer();
376
377         if (stack.empty()) {
378                 finishNoUndo(bv);
379                 return false;
380         }
381
382         shared_ptr<Undo> undo = stack.top();
383         stack.pop();
384         finishUndo();
385
386         if (!undo_frozen) {
387 /*
388                 ParIterator p = b->getParFromID(undo->number_of_before_par);
389                 bool ok = false;
390                 ParagraphList::iterator first;
391                 // default constructed?
392                 ParIterator const end = b->par_iterator_end();
393                 if (p != end) {
394                         first = p.par();
395                         if (first->next())
396                                 first = first->next();
397                 } else
398                         first = undoParagraphs(bv, undo->number_of_inset_id)->begin();
399                 if (first) {
400                         shared_ptr<Undo> u;
401                         ParIterator behind = b->getParFromID(undo->number_of_behind_par);
402                         if (createUndo(bv, undo->kind, first, behind.par(), u))
403                                 otherstack.push(u);
404                 }
405 */
406         }
407
408         // Now we can unlock the inset for saftey because the inset
409         // pointer could be changed during the undo-function. Anyway
410         // if needed we have to lock the right inset/position if this
411         // is requested.
412         freezeUndo();
413         bv->unlockInset(bv->theLockingInset());
414         bool const ret = textHandleUndo(bv, *undo.get());
415         unFreezeUndo();
416         return ret;
417 }
418
419 } // namespace anon
420
421
422 void finishUndo()
423 {
424         // Makes sure the next operation will be stored.
425         undo_finished = true;
426 }
427
428
429 void freezeUndo()
430 {
431         // This is dangerous and for internal use only.
432         undo_frozen = true;
433 }
434
435
436 void unFreezeUndo()
437 {
438         // This is dangerous and for internal use only.
439         undo_frozen = false;
440 }
441
442
443 bool textUndo(BufferView * bv)
444 {
445         return textUndoOrRedo(bv, bv->buffer()->undostack,
446                               bv->buffer()->redostack);
447 }
448
449
450 bool textRedo(BufferView * bv)
451 {
452         return textUndoOrRedo(bv, bv->buffer()->redostack,
453                               bv->buffer()->undostack);
454 }
455
456
457 void setUndo(BufferView * bv, Undo::undo_kind kind,
458              ParagraphList::iterator first)
459 {
460         setUndo(bv, kind, first, first);
461 }
462
463
464 void setUndo(BufferView * bv, Undo::undo_kind kind,
465              ParagraphList::iterator first, ParagraphList::iterator last)
466 {
467 #warning DISABLED
468         return;
469         if (!undo_frozen) {
470                 shared_ptr<Undo> u;
471                 if (createUndo(bv, kind, first, last, u))
472                         bv->buffer()->undostack.push(u);
473                 bv->buffer()->redostack.clear();
474         }
475 }
476
477
478 void setCursorParUndo(BufferView * bv)
479 {
480         setUndo(bv, Undo::FINISH, bv->text->cursor.par());
481 }