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"
21 #include "Paragraph.h"
22 #include "TocBackend.h"
24 #include "support/debug.h"
25 #include "support/gettext.h"
26 #include "support/lassert.h"
35 * Class Change has a changetime field that specifies the exact time at which
36 * a specific change was made. The change time is used as a guidance for the
37 * user while editing his document. Presently, it is not considered for LaTeX
39 * When merging two adjacent changes, the changetime is not considered,
40 * only the equality of the change type and author is checked (in method
41 * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
42 * the later change time is preserved.
45 bool Change::isSimilarTo(Change const & change) const
47 if (type != change.type)
50 if (type == Change::UNCHANGED)
53 return author == change.author;
57 ColorCode Change::color() const
59 ColorCode color = Color_none;
62 color = Color_changedtextauthor1;
65 color = Color_changedtextauthor2;
68 color = Color_changedtextauthor3;
71 color = Color_changedtextauthor4;
74 color = Color_changedtextauthor5;
81 bool operator==(Change const & l, Change const & r)
86 // two changes of type UNCHANGED are always equal
87 if (l.type == Change::UNCHANGED)
90 return l.author == r.author && l.changetime == r.changetime;
94 bool operator!=(Change const & l, Change const & r)
100 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
102 return r1.start == r2.start && r1.end == r2.end;
106 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
112 bool Changes::Range::intersects(Range const & r) const
114 return r.start < end && r.end > start; // end itself is not in the range!
118 void Changes::set(Change const & change, pos_type const pos)
120 set(change, pos, pos + 1);
124 void Changes::set(Change const & change, pos_type const start, pos_type const end)
126 if (change.type != Change::UNCHANGED) {
127 LYXERR(Debug::CHANGES, "setting change (type: " << change.type
128 << ", author: " << change.author
129 << ", time: " << long(change.changetime)
130 << ") in range (" << start << ", " << end << ")");
133 Range const newRange(start, end);
135 ChangeTable::iterator it = table_.begin();
137 for (; it != table_.end(); ) {
138 // current change starts like or follows new change
139 if (it->range.start >= start) {
143 // new change intersects with existing change
144 if (it->range.end > start) {
145 pos_type oldEnd = it->range.end;
146 it->range.end = start;
148 LYXERR(Debug::CHANGES, " cutting tail of type " << it->change.type
149 << " resulting in range (" << it->range.start << ", "
150 << it->range.end << ")");
154 LYXERR(Debug::CHANGES, " inserting tail in range ("
155 << end << ", " << oldEnd << ")");
156 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
164 if (change.type != Change::UNCHANGED) {
165 LYXERR(Debug::CHANGES, " inserting change");
166 it = table_.insert(it, ChangeRange(change, Range(start, end)));
170 for (; it != table_.end(); ) {
171 // new change 'contains' existing change
172 if (newRange.contains(it->range)) {
173 LYXERR(Debug::CHANGES, " removing subrange ("
174 << it->range.start << ", " << it->range.end << ")");
175 it = table_.erase(it);
179 // new change precedes existing change
180 if (it->range.start >= end)
183 // new change intersects with existing change
184 it->range.start = end;
185 LYXERR(Debug::CHANGES, " cutting head of type "
186 << it->change.type << " resulting in range ("
187 << end << ", " << it->range.end << ")");
188 break; // no need for another iteration
195 void Changes::erase(pos_type const pos)
197 LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
199 ChangeTable::iterator it = table_.begin();
200 ChangeTable::iterator end = table_.end();
202 for (; it != end; ++it) {
203 // range (pos,pos+x) becomes (pos,pos+x-1)
204 if (it->range.start > pos)
206 // range (pos-x,pos) stays (pos-x,pos)
207 if (it->range.end > pos)
215 void Changes::insert(Change const & change, lyx::pos_type pos)
217 if (change.type != Change::UNCHANGED) {
218 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
219 << " at position " << pos);
222 ChangeTable::iterator it = table_.begin();
223 ChangeTable::iterator end = table_.end();
225 for (; it != end; ++it) {
226 // range (pos,pos+x) becomes (pos+1,pos+x+1)
227 if (it->range.start >= pos)
230 // range (pos-x,pos) stays as it is
231 if (it->range.end > pos)
235 set(change, pos, pos + 1); // set will call merge
239 Change const & Changes::lookup(pos_type const pos) const
241 static Change const noChange = Change(Change::UNCHANGED);
243 ChangeTable::const_iterator it = table_.begin();
244 ChangeTable::const_iterator const end = table_.end();
246 for (; it != end; ++it) {
247 if (it->range.contains(pos))
255 bool Changes::isChanged(pos_type const start, pos_type const end) const
257 ChangeTable::const_iterator it = table_.begin();
258 ChangeTable::const_iterator const itend = table_.end();
260 for (; it != itend; ++it) {
261 if (it->range.intersects(Range(start, end))) {
262 LYXERR(Debug::CHANGES, "found intersection of range ("
263 << start << ", " << end << ") with ("
264 << it->range.start << ", " << it->range.end
265 << ") of type " << it->change.type);
273 void Changes::merge()
275 ChangeTable::iterator it = table_.begin();
277 while (it != table_.end()) {
278 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
279 << " and range (" << it->range.start << ", " << it->range.end
282 if (it->range.start == it->range.end) {
283 LYXERR(Debug::CHANGES, "removing empty range for pos "
292 if (it + 1 == table_.end())
295 if (it->change.isSimilarTo((it + 1)->change)
296 && it->range.end == (it + 1)->range.start) {
297 LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
298 << it->range.end << ") and (" << (it + 1)->range.start << ", "
299 << (it + 1)->range.end << ")");
301 (it + 1)->range.start = it->range.start;
302 (it + 1)->change.changetime = max(it->change.changetime,
303 (it + 1)->change.changetime);
315 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
316 Change const & oldChange, Change const & change)
318 if (!bparams.outputChanges || oldChange == change)
323 if (oldChange.type != Change::UNCHANGED) {
324 os << '}'; // close \lyxadded or \lyxdeleted
329 chgTime += ctime(&change.changetime);
330 chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
332 if (change.type == Change::DELETED) {
333 docstring str = "\\lyxdeleted{" +
334 bparams.authors().get(change.author).name() + "}{" +
337 column += str.size();
338 } else if (change.type == Change::INSERTED) {
339 docstring str = "\\lyxadded{" +
340 bparams.authors().get(change.author).name() + "}{" +
343 column += str.size();
350 void Changes::lyxMarkChange(ostream & os, int & column,
351 Change const & old, Change const & change)
358 switch (change.type) {
359 case Change::UNCHANGED:
360 os << "\n\\change_unchanged\n";
363 case Change::DELETED: {
364 os << "\n\\change_deleted " << change.author
365 << " " << change.changetime << "\n";
369 case Change::INSERTED: {
370 os << "\n\\change_inserted " << change.author
371 << " " << change.changetime << "\n";
378 void Changes::checkAuthors(AuthorList const & authorList)
380 ChangeTable::const_iterator it = table_.begin();
381 ChangeTable::const_iterator endit = table_.end();
382 for ( ; it != endit ; ++it)
383 if (it->change.type != Change::UNCHANGED)
384 authorList.get(it->change.author).setUsed(true);
388 void Changes::addToToc(DocIterator const & cdit, Buffer const & buffer) const
393 Toc & change_list = buffer.tocBackend().toc("change");
394 AuthorList const & author_list = buffer.params().authors();
395 DocIterator dit = cdit;
397 ChangeTable::const_iterator it = table_.begin();
398 ChangeTable::const_iterator const itend = table_.end();
399 for (; it != itend; ++it) {
401 switch (it->change.type) {
402 case Change::UNCHANGED:
404 case Change::DELETED:
405 // 0x2702 is a scissors symbol in the Dingbats unicode group.
406 str.push_back(0x2702);
408 case Change::INSERTED:
409 // 0x270d is the hand writting symbol in the Dingbats unicode group.
410 str.push_back(0x270d);
413 dit.pos() = it->range.start;
414 Paragraph const & par = dit.paragraph();
415 str += " " + par.asString(it->range.start, min(par.size(), it->range.end));
416 if (it->range.end > par.size())
417 // the end of paragraph symbol from the Punctuation group
418 str.push_back(0x204B);
419 docstring const & author = author_list.get(it->change.author).name();
420 Toc::iterator it = change_list.item(0, author);
421 if (it == change_list.end()) {
422 change_list.push_back(TocItem(dit, 0, author));
423 change_list.push_back(TocItem(dit, 1, str));
426 for (++it; it != change_list.end(); ++it) {
427 if (it->depth() == 0 && it->str() != author)
430 change_list.insert(it, TocItem(dit, 1, str));