]> git.lyx.org Git - lyx.git/blob - src/changes.h
277308b43f0c6a548f0c7e3e765f429f737c0767
[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 class Change {
25 public:
26         /// the type of change
27         enum Type {
28                 UNCHANGED, // no change
29                 INSERTED, // new text
30                 DELETED // deleted text
31         };
32
33         explicit Change(Type t, int a = 0, lyx::time_type ct = 0)
34                 : type(t), author(a), changetime(ct) {}
35
36         Type type;
37
38         int author;
39
40         lyx::time_type changetime;
41 };
42
43 bool operator==(Change const & l, Change const & r);
44 bool operator!=(Change const & l, Change const & r);
45
46 class Changes {
47 public:
48         /// set the pos to the given change
49         void set(Change const & change, lyx::pos_type pos);
50         /// set the range to the given change
51         void set(Change const & change, lyx::pos_type start, lyx::pos_type end);
52
53         /// mark the given change and adjust
54         void record(Change const & change, lyx::pos_type pos);
55
56         /// return the change at the given position
57         Change const lookup(lyx::pos_type pos) const;
58
59         /// return true if there is a change in the given range
60         bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
61
62         /// remove the given entry. This implies that a character was
63         /// deleted at pos, and will adjust all range bounds past it
64         void erase(lyx::pos_type pos);
65
66         /// output latex to mark a transition between two changetypes
67         /// returns length of text outputted
68         static int latexMarkChange(lyx::odocstream & os, Change::Type old,
69                 Change::Type change, bool const & output);
70
71         /// output .lyx file format for transitions between changes
72         static void lyxMarkChange(std::ostream & os, int & column,
73                 lyx::time_type curtime, Change const & old, Change const & change);
74
75 private:
76         class Range {
77         public:
78                 Range(lyx::pos_type s, lyx::pos_type e)
79                         : start(s), end(e) {}
80
81                 // does this range contain r ?
82                 bool contains(Range const & r) const;
83
84                 // does this range contain pos ?
85                 bool contains(lyx::pos_type pos) const;
86
87                 // do the ranges intersect ?
88                 bool intersects(Range const & r) const;
89
90                 lyx::pos_type start;
91                 lyx::pos_type end; // Caution: end is not in the range!
92         };
93
94         friend bool operator==(Range const & r1, Range const & r2);
95         friend bool operator!=(Range const & r1, Range const & r2);
96
97         class ChangeRange {
98         public:
99                 ChangeRange(lyx::pos_type s, lyx::pos_type e, Change const & c)
100                         : range(Range(s, e)), change(c) {}
101                 Range range;
102                 Change change;
103         };
104
105         typedef std::vector<ChangeRange> ChangeTable;
106
107         /// our table of changes, every row a range and change descriptor
108         ChangeTable table_;
109
110         /// handle a delete, either logical or physical (see erase)
111         void del(Change const & change, ChangeTable::size_type pos);
112
113         /// handle an add, adjusting range bounds past it
114         void add(Change const & change, ChangeTable::size_type pos);
115
116         /// merge neighbouring ranges, assuming that they are abutting
117         /// (as done by set())
118         void merge();
119 };
120
121 #endif // CHANGES_H