]> git.lyx.org Git - lyx.git/blob - src/changes.C
Point fix, earlier forgotten
[lyx.git] / src / changes.C
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  */
12
13 #include <config.h>
14
15 #include "changes.h"
16 #include "debug.h"
17 #include "author.h"
18
19 #include "support/LAssert.h"
20 #include "support/LOstream.h"
21
22 using namespace lyx::support;
23
24 using std::vector;
25 using std::endl;
26 using lyx::pos_type;
27
28 bool operator==(Change const & l, Change const & r)
29 {
30         return l.type == r.type && l.author == r.author
31                 && l.changetime == r.changetime;
32 }
33
34
35 bool operator!=(Change const & l, Change const & r)
36 {
37         return !(l == r);
38 }
39
40
41 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
42 {
43         return r1.start == r2.start && r1.end == r2.end;
44 }
45
46
47 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
48 {
49         return !(r1 == r2);
50 }
51
52
53 bool Changes::Range::contains(Range const & r) const
54 {
55         return r.start >= start && r.end <= end;
56 }
57
58
59 bool Changes::Range::contained(Range const & r) const
60 {
61         return r.contains(*this);
62 }
63
64
65 bool Changes::Range::contains(pos_type pos) const
66 {
67         return pos >= start && pos < end;
68 }
69
70
71 bool Changes::Range::loose_contains(pos_type pos) const
72 {
73         return pos >= start && pos <= end;
74 }
75
76
77 bool Changes::Range::intersects(Range const & r) const
78 {
79         return contained(r) || contains(r)
80                 || contains(r.start) || contains(r.end);
81 }
82
83
84 Changes::Changes(Change::Type type)
85         : empty_type_(type)
86 {
87 }
88
89
90 Changes::~Changes()
91 {
92 }
93
94
95 Changes::Changes(Changes const & c)
96 {
97         table_ = c.table_;
98 }
99
100
101 void Changes::record(Change change, pos_type pos)
102 {
103         if (lyxerr.debugging(Debug::CHANGES)) {
104                 lyxerr[Debug::CHANGES] << "record " << change.type
105                         << " at pos " << pos << " with total "
106                         << table_.size() << " changes." << endl;
107         }
108
109         switch (change.type) {
110                 case Change::INSERTED:
111                         add(change, pos);
112                         break;
113                 case Change::DELETED:
114                         del(change, pos);
115                         break;
116                 case Change::UNCHANGED:
117                         set(Change::UNCHANGED, pos);
118                         break;
119         }
120 }
121
122
123 void Changes::set(Change change, pos_type pos)
124 {
125         set(change, pos, pos + 1);
126 }
127
128
129 void Changes::set(Change::Type type, pos_type pos)
130 {
131         set(type, pos, pos + 1);
132 }
133
134
135 void Changes::set(Change::Type type, pos_type start, pos_type end)
136 {
137         set(Change(type), start, end);
138 }
139
140
141 void Changes::set(Change change, pos_type start, pos_type end)
142 {
143         ChangeTable::iterator it = table_.begin();
144
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         }
150
151         Range const new_range(start, end);
152
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         }
165
166         it = table_.begin();
167         ChangeTable::iterator itend = table_.end();
168
169         // find a super-range
170         for (; it != itend; ++it) {
171                 if (it->range.contains(new_range))
172                         break;
173         }
174
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         }
181
182         if (change.type == it->change.type) {
183                 lyxerr[Debug::CHANGES] << "Change set already." << endl;
184                 it->change = change;
185                 return;
186         }
187
188         ChangeRange c(*it);
189
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         }
194
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         }
204
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;
210
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         }
220
221         check();
222         merge();
223 }
224
225
226 void Changes::erase(pos_type pos)
227 {
228         ChangeTable::iterator it = table_.begin();
229         ChangeTable::iterator end = table_.end();
230
231         bool found = false;
232
233         for (; it != end; ++it) {
234                 Range & range(it->range);
235
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                 }
240
241                 if (range.contains(pos)) {
242                         found = true;
243                         --range.end;
244                         continue;
245                 }
246
247                 if (found) {
248                         --range.start;
249                         --range.end;
250                 }
251         }
252         check();
253         merge();
254 }
255
256
257 void Changes::del(Change change, ChangeTable::size_type pos)
258 {
259         // this case happens when building from .lyx
260         if (table_.empty()) {
261                 set(change, pos);
262                 return;
263         }
264
265         ChangeTable::iterator it = table_.begin();
266
267         for (; it != table_.end(); ++it) {
268                 Range & range(it->range);
269
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         }
283 }
284
285
286 void Changes::add(Change change, ChangeTable::size_type pos)
287 {
288         ChangeTable::iterator it = table_.begin();
289         ChangeTable::iterator end = table_.end();
290
291         bool found = false;
292
293         for (; it != end; ++it) {
294                 Range & range(it->range);
295
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                 }
305
306                 if (found) {
307                         ++range.start;
308                         ++range.end;
309                 }
310         }
311         set(change, pos);
312 }
313
314
315 Change const Changes::lookupFull(pos_type pos) const
316 {
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         }
322
323         ChangeTable::const_iterator it = table_.begin();
324         ChangeTable::const_iterator end = table_.end();
325
326         for (; it != end; ++it) {
327                 if (it->range.contains(pos))
328                         return it->change;
329         }
330
331         check();
332         Assert(false);
333         return Change(Change::UNCHANGED);
334 }
335
336
337 Change::Type Changes::lookup(pos_type pos) const
338 {
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         }
344
345         ChangeTable::const_iterator it = table_.begin();
346         ChangeTable::const_iterator end = table_.end();
347
348         for (; it != end; ++it) {
349                 if (it->range.contains(pos))
350                         return it->change.type;
351         }
352
353         check();
354         Assert(0);
355         return Change::UNCHANGED;
356 }
357
358
359 bool Changes::isChange(pos_type start, pos_type end) const
360 {
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         }
366
367         ChangeTable::const_iterator it = table_.begin();
368         ChangeTable::const_iterator itend = table_.end();
369
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                 }
376
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         }
388
389         return false;
390 }
391
392
393 bool Changes::isChangeEdited(lyx::pos_type start, lyx::pos_type end) const
394 {
395         if (!table_.size()) {
396                 if (lyxerr.debugging(Debug::CHANGES))
397                         lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
398                 return empty_type_ != Change::INSERTED;
399         }
400
401         ChangeTable::const_iterator it = table_.begin();
402         ChangeTable::const_iterator itend = table_.end();
403
404         for (; it != itend; ++it) {
405                 if (it->range.intersects(Range(start, end ? end - 1 : 0))
406                         && it->change.type != Change::INSERTED) {
407                         return true;
408                 }
409         }
410         return false;
411 }
412
413
414 void Changes::merge()
415 {
416         if (lyxerr.debugging(Debug::CHANGES))
417                 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
418
419         ChangeTable::iterator it = table_.begin();
420
421         while (it != table_.end()) {
422                 if (lyxerr.debugging(Debug::CHANGES)) {
423                         lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
424                                 << it->range.start << "," << it->range.end << endl;
425                 }
426
427                 if (it->range.start == it->range.end) {
428                         if (lyxerr.debugging(Debug::CHANGES)) {
429                                 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
430                                         << it->range.start << endl;
431                         }
432
433                         table_.erase(it);
434                         // start again
435                         it = table_.begin();
436                         continue;
437                 }
438
439                 if (it + 1 == table_.end())
440                         break;
441
442                 if (it->change == (it + 1)->change) {
443                         if (lyxerr.debugging(Debug::CHANGES)) {
444                                 lyxerr[Debug::CHANGES] << "Merging equal ranges "
445                                         << it->range.start << "," << it->range.end
446                                         << " and " << (it + 1)->range.start << ","
447                                         << (it + 1)->range.end << endl;
448                         }
449
450                         (it + 1)->range.start = it->range.start;
451                         table_.erase(it);
452                         // start again
453                         it = table_.begin();
454                         continue;
455                 }
456
457                 ++it;
458         }
459
460         lyxerr[Debug::CHANGES] << "Merge ended" << endl;
461         check();
462 }
463
464
465 void Changes::check() const
466 {
467         ChangeTable::const_iterator it = table_.begin();
468         ChangeTable::const_iterator end = table_.end();
469
470         bool dont_assert(true);
471
472         lyxerr[Debug::CHANGES] << "Changelist:" << endl;
473         for (; it != end; ++it) {
474                 if (lyxerr.debugging(Debug::CHANGES)) {
475                         lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
476                                 << it->range.start << "," << it->range.end << " author "
477                                 << it->change.author << " time " << it->change.changetime << endl;
478                 }
479
480                 if (it + 1 == end)
481                         break;
482
483                 Range const & range(it->range);
484                 Range const & next((it + 1)->range);
485                 if (range.end != next.start)
486                         dont_assert = false;
487         }
488
489         if (lyxerr.debugging(Debug::CHANGES))
490                 lyxerr[Debug::CHANGES] << "End" << endl;
491
492         Assert(dont_assert);
493 }
494
495
496 int Changes::latexMarkChange(std::ostream & os, Change::Type old, Change::Type change)
497 {
498         if (old == change)
499                 return 0;
500
501         string const start("\\changestart{}");
502         string const end("\\changeend{}");
503         string const son("\\overstrikeon{}");
504         string const soff("\\overstrikeoff{}");
505
506         int column = 0;
507
508         if (old == Change::DELETED) {
509                 os << soff;
510                 column += soff.length();
511         }
512
513         switch (change) {
514                 case Change::UNCHANGED:
515                         os << end;
516                         column += end.length();
517                         break;
518
519                 case Change::DELETED:
520                         if (old == Change::UNCHANGED) {
521                                 os << start;
522                                 column += start.length();
523                         }
524                         os << son;
525                         column += son.length();
526                         break;
527
528                 case Change::INSERTED:
529                         if (old == Change::UNCHANGED) {
530                                 os << start;
531                                 column += start.length();
532                         }
533                         break;
534         }
535
536         return column;
537 }
538
539
540 void Changes::lyxMarkChange(std::ostream & os, int & column, lyx::time_type curtime,
541         Change const & old, Change const & change)
542 {
543         if (old == change)
544                 return;
545
546         column = 0;
547
548         switch (change.type) {
549                 case Change::UNCHANGED:
550                         os << "\n\\change_unchanged\n";
551                         break;
552
553                 case Change::DELETED: {
554                         lyx::time_type t(change.changetime);
555                         if (!t)
556                                 t = curtime;
557                         os << "\n\\change_deleted " << change.author
558                                 << " " << t << "\n";
559
560                         break;
561                 }
562
563                 case Change::INSERTED:
564                         lyx::time_type t(change.changetime);
565                         if (!t)
566                                 t = curtime;
567                         os << "\n\\change_inserted " << change.author
568                                 << " " << t << "\n";
569                         break;
570         }
571 }