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
33 * export. To avoid that every keystroke results in a separate change, a
34 * tolerance interval of 5 minutes is used. That means if there are two adjacent
35 * changes that only differ in their change time with abs(ct1 - ct2) < 300 sec,
36 * they will be merged (and the later change time is preserved).
37 * Technically, the check for equality (or similarity) is made in operator==(...).
38 * The merging of similar changes happens in method merge().
41 bool operator==(Change const & l, Change const & r)
43 if (l.type != r.type) {
47 if (l.type == Change::UNCHANGED) {
51 return l.author == r.author
52 // both changes made within 5 minutes?
53 && abs(difftime(l.changetime, r.changetime)) < 300;
57 bool operator!=(Change const & l, Change const & r)
63 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
65 return r1.start == r2.start && r1.end == r2.end;
69 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
75 bool Changes::Range::contains(Range const & r) const
77 return r.start >= start && r.end <= end;
81 bool Changes::Range::contains(pos_type const pos) const
83 return pos >= start && pos < end;
87 bool Changes::Range::intersects(Range const & r) const
89 return r.start < end && r.end > start; // end itself is not in the range!
93 void Changes::set(Change const & change, pos_type const pos)
95 set(change, pos, pos + 1);
99 void Changes::set(Change const & change, pos_type const start, pos_type const end)
101 if (lyxerr.debugging(Debug::CHANGES)) {
102 lyxerr[Debug::CHANGES] << "setting change (type: " << change.type
103 << ", author: " << change.author << ", time: " << change.changetime
104 << ") in range (" << start << ", " << end << ")" << endl;
107 Range const newRange(start, end);
109 ChangeTable::iterator it = table_.begin();
111 for (; it != table_.end(); ) {
112 // current change starts like or follows new change
113 if (it->range.start >= start) {
117 // new change intersects with existing change
118 if (it->range.end > start) {
119 pos_type oldEnd = it->range.end;
120 it->range.end = start;
121 if (lyxerr.debugging(Debug::CHANGES)) {
122 lyxerr[Debug::CHANGES] << " cutting tail of type " << it->change.type
123 << " resulting in range (" << it->range.start << ", "
124 << it->range.end << ")" << endl;
128 if (lyxerr.debugging(Debug::CHANGES)) {
129 lyxerr[Debug::CHANGES] << " inserting tail in range ("
130 << end << ", " << oldEnd << ")" << endl;
132 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
140 if (change.type != Change::UNCHANGED) {
141 if (lyxerr.debugging(Debug::CHANGES)) {
142 lyxerr[Debug::CHANGES] << " inserting change" << endl;
144 it = table_.insert(it, ChangeRange(change, Range(start, end)));
148 for (; it != table_.end(); ) {
149 // new change 'contains' existing change
150 if (newRange.contains(it->range)) {
151 if (lyxerr.debugging(Debug::CHANGES)) {
152 lyxerr[Debug::CHANGES] << " removing subrange ("
153 << it->range.start << ", " << it->range.end << ")" << endl;
155 it = table_.erase(it);
159 // new change precedes existing change
160 if (it->range.start >= end) {
164 // new change intersects with existing change
165 it->range.start = end;
166 if (lyxerr.debugging(Debug::CHANGES)) {
167 lyxerr[Debug::CHANGES] << " cutting head of type "
168 << it->change.type << " resulting in range ("
169 << end << ", " << it->range.end << ")" << endl;
171 break; // no need for another iteration
178 void Changes::erase(pos_type const pos)
180 if (lyxerr.debugging(Debug::CHANGES)) {
181 lyxerr[Debug::CHANGES] << "Erasing change at position " << pos << endl;
184 ChangeTable::iterator it = table_.begin();
185 ChangeTable::iterator end = table_.end();
187 for (; it != end; ++it) {
188 // range (pos,pos+x) becomes (pos,pos+x-1)
189 if (it->range.start > pos) {
192 // range (pos-x,pos) stays (pos-x,pos)
193 if (it->range.end > pos) {
202 void Changes::insert(Change const & change, lyx::pos_type pos)
204 if (lyxerr.debugging(Debug::CHANGES)) {
205 lyxerr[Debug::CHANGES] << "Inserting change of type " << change.type
206 << " at position " << pos << endl;
209 ChangeTable::iterator it = table_.begin();
210 ChangeTable::iterator end = table_.end();
212 for (; it != end; ++it) {
213 // range (pos,pos+x) becomes (pos+1,pos+x+1)
214 if (it->range.start >= pos) {
218 // range (pos-x,pos) stays as it is
219 if (it->range.end > pos) {
224 set(change, pos, pos + 1); // set will call merge
228 Change const Changes::lookup(pos_type const pos) const
230 if (table_.empty()) {
231 return Change(Change::UNCHANGED);
233 ChangeTable::const_iterator it = table_.begin();
234 ChangeTable::const_iterator const end = table_.end();
236 for (; it != end; ++it) {
237 if (it->range.contains(pos))
241 return Change(Change::UNCHANGED);
245 bool Changes::isChanged(pos_type const start, pos_type const end) const
247 ChangeTable::const_iterator it = table_.begin();
248 ChangeTable::const_iterator const itend = table_.end();
250 for (; it != itend; ++it) {
251 if (it->range.intersects(Range(start, end))) {
252 if (lyxerr.debugging(Debug::CHANGES)) {
253 lyxerr[Debug::CHANGES] << "found intersection of range ("
254 << start << ", " << end << ") with ("
255 << it->range.start << ", " << it->range.end
256 << ") of type " << it->change.type << endl;
265 void Changes::merge()
267 if (lyxerr.debugging(Debug::CHANGES)) {
268 lyxerr[Debug::CHANGES] << "merging changes..." << endl;
271 ChangeTable::iterator it = table_.begin();
273 while (it != table_.end()) {
274 if (lyxerr.debugging(Debug::CHANGES)) {
275 lyxerr[Debug::CHANGES] << " found change of type " << it->change.type
276 << " and range (" << it->range.start << ", " << it->range.end
280 if (it->range.start == it->range.end) {
281 if (lyxerr.debugging(Debug::CHANGES)) {
282 lyxerr[Debug::CHANGES] << " removing empty range for pos "
283 << it->range.start << endl;
292 if (it + 1 == table_.end())
295 if (it->change == (it + 1)->change && it->range.end == (it + 1)->range.start) {
296 if (lyxerr.debugging(Debug::CHANGES)) {
297 lyxerr[Debug::CHANGES] << " merging ranges (" << it->range.start << ", "
298 << it->range.end << ") and (" << (it + 1)->range.start << ", "
299 << (it + 1)->range.end << ")" << endl;
301 (it + 1)->range.start = it->range.start;
302 (it + 1)->change.changetime = max(it->change.changetime,
303 (it + 1)->change.changetime);
315 int Changes::latexMarkChange(odocstream & os,
316 Change::Type const oldChangeType, Change::Type const changeType,
319 if (!output || oldChangeType == changeType)
322 static docstring const start(from_ascii("\\changestart{}"));
323 static docstring const end(from_ascii("\\changeend{}"));
324 static docstring const son(from_ascii("\\overstrikeon{}"));
325 static docstring const soff(from_ascii("\\overstrikeoff{}"));
329 if (oldChangeType == Change::DELETED) {
331 column += soff.length();
334 switch (changeType) {
335 case Change::UNCHANGED:
337 column += end.length();
340 case Change::DELETED:
341 if (oldChangeType == Change::UNCHANGED) {
343 column += start.length();
346 column += son.length();
349 case Change::INSERTED:
350 if (oldChangeType == Change::UNCHANGED) {
352 column += start.length();
361 void Changes::lyxMarkChange(std::ostream & os, int & column,
362 Change const & old, Change const & change)
369 switch (change.type) {
370 case Change::UNCHANGED:
371 os << "\n\\change_unchanged\n";
374 case Change::DELETED: {
375 os << "\n\\change_deleted " << change.author
376 << " " << change.changetime << "\n";
380 case Change::INSERTED: {
381 os << "\n\\change_inserted " << change.author
382 << " " << change.changetime << "\n";