]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
Missing ChangeLog entry for the new socktools.[Ch] files.
[lyx.git] / src / undo_funcs.C
1 /**
2  * \file undo_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author André Pönitz
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "undo_funcs.h"
18
19 #include "buffer.h"
20 #include "debug.h"
21 #include "BufferView.h"
22 #include "funcrequest.h"
23 #include "iterators.h"
24 #include "lyxtext.h"
25 #include "paragraph.h"
26
27 #include "insets/updatableinset.h"
28
29 #include <iostream>
30
31 using std::endl;
32 using lyx::paroffset_type;
33
34
35 /// The flag used by finishUndo().
36 bool undo_finished;
37
38 /// Whether actions are not added to the undo stacks.
39 bool undo_frozen;
40
41 namespace {
42
43
44 void recordUndo(BufferView * bv, Undo::undo_kind kind,
45         ParagraphList & plist, paroffset_type firstpar, paroffset_type lastpar,
46         limited_stack<Undo> & stack)
47 {
48         Buffer * buf = bv->buffer();
49
50         ParagraphList::iterator first = plist.begin();
51         advance(first, firstpar);
52         ParagraphList::iterator last = plist.begin();
53         advance(last, lastpar);
54
55         // First, record inset id, if cursor is in one
56         UpdatableInset * inset = first->inInset();
57         LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
58         int const inset_id = inset ? inset->id() : -1;
59
60         // Try to find the appropriate list by counting the,
61         ParIterator null = buf->par_iterator_end();
62
63         int pcount = 0;
64         for (ParIterator it = buf->par_iterator_begin(); it != null; ++it, ++pcount)
65                 if (&it.plist() == &plist)
66                         break;
67                 
68         // And calculate a stable reference to them
69         int const first_offset = firstpar;
70         int const last_offset = plist.size() - lastpar;
71
72         // Undo::ATOMIC are always recorded (no overlapping there).
73         // Overlapping only with insert and delete inside one paragraph:
74         // Nobody wants all removed character appear one by one when undoing.
75         if (! undo_finished && kind != Undo::ATOMIC) {
76                 // Check whether storing is needed.
77                 if (! buf->undostack().empty()
78                     && buf->undostack().top().kind == kind
79                     && buf->undostack().top().first_par_offset == first_offset
80                     && buf->undostack().top().last_par_offset == last_offset) {
81                         // No additonal undo recording needed -
82                         // effectively, we combine undo recordings to one.
83                         return;
84                 }
85         }
86
87         // Record the cursor position in a stable way.
88         int const cursor_offset = text->cursor.par();
89
90         // Make and push the Undo entry
91         stack.push(Undo(kind, inset_id,
92                 pcount,
93                 first_offset, last_offset,
94                 cursor_offset, text->cursor.pos(),
95                 ParagraphList()));
96
97         // Record the relevant paragraphs
98         ParagraphList & undo_pars = stack.top().pars;
99
100         for (ParagraphList::iterator it = first; it != last; ++it) {
101                 undo_pars.push_back(*it);
102                 undo_pars.back().id(it->id());
103         }
104         undo_pars.push_back(*last);
105         undo_pars.back().id(last->id());
106
107         // And make sure that next time, we should be combining if possible
108         undo_finished = false;
109 }
110
111
112 // Returns false if no undo possible.
113 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
114 {
115         Buffer * buf = bv->buffer();
116
117         ParIterator null = buf->par_iterator_end();
118         ParIterator plit = buf->par_iterator_begin();
119         int n = undo.plist;
120         for ( ; n && plit != null; ++plit, --n)
121                 ;
122         BOOST_ASSERT(plit != null);
123         ParagraphList & plist = plit.plist();
124
125         // Remove new stuff between first and last
126         {
127                 ParagraphList::iterator first = plist.begin();
128                 advance(first, undo.first_par_offset);
129                 ParagraphList::iterator last = plist.begin();
130                 advance(last, plist.size() - undo.last_par_offset);
131                 plist.erase(first, ++last);
132         }
133
134         // Re-insert old stuff instead
135         {
136                 if (plist.empty()) {
137                         plist.assign(undo.pars.begin(), undo.pars.end());
138                 } else {
139                         ParagraphList::iterator first = plist.begin();
140                         advance(first, undo.first_par_offset);
141                         plist.insert(first, undo.pars.begin(), undo.pars.end());
142                 }
143         }
144
145         // Rebreak the entire document
146         bv->text->fullRebreak();
147
148         // set cursor
149         {
150                 // Get a hold of the inset for the cursor, if relevant
151                 UpdatableInset * inset =
152                         static_cast<UpdatableInset *>(
153                                 buf->getInsetFromID(undo.inset_id));
154
155                 LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
156                 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
157
158                 //lyxerr << "undo, inset: " << inset << endl;
159
160                 if (inset) {
161                         //lyxerr << "undo, inset owner: " << inset->owner() << endl;
162
163                         // Magic needed to update inset internal state
164                         FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
165                         if (inset->owner())
166                                 inset->owner()->localDispatch(cmd);
167                         else
168                                 inset->localDispatch(cmd);
169                 }
170
171                 // set cursor again to force the position to be the right one
172                 text->setCursorIntern(undo.cursor_par_offset, undo.cursor_pos);
173
174                 // Clear any selection and set the selection
175                 // cursor for any new selection.
176                 text->clearSelection();
177                 text->selection.cursor = text->cursor;
178                 text->updateCounters();
179         }
180
181         finishUndo();
182         return true;
183 }
184
185
186 // Returns false if no undo possible.
187 bool textUndoOrRedo(BufferView * bv,
188         limited_stack<Undo> & stack,
189         limited_stack<Undo> & otherstack)
190 {
191         if (stack.empty()) {
192                 /*
193                  * Finish the undo operation in the case there was no entry
194                  * on the stack to perform.
195                  */
196                 freezeUndo();
197                 bv->unlockInset(bv->theLockingInset());
198                 finishUndo();
199                 unFreezeUndo();
200                 return false;
201         }
202
203         Undo undo = stack.top();
204         stack.pop();
205         finishUndo();
206
207         if (!undo_frozen) {
208                 otherstack.push(undo);
209                 otherstack.top().pars.clear();
210                 Buffer * buf = bv->buffer();
211                 ParagraphList & plist = buf->paragraphs();
212                 if (undo.first_par_offset + undo.last_par_offset <= int(plist.size())) {
213                         ParagraphList::iterator first = plist.begin();
214                         advance(first, undo.first_par_offset);
215                         ParagraphList::iterator last = plist.begin();
216                         advance(last, plist.size() - undo.last_par_offset + 1);
217                         otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
218                 }
219         }
220
221         // Now we can unlock the inset for safety because the inset
222         // pointer could be changed during the undo-function. Anyway
223         // if needed we have to lock the right inset/position if this
224         // is requested.
225         freezeUndo();
226         bv->unlockInset(bv->theLockingInset());
227         bool const ret = performUndoOrRedo(bv, undo);
228         unFreezeUndo();
229         return ret;
230 }
231
232 } // namespace anon
233
234
235 void freezeUndo()
236 {
237         // This is dangerous and for internal use only.
238         undo_frozen = true;
239 }
240
241
242 void unFreezeUndo()
243 {
244         // This is dangerous and for internal use only.
245         undo_frozen = false;
246 }
247
248
249 void finishUndo()
250 {
251         // Makes sure the next operation will be stored.
252         undo_finished = true;
253 }
254
255
256 bool textUndo(BufferView * bv)
257 {
258         return textUndoOrRedo(bv, bv->buffer()->undostack(),
259                               bv->buffer()->redostack());
260 }
261
262
263 bool textRedo(BufferView * bv)
264 {
265         return textUndoOrRedo(bv, bv->buffer()->redostack(),
266                               bv->buffer()->undostack());
267 }
268
269
270 void recordUndo(BufferView * bv, Undo::undo_kind kind,
271         ParagraphList & plist, paroffset_type first, paroffset_type last)
272 {
273         if (!undo_frozen) {
274                 recordUndo(bv, kind, plist, first, last, bv->buffer()->undostack());
275                 bv->buffer()->redostack().clear();
276         }
277 }
278
279
280 void recordUndo(BufferView * bv, Undo::undo_kind kind,
281         ParagraphList & plist, paroffset_type par)
282 {
283         recordUndo(bv, kind, plist, par, par);
284 }
285
286
287 void recordUndo(BufferView * bv, Undo::undo_kind kind)
288 {
289         recordUndo(bv, kind, bv->text->ownerParagraphs(), bv->text->cursor.par());
290 }