Consider the case where there is not any layout name.
[lyx.git] / src / changes.C
blobfd12f218fd05625e075413bf084a65128668b950
1 /**
2  * \file changes.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Record changes in a paragraph.
11  */
13 #include <config.h>
15 #include "changes.h"
16 #include "debug.h"
18 #include <boost/assert.hpp>
20 using lyx::pos_type;
22 using std::endl;
23 using std::string;
26 bool operator==(Change const & l, Change const & r)
28         return l.type == r.type && l.author == r.author
29                 && l.changetime == r.changetime;
33 bool operator!=(Change const & l, Change const & r)
35         return !(l == r);
39 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
41         return r1.start == r2.start && r1.end == r2.end;
45 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
47         return !(r1 == r2);
51 bool Changes::Range::contains(Range const & r) const
53         return r.start >= start && r.end <= end;
57 bool Changes::Range::contained(Range const & r) const
59         return r.contains(*this);
63 bool Changes::Range::contains(pos_type const pos) const
65         return pos >= start && pos < end;
69 bool Changes::Range::loose_contains(pos_type const pos) const
71         return pos >= start && pos <= end;
75 bool Changes::Range::intersects(Range const & r) const
77         return contained(r) || contains(r)
78                 || contains(r.start) || contains(r.end);
82 Changes::Changes(Change::Type const type)
83         : empty_type_(type)
88 Changes::~Changes()
93 Changes::Changes(Changes const & c)
95         table_ = c.table_;
99 void Changes::record(Change const change, pos_type const pos)
101         if (lyxerr.debugging(Debug::CHANGES)) {
102                 lyxerr[Debug::CHANGES] << "record " << change.type
103                         << " at pos " << pos << " with total "
104                         << table_.size() << " changes." << endl;
105         }
107         switch (change.type) {
108                 case Change::INSERTED:
109                         add(change, pos);
110                         break;
111                 case Change::DELETED:
112                         del(change, pos);
113                         break;
114                 case Change::UNCHANGED:
115                         set(Change::UNCHANGED, pos);
116                         break;
117         }
121 void Changes::set(Change const change, pos_type const pos)
123         set(change, pos, pos + 1);
127 void Changes::set(Change::Type const type, pos_type const pos)
129         set(type, pos, pos + 1);
133 void Changes::set(Change::Type const type,
134                   pos_type const start, pos_type const end)
136         set(Change(type), start, end);
140 void Changes::set(Change const change,
141                   pos_type const start, pos_type const end)
143         ChangeTable::iterator it = table_.begin();
145         if (lyxerr.debugging(Debug::CHANGES)) {
146                 lyxerr[Debug::CHANGES] << "changeset of " << change.type
147                         << " author " << change.author << " time " << change.changetime
148                         << " in range " << start << "," << end << endl;
149         }
151         Range const new_range(start, end);
153         // remove all sub-ranges
154         for (; it != table_.end();) {
155                 if (new_range != it->range && it->range.contained(new_range)) {
156                         if (lyxerr.debugging(Debug::CHANGES)) {
157                                 lyxerr[Debug::CHANGES] << "Removing subrange "
158                                         << it->range.start << "," << it->range.end << endl;
159                         }
160                         it = table_.erase(it);
161                 } else {
162                         ++it;
163                 }
164         }
166         it = table_.begin();
167         ChangeTable::iterator const itend = table_.end();
169         // find a super-range
170         for (; it != itend; ++it) {
171                 if (it->range.contains(new_range))
172                         break;
173         }
175         if (it == itend) {
176                 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
177                 table_.push_back(ChangeRange(start, end, change));
178                 merge();
179                 return;
180         }
182         if (change.type == it->change.type) {
183                 lyxerr[Debug::CHANGES] << "Change set already." << endl;
184                 it->change = change;
185                 return;
186         }
188         ChangeRange c(*it);
190         if (lyxerr.debugging(Debug::CHANGES)) {
191                 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
192                         << " over " << c.range.start << "," << c.range.end << endl;
193         }
195         // split head
196         if (c.range.start < start) {
197                 it = table_.insert(it, ChangeRange(c.range.start, start, c.change));
198                 if (lyxerr.debugging(Debug::CHANGES)) {
199                         lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
200                                 << " over " << c.range.start << "," << start << endl;
201                 }
202                 ++it;
203         }
205         // reset this as new type
206         it->range.start = start;
207         it->range.end = end;
208         it->change = change;
209         lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
211         // split tail
212         if (c.range.end > end) {
213                 ++it;
214                 table_.insert(it, ChangeRange(end, c.range.end, c.change));
215                 if (lyxerr.debugging(Debug::CHANGES)) {
216                         lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
217                                 << " over " << end << "," << c.range.end << endl;
218                 }
219         }
221         check();
222         merge();
226 void Changes::erase(pos_type const pos)
228         ChangeTable::iterator it = table_.begin();
229         ChangeTable::iterator end = table_.end();
231         bool found = false;
233         for (; it != end; ++it) {
234                 Range & range(it->range);
236                 if (lyxerr.debugging(Debug::CHANGES)) {
237                         lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
238                                 << it->range.start << "," << it->range.end << endl;
239                 }
241                 if (range.contains(pos)) {
242                         found = true;
243                         --range.end;
244                         continue;
245                 }
247                 if (found) {
248                         --range.start;
249                         --range.end;
250                 }
251         }
252         check();
253         merge();
257 void Changes::del(Change const change, ChangeTable::size_type const pos)
259         // this case happens when building from .lyx
260         if (table_.empty()) {
261                 set(change, pos);
262                 return;
263         }
265         ChangeTable::iterator it = table_.begin();
267         for (; it != table_.end(); ++it) {
268                 Range & range(it->range);
270                 if (range.contains(pos)) {
271                         if (it->change.type != Change::INSERTED) {
272                                 set(change, pos);
273                         } else {
274                                 erase(pos);
275                         }
276                         break;
277                 } else if (range.loose_contains(pos) && it + 1 == table_.end()) {
278                         // this case happens when building from .lyx
279                         set(change, pos);
280                         break;
281                 }
282         }
286 void Changes::add(Change const change, ChangeTable::size_type const pos)
288         ChangeTable::iterator it = table_.begin();
289         ChangeTable::iterator end = table_.end();
291         bool found = false;
293         for (; it != end; ++it) {
294                 Range & range(it->range);
296                 if (!found && range.loose_contains(pos)) {
297                         found = true;
298                         if (lyxerr.debugging(Debug::CHANGES)) {
299                                 lyxerr[Debug::CHANGES] << "Found range of "
300                                         << range.start << "," << range.end << endl;
301                         }
302                         ++range.end;
303                         continue;
304                 }
306                 if (found) {
307                         ++range.start;
308                         ++range.end;
309                 }
310         }
311         set(change, pos);
315 Change const Changes::lookupFull(pos_type const pos) const
317         if (!table_.size()) {
318                 if (lyxerr.debugging(Debug::CHANGES))
319                         lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
320                 return Change(empty_type_);
321         }
323         ChangeTable::const_iterator it = table_.begin();
324         ChangeTable::const_iterator const end = table_.end();
326         for (; it != end; ++it) {
327                 if (it->range.contains(pos))
328                         return it->change;
329         }
331         check();
332         BOOST_ASSERT(false && "missing changes for pos");
333         return Change(Change::UNCHANGED);
337 Change::Type Changes::lookup(pos_type const pos) const
339         if (!table_.size()) {
340                 if (lyxerr.debugging(Debug::CHANGES))
341                         lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
342                 return empty_type_;
343         }
345         ChangeTable::const_iterator it = table_.begin();
346         ChangeTable::const_iterator end = table_.end();
348         for (; it != end; ++it) {
349                 if (it->range.contains(pos))
350                         return it->change.type;
351         }
353         check();
354         BOOST_ASSERT(false && "missing changes for pos");
355         return Change::UNCHANGED;
359 bool Changes::isChange(pos_type const start, pos_type const end) const
361         if (!table_.size()) {
362                 if (lyxerr.debugging(Debug::CHANGES))
363                         lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
364                 return empty_type_ != Change::UNCHANGED;
365         }
367         ChangeTable::const_iterator it = table_.begin();
368         ChangeTable::const_iterator const itend = table_.end();
370         for (; it != itend; ++it) {
371                 if (lyxerr.debugging(Debug::CHANGES)) {
372                         lyxerr[Debug::CHANGES] << "Looking for " << start << ","
373                                 << end << " in " << it->range.start << ","
374                                 << it->range.end << "of type " << it->change.type << endl;
375                 }
377                 if (it->range.intersects(Range(start, end))
378                         && it->change.type != Change::UNCHANGED) {
379                         if (lyxerr.debugging(Debug::CHANGES)) {
380                                 lyxerr[Debug::CHANGES] << "Found intersection of "
381                                         << start << "," << end << " with "
382                                         << it->range.start << "," << it->range.end
383                                         << " of type " << it->change.type << endl;
384                         }
385                         return true;
386                 }
387         }
389         return false;
393 bool Changes::isChangeEdited(lyx::pos_type const start,
394                              lyx::pos_type const end) const
396         if (!table_.size()) {
397                 if (lyxerr.debugging(Debug::CHANGES))
398                         lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
399                 return empty_type_ != Change::INSERTED;
400         }
402         ChangeTable::const_iterator it = table_.begin();
403         ChangeTable::const_iterator const itend = table_.end();
405         for (; it != itend; ++it) {
406                 if (it->range.intersects(Range(start, end ? end - 1 : 0))
407                         && it->change.type != Change::INSERTED) {
408                         return true;
409                 }
410         }
411         return false;
415 void Changes::merge()
417         if (lyxerr.debugging(Debug::CHANGES))
418                 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
420         ChangeTable::iterator it = table_.begin();
422         while (it != table_.end()) {
423                 if (lyxerr.debugging(Debug::CHANGES)) {
424                         lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
425                                 << it->range.start << "," << it->range.end << endl;
426                 }
428                 if (it->range.start == it->range.end) {
429                         if (lyxerr.debugging(Debug::CHANGES)) {
430                                 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
431                                         << it->range.start << endl;
432                         }
434                         table_.erase(it);
435                         // start again
436                         it = table_.begin();
437                         continue;
438                 }
440                 if (it + 1 == table_.end())
441                         break;
443                 if (it->change == (it + 1)->change) {
444                         if (lyxerr.debugging(Debug::CHANGES)) {
445                                 lyxerr[Debug::CHANGES] << "Merging equal ranges "
446                                         << it->range.start << "," << it->range.end
447                                         << " and " << (it + 1)->range.start << ","
448                                         << (it + 1)->range.end << endl;
449                         }
451                         (it + 1)->range.start = it->range.start;
452                         table_.erase(it);
453                         // start again
454                         it = table_.begin();
455                         continue;
456                 }
458                 ++it;
459         }
461         lyxerr[Debug::CHANGES] << "Merge ended" << endl;
462         check();
466 void Changes::check() const
468         ChangeTable::const_iterator it = table_.begin();
469         ChangeTable::const_iterator end = table_.end();
471         bool dont_assert = true;
473         lyxerr[Debug::CHANGES] << "Changelist:" << endl;
474         for (; it != end; ++it) {
475                 if (lyxerr.debugging(Debug::CHANGES)) {
476                         lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
477                                 << it->range.start << "," << it->range.end << " author "
478                                 << it->change.author << " time " << it->change.changetime << endl;
479                 }
481                 if (it + 1 == end)
482                         break;
484                 Range const & range(it->range);
485                 Range const & next((it + 1)->range);
486                 if (range.end != next.start)
487                         dont_assert = false;
488         }
490         if (lyxerr.debugging(Debug::CHANGES))
491                 lyxerr[Debug::CHANGES] << "End" << endl;
493         BOOST_ASSERT(dont_assert);
497 int Changes::latexMarkChange(std::ostream & os,
498                              Change::Type const old, Change::Type const change,
499                              bool const & output)
501         if (!output || old == change)
502                 return 0;
504         string const start("\\changestart{}");
505         string const end("\\changeend{}");
506         string const son("\\overstrikeon{}");
507         string const soff("\\overstrikeoff{}");
509         int column = 0;
511         if (old == Change::DELETED) {
512                 os << soff;
513                 column += soff.length();
514         }
516         switch (change) {
517                 case Change::UNCHANGED:
518                         os << end;
519                         column += end.length();
520                         break;
522                 case Change::DELETED:
523                         if (old == Change::UNCHANGED) {
524                                 os << start;
525                                 column += start.length();
526                         }
527                         os << son;
528                         column += son.length();
529                         break;
531                 case Change::INSERTED:
532                         if (old == Change::UNCHANGED) {
533                                 os << start;
534                                 column += start.length();
535                         }
536                         break;
537         }
539         return column;
543 void Changes::lyxMarkChange(std::ostream & os, int & column,
544                             lyx::time_type const curtime,
545                             Change const & old, Change const & change)
547         if (old == change)
548                 return;
550         column = 0;
552         switch (change.type) {
553                 case Change::UNCHANGED:
554                         os << "\n\\change_unchanged\n";
555                         break;
557                 case Change::DELETED: {
558                         lyx::time_type t = change.changetime;
559                         if (!t)
560                                 t = curtime;
561                         os << "\n\\change_deleted " << change.author
562                                 << " " << t << "\n";
564                         break;
565                 }
567         case Change::INSERTED: {
568                         lyx::time_type t = change.changetime;
569                         if (!t)
570                                 t = curtime;
571                         os << "\n\\change_inserted " << change.author
572                                 << " " << t << "\n";
573                         break;
574         }
575         }