]> git.lyx.org Git - features.git/blob - src/TexRow.h
Unify naming of menu items
[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/docstring.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         /// Copy can be expensive and is not usually useful for TexRow.
122         /// Force explicit copy, prefer move instead. This also prevents
123         /// move()s from being converted into copy silently.
124         explicit TexRow(TexRow const & other) = default;
125         TexRow(TexRow && other) = default;
126         TexRow & operator=(TexRow const & other) = default;
127         TexRow & operator=(TexRow && other) = default;
128
129         /// Clears structure.
130         void reset();
131
132         /// for debugging purposes
133         static docstring asString(RowEntry entry);
134
135         /// Defines the row information for the current line
136         /// returns true if this entry will appear on the current row
137         bool start(RowEntry entry);
138         /// Defines the paragraph and position for the current line
139         /// returns true if this entry will appear on the current row
140         bool start(int id, pos_type pos);
141         /// Defines a cell and position for the current line.  Always appear in the
142         /// current row.
143         void startMath(uid_type id, idx_type cell);
144         /// Defines the paragraph for the current cell-like inset.  Always appears
145         /// in the current row like a math cell, but is detached from the normal
146         /// text flow. Note: since the cell idx is not recorded it does not work as
147         /// well as for math grids; if we were to do that properly we would need to
148         /// access the id of the parent Tabular inset from the CursorSlice.
149         void forceStart(int id, pos_type pos);
150
151         /// Insert node when line is completed
152         void newline();
153         /// Insert multiple nodes when zero or more lines are completed
154         void newlines(size_t num_lines);
155
156         /**
157          * getEntriesFromRow - find pids and position for a given row
158          * This is the main algorithm behind reverse-search.
159          * @param row number to find
160          * @return a pair of TextEntry denoting the start and end of the position.
161          * The TextEntry values can be isNone(). If no row is found then the first
162          * value isNone().
163          */
164         std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
165
166         /**
167          * getDocIteratorFromEntries - find pids and positions for a given row
168          * @param buffer where to look
169          * @return a pair of DocIterators denoting the start and end of the
170          * position.  The DocIterators can be invalid.  The starting DocIterator
171          * being invalid means that no location was found.  Note: there is no
172          * guarantee that the DocIterators are in the same inset or even at the
173          * same depth.
174          */
175         static std::pair<DocIterator, DocIterator> getDocIteratorsFromEntries(
176             TextEntry start,
177             TextEntry end,
178             Buffer const & buf);
179
180         // A FuncRequest to select from start to end
181         static FuncRequest goToFunc(TextEntry start, TextEntry end);
182         // A FuncRequest to select a row
183         FuncRequest goToFuncFromRow(int const row) const;
184
185         /**
186          * getDocIteratorFromRow - find pids and positions for a given row
187          * @param row number to find
188          * @param buffer where to look
189          * @return a pair of DocIterators as above.
190          */
191         std::pair<DocIterator, DocIterator> getDocIteratorsFromRow(
192             int row,
193             Buffer const & buf) const;
194
195         /// Finds the best pair of rows for dit
196         /// returns (-1,-1) if not found.
197         /// This is the main algorithm behind forward-search.
198         std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
199
200         /// Finds the best pair of rows for cursor, taking the selection into
201         /// account
202         /// returns (-1,-1) if not found.
203         std::pair<int,int> rowFromCursor(Cursor const & dit) const;
204
205         /// Returns the number of rows contained
206         size_t rows() const;
207         /// Fill or trim to reach the row count \param r
208         void setRows(size_t r);
209
210         /// appends texrow. the final line of this is merged with the first line of
211         /// texrow.
212         void append(TexRow texrow);
213
214         /// for debugging purpose
215         void prepend(docstring_list &) const;
216
217 private:
218         /// true iff same paragraph or math inset or begin_document
219         static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
220         /// computes the distance in pos or cell index
221         /// assumes it is the sameParOrInsetMath
222         static int comparePos(RowEntry entry1, RowEntry entry2);
223
224 };
225
226
227 /// TexString : dumb struct to pass around docstrings with TexRow information.
228 /// They are best created using otexstringstream.
229 /// They can be output to otexrowstreams and otexstreams.
230 /// A valid TexString has as many newlines in str as in texrow. Be careful not
231 /// to introduce a mismatch between the line and the row counts, as this will
232 /// assert in devel mode when outputting to a otexstream.
233 struct TexString {
234         ///
235         docstring str;
236         ///
237         TexRow texrow;
238         /// Copy can be expensive and is not usually useful for TexString.
239         /// Force explicit copy, prefer move instead. This also prevents
240         /// move()s from being converted into copy silently.
241         explicit TexString(TexString const &) = default;
242         TexString(TexString && other) = default;
243         TexString & operator=(TexString const & other) = default;
244         TexString & operator=(TexString && other) = default;
245         /// Empty TexString
246         TexString() = default;
247         /// Texstring containing str and TexRow with enough lines which are empty
248         explicit TexString(docstring str);
249         /// Texstring containing str and texrow. Must be valid.
250         TexString(docstring str, TexRow texrow);
251         /// Ensure that the string and the TexRow have as many newlines.
252         void validate();
253 };
254
255
256 // Standard container needs a complete type
257 class TexRow::RowEntryList {
258         // For each row we store a list of one special TextEntry and several
259         // RowEntries. (The order is important.)  We only want one text entry
260         // because we do not want to store every position in the lyx file. On the
261         // other hand we want to record all math and table cells positions for
262         // enough precision. Usually the count of cells is easier to handle.
263         // The RowEntries are used for forward-search and the code preview pane.
264         std::vector<RowEntry> v_;
265         // The TextEntry is currently used for reverse-search and the error
266         // reporting dialog. Once the latter are adapted to rely on the more precise
267         // RowEntries above, it can be removed.
268         TextEntry text_entry_;
269
270 public:
271         typedef std::vector<RowEntry>::iterator iterator;
272         iterator begin() { return v_.begin(); }
273         iterator end() { return v_.end(); }
274         ///
275         typedef std::vector<RowEntry>::const_iterator const_iterator;
276         const_iterator begin() const { return v_.cbegin(); }
277         const_iterator end() const { return v_.cend(); }
278         ///
279         RowEntryList() : text_entry_(TexRow::text_none) {}
280
281         // returns true if the row entry will appear in the row entry list
282         bool addEntry(RowEntry entry);
283
284         // the row entry will appear in the row entry list, but it never counts
285         // as a proper text entry.
286         void forceAddEntry(RowEntry entry);
287
288         // returns the TextEntry or TexRow::text_none if none
289         TextEntry getTextEntry() const;
290
291         // appends a row
292         void append(RowEntryList row);
293 };
294
295
296 bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2);
297
298
299 } // namespace lyx
300
301 #endif // TEXROW_H