]> git.lyx.org Git - lyx.git/blob - src/TexRow.h
a95b205d5c53a8956ade190fd7edd8e97376ec09
[lyx.git] / src / TexRow.h
1 // -*- C++ -*-
2 /**
3  * \file TexRow.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Matthias Ettrich
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author Guillaume Munch
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 /* Note about debugging options:
16  *
17  * When compiled in devel mode and run with the option -dbg latex, two ways
18  * of debugging TexRow are available:
19  *
20  * 1. The source view panel prepends the full TexRow information to the LaTeX
21  *    output.
22  *
23  * 2. Clicking on any line in the source view moves the buffer to the location
24  *    recognised by TexRow.
25  *
26  */
27
28 #ifndef TEXROW_H
29 #define TEXROW_H
30
31 #include "support/debug.h"
32 #include "support/types.h"
33
34 #include <vector>
35
36 namespace lyx {
37
38 class Buffer;
39 class Cursor;
40 class CursorSlice;
41 class DocIterator;
42 class docstring_list;
43 class FuncRequest;
44
45 /// types for cells and math insets
46 typedef void const * uid_type;
47 typedef size_t idx_type;
48
49
50 /// an individual par id/pos <=> row mapping
51 struct TextEntry { int id; pos_type pos; };
52
53 /// an individual math id/cell <=> row mapping
54 struct MathEntry { uid_type id; idx_type cell; };
55
56 /// a container for passing entries around
57 struct RowEntry {
58         bool is_math;// true iff the union is a math
59         union {
60                 struct TextEntry text;
61                 struct MathEntry math;
62         };
63 };
64
65
66 /// Represents the correspondence between paragraphs and the generated
67 /// LaTeX file
68
69 class TexRow {
70         /// id/pos correspondence for a single row
71         class RowEntryList;
72
73         /// container of id/pos <=> row mapping
74         /// invariant: in any enabled_ TexRow, rowlist_ will contain at least one
75         /// Row (the current row)
76         typedef std::vector<RowEntryList> RowList;
77         ///
78         RowList rowlist_;
79         ///
80         RowEntryList & currentRow();
81
82         ///
83         class RowListIterator;
84         ///
85         RowListIterator begin() const;
86         ///
87         RowListIterator end() const;
88 public:
89         ///
90         TexRow();
91
92 #if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6))
93         /// Copy can be expensive and is not usually useful for TexRow.
94         /// Force explicit copy, prefer move instead. This also prevents
95         /// move()s from being converted into copy silently.
96         explicit TexRow(TexRow const & other) = default;
97         TexRow(TexRow && other) = default;
98         TexRow & operator=(TexRow const & other) = default;
99         TexRow & operator=(TexRow && other) = default;
100 # else
101         //for gcc 4.6, nothing to do: it's enough to disable implicit copy during
102         // dev with more recent versions of gcc.
103 #endif
104
105         /// Clears structure.
106         void reset();
107
108         static const TextEntry text_none;
109         static const RowEntry row_none;
110         /// Returns true if RowEntry is devoid of information
111         static bool isNone(RowEntry entry);
112         /// Returns true if TextEntry is devoid of information
113         static bool isNone(TextEntry entry);
114
115         /// Converts a CursorSlice into a RowEntry
116         static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
117         /// Encapsulates the paragraph and position for later use
118         static RowEntry textEntry(int id, pos_type pos);
119         /// Encapsulates a cell and position for later use
120         static RowEntry mathEntry(uid_type id, idx_type cell);
121
122         /// for debugging purposes
123         static docstring asString(RowEntry entry);
124
125         /// Defines the row information for the current line
126         /// returns true if this entry will appear on the current row
127         bool start(RowEntry entry);
128         /// Defines the paragraph and position for the current line
129         /// returns true if this entry will appear on the current row
130         bool start(int id, pos_type pos);
131         /// Defines a cell and position for the current line.  Always appear in the
132         /// current row.
133         void startMath(uid_type id, idx_type cell);
134         /// Defines the paragraph for the current cell-like inset.  Always appears
135         /// in the current row like a math cell, but is detached from the normal
136         /// text flow. Note: since the cell idx is not recorded it does not work as
137         /// well as for math grids; if we were to do that properly we would need to
138         /// access the id of the parent Tabular inset from the CursorSlice.
139         void forceStart(int id, pos_type pos);
140
141         /// Insert node when line is completed
142         void newline();
143         /// Insert multiple nodes when zero or more lines are completed
144         void newlines(size_t num_lines);
145
146         /**
147          * getEntriesFromRow - find pids and position for a given row
148          * @param row number to find
149          * @return a pair of TextEntry denoting the start and end of the position.
150          * The TextEntry values can be isNone(). If no row is found then the first
151          * value isNone().
152          */
153         std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
154
155         /**
156          * getDocIteratorFromEntries - find pids and positions for a given row
157          * @param buffer where to look
158          * @return a pair of DocIterators denoting the start and end of the
159          * position.  The DocIterators can be invalid.  The starting DocIterator
160          * being invalid means that no location was found.  Note: there is no
161          * guarantee that the DocIterators are in the same inset or even at the
162          * same depth.
163          */
164         static std::pair<DocIterator, DocIterator> getDocIteratorsFromEntries(
165             TextEntry start,
166             TextEntry end,
167             Buffer const & buf);
168
169         // A FuncRequest to select from start to end
170         static FuncRequest goToFunc(TextEntry start, TextEntry end);
171
172         /**
173          * getDocIteratorFromRow - find pids and positions for a given row
174          * @param row number to find
175          * @param buffer where to look
176          * @return a pair of DocIterators as above.
177          */
178         std::pair<DocIterator, DocIterator> getDocIteratorsFromRow(
179             int row,
180             Buffer const & buf) const;
181         //TODO: remove the following by replacing it with the above
182         bool getIdFromRow(int row, int & id, int & pos) const;
183
184         /// Finds the best pair of rows for dit
185         /// returns (-1,-1) if not found.
186         std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
187
188         /// Finds the best pair of rows for cursor, taking the selection into
189         /// account
190         /// returns (-1,-1) if not found.
191         std::pair<int,int> rowFromCursor(Cursor const & dit) const;
192
193         /// Returns the number of rows contained
194         size_t rows() const;
195         /// Fill or trim to reach the row count \param r
196         void setRows(size_t r);
197
198         /// appends texrow. the final line of this is merged with the first line of
199         /// texrow.
200         void append(TexRow texrow);
201
202         /// for debugging purpose
203         void prepend(docstring_list &) const;
204
205 private:
206         /// true iff same paragraph or math inset
207         static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
208         /// computes the distance in pos or cell index
209         /// assumes it is the sameParOrInsetMath
210         static int comparePos(RowEntry entry1, RowEntry entry2);
211
212 };
213
214
215 /// TexString : dumb struct to pass around docstrings with TexRow information.
216 /// They are best created using otexstringstream.
217 /// They can be output to otexrowstreams and otexstreams.
218 /// A valid TexString has as many newlines in str as in texrow. Be careful not
219 /// to introduce a mismatch between the line and the row counts, as this will
220 /// assert in devel mode when outputting to a otexstream.
221 struct TexString {
222         ///
223         docstring str;
224         ///
225         TexRow texrow;
226 #if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6))
227         /// Copy can be expensive and is not usually useful for TexString.
228         /// Force explicit copy, prefer move instead. This also prevents
229         /// move()s from being converted into copy silently.
230         explicit TexString(TexString const &) = default;
231         TexString(TexString && other) = default;
232         TexString & operator=(TexString const & other) = default;
233         TexString & operator=(TexString && other) = default;
234 # else
235         //for gcc 4.6, nothing to do: it's enough to disable implicit copy during
236         // dev with more recent versions of gcc.
237 #endif
238         /// Empty TexString
239         TexString() = default;
240         /// Texstring containing str and TexRow with enough lines which are empty
241         explicit TexString(docstring str);
242         /// Texstring containing str and texrow. Must be valid.
243         TexString(docstring str, TexRow texrow);
244         /// Ensure that the string and the TexRow have as many newlines.
245         void validate();
246 };
247
248
249 // Standard container needs a complete type
250 class TexRow::RowEntryList {
251         // For each row we store a list of one special TextEntry and several
252         // RowEntries. (The order is important.)  We only want one text entry
253         // because we do not want to store every position in the lyx file. On the
254         // other hand we want to record all math and table cells positions for
255         // enough precision. Usually the count of cells is easier to handle.
256         // The RowEntries are used for forward-search and the code preview pane.
257         std::vector<RowEntry> v_;
258         // The TextEntry is currently used for reverse-search and the error
259         // reporting dialog. Once the latter are adapted to rely on the more precise
260         // RowEntries above, it can be removed.
261         TextEntry text_entry_;
262
263 public:
264         typedef std::vector<RowEntry>::iterator iterator;
265         iterator begin() { return v_.begin(); }
266         iterator end() { return v_.end(); }
267         ///
268         typedef std::vector<RowEntry>::const_iterator const_iterator;
269         const_iterator begin() const { return v_.cbegin(); }
270         const_iterator end() const { return v_.cend(); }
271         ///
272         RowEntryList() : text_entry_(TexRow::text_none) {}
273
274         // returns true if the row entry will appear in the row entry list
275         bool addEntry(RowEntry entry);
276
277         // the row entry will appear in the row entry list, but it never counts
278         // as a proper text entry.
279         void forceAddEntry(RowEntry entry);
280
281         // returns the TextEntry or TexRow::text_none if none
282         TextEntry getTextEntry() const;
283
284         // appends a row
285         void append(RowEntryList row);
286 };
287
288
289 bool operator==(RowEntry entry1, RowEntry entry2);
290
291
292 } // namespace lyx
293
294 #endif // TEXROW_H