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