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"
44 /// types for cells and math insets
45 typedef void const * uid_type;
46 typedef size_t idx_type;
49 /// an individual par id/pos <=> row mapping
50 struct TextEntry { int id; pos_type pos; };
52 /// an individual math id/cell <=> row mapping
53 struct MathEntry { uid_type id; idx_type cell; };
55 /// a container for passing entries around
57 bool is_math;// true iff the union is a math
59 struct TextEntry text;
60 struct MathEntry math;
65 /// Represents the correspondence between paragraphs and the generated
69 /// id/pos correspondence for a single row
72 /// container of id/pos <=> row mapping
73 /// invariant: in any enabled_ TexRow, rowlist_ will contain at least one
74 /// Row (the current row)
75 typedef std::vector<RowEntryList> RowList;
81 RowEntryList & currentRow();
84 class RowListIterator;
86 RowListIterator begin() const;
88 RowListIterator end() const;
91 TexRow(bool enable = true);
93 /// Clears structure. Set enable to false if texrow is not needed, to avoid
94 /// computing TexRow when it is going to be immediately discarded.
95 void reset(bool enable = true);
97 static const TextEntry text_none;
98 static const RowEntry row_none;
99 /// Returns true if RowEntry is devoid of information
100 static bool isNone(RowEntry entry);
101 /// Returns true if TextEntry is devoid of information
102 static bool isNone(TextEntry entry);
104 /// Converts a CursorSlice into a RowEntry
105 static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
106 /// Encapsulates the paragraph and position for later use
107 static RowEntry textEntry(int id, pos_type pos);
108 /// Encapsulates a cell and position for later use
109 static RowEntry mathEntry(uid_type id, idx_type cell);
111 /// for debugging purposes
112 static docstring asString(RowEntry entry);
114 /// Defines the row information for the current line
115 /// returns true if this entry will appear on the current row
116 bool start(RowEntry entry);
117 /// Defines the paragraph and position for the current line
118 /// returns true if this entry will appear on the current row
119 bool start(int id, pos_type pos);
120 /// Defines a cell and position for the current line. Always appear in the
122 void startMath(uid_type id, idx_type cell);
123 /// Defines the paragraph for the current cell-like inset. Always appears
124 /// in the current row like a math cell, but is detached from the normal
125 /// text flow. Note: since the cell idx is not recorded it does not work as
126 /// well as for math grids; if we were to do that properly we would need to
127 /// access the id of the parent Tabular inset from the CursorSlice.
128 void forceStart(int id, pos_type pos);
130 /// Insert node when line is completed
132 /// Insert multiple nodes when zero or more lines are completed
133 void newlines(size_t num_lines);
136 * getIdFromRow - find pid and position for a given row
137 * @param row row number to find
138 * @param id set to id if found
139 * @param pos set to paragraph position if found
140 * @return true if found, false otherwise
142 * If the row could not be found, pos is set to zero and
145 bool getIdFromRow(int row, int & id, int & pos) const;
147 /// Finds the best pair of rows for dit
148 /// returns (-1,-1) if not found.
149 std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
151 /// Finds the best pair of rows for cursor, taking the selection into
153 /// returns (-1,-1) if not found.
154 std::pair<int,int> rowFromCursor(Cursor const & dit) const;
156 /// Returns the number of rows contained
159 /// appends texrow. the final line of this is merged with the first line of
161 void append(TexRow texrow);
163 /// for debugging purpose
164 void prepend(docstring_list &) const;
167 /// true iff same paragraph or math inset
168 static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
169 /// computes the distance in pos or cell index
170 /// assumes it is the sameParOrInsetMath
171 static int comparePos(RowEntry entry1, RowEntry entry2);
176 // Standard container needs a complete type
177 class TexRow::RowEntryList {
178 // For each row we store a list of one special TextEntry and several
179 // RowEntries. (The order is important.) We only want one text entry
180 // because we do not want to store every position in the lyx file. On the
181 // other hand we want to record all math and table cells positions for
182 // enough precision. Usually the count of cells is easier to handle.
183 // The RowEntries are used for forward-search and the code preview pane.
184 std::vector<RowEntry> v_;
185 // The TextEntry is currently used for reverse-search and the error
186 // reporting dialog. Once the latter are adapted to rely on the more precise
187 // RowEntries above, it can be removed.
188 TextEntry text_entry_;
191 typedef std::vector<RowEntry>::iterator iterator;
192 iterator begin() { return v_.begin(); }
193 iterator end() { return v_.end(); }
195 typedef std::vector<RowEntry>::const_iterator const_iterator;
196 const_iterator begin() const { return v_.cbegin(); }
197 const_iterator end() const { return v_.cend(); }
199 RowEntryList() : text_entry_(TexRow::text_none) {}
201 // returns true if the row entry will appear in the row entry list
202 bool addEntry(RowEntry entry);
204 // the row entry will appear in the row entry list, but it never counts
205 // as a proper text entry.
206 void forceAddEntry(RowEntry entry);
208 // returns the TextEntry or TexRow::text_none if none
209 TextEntry getTextEntry() const;
212 void append(RowEntryList row);
216 bool operator==(RowEntry entry1, RowEntry entry2);
219 LyXErr & operator<<(LyXErr &, TexRow const &);