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 /// an individual par id/pos <=> row mapping
50 struct TextEntry { int id; int 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
70 // For each row we store a list of one special TextEntry and several
71 // RowEntries. (The order is important.) We only want one text entry
72 // because we do not want to store every position in the lyx file. On the
73 // other hand we want to record all math and table cells positions for
74 // enough precision. Usually the count of cells is easier to handle.
75 class RowEntryList : public std::vector<RowEntry> {
77 RowEntryList() : std::vector<RowEntry>(), text_entry_(-1) {}
79 // returns true if the row entry will appear in the row entry list
80 bool addEntry(RowEntry const &);
82 // the row entry will appear in the row entry list, but it never counts
83 // as a proper text entry.
84 void forceAddEntry(RowEntry const &);
86 // returns the TextEntry or TexRow::text_none if none
87 TextEntry getTextEntry() const;
89 // returns the first entry, or TexRow::row_none if none
90 RowEntry entry() const;
93 void append(RowEntryList const &);
99 /// Returns true if RowEntry is devoid of information
100 static bool isNone(RowEntry const &);
101 static const TextEntry text_none;
102 static const RowEntry row_none;
104 /// Returns true if TextEntry is devoid of information
105 static bool isNone(TextEntry const &);
107 /// Converts a CursorSlice into a RowEntry
108 static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
110 /// Encapsulates the paragraph and position for later use
111 static RowEntry textEntry(int id, int pos);
113 /// Encapsulates a cell and position for later use
114 static RowEntry mathEntry(uid_type id, idx_type cell);
116 /// true iff same paragraph or math inset
117 static bool sameParOrInsetMath(RowEntry const &, RowEntry const &);
119 /// computes the distance in pos or cell index
120 /// assumes it is the sameParOrInsetMath
121 static int comparePos(RowEntry const & entry1, RowEntry const & entry2);
123 /// for debugging purposes
124 static docstring asString(RowEntry const &);
127 TexRow(bool enable = true)
128 : current_row_(RowEntryList()), enabled_(enable) {}
130 /// Clears structure. Set enable to false if texrow is not needed, to avoid
131 /// computing TexRow when it is going to be immediately discarded.
132 void reset(bool enable = true);
134 /// Defines the row information for the current line
135 /// returns true if this entry will appear on the current row
136 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, int pos);
142 /// Defines a cell and position for the current line. Always appear in the
144 void startMath(uid_type id, idx_type cell);
146 /// Defines the paragraph for the current cell-like inset. Always appears
147 /// in the current row like a math cell, but is detached from the normal
148 /// text flow. Note: since the cell idx is not recorded it does not work as
149 /// well as for math grids; if we were to do that properly we would need to
150 /// access the id of the parent Tabular inset from the CursorSlice.
151 void forceStart(int id, int pos);
153 /// Insert node when line is completed
156 /// Insert multiple nodes when zero or more lines are completed
157 void newlines(int num_lines);
159 /// Call when code generation is complete
163 * getIdFromRow - find pid and position for a given row
164 * @param row row number to find
165 * @param id set to id if found
166 * @param pos set to paragraph position if found
167 * @return true if found, false otherwise
169 * If the row could not be found, pos is set to zero and
172 bool getIdFromRow(int row, int & id, int & pos) const;
174 /// Finds the best pair of rows for dit
175 /// returns (-1,-1) if not found.
176 std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
178 /// Finds the best pair of rows for cursor, taking the selection into
180 /// returns (-1,-1) if not found.
181 std::pair<int,int> rowFromCursor(Cursor const & dit) const;
183 /// Returns the number of rows contained
184 int rows() const { return rowlist_.size(); }
186 /// appends texrow. the final line of this is merged with the first line of
188 void append(TexRow const & texrow);
190 /// for debugging purpose
191 void prepend(docstring_list &) const;
194 typedef std::vector<RowEntryList> RowList;
196 class RowListIterator;
198 RowListIterator begin() const;
200 RowListIterator end() const;
201 /// container of id/pos <=> row mapping
203 /// Entry of current line
204 RowEntryList current_row_;
209 bool operator==(RowEntry const &, RowEntry const &);
211 LyXErr & operator<<(LyXErr &, TexRow &);