]> git.lyx.org Git - lyx.git/blob - src/changes.h
hopefully fix tex2lyx linking.
[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 Changes {
53 public:
54         /// set the pos to the given change
55         void set(Change const & change, pos_type pos);
56         /// set the range (excluding end) to the given change
57         void set(Change const & change, pos_type start, pos_type end);
58
59         /// erase the entry at pos and adjust all range bounds past it
60         /// (assumes that a character was deleted at pos)
61         void erase(lyx::pos_type pos);
62
63         /// insert a new entry at pos and adjust all range bounds past it
64         /// (assumes that a character was inserted at pos)
65         void insert(Change const & change, lyx::pos_type pos);
66
67         ///
68
69         /// return the change at the given pos
70         Change const lookup(pos_type pos) const;
71
72         /// return true if there is a change in the given range (excluding end)
73         bool isChanged(pos_type start, pos_type end) const;
74
75         ///
76
77         /// output latex to mark a transition between two change types
78         /// returns length of text outputted
79         static int latexMarkChange(odocstream & os, Change::Type oldChangeType,
80                 Change::Type changeType, bool const & output);
81
82         /// output .lyx file format for transitions between changes
83         static void lyxMarkChange(std::ostream & os, int & column,
84                 Change const & old, Change const & change);
85
86 private:
87         class Range {
88         public:
89                 Range(pos_type s, pos_type e)
90                         : start(s), end(e) {}
91
92                 // does this range contain r ?
93                 bool contains(Range const & r) const;
94
95                 // does this range contain pos ?
96                 bool contains(pos_type pos) const;
97
98                 // do the ranges intersect ?
99                 bool intersects(Range const & r) const;
100
101                 pos_type start;
102                 pos_type end; // Caution: end is not in the range!
103         };
104
105         friend bool operator==(Range const & r1, Range const & r2);
106         friend bool operator!=(Range const & r1, Range const & r2);
107
108         class ChangeRange {
109         public:
110                 ChangeRange(Change const & c, Range const & r)
111                         : change(c), range(r) {}
112
113                 Change change;
114                 Range range;
115         };
116
117         /// merge equal changes with adjoining ranges
118         void merge();
119
120         typedef std::vector<ChangeRange> ChangeTable;
121
122         /// table of changes, every row a change and range descriptor
123         ChangeTable table_;
124 };
125
126
127 } // namespace lyx
128
129 #endif // CHANGES_H