2003-02-08 19:18:01 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Changes.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2003-02-08 19:18:01 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author John Levon
|
2006-10-21 14:34:05 +00:00
|
|
|
* \author Michael Gerz
|
2003-08-23 00:17:00 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-02-08 19:18:01 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Record changes in a paragraph.
|
2003-02-08 19:18:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Changes.h"
|
2003-02-08 19:18:01 +00:00
|
|
|
#include "debug.h"
|
2007-05-08 17:46:03 +00:00
|
|
|
#include "Author.h"
|
|
|
|
#include "BufferParams.h"
|
|
|
|
#include "LaTeXFeatures.h"
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-09-09 17:25:35 +00:00
|
|
|
#include <boost/assert.hpp>
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-27 09:41:32 +00:00
|
|
|
using std::abs;
|
2003-02-08 19:18:01 +00:00
|
|
|
using std::endl;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2006-10-26 19:00:28 +00:00
|
|
|
using std::max;
|
|
|
|
|
2007-07-17 17:40:44 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2006-10-26 19:00:28 +00:00
|
|
|
/*
|
|
|
|
* Class Change has a changetime field that specifies the exact time at which
|
|
|
|
* a specific change was made. The change time is used as a guidance for the
|
|
|
|
* user while editing his document. Presently, it is not considered for LaTeX
|
2006-10-29 21:48:23 +00:00
|
|
|
* export.
|
|
|
|
* When merging two adjacent changes, the changetime is not considered,
|
|
|
|
* only the equality of the change type and author is checked (in method
|
|
|
|
* isSimilarTo(...)). If two changes are in fact merged (in method merge()),
|
2007-05-28 22:27:45 +00:00
|
|
|
* the later change time is preserved.
|
2006-10-26 19:00:28 +00:00
|
|
|
*/
|
2003-09-09 17:25:35 +00:00
|
|
|
|
2006-10-29 21:48:23 +00:00
|
|
|
bool Change::isSimilarTo(Change const & change)
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
2006-10-29 21:48:23 +00:00
|
|
|
if (type != change.type) {
|
2006-10-26 19:00:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-29 21:48:23 +00:00
|
|
|
if (type == Change::UNCHANGED) {
|
2006-10-26 19:00:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-10-29 21:48:23 +00:00
|
|
|
return author == change.author;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(Change const & l, Change const & r)
|
|
|
|
{
|
2007-01-08 23:28:41 +00:00
|
|
|
if (l.type != r.type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// two changes of type UNCHANGED are always equal
|
|
|
|
if (l.type == Change::UNCHANGED) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-01-08 23:28:41 +00:00
|
|
|
return l.author == r.author &&
|
2006-10-29 21:48:23 +00:00
|
|
|
l.changetime == r.changetime;
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
|
|
bool operator!=(Change const & l, Change const & r)
|
|
|
|
{
|
|
|
|
return !(l == r);
|
|
|
|
}
|
|
|
|
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
bool operator==(Changes::Range const & r1, Changes::Range const & r2)
|
|
|
|
{
|
2003-03-04 09:27:27 +00:00
|
|
|
return r1.start == r2.start && r1.end == r2.end;
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
|
|
|
|
{
|
|
|
|
return !(r1 == r2);
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
bool Changes::Range::intersects(Range const & r) const
|
|
|
|
{
|
2006-10-20 14:31:54 +00:00
|
|
|
return r.start < end && r.end > start; // end itself is not in the range!
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
|
|
|
2006-10-19 07:12:48 +00:00
|
|
|
void Changes::set(Change const & change, pos_type const pos)
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
|
|
|
set(change, pos, pos + 1);
|
|
|
|
}
|
|
|
|
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
void Changes::set(Change const & change, pos_type const start, pos_type const end)
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
2007-04-01 15:09:08 +00:00
|
|
|
if (change.type != Change::UNCHANGED) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::CHANGES) << "setting change (type: " << change.type
|
2006-10-21 14:34:05 +00:00
|
|
|
<< ", author: " << change.author << ", time: " << change.changetime
|
|
|
|
<< ") in range (" << start << ", " << end << ")" << endl;
|
2003-03-12 02:34:46 +00:00
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
Range const newRange(start, end);
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
ChangeTable::iterator it = table_.begin();
|
2003-02-08 19:18:01 +00:00
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
for (; it != table_.end(); ) {
|
|
|
|
// current change starts like or follows new change
|
|
|
|
if (it->range.start >= start) {
|
2003-02-08 19:18:01 +00:00
|
|
|
break;
|
2006-10-21 14:34:05 +00:00
|
|
|
}
|
2003-02-08 19:18:01 +00:00
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
// new change intersects with existing change
|
|
|
|
if (it->range.end > start) {
|
|
|
|
pos_type oldEnd = it->range.end;
|
|
|
|
it->range.end = start;
|
2007-04-01 15:09:08 +00:00
|
|
|
|
|
|
|
LYXERR(Debug::CHANGES) << " cutting tail of type " << it->change.type
|
|
|
|
<< " resulting in range (" << it->range.start << ", "
|
|
|
|
<< it->range.end << ")" << endl;
|
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
++it;
|
|
|
|
if (oldEnd >= end) {
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << " inserting tail in range ("
|
|
|
|
<< end << ", " << oldEnd << ")" << endl;
|
2006-10-21 14:34:05 +00:00
|
|
|
it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
++it;
|
2003-03-12 02:34:46 +00:00
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
if (change.type != Change::UNCHANGED) {
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << " inserting change" << endl;
|
2006-10-21 14:34:05 +00:00
|
|
|
it = table_.insert(it, ChangeRange(change, Range(start, end)));
|
2003-02-08 19:18:01 +00:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
for (; it != table_.end(); ) {
|
|
|
|
// new change 'contains' existing change
|
|
|
|
if (newRange.contains(it->range)) {
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << " removing subrange ("
|
|
|
|
<< it->range.start << ", " << it->range.end << ")" << endl;
|
2006-10-21 14:34:05 +00:00
|
|
|
it = table_.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-21 14:34:05 +00:00
|
|
|
// new change precedes existing change
|
|
|
|
if (it->range.start >= end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// new change intersects with existing change
|
|
|
|
it->range.start = end;
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << " cutting head of type "
|
|
|
|
<< it->change.type << " resulting in range ("
|
|
|
|
<< end << ", " << it->range.end << ")" << endl;
|
2006-10-21 14:34:05 +00:00
|
|
|
break; // no need for another iteration
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
merge();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-05 20:21:27 +00:00
|
|
|
void Changes::erase(pos_type const pos)
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << "Erasing change at position " << pos << endl;
|
2006-10-21 15:36:04 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
ChangeTable::iterator it = table_.begin();
|
|
|
|
ChangeTable::iterator end = table_.end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
2006-10-21 15:36:04 +00:00
|
|
|
// range (pos,pos+x) becomes (pos,pos+x-1)
|
|
|
|
if (it->range.start > pos) {
|
|
|
|
--(it->range.start);
|
2003-03-12 02:34:46 +00:00
|
|
|
}
|
2006-10-21 15:36:04 +00:00
|
|
|
// range (pos-x,pos) stays (pos-x,pos)
|
|
|
|
if (it->range.end > pos) {
|
|
|
|
--(it->range.end);
|
|
|
|
}
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-21 15:36:04 +00:00
|
|
|
merge();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Changes::insert(Change const & change, lyx::pos_type pos)
|
|
|
|
{
|
2007-04-01 15:09:08 +00:00
|
|
|
if (change.type != Change::UNCHANGED) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::CHANGES) << "Inserting change of type " << change.type
|
2006-10-21 15:36:04 +00:00
|
|
|
<< " at position " << pos << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChangeTable::iterator it = table_.begin();
|
|
|
|
ChangeTable::iterator end = table_.end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
// range (pos,pos+x) becomes (pos+1,pos+x+1)
|
|
|
|
if (it->range.start >= pos) {
|
|
|
|
++(it->range.start);
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 15:36:04 +00:00
|
|
|
// range (pos-x,pos) stays as it is
|
|
|
|
if (it->range.end > pos) {
|
|
|
|
++(it->range.end);
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-21 15:36:04 +00:00
|
|
|
|
|
|
|
set(change, pos, pos + 1); // set will call merge
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-07 18:13:25 +00:00
|
|
|
Change const & Changes::lookup(pos_type const pos) const
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
2007-01-07 18:13:25 +00:00
|
|
|
static Change const noChange = Change(Change::UNCHANGED);
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
ChangeTable::const_iterator it = table_.begin();
|
2005-01-05 20:21:27 +00:00
|
|
|
ChangeTable::const_iterator const end = table_.end();
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (it->range.contains(pos))
|
|
|
|
return it->change;
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2007-01-07 18:13:25 +00:00
|
|
|
return noChange;
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
2006-10-20 15:27:11 +00:00
|
|
|
bool Changes::isChanged(pos_type const start, pos_type const end) const
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
|
|
|
ChangeTable::const_iterator it = table_.begin();
|
2005-01-05 20:21:27 +00:00
|
|
|
ChangeTable::const_iterator const itend = table_.end();
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
|
|
for (; it != itend; ++it) {
|
2006-10-21 16:01:45 +00:00
|
|
|
if (it->range.intersects(Range(start, end))) {
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << "found intersection of range ("
|
|
|
|
<< start << ", " << end << ") with ("
|
|
|
|
<< it->range.start << ", " << it->range.end
|
|
|
|
<< ") of type " << it->change.type << endl;
|
2003-02-08 19:18:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
void Changes::merge()
|
|
|
|
{
|
|
|
|
ChangeTable::iterator it = table_.begin();
|
|
|
|
|
|
|
|
while (it != table_.end()) {
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << "found change of type " << it->change.type
|
|
|
|
<< " and range (" << it->range.start << ", " << it->range.end
|
|
|
|
<< ")" << endl;
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
if (it->range.start == it->range.end) {
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << "removing empty range for pos "
|
|
|
|
<< it->range.start << endl;
|
2003-03-12 02:34:46 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
table_.erase(it);
|
|
|
|
// start again
|
|
|
|
it = table_.begin();
|
|
|
|
continue;
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
if (it + 1 == table_.end())
|
|
|
|
break;
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-29 21:48:23 +00:00
|
|
|
if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
|
2007-04-01 15:09:08 +00:00
|
|
|
LYXERR(Debug::CHANGES) << "merging ranges (" << it->range.start << ", "
|
|
|
|
<< it->range.end << ") and (" << (it + 1)->range.start << ", "
|
|
|
|
<< (it + 1)->range.end << ")" << endl;
|
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
(it + 1)->range.start = it->range.start;
|
2006-10-26 19:00:28 +00:00
|
|
|
(it + 1)->change.changetime = max(it->change.changetime,
|
2007-05-28 22:27:45 +00:00
|
|
|
(it + 1)->change.changetime);
|
2003-02-08 19:18:01 +00:00
|
|
|
table_.erase(it);
|
|
|
|
// start again
|
|
|
|
it = table_.begin();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2007-05-08 17:46:03 +00:00
|
|
|
int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
|
|
|
|
Change const & oldChange, Change const & change)
|
2003-03-04 09:27:27 +00:00
|
|
|
{
|
2007-05-08 17:46:03 +00:00
|
|
|
if (!bparams.outputChanges || oldChange == change)
|
2003-02-08 19:18:01 +00:00
|
|
|
return 0;
|
2005-04-26 11:12:20 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
int column = 0;
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2007-05-08 17:46:03 +00:00
|
|
|
if (oldChange.type != Change::UNCHANGED) {
|
2007-05-13 15:17:57 +00:00
|
|
|
os << '}'; // close \lyxadded or \lyxdeleted
|
2007-05-08 17:46:03 +00:00
|
|
|
column++;
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2007-05-08 17:46:03 +00:00
|
|
|
docstring chgTime;
|
|
|
|
chgTime += ctime(&change.changetime);
|
|
|
|
chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
|
|
|
|
|
|
|
|
if (change.type == Change::DELETED) {
|
|
|
|
docstring str = "\\lyxdeleted{" +
|
|
|
|
bparams.authors().get(change.author).name() + "}{" +
|
2007-05-28 22:27:45 +00:00
|
|
|
chgTime + "}{";
|
2007-05-08 17:46:03 +00:00
|
|
|
os << str;
|
|
|
|
column += str.size();
|
|
|
|
} else if (change.type == Change::INSERTED) {
|
2007-05-13 15:17:57 +00:00
|
|
|
docstring str = "\\lyxadded{" +
|
2007-05-08 17:46:03 +00:00
|
|
|
bparams.authors().get(change.author).name() + "}{" +
|
|
|
|
chgTime + "}{";
|
|
|
|
os << str;
|
|
|
|
column += str.size();
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-05 20:21:27 +00:00
|
|
|
void Changes::lyxMarkChange(std::ostream & os, int & column,
|
|
|
|
Change const & old, Change const & change)
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
|
|
|
if (old == change)
|
|
|
|
return;
|
|
|
|
|
|
|
|
column = 0;
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
switch (change.type) {
|
|
|
|
case Change::UNCHANGED:
|
|
|
|
os << "\n\\change_unchanged\n";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Change::DELETED: {
|
|
|
|
os << "\n\\change_deleted " << change.author
|
2006-10-26 19:00:28 +00:00
|
|
|
<< " " << change.changetime << "\n";
|
2003-02-08 19:18:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-10-26 19:00:28 +00:00
|
|
|
case Change::INSERTED: {
|
2003-02-08 19:18:01 +00:00
|
|
|
os << "\n\\change_inserted " << change.author
|
2006-10-26 19:00:28 +00:00
|
|
|
<< " " << change.changetime << "\n";
|
2003-02-08 19:18:01 +00:00
|
|
|
break;
|
2006-10-26 19:00:28 +00:00
|
|
|
}
|
2005-01-05 20:21:27 +00:00
|
|
|
}
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2007-07-09 20:52:34 +00:00
|
|
|
void Changes::checkAuthors(AuthorList const & authorList)
|
|
|
|
{
|
|
|
|
ChangeTable::const_iterator it = table_.begin();
|
|
|
|
ChangeTable::const_iterator endit = table_.end();
|
|
|
|
for ( ; it != endit ; ++it)
|
|
|
|
if (it->change.type != Change::UNCHANGED)
|
|
|
|
authorList.get(it->change.author).used(true);
|
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|