]> git.lyx.org Git - lyx.git/blob - src/changes.C
4265e22c52c5c8d4c9271590271f76bb0a080b66
[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
21 namespace lyx {
22
23 using std::endl;
24 using std::string;
25
26
27 bool operator==(Change const & l, Change const & r)
28 {
29         return l.type == r.type && l.author == r.author
30                 && l.changetime == r.changetime;
31 }
32
33
34 bool operator!=(Change const & l, Change const & r)
35 {
36         return !(l == r);
37 }
38
39
40 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
41 {
42         return r1.start == r2.start && r1.end == r2.end;
43 }
44
45
46 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
47 {
48         return !(r1 == r2);
49 }
50
51
52 bool Changes::Range::contains(Range const & r) const
53 {
54         return r.start >= start && r.end <= end;
55 }
56
57
58 bool Changes::Range::contains(pos_type const pos) const
59 {
60         return pos >= start && pos < end;
61 }
62
63
64 bool Changes::Range::intersects(Range const & r) const
65 {
66         return r.start < end && r.end > start; // end itself is not in the range!
67 }
68
69
70 void Changes::set(Change const & change, pos_type const pos)
71 {
72         set(change, pos, pos + 1);
73 }
74
75
76 void Changes::set(Change const & change,
77                   pos_type const start, pos_type const end)
78 {
79         ChangeTable::iterator it = table_.begin();
80
81         if (lyxerr.debugging(Debug::CHANGES)) {
82                 lyxerr[Debug::CHANGES] << "changeset of " << change.type
83                         << " author " << change.author << " time " << change.changetime
84                         << " in range " << start << "," << end << endl;
85         }
86
87         Range const new_range(start, end);
88
89         // remove all sub-ranges
90         for (; it != table_.end();) {
91                 if (new_range != it->range /*&& it->range.contained(new_range)*/) { // FIXME: change tracking (MG)
92                         if (lyxerr.debugging(Debug::CHANGES)) {
93                                 lyxerr[Debug::CHANGES] << "Removing subrange "
94                                         << it->range.start << "," << it->range.end << endl;
95                         }
96                         it = table_.erase(it);
97                 } else {
98                         ++it;
99                 }
100         }
101
102         it = table_.begin();
103         ChangeTable::iterator const itend = table_.end();
104
105         // find a super-range
106         for (; it != itend; ++it) {
107                 if (it->range.contains(new_range))
108                         break;
109         }
110
111         if (it == itend) {
112                 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
113                 table_.push_back(ChangeRange(change, Range(start, end)));
114                 merge();
115                 return;
116         }
117
118         if (change.type == it->change.type) {
119                 lyxerr[Debug::CHANGES] << "Change set already." << endl;
120                 it->change = change;
121                 return;
122         }
123
124         ChangeRange c(*it);
125
126         if (lyxerr.debugging(Debug::CHANGES)) {
127                 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
128                         << " over " << c.range.start << "," << c.range.end << endl;
129         }
130
131         // split head
132         if (c.range.start < start) {
133                 it = table_.insert(it, ChangeRange(c.change, Range(c.range.start, start)));
134                 if (lyxerr.debugging(Debug::CHANGES)) {
135                         lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
136                                 << " over " << c.range.start << "," << start << endl;
137                 }
138                 ++it;
139         }
140
141         // reset this as new type
142         it->range.start = start;
143         it->range.end = end;
144         it->change = change;
145         lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
146
147         // split tail
148         if (c.range.end > end) {
149                 ++it;
150                 table_.insert(it, ChangeRange(c.change, Range(end, c.range.end)));
151                 if (lyxerr.debugging(Debug::CHANGES)) {
152                         lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
153                                 << " over " << end << "," << c.range.end << endl;
154                 }
155         }
156
157         merge();
158 }
159
160
161 void Changes::erase(pos_type const pos)
162 {
163         ChangeTable::iterator it = table_.begin();
164         ChangeTable::iterator end = table_.end();
165
166         bool found = false;
167
168         for (; it != end; ++it) {
169                 Range & range(it->range);
170
171                 if (lyxerr.debugging(Debug::CHANGES)) {
172                         lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
173                                 << it->range.start << "," << it->range.end << endl;
174                 }
175
176                 if (range.contains(pos)) {
177                         found = true;
178                         --range.end;
179                         continue;
180                 }
181
182                 if (found) {
183                         --range.start;
184                         --range.end;
185                 }
186         }
187         merge();
188 }
189
190
191 Change const Changes::lookup(pos_type const pos) const
192 {
193         ChangeTable::const_iterator it = table_.begin();
194         ChangeTable::const_iterator const end = table_.end();
195
196         for (; it != end; ++it) {
197                 if (it->range.contains(pos))
198                         return it->change;
199         }
200
201         BOOST_ASSERT(false && "missing changes for pos");
202         return Change(Change::UNCHANGED);
203 }
204
205
206 bool Changes::isChanged(pos_type const start, pos_type const end) const
207 {
208         ChangeTable::const_iterator it = table_.begin();
209         ChangeTable::const_iterator const itend = table_.end();
210
211         for (; it != itend; ++it) {
212                 if (lyxerr.debugging(Debug::CHANGES)) {
213                         lyxerr[Debug::CHANGES] << "Looking for " << start << ","
214                                 << end << " in " << it->range.start << ","
215                                 << it->range.end << "of type " << it->change.type << endl;
216                 }
217
218                 if (it->range.intersects(Range(start, end))
219                         && it->change.type != Change::UNCHANGED) {
220                         if (lyxerr.debugging(Debug::CHANGES)) {
221                                 lyxerr[Debug::CHANGES] << "Found intersection of "
222                                         << start << "," << end << " with "
223                                         << it->range.start << "," << it->range.end
224                                         << " of type " << it->change.type << endl;
225                         }
226                         return true;
227                 }
228         }
229
230         return false;
231 }
232
233
234 void Changes::merge()
235 {
236         if (lyxerr.debugging(Debug::CHANGES))
237                 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
238
239         ChangeTable::iterator it = table_.begin();
240
241         while (it != table_.end()) {
242                 if (lyxerr.debugging(Debug::CHANGES)) {
243                         lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
244                                 << it->range.start << "," << it->range.end << endl;
245                 }
246
247                 if (it->range.start == it->range.end) {
248                         if (lyxerr.debugging(Debug::CHANGES)) {
249                                 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
250                                         << it->range.start << endl;
251                         }
252
253                         table_.erase(it);
254                         // start again
255                         it = table_.begin();
256                         continue;
257                 }
258
259                 if (it + 1 == table_.end())
260                         break;
261
262                 if (it->change == (it + 1)->change) {
263                         if (lyxerr.debugging(Debug::CHANGES)) {
264                                 lyxerr[Debug::CHANGES] << "Merging equal ranges "
265                                         << it->range.start << "," << it->range.end
266                                         << " and " << (it + 1)->range.start << ","
267                                         << (it + 1)->range.end << endl;
268                         }
269
270                         (it + 1)->range.start = it->range.start;
271                         table_.erase(it);
272                         // start again
273                         it = table_.begin();
274                         continue;
275                 }
276
277                 ++it;
278         }
279
280         lyxerr[Debug::CHANGES] << "Merge ended" << endl;
281 }
282
283
284 int Changes::latexMarkChange(odocstream & os,
285                              Change::Type const old, Change::Type const change,
286                              bool const & output)
287 {
288         if (!output || old == change)
289                 return 0;
290
291         static docstring const start(from_ascii("\\changestart{}"));
292         static docstring const end(from_ascii("\\changeend{}"));
293         static docstring const son(from_ascii("\\overstrikeon{}"));
294         static docstring const soff(from_ascii("\\overstrikeoff{}"));
295
296         int column = 0;
297
298         if (old == Change::DELETED) {
299                 os << soff;
300                 column += soff.length();
301         }
302
303         switch (change) {
304                 case Change::UNCHANGED:
305                         os << end;
306                         column += end.length();
307                         break;
308
309                 case Change::DELETED:
310                         if (old == Change::UNCHANGED) {
311                                 os << start;
312                                 column += start.length();
313                         }
314                         os << son;
315                         column += son.length();
316                         break;
317
318                 case Change::INSERTED:
319                         if (old == Change::UNCHANGED) {
320                                 os << start;
321                                 column += start.length();
322                         }
323                         break;
324         }
325
326         return column;
327 }
328
329
330 void Changes::lyxMarkChange(std::ostream & os, int & column,
331                             time_type const curtime,
332                             Change const & old, Change const & change)
333 {
334         if (old == change)
335                 return;
336
337         column = 0;
338
339         switch (change.type) {
340                 case Change::UNCHANGED:
341                         os << "\n\\change_unchanged\n";
342                         break;
343
344                 case Change::DELETED: {
345                         time_type t = change.changetime;
346                         if (!t)
347                                 t = curtime;
348                         os << "\n\\change_deleted " << change.author
349                                 << " " << t << "\n";
350
351                         break;
352                 }
353
354         case Change::INSERTED: {
355                         time_type t = change.changetime;
356                         if (!t)
357                                 t = curtime;
358                         os << "\n\\change_inserted " << change.author
359                                 << " " << t << "\n";
360                         break;
361         }
362         }
363 }
364
365
366 } // namespace lyx