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