3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
22 #include "BufferView.h"
23 #include "iterators.h"
25 #include "paragraph.h"
27 #include "insets/updatableinset.h" // for dynamic_cast<UpdatableInset *>
29 using lyx::paroffset_type;
32 /// The flag used by finishUndo().
35 /// Whether actions are not added to the undo stacks.
38 Undo::Undo(undo_kind kind_, int text_, int index_,
39 int first_par_, int end_par_, int cursor_par_, int cursor_pos_)
44 first_par(first_par_),
46 cursor_par(cursor_par_),
47 cursor_pos(cursor_pos_)
53 std::ostream & operator<<(std::ostream & os, Undo const & undo)
55 return os << " text: " << undo.text
56 << " index: " << undo.index
57 << " first: " << undo.first_par
58 << " from end: " << undo.end_par
59 << " cursor: " << undo.cursor_par
60 << "/" << undo.cursor_pos;
64 // translates LyXText pointer into offset count from document begin
65 ParIterator text2pit(Buffer & buf, LyXText * text, int & tcount)
68 ParIterator pit = buf.par_iterator_begin();
69 ParIterator end = buf.par_iterator_end();
71 for ( ; pit != end; ++pit, ++tcount)
72 if (pit.text(buf) == text)
74 lyxerr << "undo: should not happen" << std::endl;
79 // translates offset from buffer begin to ParIterator
80 ParIterator num2pit(Buffer & buf, int num)
82 ParIterator pit = buf.par_iterator_begin();
83 ParIterator end = buf.par_iterator_end();
85 for ( ; num && pit != end; ++pit, --num)
91 // don't crash early...
92 lyxerr << "undo: num2pit: num: " << num << std::endl;
94 return buf.par_iterator_begin();
98 void recordUndo(Undo::undo_kind kind,
99 LyXText * text, paroffset_type first_par, paroffset_type last_par,
100 limited_stack<Undo> & stack)
102 Buffer & buf = *text->bv()->buffer();
104 int const end_par = text->paragraphs().size() - last_par;
106 // Undo::ATOMIC are always recorded (no overlapping there).
107 // overlapping only with insert and delete inside one paragraph:
108 // nobody wants all removed character appear one by one when undoing.
109 if (!undo_finished && kind != Undo::ATOMIC) {
110 // Check whether storing is needed.
111 if (!buf.undostack().empty()
112 && buf.undostack().top().kind == kind
113 && buf.undostack().top().first_par == first_par
114 && buf.undostack().top().end_par == end_par) {
115 // No additonal undo recording needed -
116 // effectively, we combine undo recordings to one.
121 // make and push the Undo entry
123 ParIterator pit = text2pit(buf, text, textnum);
124 stack.push(Undo(kind, textnum, pit.index(),
125 first_par, end_par, text->cursor().par(), text->cursor().pos()));
126 lyxerr << "undo record: " << stack.top() << std::endl;
128 // record the relevant paragraphs
129 ParagraphList & undo_pars = stack.top().pars;
131 ParagraphList & plist = text->paragraphs();
132 ParagraphList::iterator first = plist.begin();
133 advance(first, first_par);
134 ParagraphList::iterator last = plist.begin();
135 advance(last, last_par);
137 for (ParagraphList::iterator it = first; it != last; ++it)
138 undo_pars.push_back(*it);
139 undo_pars.push_back(*last);
141 // and make sure that next time, we should be combining if possible
142 undo_finished = false;
146 // returns false if no undo possible
147 bool performUndoOrRedo(BufferView * bv, Undo const & undo)
149 Buffer & buf = *bv->buffer();
150 lyxerr << "undo, performing: " << undo << std::endl;
151 ParIterator pit = num2pit(buf, undo.text);
152 LyXText * text = pit.text(buf);
153 ParagraphList & plist = text->paragraphs();
155 // remove new stuff between first and last
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);
164 // re-insert old stuff instead
166 plist.assign(undo.pars.begin(), undo.pars.end());
168 ParagraphList::iterator first = plist.begin();
169 advance(first, undo.first_par);
170 plist.insert(first, undo.pars.begin(), undo.pars.end());
174 lyxerr << "undo, text: " << undo.text
175 << " inset: " << pit.inset()
176 << " index: " << undo.index
177 << " par: " << undo.cursor_par
178 << " pos: " << undo.cursor_pos
181 // set cursor again to force the position to be the right one
182 LCursor & cur = bv->cursor();
183 cur.par() = undo.cursor_par;
184 cur.pos() = undo.cursor_pos;
186 // clear any selection
187 cur.clearSelection();
189 text->updateCounters();
191 // rebreak the entire lyxtext
192 buf.text().fullRebreak();
195 text->setCursor(undo.cursor_par, undo.cursor_pos);
202 // returns false if no undo possible
203 bool textUndoOrRedo(BufferView * bv,
204 limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
206 Buffer & buf = *bv->buffer();
213 Undo undo = stack.top();
218 otherstack.push(undo);
219 otherstack.top().pars.clear();
220 ParIterator pit = num2pit(buf, undo.text);
221 ParagraphList & plist = pit.plist();
222 if (undo.first_par + undo.end_par <= int(plist.size())) {
223 ParagraphList::iterator first = plist.begin();
224 advance(first, undo.first_par);
225 ParagraphList::iterator last = plist.begin();
226 advance(last, plist.size() - undo.end_par + 1);
227 otherstack.top().pars.insert(otherstack.top().pars.begin(), first, last);
229 otherstack.top().cursor_pos = bv->cursor().pos();
230 otherstack.top().cursor_par = bv->cursor().par();
231 lyxerr << " undo other: " << otherstack.top() << std::endl;
235 bool const ret = performUndoOrRedo(bv, undo);
245 // this is dangerous and for internal use only
252 // this is dangerous and for internal use only
259 // makes sure the next operation will be stored
260 undo_finished = true;
264 bool textUndo(BufferView * bv)
266 return textUndoOrRedo(bv, bv->buffer()->undostack(),
267 bv->buffer()->redostack());
271 bool textRedo(BufferView * bv)
273 return textUndoOrRedo(bv, bv->buffer()->redostack(),
274 bv->buffer()->undostack());
278 void recordUndo(Undo::undo_kind kind,
279 LyXText const * text, paroffset_type first, paroffset_type last)
283 Buffer * buf = text->bv()->buffer();
284 recordUndo(kind, const_cast<LyXText *>(text), first, last, buf->undostack());
285 buf->redostack().clear();
289 void recordUndo(Undo::undo_kind kind, LyXText const * text, paroffset_type par)
291 recordUndo(kind, text, par, par);
295 void recordUndo(BufferView * bv, Undo::undo_kind kind)
297 recordUndo(bv->cursor(), kind);
301 void recordUndo(LCursor & cur, Undo::undo_kind kind)
303 recordUndo(kind, cur.bv().text(), cur.bv().text()->cursor().par());