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 "BufferParams.h"
20 #include "LaTeXFeatures.h"
22 #include <boost/assert.hpp>
29 * Class Change has a changetime field that specifies the exact time at which
30 * a specific change was made. The change time is used as a guidance for the
31 * user while editing his document. Presently, it is not considered for LaTeX
33 * When merging two adjacent changes, the changetime is not considered,
34 * only the equality of the change type and author is checked (in method
35 * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
36 * the later change time is preserved.
39 bool Change::isSimilarTo(Change const & change)
41 if (type != change.type)
44 if (type == Change::UNCHANGED)
47 return author == change.author;
51 bool operator==(Change const & l, Change const & r)
56 // two changes of type UNCHANGED are always equal
57 if (l.type == Change::UNCHANGED)
60 return l.author == r.author && l.changetime == r.changetime;
64 bool operator!=(Change const & l, Change const & r)
70 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
72 return r1.start == r2.start && r1.end == r2.end;
76 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
82 bool Changes::Range::intersects(Range const & r) const
84 return r.start < end && r.end > start; // end itself is not in the range!
88 void Changes::set(Change const & change, pos_type const pos)
90 set(change, pos, pos + 1);
94 void Changes::set(Change const & change, pos_type const start, pos_type const end)
96 if (change.type != Change::UNCHANGED) {
97 LYXERR(Debug::CHANGES, "setting change (type: " << change.type
98 << ", author: " << change.author << ", time: " << change.changetime
99 << ") in range (" << start << ", " << end << ")");
102 Range const newRange(start, end);
104 ChangeTable::iterator it = table_.begin();
106 for (; it != table_.end(); ) {
107 // current change starts like or follows new change
108 if (it->range.start >= start) {
112 // new change intersects with existing change
113 if (it->range.end > start) {
114 pos_type oldEnd = it->range.end;
115 it->range.end = start;
117 LYXERR(Debug::CHANGES, " cutting tail of type " << it->change.type
118 << " resulting in range (" << it->range.start << ", "
119 << it->range.end << ")");
123 LYXERR(Debug::CHANGES, " inserting tail in range ("
124 << end << ", " << oldEnd << ")");
125 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
133 if (change.type != Change::UNCHANGED) {
134 LYXERR(Debug::CHANGES, " inserting change");
135 it = table_.insert(it, ChangeRange(change, Range(start, end)));
139 for (; it != table_.end(); ) {
140 // new change 'contains' existing change
141 if (newRange.contains(it->range)) {
142 LYXERR(Debug::CHANGES, " removing subrange ("
143 << it->range.start << ", " << it->range.end << ")");
144 it = table_.erase(it);
148 // new change precedes existing change
149 if (it->range.start >= end)
152 // new change intersects with existing change
153 it->range.start = end;
154 LYXERR(Debug::CHANGES, " cutting head of type "
155 << it->change.type << " resulting in range ("
156 << end << ", " << it->range.end << ")");
157 break; // no need for another iteration
164 void Changes::erase(pos_type const pos)
166 LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
168 ChangeTable::iterator it = table_.begin();
169 ChangeTable::iterator end = table_.end();
171 for (; it != end; ++it) {
172 // range (pos,pos+x) becomes (pos,pos+x-1)
173 if (it->range.start > pos)
175 // range (pos-x,pos) stays (pos-x,pos)
176 if (it->range.end > pos)
184 void Changes::insert(Change const & change, lyx::pos_type pos)
186 if (change.type != Change::UNCHANGED) {
187 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
188 << " at position " << pos);
191 ChangeTable::iterator it = table_.begin();
192 ChangeTable::iterator end = table_.end();
194 for (; it != end; ++it) {
195 // range (pos,pos+x) becomes (pos+1,pos+x+1)
196 if (it->range.start >= pos)
199 // range (pos-x,pos) stays as it is
200 if (it->range.end > pos)
204 set(change, pos, pos + 1); // set will call merge
208 Change const & Changes::lookup(pos_type const pos) const
210 static Change const noChange = Change(Change::UNCHANGED);
212 ChangeTable::const_iterator it = table_.begin();
213 ChangeTable::const_iterator const end = table_.end();
215 for (; it != end; ++it) {
216 if (it->range.contains(pos))
224 bool Changes::isChanged(pos_type const start, pos_type const end) const
226 ChangeTable::const_iterator it = table_.begin();
227 ChangeTable::const_iterator const itend = table_.end();
229 for (; it != itend; ++it) {
230 if (it->range.intersects(Range(start, end))) {
231 LYXERR(Debug::CHANGES, "found intersection of range ("
232 << start << ", " << end << ") with ("
233 << it->range.start << ", " << it->range.end
234 << ") of type " << it->change.type);
242 void Changes::merge()
244 ChangeTable::iterator it = table_.begin();
246 while (it != table_.end()) {
247 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
248 << " and range (" << it->range.start << ", " << it->range.end
251 if (it->range.start == it->range.end) {
252 LYXERR(Debug::CHANGES, "removing empty range for pos "
261 if (it + 1 == table_.end())
264 if (it->change.isSimilarTo((it + 1)->change)
265 && it->range.end == (it + 1)->range.start) {
266 LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
267 << it->range.end << ") and (" << (it + 1)->range.start << ", "
268 << (it + 1)->range.end << ")");
270 (it + 1)->range.start = it->range.start;
271 (it + 1)->change.changetime = std::max(it->change.changetime,
272 (it + 1)->change.changetime);
284 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
285 Change const & oldChange, Change const & change)
287 if (!bparams.outputChanges || oldChange == change)
292 if (oldChange.type != Change::UNCHANGED) {
293 os << '}'; // close \lyxadded or \lyxdeleted
298 chgTime += ctime(&change.changetime);
299 chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
301 if (change.type == Change::DELETED) {
302 docstring str = "\\lyxdeleted{" +
303 bparams.authors().get(change.author).name() + "}{" +
306 column += str.size();
307 } else if (change.type == Change::INSERTED) {
308 docstring str = "\\lyxadded{" +
309 bparams.authors().get(change.author).name() + "}{" +
312 column += str.size();
319 void Changes::lyxMarkChange(std::ostream & os, int & column,
320 Change const & old, Change const & change)
327 switch (change.type) {
328 case Change::UNCHANGED:
329 os << "\n\\change_unchanged\n";
332 case Change::DELETED: {
333 os << "\n\\change_deleted " << change.author
334 << " " << change.changetime << "\n";
338 case Change::INSERTED: {
339 os << "\n\\change_inserted " << change.author
340 << " " << change.changetime << "\n";
347 void Changes::checkAuthors(AuthorList const & authorList)
349 ChangeTable::const_iterator it = table_.begin();
350 ChangeTable::const_iterator endit = table_.end();
351 for ( ; it != endit ; ++it)
352 if (it->change.type != Change::UNCHANGED)
353 authorList.get(it->change.author).setUsed(true);