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/strfwd.h"
32 #include "support/types.h"
45 /// types for cells and math insets
46 typedef void const * uid_type;
47 typedef size_t idx_type;
50 /// Represents the correspondence between paragraphs and the generated
55 /// We begin with defining the types of row information we are tracking
58 /// type of row entries
65 /// an individual par id/pos <=> row mapping
66 struct TextEntry { int id; pos_type pos; };
68 /// an individual math id/cell <=> row mapping
69 struct MathEntry { uid_type id; idx_type cell; };
71 /// a container for passing entries around
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
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();
88 /// Converts a CursorSlice into a RowEntry
89 static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
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);
99 /// id/pos correspondence for a single row
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;
109 RowEntryList & currentRow();
112 class RowListIterator;
114 RowListIterator begin() const;
116 RowListIterator end() const;
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;
129 /// Clears structure.
132 /// for debugging purposes
133 static docstring asString(RowEntry entry);
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
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);
151 /// Insert node when line is completed
153 /// Insert multiple nodes when zero or more lines are completed
154 void newlines(size_t num_lines);
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
164 std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
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
175 static std::pair<DocIterator, DocIterator> getDocIteratorsFromEntries(
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;
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.
191 std::pair<DocIterator, DocIterator> getDocIteratorsFromRow(
193 Buffer const & buf) const;
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;
200 /// Finds the best pair of rows for cursor, taking the selection into
202 /// returns (-1,-1) if not found.
203 std::pair<int,int> rowFromCursor(Cursor const & dit) const;
205 /// Returns the number of rows contained
207 /// Fill or trim to reach the row count \param r
208 void setRows(size_t r);
210 /// appends texrow. the final line of this is merged with the first line of
212 void append(TexRow texrow);
214 /// for debugging purpose
215 void prepend(docstring_list &) const;
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);
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.
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;
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.
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_;
271 typedef std::vector<RowEntry>::iterator iterator;
272 iterator begin() { return v_.begin(); }
273 iterator end() { return v_.end(); }
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(); }
279 RowEntryList() : text_entry_(TexRow::text_none) {}
281 // returns true if the row entry will appear in the row entry list
282 bool addEntry(RowEntry entry);
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);
288 // returns the TextEntry or TexRow::text_none if none
289 TextEntry getTextEntry() const;
292 void append(RowEntryList row);
296 bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2);