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.
20 #include <boost/assert.hpp>
31 * Class Change has a changetime field that specifies the exact time at which
32 * a specific change was made. The change time is used as a guidance for the
33 * user while editing his document. Presently, it is not considered for LaTeX
34 * export. To avoid that every keystroke results in a separate change, a
35 * tolerance interval of 5 minutes is used. That means if there are two adjacent
36 * changes that only differ in their change time with abs(ct1 - ct2) < 300 sec,
37 * they will be merged (and the later change time is preserved).
38 * Technically, the check for equality (or similarity) is made in operator==(...).
39 * The merging of similar changes happens in method merge().
42 bool operator==(Change const & l, Change const & r)
44 if (l.type != r.type) {
48 if (l.type == Change::UNCHANGED) {
52 return l.author == r.author
53 // both changes made within 5 minutes?
54 && abs(difftime(l.changetime, r.changetime)) < 300;
58 bool operator!=(Change const & l, Change const & r)
64 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
66 return r1.start == r2.start && r1.end == r2.end;
70 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
76 bool Changes::Range::contains(Range const & r) const
78 return r.start >= start && r.end <= end;
82 bool Changes::Range::contains(pos_type const pos) const
84 return pos >= start && pos < end;
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 (lyxerr.debugging(Debug::CHANGES)) {
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;
122 if (lyxerr.debugging(Debug::CHANGES)) {
123 lyxerr[Debug::CHANGES] << " cutting tail of type " << it->change.type
124 << " resulting in range (" << it->range.start << ", "
125 << it->range.end << ")" << endl;
129 if (lyxerr.debugging(Debug::CHANGES)) {
130 lyxerr[Debug::CHANGES] << " inserting tail in range ("
131 << end << ", " << oldEnd << ")" << endl;
133 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
141 if (change.type != Change::UNCHANGED) {
142 if (lyxerr.debugging(Debug::CHANGES)) {
143 lyxerr[Debug::CHANGES] << " inserting change" << endl;
145 it = table_.insert(it, ChangeRange(change, Range(start, end)));
149 for (; it != table_.end(); ) {
150 // new change 'contains' existing change
151 if (newRange.contains(it->range)) {
152 if (lyxerr.debugging(Debug::CHANGES)) {
153 lyxerr[Debug::CHANGES] << " removing subrange ("
154 << it->range.start << ", " << it->range.end << ")" << endl;
156 it = table_.erase(it);
160 // new change precedes existing change
161 if (it->range.start >= end) {
165 // new change intersects with existing change
166 it->range.start = end;
167 if (lyxerr.debugging(Debug::CHANGES)) {
168 lyxerr[Debug::CHANGES] << " cutting head of type "
169 << it->change.type << " resulting in range ("
170 << end << ", " << it->range.end << ")" << endl;
172 break; // no need for another iteration
179 void Changes::erase(pos_type const pos)
181 if (lyxerr.debugging(Debug::CHANGES)) {
182 lyxerr[Debug::CHANGES] << "Erasing change at position " << pos << endl;
185 ChangeTable::iterator it = table_.begin();
186 ChangeTable::iterator end = table_.end();
188 for (; it != end; ++it) {
189 // range (pos,pos+x) becomes (pos,pos+x-1)
190 if (it->range.start > pos) {
193 // range (pos-x,pos) stays (pos-x,pos)
194 if (it->range.end > pos) {
203 void Changes::insert(Change const & change, lyx::pos_type pos)
205 if (lyxerr.debugging(Debug::CHANGES)) {
206 lyxerr[Debug::CHANGES] << "Inserting change of type " << change.type
207 << " at position " << pos << endl;
210 ChangeTable::iterator it = table_.begin();
211 ChangeTable::iterator end = table_.end();
213 for (; it != end; ++it) {
214 // range (pos,pos+x) becomes (pos+1,pos+x+1)
215 if (it->range.start >= pos) {
219 // range (pos-x,pos) stays as it is
220 if (it->range.end > pos) {
225 set(change, pos, pos + 1); // set will call merge
229 Change const Changes::lookup(pos_type const pos) const
231 if (table_.empty()) {
232 return Change(Change::UNCHANGED);
234 ChangeTable::const_iterator it = table_.begin();
235 ChangeTable::const_iterator const end = table_.end();
237 for (; it != end; ++it) {
238 if (it->range.contains(pos))
242 return Change(Change::UNCHANGED);
246 bool Changes::isChanged(pos_type const start, pos_type const end) const
248 ChangeTable::const_iterator it = table_.begin();
249 ChangeTable::const_iterator const itend = table_.end();
251 for (; it != itend; ++it) {
252 if (it->range.intersects(Range(start, end))) {
253 if (lyxerr.debugging(Debug::CHANGES)) {
254 lyxerr[Debug::CHANGES] << "found intersection of range ("
255 << start << ", " << end << ") with ("
256 << it->range.start << ", " << it->range.end
257 << ") of type " << it->change.type << endl;
266 void Changes::merge()
268 if (lyxerr.debugging(Debug::CHANGES)) {
269 lyxerr[Debug::CHANGES] << "merging changes..." << endl;
272 ChangeTable::iterator it = table_.begin();
274 while (it != table_.end()) {
275 if (lyxerr.debugging(Debug::CHANGES)) {
276 lyxerr[Debug::CHANGES] << " found change of type " << it->change.type
277 << " and range (" << it->range.start << ", " << it->range.end
281 if (it->range.start == it->range.end) {
282 if (lyxerr.debugging(Debug::CHANGES)) {
283 lyxerr[Debug::CHANGES] << " removing empty range for pos "
284 << it->range.start << endl;
293 if (it + 1 == table_.end())
296 if (it->change == (it + 1)->change && it->range.end == (it + 1)->range.start) {
297 if (lyxerr.debugging(Debug::CHANGES)) {
298 lyxerr[Debug::CHANGES] << " merging ranges (" << it->range.start << ", "
299 << it->range.end << ") and (" << (it + 1)->range.start << ", "
300 << (it + 1)->range.end << ")" << endl;
302 (it + 1)->range.start = it->range.start;
303 (it + 1)->change.changetime = max(it->change.changetime,
304 (it + 1)->change.changetime);
316 int Changes::latexMarkChange(odocstream & os,
317 Change::Type const oldChangeType, Change::Type const changeType,
320 if (!output || oldChangeType == changeType)
323 static docstring const start(from_ascii("\\changestart{}"));
324 static docstring const end(from_ascii("\\changeend{}"));
325 static docstring const son(from_ascii("\\overstrikeon{}"));
326 static docstring const soff(from_ascii("\\overstrikeoff{}"));
330 if (oldChangeType == Change::DELETED) {
332 column += soff.length();
335 switch (changeType) {
336 case Change::UNCHANGED:
338 column += end.length();
341 case Change::DELETED:
342 if (oldChangeType == Change::UNCHANGED) {
344 column += start.length();
347 column += son.length();
350 case Change::INSERTED:
351 if (oldChangeType == Change::UNCHANGED) {
353 column += start.length();
362 void Changes::lyxMarkChange(std::ostream & os, int & column,
363 Change const & old, Change const & change)
370 switch (change.type) {
371 case Change::UNCHANGED:
372 os << "\n\\change_unchanged\n";
375 case Change::DELETED: {
376 os << "\n\\change_deleted " << change.author
377 << " " << change.changetime << "\n";
381 case Change::INSERTED: {
382 os << "\n\\change_inserted " << change.author
383 << " " << change.changetime << "\n";