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