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>
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)
44 if (type != change.type)
47 if (type == Change::UNCHANGED)
50 return author == change.author;
54 bool operator==(Change const & l, Change const & r)
59 // two changes of type UNCHANGED are always equal
60 if (l.type == Change::UNCHANGED)
63 return l.author == r.author && l.changetime == r.changetime;
67 bool operator!=(Change const & l, Change const & r)
73 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
75 return r1.start == r2.start && r1.end == r2.end;
79 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
85 bool Changes::Range::intersects(Range const & r) const
87 return r.start < end && r.end > start; // end itself is not in the range!
91 void Changes::set(Change const & change, pos_type const pos)
93 set(change, pos, pos + 1);
97 void Changes::set(Change const & change, pos_type const start, pos_type const end)
99 if (change.type != Change::UNCHANGED) {
100 LYXERR(Debug::CHANGES, "setting change (type: " << change.type
101 << ", author: " << change.author << ", time: " << change.changetime
102 << ") in range (" << start << ", " << end << ")");
105 Range const newRange(start, end);
107 ChangeTable::iterator it = table_.begin();
109 for (; it != table_.end(); ) {
110 // current change starts like or follows new change
111 if (it->range.start >= start) {
115 // new change intersects with existing change
116 if (it->range.end > start) {
117 pos_type oldEnd = it->range.end;
118 it->range.end = start;
120 LYXERR(Debug::CHANGES, " cutting tail of type " << it->change.type
121 << " resulting in range (" << it->range.start << ", "
122 << it->range.end << ")");
126 LYXERR(Debug::CHANGES, " inserting tail in range ("
127 << end << ", " << oldEnd << ")");
128 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
136 if (change.type != Change::UNCHANGED) {
137 LYXERR(Debug::CHANGES, " inserting change");
138 it = table_.insert(it, ChangeRange(change, Range(start, end)));
142 for (; it != table_.end(); ) {
143 // new change 'contains' existing change
144 if (newRange.contains(it->range)) {
145 LYXERR(Debug::CHANGES, " removing subrange ("
146 << it->range.start << ", " << it->range.end << ")");
147 it = table_.erase(it);
151 // new change precedes existing change
152 if (it->range.start >= end)
155 // new change intersects with existing change
156 it->range.start = end;
157 LYXERR(Debug::CHANGES, " cutting head of type "
158 << it->change.type << " resulting in range ("
159 << end << ", " << it->range.end << ")");
160 break; // no need for another iteration
167 void Changes::erase(pos_type const pos)
169 LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
171 ChangeTable::iterator it = table_.begin();
172 ChangeTable::iterator end = table_.end();
174 for (; it != end; ++it) {
175 // range (pos,pos+x) becomes (pos,pos+x-1)
176 if (it->range.start > pos)
178 // range (pos-x,pos) stays (pos-x,pos)
179 if (it->range.end > pos)
187 void Changes::insert(Change const & change, lyx::pos_type pos)
189 if (change.type != Change::UNCHANGED) {
190 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
191 << " at position " << pos);
194 ChangeTable::iterator it = table_.begin();
195 ChangeTable::iterator end = table_.end();
197 for (; it != end; ++it) {
198 // range (pos,pos+x) becomes (pos+1,pos+x+1)
199 if (it->range.start >= pos)
202 // range (pos-x,pos) stays as it is
203 if (it->range.end > pos)
207 set(change, pos, pos + 1); // set will call merge
211 Change const & Changes::lookup(pos_type const pos) const
213 static Change const noChange = Change(Change::UNCHANGED);
215 ChangeTable::const_iterator it = table_.begin();
216 ChangeTable::const_iterator const end = table_.end();
218 for (; it != end; ++it) {
219 if (it->range.contains(pos))
227 bool Changes::isChanged(pos_type const start, pos_type const end) const
229 ChangeTable::const_iterator it = table_.begin();
230 ChangeTable::const_iterator const itend = table_.end();
232 for (; it != itend; ++it) {
233 if (it->range.intersects(Range(start, end))) {
234 LYXERR(Debug::CHANGES, "found intersection of range ("
235 << start << ", " << end << ") with ("
236 << it->range.start << ", " << it->range.end
237 << ") of type " << it->change.type);
245 void Changes::merge()
247 ChangeTable::iterator it = table_.begin();
249 while (it != table_.end()) {
250 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
251 << " and range (" << it->range.start << ", " << it->range.end
254 if (it->range.start == it->range.end) {
255 LYXERR(Debug::CHANGES, "removing empty range for pos "
264 if (it + 1 == table_.end())
267 if (it->change.isSimilarTo((it + 1)->change)
268 && it->range.end == (it + 1)->range.start) {
269 LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
270 << it->range.end << ") and (" << (it + 1)->range.start << ", "
271 << (it + 1)->range.end << ")");
273 (it + 1)->range.start = it->range.start;
274 (it + 1)->change.changetime = max(it->change.changetime,
275 (it + 1)->change.changetime);
287 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
288 Change const & oldChange, Change const & change)
290 if (!bparams.outputChanges || oldChange == change)
295 if (oldChange.type != Change::UNCHANGED) {
296 os << '}'; // close \lyxadded or \lyxdeleted
301 chgTime += ctime(&change.changetime);
302 chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
304 if (change.type == Change::DELETED) {
305 docstring str = "\\lyxdeleted{" +
306 bparams.authors().get(change.author).name() + "}{" +
309 column += str.size();
310 } else if (change.type == Change::INSERTED) {
311 docstring str = "\\lyxadded{" +
312 bparams.authors().get(change.author).name() + "}{" +
315 column += str.size();
322 void Changes::lyxMarkChange(std::ostream & os, int & column,
323 Change const & old, Change const & change)
330 switch (change.type) {
331 case Change::UNCHANGED:
332 os << "\n\\change_unchanged\n";
335 case Change::DELETED: {
336 os << "\n\\change_deleted " << change.author
337 << " " << change.changetime << "\n";
341 case Change::INSERTED: {
342 os << "\n\\change_inserted " << change.author
343 << " " << change.changetime << "\n";
350 void Changes::checkAuthors(AuthorList const & authorList)
352 ChangeTable::const_iterator it = table_.begin();
353 ChangeTable::const_iterator endit = table_.end();
354 for ( ; it != endit ; ++it)
355 if (it->change.type != Change::UNCHANGED)
356 authorList.get(it->change.author).setUsed(true);