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