]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
edit->LFUN_INSET_EDIT + CHangeLog + working URL insets.
[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         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(undoParagraphs(bv, num).begin(), 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 = &undoParagraphs(bv, undo.number_of_inset_id).front();
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 = undoParagraphs(bv, undo.number_of_inset_id).front().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 = undoParagraphs(bv, undo.number_of_inset_id).front().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                                 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
223                                 it->localDispatch(cmd);
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                                 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
248                                 it->localDispatch(cmd);
249                                 t = it->getLyXText(bv);
250                         } else {
251                                 t = bv->text;
252                         }
253                         t->setCursorIntern(tmppar, undo.cursor_pos);
254                         // Clear any selection and set the selection
255                         // cursor for an evt. new selection.
256                         t->clearSelection();
257                         t->selection.cursor = t->cursor;
258                         t->updateCounters();
259                 }
260         }
261
262         // And here it's safe enough to delete all removed paragraphs.
263         vector<Paragraph *>::iterator pit = deletelist.begin();
264         for(; pit != deletelist.end(); ++pit) {
265                 (*pit)->previous(0);
266                 (*pit)->next(0);
267                 delete (*pit);
268         }
269
270         // Otherwise the undo destructor would delete the paragraphs
271         undo.pars.resize(0);
272
273         finishUndo();
274         bv->text->postPaint(0);
275         return true;
276 }
277
278
279 bool createUndo(BufferView * bv, Undo::undo_kind kind,
280         ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
281         shared_ptr<Undo> & u)
282 {
283         Paragraph * const first = &*itfirst;
284         Paragraph * const behind = &*itbehind;
285         lyx::Assert(first);
286
287         int before_number = -1;
288         int behind_number = -1;
289         int inset_id = -1;
290
291         if (first->previous())
292                 before_number = first->previous()->id();
293         if (behind)
294                 behind_number = behind->id();
295         if (first->inInset())
296                 inset_id = first->inInset()->id();
297
298         Buffer * b = bv->buffer();
299
300         // Undo::EDIT  and Undo::FINISH are
301         // always finished. (no overlapping there)
302         // overlapping only with insert and delete inside one paragraph:
303         // Nobody wants all removed  character
304         // appear one by one when undoing.
305         // EDIT is special since only layout information, not the
306         // contents of a paragaph are stored.
307         if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
308                 // Check whether storing is needed.
309                 if (!b->undostack.empty() &&
310                     b->undostack.top()->kind == kind &&
311                     b->undostack.top()->number_of_before_par == before_number &&
312                     b->undostack.top()->number_of_behind_par == behind_number) {
313                         // No undo needed.
314                         return false;
315                 }
316         }
317
318         // Create a new Undo.
319         std::vector<Paragraph *> undo_pars;
320
321         Paragraph const * end = 0;
322
323         if (behind)
324                 end = behind->previous();
325         else {
326                 end = first;
327                 while (end->next())
328                         end = end->next();
329         }
330
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                 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
385                 if (first && first->next())
386                         first = first->next();
387                 else if (!first)
388                         first = &*undoParagraphs(bv, undo->number_of_inset_id).begin();
389                 if (first) {
390                         shared_ptr<Undo> u;
391                         if (createUndo(bv, undo->kind, first,
392                                              b->getParFromID(undo->number_of_behind_par), u))
393                                 otherstack.push(u);
394                 }
395         }
396
397         // Now we can unlock the inset for saftey because the inset
398         // pointer could be changed during the undo-function. Anyway
399         // if needed we have to lock the right inset/position if this
400         // is requested.
401         freezeUndo();
402         bv->unlockInset(bv->theLockingInset());
403         bool const ret = textHandleUndo(bv, *undo.get());
404         unFreezeUndo();
405         return ret;
406 }
407
408 } // namespace anon
409
410
411 void finishUndo()
412 {
413         // Makes sure the next operation will be stored.
414         undo_finished = true;
415 }
416
417
418 void freezeUndo()
419 {
420         // This is dangerous and for internal use only.
421         undo_frozen = true;
422 }
423
424
425 void unFreezeUndo()
426 {
427         // This is dangerous and for internal use only.
428         undo_frozen = false;
429 }
430
431
432 bool textUndo(BufferView * bv)
433 {
434         return textUndoOrRedo(bv, bv->buffer()->undostack,
435                               bv->buffer()->redostack);
436 }
437
438
439 bool textRedo(BufferView * bv)
440 {
441         return textUndoOrRedo(bv, bv->buffer()->redostack,
442                               bv->buffer()->undostack);
443 }
444
445
446 void setUndo(BufferView * bv, Undo::undo_kind kind,
447              ParagraphList::iterator first, ParagraphList::iterator behind)
448 {
449         if (!undo_frozen) {
450                 shared_ptr<Undo> u;
451                 if (createUndo(bv, kind, first, behind, u))
452                         bv->buffer()->undostack.push(u);
453                 bv->buffer()->redostack.clear();
454         }
455 }
456
457
458 void setRedo(BufferView * bv, Undo::undo_kind kind,
459              ParagraphList::iterator first, ParagraphList::iterator behind)
460 {
461         shared_ptr<Undo> u;
462         if (createUndo(bv, kind, first, behind, u))
463                 bv->buffer()->redostack.push(u);
464 }
465
466
467 void setCursorParUndo(BufferView * bv)
468 {
469         setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
470                 boost::next(bv->text->cursor.par()));
471 }