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