]> git.lyx.org Git - lyx.git/blob - src/undo.C
Small refactoring of DocBook stuff
[lyx.git] / src / undo.C
1 /**
2  * \file undo.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.h"
18
19 #include "buffer.h"
20 #include "debug.h"
21 #include "BufferView.h"
22 #include "iterators.h"
23 #include "lyxtext.h"
24 #include "paragraph.h"
25
26 #include "insets/updatableinset.h" // for dynamic_cast<UpdatableInset *>
27
28 using lyx::paroffset_type;
29
30
31 /// The flag used by finishUndo().
32 bool undo_finished;
33
34 /// Whether actions are not added to the undo stacks.
35 bool undo_frozen;
36
37 Undo::Undo(undo_kind kind_, int text_, int index_,
38            int first_par_, int end_par_, int cursor_par_, int cursor_pos_)
39         :
40                 kind(kind_),
41                 text(text_),
42                 index(index_),
43                 first_par(first_par_),
44                 end_par(end_par_),
45                 cursor_par(cursor_par_),
46                 cursor_pos(cursor_pos_)
47 {}
48
49
50 namespace {
51
52 std::ostream & operator<<(std::ostream & os, Undo const & undo)
53 {
54         return os << " text: " << undo.text
55                 << " index: " << undo.index
56                 << " first: " << undo.first_par
57                 << " from end: " << undo.end_par
58                 << " cursor: " << undo.cursor_par
59                 << "/" << undo.cursor_pos;
60 }
61
62
63 // translates LyXText pointer into offset count from document begin
64 ParIterator text2pit(LyXText * text, int & tcount)
65 {
66         tcount = 0;
67         Buffer * buf = text->bv()->buffer();
68         ParIterator pit = buf->par_iterator_begin();
69         ParIterator end = buf->par_iterator_end();
70
71         // it.text() returns 0 for outermost text.
72         if (text == text->bv()->text)
73                 return pit;
74
75         for ( ; pit != end; ++pit, ++tcount)
76                 if (pit.text() == text)
77                         return pit;
78         lyxerr << "undo: should not happen" << std::endl;
79         return end;
80 }
81
82
83 // translates offset from buffer begin to ParIterator
84 ParIterator num2pit(BufferView * bv, int num)
85 {
86         Buffer * buf = bv->buffer();
87         ParIterator pit = buf->par_iterator_begin();
88         ParIterator end = buf->par_iterator_end();
89
90         for ( ; num && pit != end; ++pit, --num)
91                 ;
92         
93         if (pit != end)
94                 return pit;
95
96         // don't crash early...
97         lyxerr << "undo: num2pit: num: " << num << std::endl;
98         BOOST_ASSERT(false);
99         return buf->par_iterator_begin();
100 }
101
102
103 // translates offset from buffer begin to LyXText
104 LyXText * pit2text(BufferView * bv, ParIterator const & pit)
105 {
106         LyXText * text = pit.text();
107         return text ? text : bv->text;
108 }
109
110
111 void recordUndo(Undo::undo_kind kind,
112         LyXText * text, paroffset_type first_par, paroffset_type last_par,
113         limited_stack<Undo> & stack)
114 {
115         Buffer * buf = text->bv()->buffer();
116
117         int const end_par = text->ownerParagraphs().size() - last_par;
118
119         // Undo::ATOMIC are always recorded (no overlapping there).
120         // overlapping only with insert and delete inside one paragraph:
121         // nobody wants all removed character appear one by one when undoing.
122         if (!undo_finished && kind != Undo::ATOMIC) {
123                 // Check whether storing is needed.
124                 if (!buf->undostack().empty()
125                     && buf->undostack().top().kind == kind
126                     && buf->undostack().top().first_par == first_par
127                     && buf->undostack().top().end_par == end_par) {
128                         // No additonal undo recording needed -
129                         // effectively, we combine undo recordings to one.
130                         return;
131                 }
132         }
133
134         // make and push the Undo entry
135         int textnum;
136         ParIterator pit = text2pit(text, textnum);
137         stack.push(Undo(kind, textnum, pit.index(),
138                 first_par, end_par, text->cursor.par(), text->cursor.pos()));
139         lyxerr << "undo record: " << stack.top() << std::endl;
140
141         // record the relevant paragraphs
142         ParagraphList & undo_pars = stack.top().pars;
143
144         ParagraphList & plist = text->ownerParagraphs();
145         ParagraphList::iterator first = plist.begin();
146         advance(first, first_par);
147         ParagraphList::iterator last = plist.begin();
148         advance(last, last_par);
149
150         for (ParagraphList::iterator it = first; it != last; ++it)
151                 undo_pars.push_back(*it);
152         undo_pars.push_back(*last);
153
154         // and make sure that next time, we should be combining if possible
155         undo_finished = false;
156 }
157
158
159 // returns false if no undo possible
160 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
161 {
162         lyxerr << "undo, performing: " << undo << std::endl;
163         ParIterator pit = num2pit(bv, undo.text);
164         LyXText * text = pit2text(bv, pit);
165         ParagraphList & plist = text->ownerParagraphs();
166
167         // remove new stuff between first and last
168         {
169                 ParagraphList::iterator first = plist.begin();
170                 advance(first, undo.first_par);
171                 ParagraphList::iterator last = plist.begin();
172                 advance(last, plist.size() - undo.end_par);
173                 plist.erase(first, ++last);
174         }
175
176         // re-insert old stuff instead
177         if (plist.empty()) {
178                 plist.assign(undo.pars.begin(), undo.pars.end());
179         } else {
180                 ParagraphList::iterator first = plist.begin();
181                 advance(first, undo.first_par);
182                 plist.insert(first, undo.pars.begin(), undo.pars.end());
183         }
184
185         // set cursor
186         lyxerr <<   "undo, text: " << undo.text
187                 << " inset: " << pit.inset()
188                 << " index: " << undo.index
189                 << std::endl;
190
191         // set cursor again to force the position to be the right one
192         text->cursor.par(undo.cursor_par);
193         text->cursor.pos(undo.cursor_pos);
194
195         // clear any selection
196         text->clearSelection();
197         text->selection.cursor = text->cursor;
198         text->updateCounters();
199
200         // rebreak the entire lyxtext
201         bv->text->fullRebreak();
202
203         InsetOld * inset = pit.inset();
204         if (inset) {
205                 // magic needed to cope with inset locking
206                 bv->lockInset(dynamic_cast<UpdatableInset *>(inset));
207         }
208
209         finishUndo();
210         return true;
211 }
212
213
214 // returns false if no undo possible
215 bool textUndoOrRedo(BufferView * bv,
216         limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
217 {
218         if (stack.empty()) {
219                 // nothing to do
220                 freezeUndo();
221                 bv->unlockInset(bv->theLockingInset());
222                 finishUndo();
223                 unFreezeUndo();
224                 return false;
225         }
226
227         Undo undo = stack.top();
228         stack.pop();
229         finishUndo();
230
231         if (!undo_frozen) {
232                 otherstack.push(undo);
233                 otherstack.top().pars.clear();
234                 ParIterator pit = num2pit(bv, undo.text);
235                 ParagraphList & plist = pit.plist();
236                 if (undo.first_par + undo.end_par <= int(plist.size())) {
237                         ParagraphList::iterator first = plist.begin();
238                         advance(first, undo.first_par);
239                         ParagraphList::iterator last = plist.begin();
240                         advance(last, plist.size() - undo.end_par + 1);
241                         otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
242                 }
243                 LyXText * text = pit2text(bv, pit);
244                 otherstack.top().cursor_pos = text->cursor.pos();
245                 otherstack.top().cursor_par = text->cursor.par();
246                 lyxerr << " undo other: " << otherstack.top() << std::endl;
247         }
248
249         // Now we can unlock the inset for safety because the inset
250         // pointer could be changed during the undo-function. Anyway
251         // if needed we have to lock the right inset/position if this
252         // is requested.
253         freezeUndo();
254         bv->unlockInset(bv->theLockingInset());
255         bool const ret = performUndoOrRedo(bv, undo);
256         unFreezeUndo();
257         return ret;
258 }
259
260 } // namespace anon
261
262
263 void freezeUndo()
264 {
265         // this is dangerous and for internal use only
266         undo_frozen = true;
267 }
268
269
270 void unFreezeUndo()
271 {
272         // this is dangerous and for internal use only
273         undo_frozen = false;
274 }
275
276
277 void finishUndo()
278 {
279         // makes sure the next operation will be stored
280         undo_finished = true;
281 }
282
283
284 bool textUndo(BufferView * bv)
285 {
286         return textUndoOrRedo(bv, bv->buffer()->undostack(),
287                               bv->buffer()->redostack());
288 }
289
290
291 bool textRedo(BufferView * bv)
292 {
293         return textUndoOrRedo(bv, bv->buffer()->redostack(),
294                               bv->buffer()->undostack());
295 }
296
297
298 void recordUndo(Undo::undo_kind kind,
299         LyXText const * text, paroffset_type first, paroffset_type last)
300 {
301         if (undo_frozen)
302                 return;
303         Buffer * buf = text->bv()->buffer();
304         recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
305         buf->redostack().clear();
306 }
307
308
309 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
310 {
311         recordUndo(kind, text, par, par);
312 }
313
314
315 void recordUndo(BufferView * bv, Undo::undo_kind kind)
316 {
317         recordUndo(kind, bv->text, bv->text->cursor.par());
318 }