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 << ", time: " << change.changetime
100 << ") in range (" << start << ", " << end << ")");
103 Range const newRange(start, end);
105 ChangeTable::iterator it = table_.begin();
107 for (; it != table_.end(); ) {
108 // current change starts like or follows new change
109 if (it->range.start >= start) {
113 // new change intersects with existing change
114 if (it->range.end > start) {
115 pos_type oldEnd = it->range.end;
116 it->range.end = start;
118 LYXERR(Debug::CHANGES, " cutting tail of type " << it->change.type
119 << " resulting in range (" << it->range.start << ", "
120 << it->range.end << ")");
124 LYXERR(Debug::CHANGES, " inserting tail in range ("
125 << end << ", " << oldEnd << ")");
126 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
134 if (change.type != Change::UNCHANGED) {
135 LYXERR(Debug::CHANGES, " inserting change");
136 it = table_.insert(it, ChangeRange(change, Range(start, end)));
140 for (; it != table_.end(); ) {
141 // new change 'contains' existing change
142 if (newRange.contains(it->range)) {
143 LYXERR(Debug::CHANGES, " removing subrange ("
144 << it->range.start << ", " << it->range.end << ")");
145 it = table_.erase(it);
149 // new change precedes existing change
150 if (it->range.start >= end)
153 // new change intersects with existing change
154 it->range.start = end;
155 LYXERR(Debug::CHANGES, " cutting head of type "
156 << it->change.type << " resulting in range ("
157 << end << ", " << it->range.end << ")");
158 break; // no need for another iteration
165 void Changes::erase(pos_type const pos)
167 LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
169 ChangeTable::iterator it = table_.begin();
170 ChangeTable::iterator end = table_.end();
172 for (; it != end; ++it) {
173 // range (pos,pos+x) becomes (pos,pos+x-1)
174 if (it->range.start > pos)
176 // range (pos-x,pos) stays (pos-x,pos)
177 if (it->range.end > pos)
185 void Changes::insert(Change const & change, lyx::pos_type pos)
187 if (change.type != Change::UNCHANGED) {
188 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
189 << " at position " << pos);
192 ChangeTable::iterator it = table_.begin();
193 ChangeTable::iterator end = table_.end();
195 for (; it != end; ++it) {
196 // range (pos,pos+x) becomes (pos+1,pos+x+1)
197 if (it->range.start >= pos)
200 // range (pos-x,pos) stays as it is
201 if (it->range.end > pos)
205 set(change, pos, pos + 1); // set will call merge
209 Change const & Changes::lookup(pos_type const pos) const
211 static Change const noChange = Change(Change::UNCHANGED);
213 ChangeTable::const_iterator it = table_.begin();
214 ChangeTable::const_iterator const end = table_.end();
216 for (; it != end; ++it) {
217 if (it->range.contains(pos))
225 bool Changes::isChanged(pos_type const start, pos_type const end) const
227 ChangeTable::const_iterator it = table_.begin();
228 ChangeTable::const_iterator const itend = table_.end();
230 for (; it != itend; ++it) {
231 if (it->range.intersects(Range(start, end))) {
232 LYXERR(Debug::CHANGES, "found intersection of range ("
233 << start << ", " << end << ") with ("
234 << it->range.start << ", " << it->range.end
235 << ") of type " << it->change.type);
243 void Changes::merge()
245 ChangeTable::iterator it = table_.begin();
247 while (it != table_.end()) {
248 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
249 << " and range (" << it->range.start << ", " << it->range.end
252 if (it->range.start == it->range.end) {
253 LYXERR(Debug::CHANGES, "removing empty range for pos "
262 if (it + 1 == table_.end())
265 if (it->change.isSimilarTo((it + 1)->change)
266 && it->range.end == (it + 1)->range.start) {
267 LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
268 << it->range.end << ") and (" << (it + 1)->range.start << ", "
269 << (it + 1)->range.end << ")");
271 (it + 1)->range.start = it->range.start;
272 (it + 1)->change.changetime = std::max(it->change.changetime,
273 (it + 1)->change.changetime);
285 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
286 Change const & oldChange, Change const & change)
288 if (!bparams.outputChanges || oldChange == change)
293 if (oldChange.type != Change::UNCHANGED) {
294 os << '}'; // close \lyxadded or \lyxdeleted
299 chgTime += ctime(&change.changetime);
300 chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
302 if (change.type == Change::DELETED) {
303 docstring str = "\\lyxdeleted{" +
304 bparams.authors().get(change.author).name() + "}{" +
307 column += str.size();
308 } else if (change.type == Change::INSERTED) {
309 docstring str = "\\lyxadded{" +
310 bparams.authors().get(change.author).name() + "}{" +
313 column += str.size();
320 void Changes::lyxMarkChange(std::ostream & os, int & column,
321 Change const & old, Change const & change)
328 switch (change.type) {
329 case Change::UNCHANGED:
330 os << "\n\\change_unchanged\n";
333 case Change::DELETED: {
334 os << "\n\\change_deleted " << change.author
335 << " " << change.changetime << "\n";
339 case Change::INSERTED: {
340 os << "\n\\change_inserted " << change.author
341 << " " << change.changetime << "\n";
348 void Changes::checkAuthors(AuthorList const & authorList)
350 ChangeTable::const_iterator it = table_.begin();
351 ChangeTable::const_iterator endit = table_.end();
352 for ( ; it != endit ; ++it)
353 if (it->change.type != Change::UNCHANGED)
354 authorList.get(it->change.author).setUsed(true);