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>
29 * Class Change has a changetime field that specifies the exact time at which
30 * a specific change was made. The change time is used as a guidance for the
31 * user while editing his document. Presently, it is not considered for LaTeX
32 * export. To avoid that every keystroke results in a separate change, a
33 * tolerance interval of 5 minutes is used. That means if there are two adjacent
34 * changes that only differ in their change time with abs(ct1 - ct2) < 300 sec,
35 * they will be merged (and the later change time is preserved).
36 * Technically, the check for equality (or similarity) is made in operator==(...).
37 * The merging of similar changes happens in method merge().
40 bool operator==(Change const & l, Change const & r)
42 if (l.type != r.type) {
46 if (l.type == Change::UNCHANGED) {
50 return l.author == r.author
51 // both changes made within 5 minutes?
52 && abs(l.changetime - r.changetime) < 300;
56 bool operator!=(Change const & l, Change const & r)
62 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
64 return r1.start == r2.start && r1.end == r2.end;
68 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
74 bool Changes::Range::contains(Range const & r) const
76 return r.start >= start && r.end <= end;
80 bool Changes::Range::contains(pos_type const pos) const
82 return pos >= start && pos < end;
86 bool Changes::Range::intersects(Range const & r) const
88 return r.start < end && r.end > start; // end itself is not in the range!
92 void Changes::set(Change const & change, pos_type const pos)
94 set(change, pos, pos + 1);
98 void Changes::set(Change const & change, pos_type const start, pos_type const end)
100 if (lyxerr.debugging(Debug::CHANGES)) {
101 lyxerr[Debug::CHANGES] << "setting change (type: " << change.type
102 << ", author: " << change.author << ", time: " << change.changetime
103 << ") in range (" << start << ", " << end << ")" << endl;
106 Range const newRange(start, end);
108 ChangeTable::iterator it = table_.begin();
110 for (; it != table_.end(); ) {
111 // current change starts like or follows new change
112 if (it->range.start >= start) {
116 // new change intersects with existing change
117 if (it->range.end > start) {
118 pos_type oldEnd = it->range.end;
119 it->range.end = start;
120 if (lyxerr.debugging(Debug::CHANGES)) {
121 lyxerr[Debug::CHANGES] << " cutting tail of type " << it->change.type
122 << " resulting in range (" << it->range.start << ", "
123 << it->range.end << ")" << endl;
127 if (lyxerr.debugging(Debug::CHANGES)) {
128 lyxerr[Debug::CHANGES] << " inserting tail in range ("
129 << end << ", " << oldEnd << ")" << endl;
131 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
139 if (change.type != Change::UNCHANGED) {
140 if (lyxerr.debugging(Debug::CHANGES)) {
141 lyxerr[Debug::CHANGES] << " inserting change" << endl;
143 it = table_.insert(it, ChangeRange(change, Range(start, end)));
147 for (; it != table_.end(); ) {
148 // new change 'contains' existing change
149 if (newRange.contains(it->range)) {
150 if (lyxerr.debugging(Debug::CHANGES)) {
151 lyxerr[Debug::CHANGES] << " removing subrange ("
152 << it->range.start << ", " << it->range.end << ")" << endl;
154 it = table_.erase(it);
158 // new change precedes existing change
159 if (it->range.start >= end) {
163 // new change intersects with existing change
164 it->range.start = end;
165 if (lyxerr.debugging(Debug::CHANGES)) {
166 lyxerr[Debug::CHANGES] << " cutting head of type "
167 << it->change.type << " resulting in range ("
168 << end << ", " << it->range.end << ")" << endl;
170 break; // no need for another iteration
177 void Changes::erase(pos_type const pos)
179 if (lyxerr.debugging(Debug::CHANGES)) {
180 lyxerr[Debug::CHANGES] << "Erasing change at position " << pos << endl;
183 ChangeTable::iterator it = table_.begin();
184 ChangeTable::iterator end = table_.end();
186 for (; it != end; ++it) {
187 // range (pos,pos+x) becomes (pos,pos+x-1)
188 if (it->range.start > pos) {
191 // range (pos-x,pos) stays (pos-x,pos)
192 if (it->range.end > pos) {
201 void Changes::insert(Change const & change, lyx::pos_type pos)
203 if (lyxerr.debugging(Debug::CHANGES)) {
204 lyxerr[Debug::CHANGES] << "Inserting change of type " << change.type
205 << " at position " << pos << endl;
208 ChangeTable::iterator it = table_.begin();
209 ChangeTable::iterator end = table_.end();
211 for (; it != end; ++it) {
212 // range (pos,pos+x) becomes (pos+1,pos+x+1)
213 if (it->range.start >= pos) {
217 // range (pos-x,pos) stays as it is
218 if (it->range.end > pos) {
223 set(change, pos, pos + 1); // set will call merge
227 Change const Changes::lookup(pos_type const pos) const
229 if (table_.empty()) {
230 return Change(Change::UNCHANGED);
232 ChangeTable::const_iterator it = table_.begin();
233 ChangeTable::const_iterator const end = table_.end();
235 for (; it != end; ++it) {
236 if (it->range.contains(pos))
240 return Change(Change::UNCHANGED);
244 bool Changes::isChanged(pos_type const start, pos_type const end) const
246 ChangeTable::const_iterator it = table_.begin();
247 ChangeTable::const_iterator const itend = table_.end();
249 for (; it != itend; ++it) {
250 if (it->range.intersects(Range(start, end))) {
251 if (lyxerr.debugging(Debug::CHANGES)) {
252 lyxerr[Debug::CHANGES] << "found intersection of range ("
253 << start << ", " << end << ") with ("
254 << it->range.start << ", " << it->range.end
255 << ") of type " << it->change.type << endl;
264 void Changes::merge()
266 if (lyxerr.debugging(Debug::CHANGES)) {
267 lyxerr[Debug::CHANGES] << "merging changes..." << endl;
270 ChangeTable::iterator it = table_.begin();
272 while (it != table_.end()) {
273 if (lyxerr.debugging(Debug::CHANGES)) {
274 lyxerr[Debug::CHANGES] << " found change of type " << it->change.type
275 << " and range (" << it->range.start << ", " << it->range.end
279 if (it->range.start == it->range.end) {
280 if (lyxerr.debugging(Debug::CHANGES)) {
281 lyxerr[Debug::CHANGES] << " removing empty range for pos "
282 << it->range.start << endl;
291 if (it + 1 == table_.end())
294 if (it->change == (it + 1)->change && it->range.end == (it + 1)->range.start) {
295 if (lyxerr.debugging(Debug::CHANGES)) {
296 lyxerr[Debug::CHANGES] << " merging ranges (" << it->range.start << ", "
297 << it->range.end << ") and (" << (it + 1)->range.start << ", "
298 << (it + 1)->range.end << ")" << endl;
300 (it + 1)->range.start = it->range.start;
301 (it + 1)->change.changetime = max(it->change.changetime,
302 (it + 1)->change.changetime);
314 int Changes::latexMarkChange(odocstream & os,
315 Change::Type const oldChangeType, Change::Type const changeType,
318 if (!output || oldChangeType == changeType)
321 static docstring const start(from_ascii("\\changestart{}"));
322 static docstring const end(from_ascii("\\changeend{}"));
323 static docstring const son(from_ascii("\\overstrikeon{}"));
324 static docstring const soff(from_ascii("\\overstrikeoff{}"));
328 if (oldChangeType == Change::DELETED) {
330 column += soff.length();
333 switch (changeType) {
334 case Change::UNCHANGED:
336 column += end.length();
339 case Change::DELETED:
340 if (oldChangeType == Change::UNCHANGED) {
342 column += start.length();
345 column += son.length();
348 case Change::INSERTED:
349 if (oldChangeType == Change::UNCHANGED) {
351 column += start.length();
360 void Changes::lyxMarkChange(std::ostream & os, int & column,
361 Change const & old, Change const & change)
368 switch (change.type) {
369 case Change::UNCHANGED:
370 os << "\n\\change_unchanged\n";
373 case Change::DELETED: {
374 os << "\n\\change_deleted " << change.author
375 << " " << change.changetime << "\n";
379 case Change::INSERTED: {
380 os << "\n\\change_inserted " << change.author
381 << " " << change.changetime << "\n";