]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
cosmetic fix
[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
160         // And repaint the lot
161         bv->text->postPaint();
162
163         return true;
164 }
165
166
167 // Returns false if no undo possible.
168 bool textUndoOrRedo(BufferView * bv,
169         limited_stack<Undo> & stack,
170         limited_stack<Undo> & otherstack)
171 {
172         if (stack.empty()) {
173                 /*
174                  * Finish the undo operation in the case there was no entry
175                  * on the stack to perform.
176                  */
177                 freezeUndo();
178                 bv->unlockInset(bv->theLockingInset());
179                 finishUndo();
180                 bv->text->postPaint();
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