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 && 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 (change.type != Change::UNCHANGED && 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 static Change const noChange = 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))
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 ChangeTable::iterator it = table_.begin();
269 while (it != table_.end()) {
270 if (lyxerr.debugging(Debug::CHANGES)) {
271 lyxerr[Debug::CHANGES] << "found change of type " << it->change.type
272 << " and range (" << it->range.start << ", " << it->range.end
276 if (it->range.start == it->range.end) {
277 if (lyxerr.debugging(Debug::CHANGES)) {
278 lyxerr[Debug::CHANGES] << "removing empty range for pos "
279 << it->range.start << endl;
288 if (it + 1 == table_.end())
291 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
292 if (lyxerr.debugging(Debug::CHANGES)) {
293 lyxerr[Debug::CHANGES] << "merging ranges (" << it->range.start << ", "
294 << it->range.end << ") and (" << (it + 1)->range.start << ", "
295 << (it + 1)->range.end << ")" << endl;
297 (it + 1)->range.start = it->range.start;
298 (it + 1)->change.changetime = max(it->change.changetime,
299 (it + 1)->change.changetime);
311 int Changes::latexMarkChange(odocstream & os,
312 Change::Type const oldChangeType, Change::Type const changeType,
315 if (!output || oldChangeType == changeType)
318 static docstring const start(from_ascii("\\changestart{}"));
319 static docstring const end(from_ascii("\\changeend{}"));
320 static docstring const son(from_ascii("\\overstrikeon{}"));
321 static docstring const soff(from_ascii("\\overstrikeoff{}"));
325 if (oldChangeType == Change::DELETED) {
327 column += soff.length();
330 switch (changeType) {
331 case Change::UNCHANGED:
333 column += end.length();
336 case Change::DELETED:
337 if (oldChangeType == Change::UNCHANGED) {
339 column += start.length();
342 column += son.length();
345 case Change::INSERTED:
346 if (oldChangeType == Change::UNCHANGED) {
348 column += start.length();
357 void Changes::lyxMarkChange(std::ostream & os, int & column,
358 Change const & old, Change const & change)
365 switch (change.type) {
366 case Change::UNCHANGED:
367 os << "\n\\change_unchanged\n";
370 case Change::DELETED: {
371 os << "\n\\change_deleted " << change.author
372 << " " << change.changetime << "\n";
376 case Change::INSERTED: {
377 os << "\n\\change_inserted " << change.author
378 << " " << change.changetime << "\n";