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