]> git.lyx.org Git - lyx.git/blob - src/Changes.cpp
* some more traces of the signals in CursorSlice
[lyx.git] / src / Changes.cpp
1 /**
2  * \file Changes.cpp
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  * \author Michael Gerz
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Record changes in a paragraph.
12  */
13
14 #include <config.h>
15
16 #include "Changes.h"
17 #include "debug.h"
18 #include "Author.h"
19 #include "BufferParams.h"
20 #include "LaTeXFeatures.h"
21
22 #include <boost/assert.hpp>
23
24
25 namespace lyx {
26
27 using std::abs;
28 using std::endl;
29 using std::string;
30 using std::max;
31
32 /*
33  * Class Change has a changetime field that specifies the exact time at which
34  * a specific change was made. The change time is used as a guidance for the
35  * user while editing his document. Presently, it is not considered for LaTeX
36  * export.
37  * When merging two adjacent changes, the changetime is not considered,
38  * only the equality of the change type and author is checked (in method
39  * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
40  * the later change time is preserved.
41  */
42
43 bool Change::isSimilarTo(Change const & change)
44 {
45         if (type != change.type) {
46                 return false;
47         }
48
49         if (type == Change::UNCHANGED) {
50                 return true;
51         }
52
53         return author == change.author;
54 }
55
56
57 bool operator==(Change const & l, Change const & r)
58 {
59         if (l.type != r.type) {
60                 return false;
61         }
62
63         // two changes of type UNCHANGED are always equal
64         if (l.type == Change::UNCHANGED) {
65                 return true;
66         }
67
68         return l.author == r.author &&
69                l.changetime == r.changetime;
70 }
71
72
73 bool operator!=(Change const & l, Change const & r)
74 {
75         return !(l == r);
76 }
77
78
79 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
80 {
81         return r1.start == r2.start && r1.end == r2.end;
82 }
83
84
85 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
86 {
87         return !(r1 == r2);
88 }
89
90
91 bool Changes::Range::intersects(Range const & r) const
92 {
93         return r.start < end && r.end > start; // end itself is not in the range!
94 }
95
96
97 void Changes::set(Change const & change, pos_type const pos)
98 {
99         set(change, pos, pos + 1);
100 }
101
102
103 void Changes::set(Change const & change, pos_type const start, pos_type const end)
104 {
105         if (change.type != Change::UNCHANGED) {
106                 LYXERR(Debug::CHANGES) << "setting change (type: " << change.type
107                         << ", author: " << change.author << ", time: " << change.changetime
108                         << ") in range (" << start << ", " << end << ")" << endl;
109         }
110
111         Range const newRange(start, end);
112
113         ChangeTable::iterator it = table_.begin();
114
115         for (; it != table_.end(); ) {
116                 // current change starts like or follows new change
117                 if (it->range.start >= start) {
118                         break;
119                 }
120
121                 // new change intersects with existing change
122                 if (it->range.end > start) {
123                         pos_type oldEnd = it->range.end;
124                         it->range.end = start;
125
126                         LYXERR(Debug::CHANGES) << "  cutting tail of type " << it->change.type
127                                 << " resulting in range (" << it->range.start << ", "
128                                 << it->range.end << ")" << endl;
129
130                         ++it;
131                         if (oldEnd >= end) {
132                                 LYXERR(Debug::CHANGES) << "  inserting tail in range ("
133                                         << end << ", " << oldEnd << ")" << endl;
134                                 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
135                         }
136                         continue;
137                 }
138
139                 ++it;
140         }
141
142         if (change.type != Change::UNCHANGED) {
143                 LYXERR(Debug::CHANGES) << "  inserting change" << endl;
144                 it = table_.insert(it, ChangeRange(change, Range(start, end)));
145                 ++it;
146         }
147
148         for (; it != table_.end(); ) {
149                 // new change 'contains' existing change
150                 if (newRange.contains(it->range)) {
151                         LYXERR(Debug::CHANGES) << "  removing subrange ("
152                                 << it->range.start << ", " << it->range.end << ")" << endl;
153                         it = table_.erase(it);
154                         continue;
155                 }
156
157                 // new change precedes existing change
158                 if (it->range.start >= end) {
159                         break;
160                 }
161
162                 // new change intersects with existing change
163                 it->range.start = end;
164                 LYXERR(Debug::CHANGES) << "  cutting head of type "
165                         << it->change.type << " resulting in range ("
166                         << end << ", " << it->range.end << ")" << endl;
167                 break; // no need for another iteration
168         }
169
170         merge();
171 }
172
173
174 void Changes::erase(pos_type const pos)
175 {
176         LYXERR(Debug::CHANGES) << "Erasing change at position " << pos << endl;
177
178         ChangeTable::iterator it = table_.begin();
179         ChangeTable::iterator end = table_.end();
180
181         for (; it != end; ++it) {
182                 // range (pos,pos+x) becomes (pos,pos+x-1)
183                 if (it->range.start > pos) {
184                         --(it->range.start);
185                 }
186                 // range (pos-x,pos) stays (pos-x,pos)
187                 if (it->range.end > pos) {
188                         --(it->range.end);
189                 }
190         }
191
192         merge();
193 }
194
195
196 void Changes::insert(Change const & change, lyx::pos_type pos)
197 {
198         if (change.type != Change::UNCHANGED) {
199                 LYXERR(Debug::CHANGES) << "Inserting change of type " << change.type
200                         << " at position " << pos << endl;
201         }
202
203         ChangeTable::iterator it = table_.begin();
204         ChangeTable::iterator end = table_.end();
205
206         for (; it != end; ++it) {
207                 // range (pos,pos+x) becomes (pos+1,pos+x+1)
208                 if (it->range.start >= pos) {
209                         ++(it->range.start);
210                 }
211
212                 // range (pos-x,pos) stays as it is
213                 if (it->range.end > pos) {
214                         ++(it->range.end);
215                 }
216         }
217
218         set(change, pos, pos + 1); // set will call merge
219 }
220
221
222 Change const & Changes::lookup(pos_type const pos) const
223 {
224         static Change const noChange = Change(Change::UNCHANGED);
225
226         ChangeTable::const_iterator it = table_.begin();
227         ChangeTable::const_iterator const end = table_.end();
228
229         for (; it != end; ++it) {
230                 if (it->range.contains(pos))
231                         return it->change;
232         }
233
234         return noChange;
235 }
236
237
238 bool Changes::isChanged(pos_type const start, pos_type const end) const
239 {
240         ChangeTable::const_iterator it = table_.begin();
241         ChangeTable::const_iterator const itend = table_.end();
242
243         for (; it != itend; ++it) {
244                 if (it->range.intersects(Range(start, end))) {
245                         LYXERR(Debug::CHANGES) << "found intersection of range ("
246                                 << start << ", " << end << ") with ("
247                                 << it->range.start << ", " << it->range.end
248                                 << ") of type " << it->change.type << endl;
249                         return true;
250                 }
251         }
252         return false;
253 }
254
255
256 void Changes::merge()
257 {
258         ChangeTable::iterator it = table_.begin();
259
260         while (it != table_.end()) {
261                 LYXERR(Debug::CHANGES) << "found change of type " << it->change.type
262                         << " and range (" << it->range.start << ", " << it->range.end
263                         << ")" << endl;
264
265                 if (it->range.start == it->range.end) {
266                         LYXERR(Debug::CHANGES) << "removing empty range for pos "
267                                 << it->range.start << endl;
268
269                         table_.erase(it);
270                         // start again
271                         it = table_.begin();
272                         continue;
273                 }
274
275                 if (it + 1 == table_.end())
276                         break;
277
278                 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
279                         LYXERR(Debug::CHANGES) << "merging ranges (" << it->range.start << ", "
280                                 << it->range.end << ") and (" << (it + 1)->range.start << ", "
281                                 << (it + 1)->range.end << ")" << endl;
282
283                         (it + 1)->range.start = it->range.start;
284                         (it + 1)->change.changetime = max(it->change.changetime,
285                                                           (it + 1)->change.changetime);
286                         table_.erase(it);
287                         // start again
288                         it = table_.begin();
289                         continue;
290                 }
291
292                 ++it;
293         }
294 }
295
296
297 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
298                              Change const & oldChange, Change const & change)
299 {
300         if (!bparams.outputChanges || oldChange == change)
301                 return 0;
302
303         int column = 0;
304
305         if (oldChange.type != Change::UNCHANGED) {
306                 os << '}'; // close \lyxadded or \lyxdeleted
307                 column++;
308         }
309
310         docstring chgTime;
311         chgTime += ctime(&change.changetime);
312         chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
313
314         if (change.type == Change::DELETED) {
315                 docstring str = "\\lyxdeleted{" +
316                         bparams.authors().get(change.author).name() + "}{" +
317                         chgTime + "}{";
318                 os << str;
319                 column += str.size();
320         } else if (change.type == Change::INSERTED) {
321                 docstring str = "\\lyxadded{" +
322                         bparams.authors().get(change.author).name() + "}{" +
323                         chgTime + "}{";
324                 os << str;
325                 column += str.size();
326         }
327
328         return column;
329 }
330
331
332 void Changes::lyxMarkChange(std::ostream & os, int & column,
333                             Change const & old, Change const & change)
334 {
335         if (old == change)
336                 return;
337
338         column = 0;
339
340         switch (change.type) {
341                 case Change::UNCHANGED:
342                         os << "\n\\change_unchanged\n";
343                         break;
344
345                 case Change::DELETED: {
346                         os << "\n\\change_deleted " << change.author
347                                 << " " << change.changetime << "\n";
348                         break;
349                 }
350
351                 case Change::INSERTED: {
352                         os << "\n\\change_inserted " << change.author
353                                 << " " << change.changetime << "\n";
354                         break;
355                 }
356         }
357 }
358
359
360 } // namespace lyx