]> git.lyx.org Git - lyx.git/blob - src/TexRow.h
French UserGuide.lyx: back to format of 2.2
[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 LyXErr;
39 class Buffer;
40 class Cursor;
41 class CursorSlice;
42 class DocIterator;
43 class docstring_list;
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         /// Clears structure.
93         void reset();
94
95         static const TextEntry text_none;
96         static const RowEntry row_none;
97         /// Returns true if RowEntry is devoid of information
98         static bool isNone(RowEntry entry);
99         /// Returns true if TextEntry is devoid of information
100         static bool isNone(TextEntry entry);
101
102         /// Converts a CursorSlice into a RowEntry
103         static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
104         /// Encapsulates the paragraph and position for later use
105         static RowEntry textEntry(int id, pos_type pos);
106         /// Encapsulates a cell and position for later use
107         static RowEntry mathEntry(uid_type id, idx_type cell);
108
109         /// for debugging purposes
110         static docstring asString(RowEntry entry);
111
112         /// Defines the row information for the current line
113         /// returns true if this entry will appear on the current row
114         bool start(RowEntry entry);
115         /// Defines the paragraph and position for the current line
116         /// returns true if this entry will appear on the current row
117         bool start(int id, pos_type pos);
118         /// Defines a cell and position for the current line.  Always appear in the
119         /// current row.
120         void startMath(uid_type id, idx_type cell);
121         /// Defines the paragraph for the current cell-like inset.  Always appears
122         /// in the current row like a math cell, but is detached from the normal
123         /// text flow. Note: since the cell idx is not recorded it does not work as
124         /// well as for math grids; if we were to do that properly we would need to
125         /// access the id of the parent Tabular inset from the CursorSlice.
126         void forceStart(int id, pos_type pos);
127
128         /// Insert node when line is completed
129         void newline();
130         /// Insert multiple nodes when zero or more lines are completed
131         void newlines(size_t num_lines);
132
133         /**
134          * getEntriesFromRow - find pids and position for a given row
135          * @param row row number to find
136          * @return a pair of TextEntry denoting the start and end of the position.
137          * The TextEntry values can be isNone(). If no row is found then the first
138          * value isNone().
139          */
140         std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
141
142         /**
143          * getDocIteratorFromRow - find pids and positions for a given row
144          * @param row number to find
145          * @param buffer here to look
146          * @return a pair of DocIterators the start and end of the position.
147          * The DocIterators can be invalid. The starting DocIterator being invalid
148          * means that no row was found. Note: there is no guarantee that the
149          * DocIterators are in the same inset or even at the same depth.
150          */
151         std::pair<DocIterator, DocIterator> getDocIteratorFromRow(
152             int row,
153             Buffer const & buf) const;
154         //TODO: remove the following by replacing it with the above
155         bool getIdFromRow(int row, int & id, int & pos) const;
156
157         /// Finds the best pair of rows for dit
158         /// returns (-1,-1) if not found.
159         std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
160
161         /// Finds the best pair of rows for cursor, taking the selection into
162         /// account
163         /// returns (-1,-1) if not found.
164         std::pair<int,int> rowFromCursor(Cursor const & dit) const;
165
166         /// Returns the number of rows contained
167         int rows() const;
168
169         /// appends texrow. the final line of this is merged with the first line of
170         /// texrow.
171         void append(TexRow texrow);
172
173         /// for debugging purpose
174         void prepend(docstring_list &) const;
175
176 private:
177         /// true iff same paragraph or math inset
178         static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
179         /// computes the distance in pos or cell index
180         /// assumes it is the sameParOrInsetMath
181         static int comparePos(RowEntry entry1, RowEntry entry2);
182
183 };
184
185
186 // Standard container needs a complete type
187 class TexRow::RowEntryList {
188         // For each row we store a list of one special TextEntry and several
189         // RowEntries. (The order is important.)  We only want one text entry
190         // because we do not want to store every position in the lyx file. On the
191         // other hand we want to record all math and table cells positions for
192         // enough precision. Usually the count of cells is easier to handle.
193         // The RowEntries are used for forward-search and the code preview pane.
194         std::vector<RowEntry> v_;
195         // The TextEntry is currently used for reverse-search and the error
196         // reporting dialog. Once the latter are adapted to rely on the more precise
197         // RowEntries above, it can be removed.
198         TextEntry text_entry_;
199
200 public:
201         typedef std::vector<RowEntry>::iterator iterator;
202         iterator begin() { return v_.begin(); }
203         iterator end() { return v_.end(); }
204         ///
205         typedef std::vector<RowEntry>::const_iterator const_iterator;
206         const_iterator begin() const { return v_.cbegin(); }
207         const_iterator end() const { return v_.cend(); }
208         ///
209         RowEntryList() : text_entry_(TexRow::text_none) {}
210
211         // returns true if the row entry will appear in the row entry list
212         bool addEntry(RowEntry entry);
213
214         // the row entry will appear in the row entry list, but it never counts
215         // as a proper text entry.
216         void forceAddEntry(RowEntry entry);
217
218         // returns the TextEntry or TexRow::text_none if none
219         TextEntry getTextEntry() const;
220
221         // appends a row
222         void append(RowEntryList row);
223 };
224
225
226 bool operator==(RowEntry entry1, RowEntry entry2);
227
228
229 LyXErr & operator<<(LyXErr &, TexRow const &);
230
231
232 } // namespace lyx
233
234 #endif // TEXROW_H