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/types.h"
32 #include "support/debug.h"
44 /// types for cells and math insets
45 typedef void const * uid_type;
46 typedef size_t idx_type;
49 /// Represents the correspondence between paragraphs and the generated
54 /// an individual par id/pos <=> row mapping
55 struct TextEntry { int id; int pos; };
57 /// an individual math id/cell <=> row mapping
58 struct MathEntry { uid_type id; idx_type cell; };
60 /// a container for passing entries around
62 bool is_math;// true iff the union is a math
64 struct TextEntry text;
65 struct MathEntry math;
69 // For each row we store a list of one special TextEntry and several
70 // RowEntries. (The order is important.) We only want one text entry
71 // because we do not want to store every position in the lyx file. On the
72 // other hand we want to record all math and table cells positions for
73 // enough precision. Usually the count of cells is easier to handle.
74 class RowEntryList : public std::vector<RowEntry> {
76 RowEntryList() : std::vector<RowEntry>(), text_entry_(-1) {}
78 // returns true if the row entry will appear in the row entry list
79 bool addEntry(RowEntry const &);
81 // the row entry will appear in the row entry list, but it never counts
82 // as a proper text entry.
83 void forceAddEntry(RowEntry const &);
85 // returns the TextEntry or TexRow::text_none if none
86 TextEntry getTextEntry() const;
88 // returns the first entry, or TexRow::row_none if none
89 RowEntry entry() const;
92 void append(RowEntryList const &);
98 /// Returns true if RowEntry is devoid of information
99 static bool isNone(RowEntry const &);
100 static const TextEntry text_none;
101 static const RowEntry row_none;
103 /// Returns true if TextEntry is devoid of information
104 static bool isNone(TextEntry const &);
106 /// Converts a CursorSlice into a RowEntry
107 static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
109 /// Encapsulates the paragraph and position for later use
110 static RowEntry textEntry(int id, int pos);
112 /// Encapsulates a cell and position for later use
113 static RowEntry mathEntry(uid_type id, idx_type cell);
115 /// true iff same paragraph or math inset
116 static bool sameParOrInsetMath(RowEntry const &, RowEntry const &);
118 /// computes the distance in pos or cell index
119 /// assumes it is the sameParOrInsetMath
120 static int comparePos(RowEntry const & entry1, RowEntry const & entry2);
122 /// for debugging purposes
123 static docstring asString(RowEntry const &);
126 TexRow(bool enable = true)
127 : current_row_(RowEntryList()), enabled_(enable) {}
129 /// Clears structure. Set enable to false if texrow is not needed, to avoid
130 /// computing TexRow when it is going to be immediately discarded.
131 void reset(bool enable = true);
133 /// Defines the row information for the current line
134 /// returns true if this entry will appear on the current row
135 bool start(RowEntry entry);
137 /// Defines the paragraph and position for the current line
138 /// returns true if this entry will appear on the current row
139 bool start(int id, int pos);
141 /// Defines a cell and position for the current line. Always appear in the
143 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, int pos);
152 /// Insert node when line is completed
155 /// Insert multiple nodes when zero or more lines are completed
156 void newlines(int num_lines);
158 /// Call when code generation is complete
162 * getIdFromRow - find pid and position for a given row
163 * @param row row number to find
164 * @param id set to id if found
165 * @param pos set to paragraph position if found
166 * @return true if found, false otherwise
168 * If the row could not be found, pos is set to zero and
171 bool getIdFromRow(int row, int & id, int & pos) const;
173 /// Finds the best pair of rows for dit
174 /// returns (-1,-1) if not found.
175 std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
177 /// Finds the best pair of rows for cursor, taking the selection into
179 /// returns (-1,-1) if not found.
180 std::pair<int,int> rowFromCursor(Cursor const & dit) const;
182 /// Returns the number of rows contained
183 int rows() const { return rowlist_.size(); }
185 /// appends texrow. the final line of this is merged with the first line of
187 void append(TexRow const & texrow);
189 /// for debugging purpose
190 void prepend(docstring_list &) const;
193 typedef std::vector<RowEntryList> RowList;
195 class RowListIterator;
197 RowListIterator begin() const;
199 RowListIterator end() const;
200 /// container of id/pos <=> row mapping
202 /// Entry of current line
203 RowEntryList current_row_;
208 bool operator==(TexRow::RowEntry const &, TexRow::RowEntry const &);
210 LyXErr & operator<<(LyXErr &, TexRow &);