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