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