]> git.lyx.org Git - lyx.git/blob - src/Changes.h
LaTeXFeatures.cpp: fix part of http://bugzilla.lyx.org/show_bug.cgi?id=5294 also...
[lyx.git] / src / Changes.h
1 // -*- C++ -*-
2 /**
3  * \file Changes.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  * \author Michael Gerz
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * Record changes in a paragraph.
13  */
14
15 #ifndef CHANGES_H
16 #define CHANGES_H
17
18 #include "ColorCode.h"
19
20 #include "support/strfwd.h"
21 #include "support/types.h"
22 #include "support/lyxtime.h"
23
24 #include <vector>
25
26
27 namespace lyx {
28
29 class AuthorList;
30
31 class Change {
32 public:
33         /// the type of change
34         enum Type {
35                 UNCHANGED, // no change
36                 INSERTED, // new text
37                 DELETED // deleted text
38         };
39
40         explicit Change(Type t = UNCHANGED, int a = 0, time_t ct = current_time())
41                 : type(t), author(a), changetime(ct) {}
42
43         /// is the change similar to the given change such that both can be merged?
44         bool isSimilarTo(Change const & change) const;
45         /// The color of this change on screen
46         ColorCode color() const;
47         ///
48         bool changed() const { return type != UNCHANGED; }
49         ///
50         void setUnchanged() { type = UNCHANGED; }
51         ///
52         bool inserted() const { return type == INSERTED; }
53         ///
54         void setInserted() { type = INSERTED; }
55         ///
56         bool deleted() const { return type == DELETED; }
57         ///
58         void setDeleted() { type = DELETED; }
59
60         Type type;
61
62         int author;
63
64         time_t changetime;
65 };
66
67 bool operator==(Change const & l, Change const & r);
68 bool operator!=(Change const & l, Change const & r);
69
70 class BufferParams;
71
72 class Changes {
73 public:
74         /// set the pos to the given change
75         void set(Change const & change, pos_type pos);
76         /// set the range (excluding end) to the given change
77         void set(Change const & change, pos_type start, pos_type end);
78
79         /// erase the entry at pos and adjust all range bounds past it
80         /// (assumes that a character was deleted at pos)
81         void erase(lyx::pos_type pos);
82
83         /// insert a new entry at pos and adjust all range bounds past it
84         /// (assumes that a character was inserted at pos)
85         void insert(Change const & change, lyx::pos_type pos);
86
87         ///
88
89         /// return the change at the given pos
90         Change const & lookup(pos_type pos) const;
91
92         /// return true if there is a change in the given range (excluding end)
93         bool isChanged(pos_type start, pos_type end) const;
94
95         ///
96
97         /// output latex to mark a transition between two change types
98         /// returns length of text outputted
99         static int latexMarkChange(odocstream & os, BufferParams const & bparams,
100                                    Change const & oldChange, Change const & change);
101
102         /// output .lyx file format for transitions between changes
103         static void lyxMarkChange(std::ostream & os, int & column,
104                 Change const & old, Change const & change);
105
106         ///
107         void checkAuthors(AuthorList const & authorList);
108
109 private:
110         class Range {
111         public:
112                 Range(pos_type s, pos_type e)
113                         : start(s), end(e) {}
114
115                 // does this range contain r ? (inlined as the result of profiling)
116                 bool contains(Range const & r) const {
117                         return r.start >= start && r.end <= end;
118                 }
119
120                 // does this range contain pos ? (inlined as the result of profiling)
121                 bool contains(pos_type pos) const {
122                         return pos >= start && pos < end;
123                 }
124
125                 // do the ranges intersect ?
126                 bool intersects(Range const & r) const;
127
128                 pos_type start;
129                 pos_type end; // Caution: end is not in the range!
130         };
131
132         friend bool operator==(Range const & r1, Range const & r2);
133         friend bool operator!=(Range const & r1, Range const & r2);
134
135         class ChangeRange {
136         public:
137                 ChangeRange(Change const & c, Range const & r)
138                         : change(c), range(r) {}
139
140                 Change change;
141                 Range range;
142         };
143
144         /// merge equal changes with adjoining ranges
145         void merge();
146
147         typedef std::vector<ChangeRange> ChangeTable;
148
149         /// table of changes, every row a change and range descriptor
150         ChangeTable table_;
151 };
152
153
154 } // namespace lyx
155
156 #endif // CHANGES_H