4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Matthias Ettrich
8 * \author Lars Gullik Bjønnes
10 * \author Guillaume Munch
12 * Full author contact details are available in file CREDITS.
15 /* Note about debugging options:
17 * When compiled in devel mode and run with the option -dbg latex, two ways
18 * of debugging TexRow are available:
20 * 1. The source view panel prepends the full TexRow information to the LaTeX
23 * 2. Clicking on any line in the source view moves the buffer to the location
24 * recognised by TexRow.
31 #include "support/debug.h"
32 #include "support/docstring.h"
33 #include "support/types.h"
46 /// types for cells and math insets
47 typedef void const * uid_type;
48 typedef size_t idx_type;
51 /// Represents the correspondence between paragraphs and the generated
56 /// We begin with defining the types of row information we are tracking
59 /// type of row entries
66 /// an individual par id/pos <=> row mapping
67 struct TextEntry { int id; pos_type pos; };
69 /// an individual math id/cell <=> row mapping
70 struct MathEntry { uid_type id; idx_type cell; };
72 /// a container for passing entries around
76 struct TextEntry text;// iff the type is text_entry
77 struct MathEntry math;// iff the type is row_entry
78 struct {} begindocument;// iff the type is begin_document
82 /// Encapsulates the paragraph and position for later use
83 static RowEntry textEntry(int id, pos_type pos);
84 /// Encapsulates a cell and position for later use
85 static RowEntry mathEntry(uid_type id, idx_type cell);
86 /// Denotes the beginning of the document
87 static RowEntry beginDocument();
89 /// Converts a CursorSlice into a RowEntry
90 static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
92 static const TextEntry text_none;
93 static const RowEntry row_none;
94 /// Returns true if RowEntry is devoid of information
95 static bool isNone(RowEntry entry);
96 /// Returns true if TextEntry is devoid of information
97 static bool isNone(TextEntry entry);
100 /// id/pos correspondence for a single row
103 /// container of id/pos <=> row mapping
104 /// invariant: in any enabled_ TexRow, rowlist_ will contain at least one
105 /// Row (the current row)
106 typedef std::vector<RowEntryList> RowList;
110 RowEntryList & currentRow();
113 class RowListIterator;
115 RowListIterator begin() const;
117 RowListIterator end() const;
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;
130 /// Clears structure.
133 /// for debugging purposes
134 static docstring asString(RowEntry entry);
136 /// Defines the row information for the current line
137 /// returns true if this entry will appear on the current row
138 bool start(RowEntry entry);
139 /// Defines the paragraph and position for the current line
140 /// returns true if this entry will appear on the current row
141 bool start(int id, pos_type pos);
142 /// Defines a cell and position for the current line. Always appear in the
144 void startMath(uid_type id, idx_type cell);
145 /// Defines the paragraph for the current cell-like inset. Always appears
146 /// in the current row like a math cell, but is detached from the normal
147 /// text flow. Note: since the cell idx is not recorded it does not work as
148 /// well as for math grids; if we were to do that properly we would need to
149 /// access the id of the parent Tabular inset from the CursorSlice.
150 void forceStart(int id, pos_type pos);
152 /// Insert node when line is completed
154 /// Insert multiple nodes when zero or more lines are completed
155 void newlines(size_t num_lines);
158 * getEntriesFromRow - find pids and position for a given row
159 * This is the main algorithm behind reverse-search.
160 * @param row number to find
161 * @return a pair of TextEntry denoting the start and end of the position.
162 * The TextEntry values can be isNone(). If no row is found then the first
165 std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
168 * getDocIteratorFromEntries - find pids and positions for a given row
169 * @param buffer where to look
170 * @return a pair of DocIterators denoting the start and end of the
171 * position. The DocIterators can be invalid. The starting DocIterator
172 * being invalid means that no location was found. Note: there is no
173 * guarantee that the DocIterators are in the same inset or even at the
176 static std::pair<DocIterator, DocIterator> getDocIteratorsFromEntries(
181 // A FuncRequest to select from start to end
182 static FuncRequest goToFunc(TextEntry start, TextEntry end);
183 // A FuncRequest to select a row
184 FuncRequest goToFuncFromRow(int const row) const;
187 * getDocIteratorFromRow - find pids and positions for a given row
188 * @param row number to find
189 * @param buffer where to look
190 * @return a pair of DocIterators as above.
192 std::pair<DocIterator, DocIterator> getDocIteratorsFromRow(
194 Buffer const & buf) const;
196 /// Finds the best pair of rows for dit
197 /// returns (-1,-1) if not found.
198 /// This is the main algorithm behind forward-search.
199 std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
201 /// Finds the best pair of rows for cursor, taking the selection into
203 /// returns (-1,-1) if not found.
204 std::pair<int,int> rowFromCursor(Cursor const & dit) const;
206 /// Returns the number of rows contained
208 /// Fill or trim to reach the row count \param r
209 void setRows(size_t r);
211 /// appends texrow. the final line of this is merged with the first line of
213 void append(TexRow texrow);
215 /// for debugging purpose
216 void prepend(docstring_list &) const;
219 /// true iff same paragraph or math inset or begin_document
220 static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
221 /// computes the distance in pos or cell index
222 /// assumes it is the sameParOrInsetMath
223 static int comparePos(RowEntry entry1, RowEntry entry2);
228 /// TexString : dumb struct to pass around docstrings with TexRow information.
229 /// They are best created using otexstringstream.
230 /// They can be output to otexrowstreams and otexstreams.
231 /// A valid TexString has as many newlines in str as in texrow. Be careful not
232 /// to introduce a mismatch between the line and the row counts, as this will
233 /// assert in devel mode when outputting to a otexstream.
239 /// Copy can be expensive and is not usually useful for TexString.
240 /// Force explicit copy, prefer move instead. This also prevents
241 /// move()s from being converted into copy silently.
242 explicit TexString(TexString const &) = default;
243 TexString(TexString && other) = default;
244 TexString & operator=(TexString const & other) = default;
245 TexString & operator=(TexString && other) = default;
247 TexString() = default;
248 /// Texstring containing str and TexRow with enough lines which are empty
249 explicit TexString(docstring str);
250 /// Texstring containing str and texrow. Must be valid.
251 TexString(docstring str, TexRow texrow);
252 /// Ensure that the string and the TexRow have as many newlines.
257 // Standard container needs a complete type
258 class TexRow::RowEntryList {
259 // For each row we store a list of one special TextEntry and several
260 // RowEntries. (The order is important.) We only want one text entry
261 // because we do not want to store every position in the lyx file. On the
262 // other hand we want to record all math and table cells positions for
263 // enough precision. Usually the count of cells is easier to handle.
264 // The RowEntries are used for forward-search and the code preview pane.
265 std::vector<RowEntry> v_;
266 // The TextEntry is currently used for reverse-search and the error
267 // reporting dialog. Once the latter are adapted to rely on the more precise
268 // RowEntries above, it can be removed.
269 TextEntry text_entry_;
272 typedef std::vector<RowEntry>::iterator iterator;
273 iterator begin() { return v_.begin(); }
274 iterator end() { return v_.end(); }
276 typedef std::vector<RowEntry>::const_iterator const_iterator;
277 const_iterator begin() const { return v_.cbegin(); }
278 const_iterator end() const { return v_.cend(); }
280 RowEntryList() : text_entry_(TexRow::text_none) {}
282 // returns true if the row entry will appear in the row entry list
283 bool addEntry(RowEntry entry);
285 // the row entry will appear in the row entry list, but it never counts
286 // as a proper text entry.
287 void forceAddEntry(RowEntry entry);
289 // returns the TextEntry or TexRow::text_none if none
290 TextEntry getTextEntry() const;
293 void append(RowEntryList row);
297 bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2);