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