]> git.lyx.org Git - lyx.git/blob - src/undo.C
setFont rework + some code simplification
[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(BufferView * bv, 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         for ( ; pit != end; ++pit, ++tcount)
72                 if (pit.text(bv) == text)
73                         return pit;
74         lyxerr << "undo: should not happen" << std::endl;
75         return end;
76 }
77
78
79 // translates offset from buffer begin to ParIterator
80 ParIterator num2pit(BufferView * bv, int num)
81 {
82         Buffer * buf = bv->buffer();
83         ParIterator pit = buf->par_iterator_begin();
84         ParIterator end = buf->par_iterator_end();
85
86         for ( ; num && pit != end; ++pit, --num)
87                 ;
88
89         if (pit != end)
90                 return pit;
91
92         // don't crash early...
93         lyxerr << "undo: num2pit: num: " << num << std::endl;
94         BOOST_ASSERT(false);
95         return buf->par_iterator_begin();
96 }
97
98
99 void recordUndo(Undo::undo_kind kind,
100         LyXText * text, paroffset_type first_par, paroffset_type last_par,
101         limited_stack<Undo> & stack)
102 {
103         Buffer * buf = text->bv()->buffer();
104
105         int const end_par = text->ownerParagraphs().size() - last_par;
106
107         // Undo::ATOMIC are always recorded (no overlapping there).
108         // overlapping only with insert and delete inside one paragraph:
109         // nobody wants all removed character appear one by one when undoing.
110         if (!undo_finished && kind != Undo::ATOMIC) {
111                 // Check whether storing is needed.
112                 if (!buf->undostack().empty()
113                     && buf->undostack().top().kind == kind
114                     && buf->undostack().top().first_par == first_par
115                     && buf->undostack().top().end_par == end_par) {
116                         // No additonal undo recording needed -
117                         // effectively, we combine undo recordings to one.
118                         return;
119                 }
120         }
121
122         // make and push the Undo entry
123         int textnum;
124         ParIterator pit = text2pit(text->bv(), text, textnum);
125         stack.push(Undo(kind, textnum, pit.index(),
126                 first_par, end_par, text->cursor.par(), text->cursor.pos()));
127         lyxerr << "undo record: " << stack.top() << std::endl;
128
129         // record the relevant paragraphs
130         ParagraphList & undo_pars = stack.top().pars;
131
132         ParagraphList & plist = text->ownerParagraphs();
133         ParagraphList::iterator first = plist.begin();
134         advance(first, first_par);
135         ParagraphList::iterator last = plist.begin();
136         advance(last, last_par);
137
138         for (ParagraphList::iterator it = first; it != last; ++it)
139                 undo_pars.push_back(*it);
140         undo_pars.push_back(*last);
141
142         // and make sure that next time, we should be combining if possible
143         undo_finished = false;
144 }
145
146
147 // returns false if no undo possible
148 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
149 {
150         lyxerr << "undo, performing: " << undo << std::endl;
151         ParIterator pit = num2pit(bv, undo.text);
152         LyXText * text = pit.text(bv);
153         ParagraphList & plist = text->ownerParagraphs();
154
155         // remove new stuff between first and last
156         {
157                 ParagraphList::iterator first = plist.begin();
158                 advance(first, undo.first_par);
159                 ParagraphList::iterator last = plist.begin();
160                 advance(last, plist.size() - undo.end_par);
161                 plist.erase(first, ++last);
162         }
163
164         // re-insert old stuff instead
165         if (plist.empty()) {
166                 plist.assign(undo.pars.begin(), undo.pars.end());
167         } else {
168                 ParagraphList::iterator first = plist.begin();
169                 advance(first, undo.first_par);
170                 plist.insert(first, undo.pars.begin(), undo.pars.end());
171         }
172
173         // set cursor
174         lyxerr <<   "undo, text: " << undo.text
175                << " inset: " << pit.inset()
176                << " index: " << undo.index
177                << " par: " << undo.cursor_par
178                << " pos: " << undo.cursor_pos
179                << std::endl;
180
181         // set cursor again to force the position to be the right one
182         text->cursor.par(undo.cursor_par);
183         text->cursor.pos(undo.cursor_pos);
184
185         // clear any selection
186         text->clearSelection();
187         text->selection.cursor = text->cursor;
188         text->updateCounters();
189
190         // rebreak the entire lyxtext
191         bv->text->fullRebreak();
192
193         pit.lockPath(bv);
194         
195         finishUndo();
196         return true;
197 }
198
199
200 // returns false if no undo possible
201 bool textUndoOrRedo(BufferView * bv,
202         limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
203 {
204         if (stack.empty()) {
205                 // nothing to do
206                 finishUndo();
207                 return false;
208         }
209
210         Undo undo = stack.top();
211         stack.pop();
212         finishUndo();
213
214         if (!undo_frozen) {
215                 otherstack.push(undo);
216                 otherstack.top().pars.clear();
217                 ParIterator pit = num2pit(bv, undo.text);
218                 ParagraphList & plist = pit.plist();
219                 if (undo.first_par + undo.end_par <= int(plist.size())) {
220                         ParagraphList::iterator first = plist.begin();
221                         advance(first, undo.first_par);
222                         ParagraphList::iterator last = plist.begin();
223                         advance(last, plist.size() - undo.end_par + 1);
224                         otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
225                 }
226                 LyXText * text = pit.text(bv);
227                 otherstack.top().cursor_pos = text->cursor.pos();
228                 otherstack.top().cursor_par = text->cursor.par();
229                 lyxerr << " undo other: " << otherstack.top() << std::endl;
230         }
231
232         freezeUndo();
233         bool const ret = performUndoOrRedo(bv, undo);
234         unFreezeUndo();
235         return ret;
236 }
237
238 } // namespace anon
239
240
241 void freezeUndo()
242 {
243         // this is dangerous and for internal use only
244         undo_frozen = true;
245 }
246
247
248 void unFreezeUndo()
249 {
250         // this is dangerous and for internal use only
251         undo_frozen = false;
252 }
253
254
255 void finishUndo()
256 {
257         // makes sure the next operation will be stored
258         undo_finished = true;
259 }
260
261
262 bool textUndo(BufferView * bv)
263 {
264         return textUndoOrRedo(bv, bv->buffer()->undostack(),
265                               bv->buffer()->redostack());
266 }
267
268
269 bool textRedo(BufferView * bv)
270 {
271         return textUndoOrRedo(bv, bv->buffer()->redostack(),
272                               bv->buffer()->undostack());
273 }
274
275
276 void recordUndo(Undo::undo_kind kind,
277         LyXText const * text, paroffset_type first, paroffset_type last)
278 {
279         if (undo_frozen)
280                 return;
281         Buffer * buf = text->bv()->buffer();
282         recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
283         buf->redostack().clear();
284 }
285
286
287 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
288 {
289         recordUndo(kind, text, par, par);
290 }
291
292
293 void recordUndo(BufferView * bv, Undo::undo_kind kind)
294 {
295         recordUndo(kind, bv->text, bv->text->cursor.par());
296 }