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