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/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 #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;
130 //for gcc 4.6, nothing to do: it's enough to disable implicit copy during
131 // dev with more recent versions of gcc.
134 /// Clears structure.
137 /// for debugging purposes
138 static docstring asString(RowEntry entry);
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
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);
156 /// Insert node when line is completed
158 /// Insert multiple nodes when zero or more lines are completed
159 void newlines(size_t num_lines);
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
169 std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
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
180 static std::pair<DocIterator, DocIterator> getDocIteratorsFromEntries(
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;
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.
196 std::pair<DocIterator, DocIterator> getDocIteratorsFromRow(
198 Buffer const & buf) const;
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;
205 /// Finds the best pair of rows for cursor, taking the selection into
207 /// returns (-1,-1) if not found.
208 std::pair<int,int> rowFromCursor(Cursor const & dit) const;
210 /// Returns the number of rows contained
212 /// Fill or trim to reach the row count \param r
213 void setRows(size_t r);
215 /// appends texrow. the final line of this is merged with the first line of
217 void append(TexRow texrow);
219 /// for debugging purpose
220 void prepend(docstring_list &) const;
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);
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.
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;
252 //for gcc 4.6, nothing to do: it's enough to disable implicit copy during
253 // dev with more recent versions of gcc.
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.
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_;
281 typedef std::vector<RowEntry>::iterator iterator;
282 iterator begin() { return v_.begin(); }
283 iterator end() { return v_.end(); }
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(); }
289 RowEntryList() : text_entry_(TexRow::text_none) {}
291 // returns true if the row entry will appear in the row entry list
292 bool addEntry(RowEntry entry);
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);
298 // returns the TextEntry or TexRow::text_none if none
299 TextEntry getTextEntry() const;
302 void append(RowEntryList row);
306 bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2);