2003-02-08 19:18:01 +00:00
|
|
|
/**
|
|
|
|
* \file changes.C
|
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
|
|
|
|
|
|
|
#include "changes.h"
|
2003-02-08 19:18:01 +00:00
|
|
|
#include "debug.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-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
2003-06-30 23:56:22 +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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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()),
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
return l.type == r.type &&
|
|
|
|
l.author == r.author &&
|
|
|
|
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::contains(Range const & r) const
|
|
|
|
{
|
|
|
|
return r.start >= start && r.end <= end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-05 20:21:27 +00:00
|
|
|
bool Changes::Range::contains(pos_type const pos) const
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
|
|
|
return pos >= start && pos < end;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2003-03-12 02:34:46 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
2006-10-21 14:34:05 +00:00
|
|
|
lyxerr[Debug::CHANGES] << "setting change (type: " << change.type
|
|
|
|
<< ", 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;
|
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
|
|
|
lyxerr[Debug::CHANGES] << " cutting tail of type " << it->change.type
|
|
|
|
<< " resulting in range (" << it->range.start << ", "
|
|
|
|
<< it->range.end << ")" << endl;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
if (oldEnd >= end) {
|
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
|
|
|
lyxerr[Debug::CHANGES] << " inserting tail in range ("
|
|
|
|
<< end << ", " << oldEnd << ")" << endl;
|
|
|
|
}
|
|
|
|
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) {
|
2003-03-12 02:34:46 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
2006-10-21 14:34:05 +00:00
|
|
|
lyxerr[Debug::CHANGES] << " inserting change" << endl;
|
2003-03-12 02:34:46 +00:00
|
|
|
}
|
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)) {
|
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
|
|
|
lyxerr[Debug::CHANGES] << " removing subrange ("
|
|
|
|
<< it->range.start << ", " << it->range.end << ")" << endl;
|
|
|
|
}
|
|
|
|
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;
|
2003-03-12 02:34:46 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
2006-10-21 14:34:05 +00:00
|
|
|
lyxerr[Debug::CHANGES] << " cutting head of type "
|
|
|
|
<< it->change.type << " resulting in range ("
|
|
|
|
<< end << ", " << it->range.end << ")" << endl;
|
2003-03-12 02:34:46 +00:00
|
|
|
}
|
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
|
|
|
{
|
2006-10-21 15:36:04 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
|
|
|
lyxerr[Debug::CHANGES] << "Erasing change at position " << pos << endl;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
|
|
|
lyxerr[Debug::CHANGES] << "Inserting change of type " << change.type
|
|
|
|
<< " 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-08 20:30:52 +00:00
|
|
|
Change const Changes::lookup(pos_type const pos) 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 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
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
return Change(Change::UNCHANGED);
|
|
|
|
}
|
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))) {
|
2003-03-12 02:34:46 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
2006-10-21 16:01:45 +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-03-12 02:34:46 +00:00
|
|
|
}
|
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()
|
|
|
|
{
|
2006-10-21 16:01:45 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
|
|
|
lyxerr[Debug::CHANGES] << "merging changes..." << endl;
|
|
|
|
}
|
2003-03-12 02:34:46 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
ChangeTable::iterator it = table_.begin();
|
|
|
|
|
|
|
|
while (it != table_.end()) {
|
2003-03-12 02:34:46 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
2006-10-21 16:01:45 +00:00
|
|
|
lyxerr[Debug::CHANGES] << " found change of type " << it->change.type
|
|
|
|
<< " and range (" << it->range.start << ", " << it->range.end
|
|
|
|
<< ")" << endl;
|
2003-03-12 02:34:46 +00:00
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
if (it->range.start == it->range.end) {
|
2003-03-12 02:34:46 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
2006-10-21 16:01:45 +00:00
|
|
|
lyxerr[Debug::CHANGES] << " removing empty range for pos "
|
2003-03-12 02:34:46 +00:00
|
|
|
<< it->range.start << endl;
|
|
|
|
}
|
|
|
|
|
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) {
|
2003-03-12 02:34:46 +00:00
|
|
|
if (lyxerr.debugging(Debug::CHANGES)) {
|
2006-10-21 16:01:45 +00:00
|
|
|
lyxerr[Debug::CHANGES] << " merging ranges (" << it->range.start << ", "
|
|
|
|
<< it->range.end << ") and (" << (it + 1)->range.start << ", "
|
|
|
|
<< (it + 1)->range.end << ")" << endl;
|
2003-03-12 02:34:46 +00:00
|
|
|
}
|
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,
|
|
|
|
(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
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
int Changes::latexMarkChange(odocstream & os,
|
2006-10-26 19:00:28 +00:00
|
|
|
Change::Type const oldChangeType, Change::Type const changeType,
|
2005-01-24 17:12:19 +00:00
|
|
|
bool const & output)
|
2003-03-04 09:27:27 +00:00
|
|
|
{
|
2006-10-26 19:00:28 +00:00
|
|
|
if (!output || oldChangeType == changeType)
|
2003-02-08 19:18:01 +00:00
|
|
|
return 0;
|
2005-04-26 11:12:20 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
static docstring const start(from_ascii("\\changestart{}"));
|
|
|
|
static docstring const end(from_ascii("\\changeend{}"));
|
|
|
|
static docstring const son(from_ascii("\\overstrikeon{}"));
|
|
|
|
static docstring const soff(from_ascii("\\overstrikeoff{}"));
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
int column = 0;
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-26 19:00:28 +00:00
|
|
|
if (oldChangeType == Change::DELETED) {
|
2003-02-08 19:18:01 +00:00
|
|
|
os << soff;
|
|
|
|
column += soff.length();
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
2006-10-26 19:00:28 +00:00
|
|
|
switch (changeType) {
|
2003-02-08 19:18:01 +00:00
|
|
|
case Change::UNCHANGED:
|
|
|
|
os << end;
|
|
|
|
column += end.length();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Change::DELETED:
|
2006-10-26 19:00:28 +00:00
|
|
|
if (oldChangeType == Change::UNCHANGED) {
|
2003-02-08 19:18:01 +00:00
|
|
|
os << start;
|
|
|
|
column += start.length();
|
|
|
|
}
|
|
|
|
os << son;
|
|
|
|
column += son.length();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Change::INSERTED:
|
2006-10-26 19:00:28 +00:00
|
|
|
if (oldChangeType == Change::UNCHANGED) {
|
2003-02-08 19:18:01 +00:00
|
|
|
os << start;
|
|
|
|
column += start.length();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|