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 <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)
45 if (type == Change::UNCHANGED)
48 return author == change.author;
52 bool operator==(Change const & l, Change const & r)
57 // two changes of type UNCHANGED are always equal
58 if (l.type == Change::UNCHANGED)
61 return l.author == r.author && l.changetime == r.changetime;
65 bool operator!=(Change const & l, Change const & r)
71 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
73 return r1.start == r2.start && r1.end == r2.end;
77 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
83 bool Changes::Range::intersects(Range const & r) const
85 return r.start < end && r.end > start; // end itself is not in the range!
89 void Changes::set(Change const & change, pos_type const pos)
91 set(change, pos, pos + 1);
95 void Changes::set(Change const & change, pos_type const start, pos_type const end)
97 if (change.type != Change::UNCHANGED) {
98 LYXERR(Debug::CHANGES, "setting change (type: " << change.type
99 << ", author: " << change.author
100 << ", time: " << long(change.changetime)
101 << ") in range (" << start << ", " << end << ")");
104 Range const newRange(start, end);
106 ChangeTable::iterator it = table_.begin();
108 for (; it != table_.end(); ) {
109 // current change starts like or follows new change
110 if (it->range.start >= start) {
114 // new change intersects with existing change
115 if (it->range.end > start) {
116 pos_type oldEnd = it->range.end;
117 it->range.end = start;
119 LYXERR(Debug::CHANGES, " cutting tail of type " << it->change.type
120 << " resulting in range (" << it->range.start << ", "
121 << it->range.end << ")");
125 LYXERR(Debug::CHANGES, " inserting tail in range ("
126 << end << ", " << oldEnd << ")");
127 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
135 if (change.type != Change::UNCHANGED) {
136 LYXERR(Debug::CHANGES, " inserting change");
137 it = table_.insert(it, ChangeRange(change, Range(start, end)));
141 for (; it != table_.end(); ) {
142 // new change 'contains' existing change
143 if (newRange.contains(it->range)) {
144 LYXERR(Debug::CHANGES, " removing subrange ("
145 << it->range.start << ", " << it->range.end << ")");
146 it = table_.erase(it);
150 // new change precedes existing change
151 if (it->range.start >= end)
154 // new change intersects with existing change
155 it->range.start = end;
156 LYXERR(Debug::CHANGES, " cutting head of type "
157 << it->change.type << " resulting in range ("
158 << end << ", " << it->range.end << ")");
159 break; // no need for another iteration
166 void Changes::erase(pos_type const pos)
168 LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
170 ChangeTable::iterator it = table_.begin();
171 ChangeTable::iterator end = table_.end();
173 for (; it != end; ++it) {
174 // range (pos,pos+x) becomes (pos,pos+x-1)
175 if (it->range.start > pos)
177 // range (pos-x,pos) stays (pos-x,pos)
178 if (it->range.end > pos)
186 void Changes::insert(Change const & change, lyx::pos_type pos)
188 if (change.type != Change::UNCHANGED) {
189 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
190 << " at position " << pos);
193 ChangeTable::iterator it = table_.begin();
194 ChangeTable::iterator end = table_.end();
196 for (; it != end; ++it) {
197 // range (pos,pos+x) becomes (pos+1,pos+x+1)
198 if (it->range.start >= pos)
201 // range (pos-x,pos) stays as it is
202 if (it->range.end > pos)
206 set(change, pos, pos + 1); // set will call merge
210 Change const & Changes::lookup(pos_type const pos) const
212 static Change const noChange = Change(Change::UNCHANGED);
214 ChangeTable::const_iterator it = table_.begin();
215 ChangeTable::const_iterator const end = table_.end();
217 for (; it != end; ++it) {
218 if (it->range.contains(pos))
226 bool Changes::isChanged(pos_type const start, pos_type const end) const
228 ChangeTable::const_iterator it = table_.begin();
229 ChangeTable::const_iterator const itend = table_.end();
231 for (; it != itend; ++it) {
232 if (it->range.intersects(Range(start, end))) {
233 LYXERR(Debug::CHANGES, "found intersection of range ("
234 << start << ", " << end << ") with ("
235 << it->range.start << ", " << it->range.end
236 << ") of type " << it->change.type);
244 void Changes::merge()
246 ChangeTable::iterator it = table_.begin();
248 while (it != table_.end()) {
249 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
250 << " and range (" << it->range.start << ", " << it->range.end
253 if (it->range.start == it->range.end) {
254 LYXERR(Debug::CHANGES, "removing empty range for pos "
263 if (it + 1 == table_.end())
266 if (it->change.isSimilarTo((it + 1)->change)
267 && it->range.end == (it + 1)->range.start) {
268 LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
269 << it->range.end << ") and (" << (it + 1)->range.start << ", "
270 << (it + 1)->range.end << ")");
272 (it + 1)->range.start = it->range.start;
273 (it + 1)->change.changetime = std::max(it->change.changetime,
274 (it + 1)->change.changetime);
286 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
287 Change const & oldChange, Change const & change)
289 if (!bparams.outputChanges || oldChange == change)
294 if (oldChange.type != Change::UNCHANGED) {
295 os << '}'; // close \lyxadded or \lyxdeleted
300 chgTime += ctime(&change.changetime);
301 chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
303 if (change.type == Change::DELETED) {
304 docstring str = "\\lyxdeleted{" +
305 bparams.authors().get(change.author).name() + "}{" +
308 column += str.size();
309 } else if (change.type == Change::INSERTED) {
310 docstring str = "\\lyxadded{" +
311 bparams.authors().get(change.author).name() + "}{" +
314 column += str.size();
321 void Changes::lyxMarkChange(std::ostream & os, int & column,
322 Change const & old, Change const & change)
329 switch (change.type) {
330 case Change::UNCHANGED:
331 os << "\n\\change_unchanged\n";
334 case Change::DELETED: {
335 os << "\n\\change_deleted " << change.author
336 << " " << change.changetime << "\n";
340 case Change::INSERTED: {
341 os << "\n\\change_inserted " << change.author
342 << " " << change.changetime << "\n";
349 void Changes::checkAuthors(AuthorList const & authorList)
351 ChangeTable::const_iterator it = table_.begin();
352 ChangeTable::const_iterator endit = table_.end();
353 for ( ; it != endit ; ++it)
354 if (it->change.type != Change::UNCHANGED)
355 authorList.get(it->change.author).setUsed(true);