]> git.lyx.org Git - features.git/blob - src/TexRow.h
Remove option to disable texrow
[features.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 LyXErr;
39 class Cursor;
40 class CursorSlice;
41 class DocIterator;
42 class docstring_list;
43
44 /// types for cells and math insets
45 typedef void const * uid_type;
46 typedef size_t idx_type;
47
48
49 /// an individual par id/pos <=> row mapping
50 struct TextEntry { int id; pos_type pos; };
51
52 /// an individual math id/cell <=> row mapping
53 struct MathEntry { uid_type id; idx_type cell; };
54
55 /// a container for passing entries around
56 struct RowEntry {
57         bool is_math;// true iff the union is a math
58         union {
59                 struct TextEntry text;
60                 struct MathEntry math;
61         };
62 };
63
64
65 /// Represents the correspondence between paragraphs and the generated
66 /// LaTeX file
67
68 class TexRow {
69         /// id/pos correspondence for a single row
70         class RowEntryList;
71
72         /// container of id/pos <=> row mapping
73         /// invariant: in any enabled_ TexRow, rowlist_ will contain at least one
74         /// Row (the current row)
75         typedef std::vector<RowEntryList> RowList;
76         ///
77         RowList rowlist_;
78         ///
79         RowEntryList & currentRow();
80
81         ///
82         class RowListIterator;
83         ///
84         RowListIterator begin() const;
85         ///
86         RowListIterator end() const;
87 public:
88         ///
89         TexRow();
90
91         /// Clears structure.
92         void reset();
93
94         static const TextEntry text_none;
95         static const RowEntry row_none;
96         /// Returns true if RowEntry is devoid of information
97         static bool isNone(RowEntry entry);
98         /// Returns true if TextEntry is devoid of information
99         static bool isNone(TextEntry entry);
100
101         /// Converts a CursorSlice into a RowEntry
102         static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
103         /// Encapsulates the paragraph and position for later use
104         static RowEntry textEntry(int id, pos_type pos);
105         /// Encapsulates a cell and position for later use
106         static RowEntry mathEntry(uid_type id, idx_type cell);
107
108         /// for debugging purposes
109         static docstring asString(RowEntry entry);
110
111         /// Defines the row information for the current line
112         /// returns true if this entry will appear on the current row
113         bool start(RowEntry entry);
114         /// Defines the paragraph and position for the current line
115         /// returns true if this entry will appear on the current row
116         bool start(int id, pos_type pos);
117         /// Defines a cell and position for the current line.  Always appear in the
118         /// current row.
119         void startMath(uid_type id, idx_type cell);
120         /// Defines the paragraph for the current cell-like inset.  Always appears
121         /// in the current row like a math cell, but is detached from the normal
122         /// text flow. Note: since the cell idx is not recorded it does not work as
123         /// well as for math grids; if we were to do that properly we would need to
124         /// access the id of the parent Tabular inset from the CursorSlice.
125         void forceStart(int id, pos_type pos);
126
127         /// Insert node when line is completed
128         void newline();
129         /// Insert multiple nodes when zero or more lines are completed
130         void newlines(size_t num_lines);
131
132         /**
133          * getIdFromRow - find pid and position for a given row
134          * @param row row number to find
135          * @param id set to id if found
136          * @param pos set to paragraph position if found
137          * @return true if found, false otherwise
138          *
139          * If the row could not be found, pos is set to zero and
140          * id is set to -1
141          */
142         bool getIdFromRow(int row, int & id, int & pos) const;
143
144         /// Finds the best pair of rows for dit
145         /// returns (-1,-1) if not found.
146         std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
147
148         /// Finds the best pair of rows for cursor, taking the selection into
149         /// account
150         /// returns (-1,-1) if not found.
151         std::pair<int,int> rowFromCursor(Cursor const & dit) const;
152
153         /// Returns the number of rows contained
154         int rows() const;
155
156         /// appends texrow. the final line of this is merged with the first line of
157         /// texrow.
158         void append(TexRow texrow);
159
160         /// for debugging purpose
161         void prepend(docstring_list &) const;
162
163 private:
164         /// true iff same paragraph or math inset
165         static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
166         /// computes the distance in pos or cell index
167         /// assumes it is the sameParOrInsetMath
168         static int comparePos(RowEntry entry1, RowEntry entry2);
169
170 };
171
172
173 // Standard container needs a complete type
174 class TexRow::RowEntryList {
175         // For each row we store a list of one special TextEntry and several
176         // RowEntries. (The order is important.)  We only want one text entry
177         // because we do not want to store every position in the lyx file. On the
178         // other hand we want to record all math and table cells positions for
179         // enough precision. Usually the count of cells is easier to handle.
180         // The RowEntries are used for forward-search and the code preview pane.
181         std::vector<RowEntry> v_;
182         // The TextEntry is currently used for reverse-search and the error
183         // reporting dialog. Once the latter are adapted to rely on the more precise
184         // RowEntries above, it can be removed.
185         TextEntry text_entry_;
186
187 public:
188         typedef std::vector<RowEntry>::iterator iterator;
189         iterator begin() { return v_.begin(); }
190         iterator end() { return v_.end(); }
191         ///
192         typedef std::vector<RowEntry>::const_iterator const_iterator;
193         const_iterator begin() const { return v_.cbegin(); }
194         const_iterator end() const { return v_.cend(); }
195         ///
196         RowEntryList() : text_entry_(TexRow::text_none) {}
197
198         // returns true if the row entry will appear in the row entry list
199         bool addEntry(RowEntry entry);
200
201         // the row entry will appear in the row entry list, but it never counts
202         // as a proper text entry.
203         void forceAddEntry(RowEntry entry);
204
205         // returns the TextEntry or TexRow::text_none if none
206         TextEntry getTextEntry() const;
207
208         // appends a row
209         void append(RowEntryList row);
210 };
211
212
213 bool operator==(RowEntry entry1, RowEntry entry2);
214
215
216 LyXErr & operator<<(LyXErr &, TexRow const &);
217
218
219 } // namespace lyx
220
221 #endif // TEXROW_H