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