]> git.lyx.org Git - lyx.git/blob - src/Changes.h
Don't show citation engines in the list of modules. They are found under the bibliogr...
[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 "Color.h"
19
20 #include "support/docstream.h"
21 #include "support/strfwd.h"
22 #include "support/types.h"
23 #include "support/lyxtime.h"
24
25 #include <vector>
26
27
28 namespace lyx {
29
30 class AuthorList;
31 class Buffer;
32 class DocIterator;
33 class OutputParams;
34
35 class Change {
36 public:
37         /// the type of change
38         enum Type {
39                 UNCHANGED, // no change
40                 INSERTED, // new text
41                 DELETED // deleted text
42         };
43
44         explicit Change(Type t = UNCHANGED, int a = 0, time_t ct = support::current_time())
45                 : type(t), author(a), changetime(ct) {}
46
47         /// is the change similar to the given change such that both can be merged?
48         bool isSimilarTo(Change const & change) const;
49         /// The color of this change on screen
50         Color color() const;
51         ///
52         bool changed() const { return type != UNCHANGED; }
53         ///
54         void setUnchanged() { type = UNCHANGED; }
55         ///
56         bool inserted() const { return type == INSERTED; }
57         ///
58         void setInserted() { type = INSERTED; }
59         ///
60         bool deleted() const { return type == DELETED; }
61         ///
62         void setDeleted() { type = DELETED; }
63         /// Is this change made by the current author ?
64         bool currentAuthor() const { return author == 0; }
65
66         Type type;
67
68         int author;
69
70         time_t changetime;
71 };
72
73 bool operator==(Change const & l, Change const & r);
74 bool operator!=(Change const & l, Change const & r);
75
76 class BufferParams;
77
78 class Changes {
79 public:
80         /// set the pos to the given change
81         void set(Change const & change, pos_type pos);
82         /// set the range (excluding end) to the given change
83         void set(Change const & change, pos_type start, pos_type end);
84
85         /// erase the entry at pos and adjust all range bounds past it
86         /// (assumes that a character was deleted at pos)
87         void erase(lyx::pos_type pos);
88
89         /// insert a new entry at pos and adjust all range bounds past it
90         /// (assumes that a character was inserted at pos)
91         void insert(Change const & change, lyx::pos_type pos);
92
93         ///
94
95         /// return the change at the given pos
96         Change const & lookup(pos_type pos) const;
97
98         /// return true if there is a change in the given range (excluding end)
99         bool isChanged(pos_type start, pos_type end) const;
100
101         /// return true if the whole range is deleted
102         bool isDeleted(pos_type start, pos_type end) const;
103
104         /// output latex to mark a transition between two change types
105         /// returns length of text outputted
106         static int latexMarkChange(otexstream & os, BufferParams const & bparams,
107                                    Change const & oldChange, Change const & change,
108                                    OutputParams const & runparams);
109
110         /// output .lyx file format for transitions between changes
111         static void lyxMarkChange(std::ostream & os, BufferParams const & bparams,
112                 int & column, Change const & old, Change const & change);
113
114         ///
115         void checkAuthors(AuthorList const & authorList);
116
117         ///
118         void addToToc(DocIterator const & cdit, Buffer const & buffer) const;
119
120 private:
121         class Range {
122         public:
123                 Range(pos_type s, pos_type e)
124                         : start(s), end(e) {}
125
126                 // does this range contain r ? (inlined as the result of profiling)
127                 bool contains(Range const & r) const {
128                         return r.start >= start && r.end <= end;
129                 }
130
131                 // does this range contain pos ? (inlined as the result of profiling)
132                 bool contains(pos_type pos) const {
133                         return pos >= start && pos < end;
134                 }
135
136                 // do the ranges intersect ?
137                 bool intersects(Range const & r) const;
138
139                 pos_type start;
140                 pos_type end; // Caution: end is not in the range!
141         };
142
143         friend bool operator==(Range const & r1, Range const & r2);
144         friend bool operator!=(Range const & r1, Range const & r2);
145
146         class ChangeRange {
147         public:
148                 ChangeRange(Change const & c, Range const & r)
149                         : change(c), range(r) {}
150
151                 Change change;
152                 Range range;
153         };
154
155         /// merge equal changes with adjoining ranges
156         void merge();
157
158         typedef std::vector<ChangeRange> ChangeTable;
159
160         /// table of changes, every row a change and range descriptor
161         ChangeTable table_;
162 };
163
164
165 } // namespace lyx
166
167 #endif // CHANGES_H