3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
11 * Record changes in a paragraph.
19 #include <boost/assert.hpp>
30 * Class Change has a changetime field that specifies the exact time at which
31 * a specific change was made. The change time is used as a guidance for the
32 * user while editing his document. Presently, it is not considered for LaTeX
34 * When merging two adjacent changes, the changetime is not considered,
35 * only the equality of the change type and author is checked (in method
36 * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
37 * the later change time is preserved.
40 bool Change::isSimilarTo(Change const & change)
42 if (type != change.type) {
46 if (type == Change::UNCHANGED) {
50 return author == change.author;
54 bool operator==(Change const & l, Change const & r)
56 if (l.type != r.type) {
60 // two changes of type UNCHANGED are always equal
61 if (l.type == Change::UNCHANGED) {
65 return l.author == r.author &&
66 l.changetime == r.changetime;
70 bool operator!=(Change const & l, Change const & r)
76 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
78 return r1.start == r2.start && r1.end == r2.end;
82 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
88 bool Changes::Range::intersects(Range const & r) const
90 return r.start < end && r.end > start; // end itself is not in the range!
94 void Changes::set(Change const & change, pos_type const pos)
96 set(change, pos, pos + 1);
100 void Changes::set(Change const & change, pos_type const start, pos_type const end)
102 if (change.type != Change::UNCHANGED) {
103 LYXERR(Debug::CHANGES) << "setting change (type: " << change.type
104 << ", author: " << change.author << ", time: " << change.changetime
105 << ") in range (" << start << ", " << end << ")" << endl;
108 Range const newRange(start, end);
110 ChangeTable::iterator it = table_.begin();
112 for (; it != table_.end(); ) {
113 // current change starts like or follows new change
114 if (it->range.start >= start) {
118 // new change intersects with existing change
119 if (it->range.end > start) {
120 pos_type oldEnd = it->range.end;
121 it->range.end = start;
123 LYXERR(Debug::CHANGES) << " cutting tail of type " << it->change.type
124 << " resulting in range (" << it->range.start << ", "
125 << it->range.end << ")" << endl;
129 LYXERR(Debug::CHANGES) << " inserting tail in range ("
130 << end << ", " << oldEnd << ")" << endl;
131 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
139 if (change.type != Change::UNCHANGED) {
140 LYXERR(Debug::CHANGES) << " inserting change" << endl;
141 it = table_.insert(it, ChangeRange(change, Range(start, end)));
145 for (; it != table_.end(); ) {
146 // new change 'contains' existing change
147 if (newRange.contains(it->range)) {
148 LYXERR(Debug::CHANGES) << " removing subrange ("
149 << it->range.start << ", " << it->range.end << ")" << endl;
150 it = table_.erase(it);
154 // new change precedes existing change
155 if (it->range.start >= end) {
159 // new change intersects with existing change
160 it->range.start = end;
161 LYXERR(Debug::CHANGES) << " cutting head of type "
162 << it->change.type << " resulting in range ("
163 << end << ", " << it->range.end << ")" << endl;
164 break; // no need for another iteration
171 void Changes::erase(pos_type const pos)
173 LYXERR(Debug::CHANGES) << "Erasing change at position " << pos << endl;
175 ChangeTable::iterator it = table_.begin();
176 ChangeTable::iterator end = table_.end();
178 for (; it != end; ++it) {
179 // range (pos,pos+x) becomes (pos,pos+x-1)
180 if (it->range.start > pos) {
183 // range (pos-x,pos) stays (pos-x,pos)
184 if (it->range.end > pos) {
193 void Changes::insert(Change const & change, lyx::pos_type pos)
195 if (change.type != Change::UNCHANGED) {
196 LYXERR(Debug::CHANGES) << "Inserting change of type " << change.type
197 << " at position " << pos << endl;
200 ChangeTable::iterator it = table_.begin();
201 ChangeTable::iterator end = table_.end();
203 for (; it != end; ++it) {
204 // range (pos,pos+x) becomes (pos+1,pos+x+1)
205 if (it->range.start >= pos) {
209 // range (pos-x,pos) stays as it is
210 if (it->range.end > pos) {
215 set(change, pos, pos + 1); // set will call merge
219 Change const & Changes::lookup(pos_type const pos) const
221 static Change const noChange = Change(Change::UNCHANGED);
223 ChangeTable::const_iterator it = table_.begin();
224 ChangeTable::const_iterator const end = table_.end();
226 for (; it != end; ++it) {
227 if (it->range.contains(pos))
235 bool Changes::isChanged(pos_type const start, pos_type const end) const
237 ChangeTable::const_iterator it = table_.begin();
238 ChangeTable::const_iterator const itend = table_.end();
240 for (; it != itend; ++it) {
241 if (it->range.intersects(Range(start, end))) {
242 LYXERR(Debug::CHANGES) << "found intersection of range ("
243 << start << ", " << end << ") with ("
244 << it->range.start << ", " << it->range.end
245 << ") of type " << it->change.type << endl;
253 void Changes::merge()
255 ChangeTable::iterator it = table_.begin();
257 while (it != table_.end()) {
258 LYXERR(Debug::CHANGES) << "found change of type " << it->change.type
259 << " and range (" << it->range.start << ", " << it->range.end
262 if (it->range.start == it->range.end) {
263 LYXERR(Debug::CHANGES) << "removing empty range for pos "
264 << it->range.start << endl;
272 if (it + 1 == table_.end())
275 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
276 LYXERR(Debug::CHANGES) << "merging ranges (" << it->range.start << ", "
277 << it->range.end << ") and (" << (it + 1)->range.start << ", "
278 << (it + 1)->range.end << ")" << endl;
280 (it + 1)->range.start = it->range.start;
281 (it + 1)->change.changetime = max(it->change.changetime,
282 (it + 1)->change.changetime);
294 int Changes::latexMarkChange(odocstream & os,
295 Change::Type const oldChangeType, Change::Type const changeType,
298 if (!output || oldChangeType == changeType)
301 static docstring const start(from_ascii("\\changestart{}"));
302 static docstring const end(from_ascii("\\changeend{}"));
303 static docstring const son(from_ascii("\\overstrikeon{}"));
304 static docstring const soff(from_ascii("\\overstrikeoff{}"));
308 if (oldChangeType == Change::DELETED) {
310 column += soff.length();
313 switch (changeType) {
314 case Change::UNCHANGED:
316 column += end.length();
319 case Change::DELETED:
320 if (oldChangeType == Change::UNCHANGED) {
322 column += start.length();
325 column += son.length();
328 case Change::INSERTED:
329 if (oldChangeType == Change::UNCHANGED) {
331 column += start.length();
340 void Changes::lyxMarkChange(std::ostream & os, int & column,
341 Change const & old, Change const & change)
348 switch (change.type) {
349 case Change::UNCHANGED:
350 os << "\n\\change_unchanged\n";
353 case Change::DELETED: {
354 os << "\n\\change_deleted " << change.author
355 << " " << change.changetime << "\n";
359 case Change::INSERTED: {
360 os << "\n\\change_inserted " << change.author
361 << " " << change.changetime << "\n";