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