]> git.lyx.org Git - features.git/blob - src/TexRow.h
TexRow: enable new RowEntry types
[features.git] / src / TexRow.h
1 // -*- C++ -*-
2 /**
3  * \file TexRow.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Matthias Ettrich
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author Guillaume Munch
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 /* Note about debugging options:
16  *
17  * When compiled in devel mode and run with the option -dbg latex, two ways
18  * of debugging TexRow are available:
19  *
20  * 1. The source view panel prepends the full TexRow information to the LaTeX
21  *    output.
22  *
23  * 2. Clicking on any line in the source view moves the buffer to the location
24  *    recognised by TexRow.
25  *
26  */
27
28 #ifndef TEXROW_H
29 #define TEXROW_H
30
31 #include "support/debug.h"
32 #include "support/types.h"
33
34 #include <vector>
35
36 namespace lyx {
37
38 class Buffer;
39 class Cursor;
40 class CursorSlice;
41 class DocIterator;
42 class docstring_list;
43 class FuncRequest;
44
45 /// types for cells and math insets
46 typedef void const * uid_type;
47 typedef size_t idx_type;
48
49
50 /// Represents the correspondence between paragraphs and the generated
51 /// LaTeX file
52
53 class TexRow {
54 public:
55         /// We begin with defining the types of row information we are tracking
56         ///
57
58         /// type of row entries
59         enum RowType {
60                 text_entry,
61                 math_entry,
62                 begin_document
63         };
64
65         /// an individual par id/pos <=> row mapping
66         struct TextEntry { int id; pos_type pos; };
67
68         /// an individual math id/cell <=> row mapping
69         struct MathEntry { uid_type id; idx_type cell; };
70
71         /// a container for passing entries around
72         struct RowEntry {
73                 RowType type;
74                 union {
75                         struct TextEntry text;// iff the type is text_entry
76                         struct MathEntry math;// iff the type is row_entry
77                         struct {} begindocument;// iff the type is begin_document
78                 };
79         };
80
81         /// Encapsulates the paragraph and position for later use
82         static RowEntry textEntry(int id, pos_type pos);
83         /// Encapsulates a cell and position for later use
84         static RowEntry mathEntry(uid_type id, idx_type cell);
85         /// Denotes the beginning of the document
86         static RowEntry beginDocument();
87
88         /// Converts a CursorSlice into a RowEntry
89         static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
90
91         static const TextEntry text_none;
92         static const RowEntry row_none;
93         /// Returns true if RowEntry is devoid of information
94         static bool isNone(RowEntry entry);
95         /// Returns true if TextEntry is devoid of information
96         static bool isNone(TextEntry entry);
97
98 private:
99         /// id/pos correspondence for a single row
100         class RowEntryList;
101
102         /// container of id/pos <=> row mapping
103         /// invariant: in any enabled_ TexRow, rowlist_ will contain at least one
104         /// Row (the current row)
105         typedef std::vector<RowEntryList> RowList;
106         ///
107         RowList rowlist_;
108         ///
109         RowEntryList & currentRow();
110
111         ///
112         class RowListIterator;
113         ///
114         RowListIterator begin() const;
115         ///
116         RowListIterator end() const;
117 public:
118         ///
119         TexRow();
120
121 #if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6))
122         /// Copy can be expensive and is not usually useful for TexRow.
123         /// Force explicit copy, prefer move instead. This also prevents
124         /// move()s from being converted into copy silently.
125         explicit TexRow(TexRow const & other) = default;
126         TexRow(TexRow && other) = default;
127         TexRow & operator=(TexRow const & other) = default;
128         TexRow & operator=(TexRow && other) = default;
129 # else
130         //for gcc 4.6, nothing to do: it's enough to disable implicit copy during
131         // dev with more recent versions of gcc.
132 #endif
133
134         /// Clears structure.
135         void reset();
136
137         /// for debugging purposes
138         static docstring asString(RowEntry entry);
139
140         /// Defines the row information for the current line
141         /// returns true if this entry will appear on the current row
142         bool start(RowEntry entry);
143         /// Defines the paragraph and position for the current line
144         /// returns true if this entry will appear on the current row
145         bool start(int id, pos_type pos);
146         /// Defines a cell and position for the current line.  Always appear in the
147         /// current row.
148         void startMath(uid_type id, idx_type cell);
149         /// Defines the paragraph for the current cell-like inset.  Always appears
150         /// in the current row like a math cell, but is detached from the normal
151         /// text flow. Note: since the cell idx is not recorded it does not work as
152         /// well as for math grids; if we were to do that properly we would need to
153         /// access the id of the parent Tabular inset from the CursorSlice.
154         void forceStart(int id, pos_type pos);
155
156         /// Insert node when line is completed
157         void newline();
158         /// Insert multiple nodes when zero or more lines are completed
159         void newlines(size_t num_lines);
160
161         /**
162          * getEntriesFromRow - find pids and position for a given row
163          * This is the main algorithm behind reverse-search.
164          * @param row number to find
165          * @return a pair of TextEntry denoting the start and end of the position.
166          * The TextEntry values can be isNone(). If no row is found then the first
167          * value isNone().
168          */
169         std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
170
171         /**
172          * getDocIteratorFromEntries - find pids and positions for a given row
173          * @param buffer where to look
174          * @return a pair of DocIterators denoting the start and end of the
175          * position.  The DocIterators can be invalid.  The starting DocIterator
176          * being invalid means that no location was found.  Note: there is no
177          * guarantee that the DocIterators are in the same inset or even at the
178          * same depth.
179          */
180         static std::pair<DocIterator, DocIterator> getDocIteratorsFromEntries(
181             TextEntry start,
182             TextEntry end,
183             Buffer const & buf);
184
185         // A FuncRequest to select from start to end
186         static FuncRequest goToFunc(TextEntry start, TextEntry end);
187         // A FuncRequest to select a row
188         FuncRequest goToFuncFromRow(int const row) const;
189
190         /**
191          * getDocIteratorFromRow - find pids and positions for a given row
192          * @param row number to find
193          * @param buffer where to look
194          * @return a pair of DocIterators as above.
195          */
196         std::pair<DocIterator, DocIterator> getDocIteratorsFromRow(
197             int row,
198             Buffer const & buf) const;
199
200         /// Finds the best pair of rows for dit
201         /// returns (-1,-1) if not found.
202         /// This is the main algorithm behind forward-search.
203         std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
204
205         /// Finds the best pair of rows for cursor, taking the selection into
206         /// account
207         /// returns (-1,-1) if not found.
208         std::pair<int,int> rowFromCursor(Cursor const & dit) const;
209
210         /// Returns the number of rows contained
211         size_t rows() const;
212         /// Fill or trim to reach the row count \param r
213         void setRows(size_t r);
214
215         /// appends texrow. the final line of this is merged with the first line of
216         /// texrow.
217         void append(TexRow texrow);
218
219         /// for debugging purpose
220         void prepend(docstring_list &) const;
221
222 private:
223         /// true iff same paragraph or math inset or begin_document
224         static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
225         /// computes the distance in pos or cell index
226         /// assumes it is the sameParOrInsetMath
227         static int comparePos(RowEntry entry1, RowEntry entry2);
228
229 };
230
231
232 /// TexString : dumb struct to pass around docstrings with TexRow information.
233 /// They are best created using otexstringstream.
234 /// They can be output to otexrowstreams and otexstreams.
235 /// A valid TexString has as many newlines in str as in texrow. Be careful not
236 /// to introduce a mismatch between the line and the row counts, as this will
237 /// assert in devel mode when outputting to a otexstream.
238 struct TexString {
239         ///
240         docstring str;
241         ///
242         TexRow texrow;
243 #if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6))
244         /// Copy can be expensive and is not usually useful for TexString.
245         /// Force explicit copy, prefer move instead. This also prevents
246         /// move()s from being converted into copy silently.
247         explicit TexString(TexString const &) = default;
248         TexString(TexString && other) = default;
249         TexString & operator=(TexString const & other) = default;
250         TexString & operator=(TexString && other) = default;
251 # else
252         //for gcc 4.6, nothing to do: it's enough to disable implicit copy during
253         // dev with more recent versions of gcc.
254 #endif
255         /// Empty TexString
256         TexString() = default;
257         /// Texstring containing str and TexRow with enough lines which are empty
258         explicit TexString(docstring str);
259         /// Texstring containing str and texrow. Must be valid.
260         TexString(docstring str, TexRow texrow);
261         /// Ensure that the string and the TexRow have as many newlines.
262         void validate();
263 };
264
265
266 // Standard container needs a complete type
267 class TexRow::RowEntryList {
268         // For each row we store a list of one special TextEntry and several
269         // RowEntries. (The order is important.)  We only want one text entry
270         // because we do not want to store every position in the lyx file. On the
271         // other hand we want to record all math and table cells positions for
272         // enough precision. Usually the count of cells is easier to handle.
273         // The RowEntries are used for forward-search and the code preview pane.
274         std::vector<RowEntry> v_;
275         // The TextEntry is currently used for reverse-search and the error
276         // reporting dialog. Once the latter are adapted to rely on the more precise
277         // RowEntries above, it can be removed.
278         TextEntry text_entry_;
279
280 public:
281         typedef std::vector<RowEntry>::iterator iterator;
282         iterator begin() { return v_.begin(); }
283         iterator end() { return v_.end(); }
284         ///
285         typedef std::vector<RowEntry>::const_iterator const_iterator;
286         const_iterator begin() const { return v_.cbegin(); }
287         const_iterator end() const { return v_.cend(); }
288         ///
289         RowEntryList() : text_entry_(TexRow::text_none) {}
290
291         // returns true if the row entry will appear in the row entry list
292         bool addEntry(RowEntry entry);
293
294         // the row entry will appear in the row entry list, but it never counts
295         // as a proper text entry.
296         void forceAddEntry(RowEntry entry);
297
298         // returns the TextEntry or TexRow::text_none if none
299         TextEntry getTextEntry() const;
300
301         // appends a row
302         void append(RowEntryList row);
303 };
304
305
306 bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2);
307
308
309 } // namespace lyx
310
311 #endif // TEXROW_H