]> git.lyx.org Git - lyx.git/blob - src/changes.C
Hopefully temporary fix for the Tabular crash problem. Of course, this is not the...
[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
18 #include <boost/assert.hpp>
19
20 using lyx::pos_type;
21
22 using std::endl;
23 using std::string;
24
25
26 bool operator==(Change const & l, Change const & r)
27 {
28         return l.type == r.type && l.author == r.author
29                 && l.changetime == r.changetime;
30 }
31
32
33 bool operator!=(Change const & l, Change const & r)
34 {
35         return !(l == r);
36 }
37
38
39 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
40 {
41         return r1.start == r2.start && r1.end == r2.end;
42 }
43
44
45 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
46 {
47         return !(r1 == r2);
48 }
49
50
51 bool Changes::Range::contains(Range const & r) const
52 {
53         return r.start >= start && r.end <= end;
54 }
55
56
57 bool Changes::Range::contained(Range const & r) const
58 {
59         return r.contains(*this);
60 }
61
62
63 bool Changes::Range::contains(pos_type const pos) const
64 {
65         return pos >= start && pos < end;
66 }
67
68
69 bool Changes::Range::containsOrPrecedes(pos_type const pos) const
70 {
71         return pos >= start && pos <= end;
72 }
73
74
75 bool Changes::Range::intersects(Range const & r) const
76 {
77         return contained(r) || contains(r)
78                 || contains(r.start) || contains(r.end);
79 }
80
81
82 Changes::Changes(Change::Type const type)
83         : empty_type_(type)
84 {
85 }
86
87
88 Changes::~Changes()
89 {
90 }
91
92
93 Changes::Changes(Changes const & c)
94 {
95         table_ = c.table_;
96 }
97
98
99 void Changes::record(Change const & change, pos_type const pos)
100 {
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         }
106
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         }
118 }
119
120
121 void Changes::set(Change const & change, pos_type const pos)
122 {
123         set(change, pos, pos + 1);
124 }
125
126
127 void Changes::set(Change::Type const type, pos_type const pos)
128 {
129         set(type, pos, pos + 1);
130 }
131
132
133 void Changes::set(Change::Type const type,
134                   pos_type const start, pos_type const end)
135 {
136         set(Change(type), start, end);
137 }
138
139
140 void Changes::set(Change const & change,
141                   pos_type const start, pos_type const 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 const 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 const 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 const & change, ChangeTable::size_type const 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.containsOrPrecedes(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 const & change, ChangeTable::size_type const 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.containsOrPrecedes(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::lookup(pos_type const 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 const end = table_.end();
325
326         for (; it != end; ++it) {
327                 if (it->range.contains(pos))
328                         return it->change;
329         }
330
331         check();
332         BOOST_ASSERT(false && "missing changes for pos");
333         return Change(Change::UNCHANGED);
334 }
335
336
337 bool Changes::isChange(pos_type const start, pos_type const end) 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_ != Change::UNCHANGED;
343         }
344
345         ChangeTable::const_iterator it = table_.begin();
346         ChangeTable::const_iterator const itend = table_.end();
347
348         for (; it != itend; ++it) {
349                 if (lyxerr.debugging(Debug::CHANGES)) {
350                         lyxerr[Debug::CHANGES] << "Looking for " << start << ","
351                                 << end << " in " << it->range.start << ","
352                                 << it->range.end << "of type " << it->change.type << endl;
353                 }
354
355                 if (it->range.intersects(Range(start, end))
356                         && it->change.type != Change::UNCHANGED) {
357                         if (lyxerr.debugging(Debug::CHANGES)) {
358                                 lyxerr[Debug::CHANGES] << "Found intersection of "
359                                         << start << "," << end << " with "
360                                         << it->range.start << "," << it->range.end
361                                         << " of type " << it->change.type << endl;
362                         }
363                         return true;
364                 }
365         }
366
367         return false;
368 }
369
370
371 void Changes::merge()
372 {
373         if (lyxerr.debugging(Debug::CHANGES))
374                 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
375
376         ChangeTable::iterator it = table_.begin();
377
378         while (it != table_.end()) {
379                 if (lyxerr.debugging(Debug::CHANGES)) {
380                         lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
381                                 << it->range.start << "," << it->range.end << endl;
382                 }
383
384                 if (it->range.start == it->range.end) {
385                         if (lyxerr.debugging(Debug::CHANGES)) {
386                                 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
387                                         << it->range.start << endl;
388                         }
389
390                         table_.erase(it);
391                         // start again
392                         it = table_.begin();
393                         continue;
394                 }
395
396                 if (it + 1 == table_.end())
397                         break;
398
399                 if (it->change == (it + 1)->change) {
400                         if (lyxerr.debugging(Debug::CHANGES)) {
401                                 lyxerr[Debug::CHANGES] << "Merging equal ranges "
402                                         << it->range.start << "," << it->range.end
403                                         << " and " << (it + 1)->range.start << ","
404                                         << (it + 1)->range.end << endl;
405                         }
406
407                         (it + 1)->range.start = it->range.start;
408                         table_.erase(it);
409                         // start again
410                         it = table_.begin();
411                         continue;
412                 }
413
414                 ++it;
415         }
416
417         lyxerr[Debug::CHANGES] << "Merge ended" << endl;
418         check();
419 }
420
421
422 void Changes::check() const
423 {
424         ChangeTable::const_iterator it = table_.begin();
425         ChangeTable::const_iterator end = table_.end();
426
427         bool dont_assert = true;
428
429         lyxerr[Debug::CHANGES] << "Changelist:" << endl;
430         for (; it != end; ++it) {
431                 if (lyxerr.debugging(Debug::CHANGES)) {
432                         lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
433                                 << it->range.start << "," << it->range.end << " author "
434                                 << it->change.author << " time " << it->change.changetime << endl;
435                 }
436
437                 if (it + 1 == end)
438                         break;
439
440                 Range const & range(it->range);
441                 Range const & next((it + 1)->range);
442                 if (range.end != next.start)
443                         dont_assert = false;
444         }
445
446         if (lyxerr.debugging(Debug::CHANGES))
447                 lyxerr[Debug::CHANGES] << "End" << endl;
448
449         BOOST_ASSERT(dont_assert);
450 }
451
452
453 int Changes::latexMarkChange(std::ostream & os,
454                              Change::Type const old, Change::Type const change,
455                              bool const & output)
456 {
457         if (!output || old == change)
458                 return 0;
459
460         string const start("\\changestart{}");
461         string const end("\\changeend{}");
462         string const son("\\overstrikeon{}");
463         string const soff("\\overstrikeoff{}");
464
465         int column = 0;
466
467         if (old == Change::DELETED) {
468                 os << soff;
469                 column += soff.length();
470         }
471
472         switch (change) {
473                 case Change::UNCHANGED:
474                         os << end;
475                         column += end.length();
476                         break;
477
478                 case Change::DELETED:
479                         if (old == Change::UNCHANGED) {
480                                 os << start;
481                                 column += start.length();
482                         }
483                         os << son;
484                         column += son.length();
485                         break;
486
487                 case Change::INSERTED:
488                         if (old == Change::UNCHANGED) {
489                                 os << start;
490                                 column += start.length();
491                         }
492                         break;
493         }
494
495         return column;
496 }
497
498
499 void Changes::lyxMarkChange(std::ostream & os, int & column,
500                             lyx::time_type const curtime,
501                             Change const & old, Change const & change)
502 {
503         if (old == change)
504                 return;
505
506         column = 0;
507
508         switch (change.type) {
509                 case Change::UNCHANGED:
510                         os << "\n\\change_unchanged\n";
511                         break;
512
513                 case Change::DELETED: {
514                         lyx::time_type t = change.changetime;
515                         if (!t)
516                                 t = curtime;
517                         os << "\n\\change_deleted " << change.author
518                                 << " " << t << "\n";
519
520                         break;
521                 }
522
523         case Change::INSERTED: {
524                         lyx::time_type t = change.changetime;
525                         if (!t)
526                                 t = curtime;
527                         os << "\n\\change_inserted " << change.author
528                                 << " " << t << "\n";
529                         break;
530         }
531         }
532 }