]> git.lyx.org Git - lyx.git/blob - src/Changes.cpp
InsetTabular.cpp: whitespace
[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 "Buffer.h"
19 #include "BufferParams.h"
20 #include "LaTeXFeatures.h"
21 #include "Paragraph.h"
22 #include "TocBackend.h"
23
24 #include "support/debug.h"
25 #include "support/gettext.h"
26 #include "support/lassert.h"
27
28 #include <ostream>
29
30 using namespace std;
31
32 namespace lyx {
33
34 /*
35  * Class Change has a changetime field that specifies the exact time at which
36  * a specific change was made. The change time is used as a guidance for the
37  * user while editing his document. Presently, it is not considered for LaTeX
38  * export.
39  * When merging two adjacent changes, the changetime is not considered,
40  * only the equality of the change type and author is checked (in method
41  * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
42  * the later change time is preserved.
43  */
44
45 bool Change::isSimilarTo(Change const & change) const
46 {
47         if (type != change.type)
48                 return false;
49
50         if (type == Change::UNCHANGED)
51                 return true;
52
53         return author == change.author;
54 }
55
56
57 Color Change::color() const
58 {
59         Color color = Color_none;
60         switch (author % 5) {
61                 case 0:
62                         color = Color_changedtextauthor1;
63                         break;
64                 case 1:
65                         color = Color_changedtextauthor2;
66                         break;
67                 case 2:
68                         color = Color_changedtextauthor3;
69                         break;
70                 case 3:
71                         color = Color_changedtextauthor4;
72                         break;
73                 case 4:
74                         color = Color_changedtextauthor5;
75                         break;
76         }
77
78         if (deleted())
79                 color.mergeColor = Color_deletedtextmodifier;
80
81         return color;
82 }
83
84
85 bool operator==(Change const & l, Change const & r)
86 {
87         if (l.type != r.type)
88                 return false;
89
90         // two changes of type UNCHANGED are always equal
91         if (l.type == Change::UNCHANGED)
92                 return true;
93
94         return l.author == r.author && l.changetime == r.changetime;
95 }
96
97
98 bool operator!=(Change const & l, Change const & r)
99 {
100         return !(l == r);
101 }
102
103
104 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
105 {
106         return r1.start == r2.start && r1.end == r2.end;
107 }
108
109
110 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
111 {
112         return !(r1 == r2);
113 }
114
115
116 bool Changes::Range::intersects(Range const & r) const
117 {
118         return r.start < end && r.end > start; // end itself is not in the range!
119 }
120
121
122 void Changes::set(Change const & change, pos_type const pos)
123 {
124         set(change, pos, pos + 1);
125 }
126
127
128 void Changes::set(Change const & change, pos_type const start, pos_type const end)
129 {
130         if (change.type != Change::UNCHANGED) {
131                 LYXERR(Debug::CHANGES, "setting change (type: " << change.type
132                         << ", author: " << change.author 
133                         << ", time: " << long(change.changetime)
134                         << ") in range (" << start << ", " << end << ")");
135         }
136
137         Range const newRange(start, end);
138
139         ChangeTable::iterator it = table_.begin();
140
141         for (; it != table_.end(); ) {
142                 // current change starts like or follows new change
143                 if (it->range.start >= start) {
144                         break;
145                 }
146
147                 // new change intersects with existing change
148                 if (it->range.end > start) {
149                         pos_type oldEnd = it->range.end;
150                         it->range.end = start;
151
152                         LYXERR(Debug::CHANGES, "  cutting tail of type " << it->change.type
153                                 << " resulting in range (" << it->range.start << ", "
154                                 << it->range.end << ")");
155
156                         ++it;
157                         if (oldEnd >= end) {
158                                 LYXERR(Debug::CHANGES, "  inserting tail in range ("
159                                         << end << ", " << oldEnd << ")");
160                                 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
161                         }
162                         continue;
163                 }
164
165                 ++it;
166         }
167
168         if (change.type != Change::UNCHANGED) {
169                 LYXERR(Debug::CHANGES, "  inserting change");
170                 it = table_.insert(it, ChangeRange(change, Range(start, end)));
171                 ++it;
172         }
173
174         for (; it != table_.end(); ) {
175                 // new change 'contains' existing change
176                 if (newRange.contains(it->range)) {
177                         LYXERR(Debug::CHANGES, "  removing subrange ("
178                                 << it->range.start << ", " << it->range.end << ")");
179                         it = table_.erase(it);
180                         continue;
181                 }
182
183                 // new change precedes existing change
184                 if (it->range.start >= end)
185                         break;
186
187                 // new change intersects with existing change
188                 it->range.start = end;
189                 LYXERR(Debug::CHANGES, "  cutting head of type "
190                         << it->change.type << " resulting in range ("
191                         << end << ", " << it->range.end << ")");
192                 break; // no need for another iteration
193         }
194
195         merge();
196 }
197
198
199 void Changes::erase(pos_type const pos)
200 {
201         LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
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,pos+x-1)
208                 if (it->range.start > pos)
209                         --(it->range.start);
210                 // range (pos-x,pos) stays (pos-x,pos)
211                 if (it->range.end > pos)
212                         --(it->range.end);
213         }
214
215         merge();
216 }
217
218
219 void Changes::insert(Change const & change, lyx::pos_type pos)
220 {
221         if (change.type != Change::UNCHANGED) {
222                 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
223                         << " at position " << pos);
224         }
225
226         ChangeTable::iterator it = table_.begin();
227         ChangeTable::iterator end = table_.end();
228
229         for (; it != end; ++it) {
230                 // range (pos,pos+x) becomes (pos+1,pos+x+1)
231                 if (it->range.start >= pos)
232                         ++(it->range.start);
233
234                 // range (pos-x,pos) stays as it is
235                 if (it->range.end > pos)
236                         ++(it->range.end);
237         }
238
239         set(change, pos, pos + 1); // set will call merge
240 }
241
242
243 Change const & Changes::lookup(pos_type const pos) const
244 {
245         static Change const noChange = Change(Change::UNCHANGED);
246
247         ChangeTable::const_iterator it = table_.begin();
248         ChangeTable::const_iterator const end = table_.end();
249
250         for (; it != end; ++it) {
251                 if (it->range.contains(pos))
252                         return it->change;
253         }
254
255         return noChange;
256 }
257
258
259 bool Changes::isFullyDeleted(pos_type start, pos_type end) const
260 {
261         ChangeTable::const_iterator it = table_.begin();
262         ChangeTable::const_iterator const itend = table_.end();
263
264         for (; it != itend; ++it) {
265                 if (it->range.contains(Range(start, end))) {
266                         LYXERR(Debug::CHANGES, "range ("
267                                 << start << ", " << end << ") fully contains ("
268                                 << it->range.start << ", " << it->range.end
269                                 << ") of type " << it->change.type);
270                         return it->change.type == Change::DELETED;
271                 }
272         }
273         return false;
274 }
275
276
277 bool Changes::isChanged(pos_type const start, pos_type const end) const
278 {
279         ChangeTable::const_iterator it = table_.begin();
280         ChangeTable::const_iterator const itend = table_.end();
281
282         for (; it != itend; ++it) {
283                 if (it->range.intersects(Range(start, end))) {
284                         LYXERR(Debug::CHANGES, "found intersection of range ("
285                                 << start << ", " << end << ") with ("
286                                 << it->range.start << ", " << it->range.end
287                                 << ") of type " << it->change.type);
288                         return true;
289                 }
290         }
291         return false;
292 }
293
294
295 void Changes::merge()
296 {
297         ChangeTable::iterator it = table_.begin();
298
299         while (it != table_.end()) {
300                 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
301                         << " and range (" << it->range.start << ", " << it->range.end
302                         << ")");
303
304                 if (it->range.start == it->range.end) {
305                         LYXERR(Debug::CHANGES, "removing empty range for pos "
306                                 << it->range.start);
307
308                         table_.erase(it);
309                         // start again
310                         it = table_.begin();
311                         continue;
312                 }
313
314                 if (it + 1 == table_.end())
315                         break;
316
317                 if (it->change.isSimilarTo((it + 1)->change)
318                     && it->range.end == (it + 1)->range.start) {
319                         LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
320                                 << it->range.end << ") and (" << (it + 1)->range.start << ", "
321                                 << (it + 1)->range.end << ")");
322
323                         (it + 1)->range.start = it->range.start;
324                         (it + 1)->change.changetime = max(it->change.changetime,
325                                                           (it + 1)->change.changetime);
326                         table_.erase(it);
327                         // start again
328                         it = table_.begin();
329                         continue;
330                 }
331
332                 ++it;
333         }
334 }
335
336
337 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
338                              Change const & oldChange, Change const & change)
339 {
340         if (!bparams.outputChanges || oldChange == change)
341                 return 0;
342
343         int column = 0;
344
345         if (oldChange.type != Change::UNCHANGED) {
346                 os << '}'; // close \lyxadded or \lyxdeleted
347                 column++;
348         }
349
350         docstring chgTime;
351         chgTime += ctime(&change.changetime);
352         chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
353
354         if (change.type == Change::DELETED) {
355                 docstring str = "\\lyxdeleted{" +
356                         bparams.authors().get(change.author).name() + "}{" +
357                         chgTime + "}{";
358                 os << str;
359                 column += str.size();
360         } else if (change.type == Change::INSERTED) {
361                 docstring str = "\\lyxadded{" +
362                         bparams.authors().get(change.author).name() + "}{" +
363                         chgTime + "}{";
364                 os << str;
365                 column += str.size();
366         }
367
368         return column;
369 }
370
371
372 void Changes::lyxMarkChange(ostream & os, int & column,
373                             Change const & old, Change const & change)
374 {
375         if (old == change)
376                 return;
377
378         column = 0;
379
380         switch (change.type) {
381                 case Change::UNCHANGED:
382                         os << "\n\\change_unchanged\n";
383                         break;
384
385                 case Change::DELETED: {
386                         os << "\n\\change_deleted " << change.author
387                                 << " " << change.changetime << "\n";
388                         break;
389                 }
390
391                 case Change::INSERTED: {
392                         os << "\n\\change_inserted " << change.author
393                                 << " " << change.changetime << "\n";
394                         break;
395                 }
396         }
397 }
398
399
400 void Changes::checkAuthors(AuthorList const & authorList)
401 {
402         ChangeTable::const_iterator it = table_.begin();
403         ChangeTable::const_iterator endit = table_.end();
404         for ( ; it != endit ; ++it) 
405                 if (it->change.type != Change::UNCHANGED)
406                         authorList.get(it->change.author).setUsed(true);
407 }
408
409
410 void Changes::addToToc(DocIterator const & cdit, Buffer const & buffer) const
411 {
412         if (table_.empty())
413                 return;
414
415         Toc & change_list = buffer.tocBackend().toc("change");
416         AuthorList const & author_list = buffer.params().authors();
417         DocIterator dit = cdit;
418
419         ChangeTable::const_iterator it = table_.begin();
420         ChangeTable::const_iterator const itend = table_.end();
421         for (; it != itend; ++it) {
422                 docstring str;
423                 switch (it->change.type) {
424                 case Change::UNCHANGED:
425                         continue;
426                 case Change::DELETED:
427                         // 0x2702 is a scissors symbol in the Dingbats unicode group.
428                         str.push_back(0x2702);
429                         break;
430                 case Change::INSERTED:
431                         // 0x270d is the hand writting symbol in the Dingbats unicode group.
432                         str.push_back(0x270d);
433                         break;
434                 }
435                 dit.pos() = it->range.start;
436                 Paragraph const & par = dit.paragraph();
437                 str += " " + par.asString(it->range.start, min(par.size(), it->range.end));
438                 if (it->range.end > par.size())
439                         // the end of paragraph symbol from the Punctuation group
440                         str.push_back(0x204B);
441                 docstring const & author = author_list.get(it->change.author).name();
442                 Toc::iterator it = change_list.item(0, author);
443                 if (it == change_list.end()) {
444                         change_list.push_back(TocItem(dit, 0, author));
445                         change_list.push_back(TocItem(dit, 1, str));
446                         continue;
447                 }
448                 for (++it; it != change_list.end(); ++it) {
449                         if (it->depth() == 0 && it->str() != author)
450                                 break;
451                 }
452                 change_list.insert(it, TocItem(dit, 1, str));
453         }
454 }
455
456 } // namespace lyx