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.
18 #include "BufferParams.h"
19 #include "LaTeXFeatures.h"
21 #include "support/debug.h"
23 #include "support/lassert.h"
32 * Class Change has a changetime field that specifies the exact time at which
33 * a specific change was made. The change time is used as a guidance for the
34 * user while editing his document. Presently, it is not considered for LaTeX
36 * When merging two adjacent changes, the changetime is not considered,
37 * only the equality of the change type and author is checked (in method
38 * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
39 * the later change time is preserved.
42 bool Change::isSimilarTo(Change const & change) const
44 if (type != change.type)
47 if (type == Change::UNCHANGED)
50 return author == change.author;
54 ColorCode Change::color() const
56 ColorCode color = Color_none;
59 color = Color_changedtextauthor1;
62 color = Color_changedtextauthor2;
65 color = Color_changedtextauthor3;
68 color = Color_changedtextauthor4;
71 color = Color_changedtextauthor5;
78 bool operator==(Change const & l, Change const & r)
83 // two changes of type UNCHANGED are always equal
84 if (l.type == Change::UNCHANGED)
87 return l.author == r.author && l.changetime == r.changetime;
91 bool operator!=(Change const & l, Change const & r)
97 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
99 return r1.start == r2.start && r1.end == r2.end;
103 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
109 bool Changes::Range::intersects(Range const & r) const
111 return r.start < end && r.end > start; // end itself is not in the range!
115 void Changes::set(Change const & change, pos_type const pos)
117 set(change, pos, pos + 1);
121 void Changes::set(Change const & change, pos_type const start, pos_type const end)
123 if (change.type != Change::UNCHANGED) {
124 LYXERR(Debug::CHANGES, "setting change (type: " << change.type
125 << ", author: " << change.author
126 << ", time: " << long(change.changetime)
127 << ") in range (" << start << ", " << end << ")");
130 Range const newRange(start, end);
132 ChangeTable::iterator it = table_.begin();
134 for (; it != table_.end(); ) {
135 // current change starts like or follows new change
136 if (it->range.start >= start) {
140 // new change intersects with existing change
141 if (it->range.end > start) {
142 pos_type oldEnd = it->range.end;
143 it->range.end = start;
145 LYXERR(Debug::CHANGES, " cutting tail of type " << it->change.type
146 << " resulting in range (" << it->range.start << ", "
147 << it->range.end << ")");
151 LYXERR(Debug::CHANGES, " inserting tail in range ("
152 << end << ", " << oldEnd << ")");
153 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
161 if (change.type != Change::UNCHANGED) {
162 LYXERR(Debug::CHANGES, " inserting change");
163 it = table_.insert(it, ChangeRange(change, Range(start, end)));
167 for (; it != table_.end(); ) {
168 // new change 'contains' existing change
169 if (newRange.contains(it->range)) {
170 LYXERR(Debug::CHANGES, " removing subrange ("
171 << it->range.start << ", " << it->range.end << ")");
172 it = table_.erase(it);
176 // new change precedes existing change
177 if (it->range.start >= end)
180 // new change intersects with existing change
181 it->range.start = end;
182 LYXERR(Debug::CHANGES, " cutting head of type "
183 << it->change.type << " resulting in range ("
184 << end << ", " << it->range.end << ")");
185 break; // no need for another iteration
192 void Changes::erase(pos_type const pos)
194 LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
196 ChangeTable::iterator it = table_.begin();
197 ChangeTable::iterator end = table_.end();
199 for (; it != end; ++it) {
200 // range (pos,pos+x) becomes (pos,pos+x-1)
201 if (it->range.start > pos)
203 // range (pos-x,pos) stays (pos-x,pos)
204 if (it->range.end > pos)
212 void Changes::insert(Change const & change, lyx::pos_type pos)
214 if (change.type != Change::UNCHANGED) {
215 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
216 << " at position " << pos);
219 ChangeTable::iterator it = table_.begin();
220 ChangeTable::iterator end = table_.end();
222 for (; it != end; ++it) {
223 // range (pos,pos+x) becomes (pos+1,pos+x+1)
224 if (it->range.start >= pos)
227 // range (pos-x,pos) stays as it is
228 if (it->range.end > pos)
232 set(change, pos, pos + 1); // set will call merge
236 Change const & Changes::lookup(pos_type const pos) const
238 static Change const noChange = Change(Change::UNCHANGED);
240 ChangeTable::const_iterator it = table_.begin();
241 ChangeTable::const_iterator const end = table_.end();
243 for (; it != end; ++it) {
244 if (it->range.contains(pos))
252 bool Changes::isChanged(pos_type const start, pos_type const end) const
254 ChangeTable::const_iterator it = table_.begin();
255 ChangeTable::const_iterator const itend = table_.end();
257 for (; it != itend; ++it) {
258 if (it->range.intersects(Range(start, end))) {
259 LYXERR(Debug::CHANGES, "found intersection of range ("
260 << start << ", " << end << ") with ("
261 << it->range.start << ", " << it->range.end
262 << ") of type " << it->change.type);
270 void Changes::merge()
272 ChangeTable::iterator it = table_.begin();
274 while (it != table_.end()) {
275 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
276 << " and range (" << it->range.start << ", " << it->range.end
279 if (it->range.start == it->range.end) {
280 LYXERR(Debug::CHANGES, "removing empty range for pos "
289 if (it + 1 == table_.end())
292 if (it->change.isSimilarTo((it + 1)->change)
293 && it->range.end == (it + 1)->range.start) {
294 LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
295 << it->range.end << ") and (" << (it + 1)->range.start << ", "
296 << (it + 1)->range.end << ")");
298 (it + 1)->range.start = it->range.start;
299 (it + 1)->change.changetime = max(it->change.changetime,
300 (it + 1)->change.changetime);
312 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
313 Change const & oldChange, Change const & change)
315 if (!bparams.outputChanges || oldChange == change)
320 if (oldChange.type != Change::UNCHANGED) {
321 os << '}'; // close \lyxadded or \lyxdeleted
326 chgTime += ctime(&change.changetime);
327 chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
329 if (change.type == Change::DELETED) {
330 docstring str = "\\lyxdeleted{" +
331 bparams.authors().get(change.author).name() + "}{" +
334 column += str.size();
335 } else if (change.type == Change::INSERTED) {
336 docstring str = "\\lyxadded{" +
337 bparams.authors().get(change.author).name() + "}{" +
340 column += str.size();
347 void Changes::lyxMarkChange(ostream & os, int & column,
348 Change const & old, Change const & change)
355 switch (change.type) {
356 case Change::UNCHANGED:
357 os << "\n\\change_unchanged\n";
360 case Change::DELETED: {
361 os << "\n\\change_deleted " << change.author
362 << " " << change.changetime << "\n";
366 case Change::INSERTED: {
367 os << "\n\\change_inserted " << change.author
368 << " " << change.changetime << "\n";
375 void Changes::checkAuthors(AuthorList const & authorList)
377 ChangeTable::const_iterator it = table_.begin();
378 ChangeTable::const_iterator endit = table_.end();
379 for ( ; it != endit ; ++it)
380 if (it->change.type != Change::UNCHANGED)
381 authorList.get(it->change.author).setUsed(true);