]> git.lyx.org Git - lyx.git/blob - src/TexRow.cpp
0b3f7179f27bf4c0dfc03a8e9448a66953c04868
[lyx.git] / src / TexRow.cpp
1 /**
2  * \file TexRow.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Matthias Ettrich
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author Guillaume Munch
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "Cursor.h"
17 #include "Paragraph.h"
18 #include "TexRow.h"
19
20 #include "mathed/InsetMath.h"
21
22 #include "support/debug.h"
23 #include "support/docstring_list.h"
24 #include "support/lassert.h"
25
26 #include <algorithm>
27 #include <sstream>
28
29 using namespace std;
30
31
32 namespace lyx {
33
34
35 bool TexRow::RowEntryList::addEntry(RowEntry entry)
36 {
37         if (!entry.is_math) {
38                 if (!isNone(text_entry_))
39                         return false;
40                 else
41                         text_entry_ = entry.text;
42         }
43         forceAddEntry(entry);
44         return true;
45 }
46
47
48 void TexRow::RowEntryList::forceAddEntry(RowEntry entry)
49 {
50         if (v_.empty() || !(v_.back() == entry))
51                 v_.push_back(entry);
52 }
53
54
55 TextEntry TexRow::RowEntryList::getTextEntry() const
56 {
57         if (!isNone(text_entry_))
58                 return text_entry_;
59         return TexRow::text_none;
60 }
61
62
63 void TexRow::RowEntryList::append(RowEntryList row)
64 {
65         if (isNone(text_entry_))
66                 text_entry_ = row.text_entry_;
67         move(row.begin(), row.end(), back_inserter(v_));
68 }
69
70
71 TexRow::TexRow(bool enable)
72 {
73         reset(enable);
74 }
75
76
77 TextEntry const TexRow::text_none = { -1, 0 };
78 RowEntry const TexRow::row_none = { false, { TexRow::text_none } };
79
80
81 //static
82 bool TexRow::isNone(TextEntry t)
83 {
84         return t.id < 0;
85 }
86
87
88 //static
89 bool TexRow::isNone(RowEntry r)
90 {
91         return !r.is_math && isNone(r.text);
92 }
93
94
95 void TexRow::reset(bool enable)
96 {
97         rowlist_.clear();
98         enabled_ = enable;
99         newline();
100 }
101
102
103 TexRow::RowEntryList & TexRow::currentRow()
104 {
105         return rowlist_.back();
106 }
107
108
109 //static
110 RowEntry TexRow::textEntry(int id, pos_type pos)
111 {
112         RowEntry entry;
113         entry.is_math = false;
114         entry.text.pos = pos;
115         entry.text.id = id;
116         return entry;
117 }
118
119
120 //static
121 RowEntry TexRow::mathEntry(uid_type id, idx_type cell)
122 {
123         RowEntry entry;
124         entry.is_math = true;
125         entry.math.cell = cell;
126         entry.math.id = id;
127         return entry;
128 }
129
130
131 bool operator==(RowEntry entry1, RowEntry entry2)
132 {
133         return entry1.is_math == entry2.is_math
134                 && (entry1.is_math
135                     ? (entry1.math.id == entry2.math.id
136                        && entry1.math.cell == entry2.math.cell)
137                     : (entry1.text.id == entry2.text.id
138                        && entry1.text.pos == entry2.text.pos));
139 }
140
141
142 bool TexRow::start(RowEntry entry)
143 {
144         if (!enabled_)
145                 return false;
146         return currentRow().addEntry(entry);
147 }
148
149
150 bool TexRow::start(int id, pos_type pos)
151 {
152         return start(textEntry(id,pos));
153 }
154
155
156 void TexRow::forceStart(int id, pos_type pos)
157 {
158         if (!enabled_)
159                 return;
160         return currentRow().forceAddEntry(textEntry(id,pos));
161 }
162
163
164 void TexRow::startMath(uid_type id, idx_type cell)
165 {
166         start(mathEntry(id,cell));
167 }
168
169
170 void TexRow::newline()
171 {
172         if (!enabled_)
173                 return;
174         rowlist_.push_back(RowEntryList());
175 }
176
177
178 void TexRow::newlines(size_t num_lines)
179 {
180         while (num_lines--)
181                 newline();
182 }
183
184
185 void TexRow::append(TexRow other)
186 {
187         if (!enabled_ || !other.enabled_)
188                 return;
189         RowList::iterator it = other.rowlist_.begin();
190         RowList::iterator const end = other.rowlist_.end();
191         LASSERT(it != end, return);
192         currentRow().append(move(*it++));
193         move(it, end, back_inserter(rowlist_));
194 }
195
196
197 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
198 {
199         LYXERR(Debug::LATEX, "getIdFromRow: row " << row << " requested");
200         TextEntry t = text_none;
201         if (row <= int(rowlist_.size()))
202                 while (row > 0 && isNone(t = rowlist_[row - 1].getTextEntry()))
203                         --row;
204         id = t.id;
205         pos = t.pos;
206         return !isNone(t);
207 }
208
209
210 //static
211 RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
212 {
213         RowEntry entry;
214         InsetMath * insetMath = slice.asInsetMath();
215         if (insetMath) {
216                 entry.is_math = 1;
217                 entry.math.id = insetMath->id();
218                 entry.math.cell = slice.idx();
219         } else if (slice.text()) {
220                 entry.is_math = 0;
221                 entry.text.id = slice.paragraph().id();
222                 entry.text.pos = slice.pos();
223         } else
224                 LASSERT(false, return row_none);
225         return entry;
226 }
227
228
229 //static
230 bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
231 {
232         return entry1.is_math == entry2.is_math
233                 && (entry1.is_math
234                     ? (entry1.math.id == entry2.math.id)
235                     : (entry1.text.id == entry2.text.id));
236 }
237
238
239 //static
240 int TexRow::comparePos(RowEntry entry1, RowEntry entry2)
241 {
242         // assume it is sameParOrInsetMath
243         if (entry1.is_math)
244                 return entry2.math.cell - entry1.math.cell;
245         else
246                 return entry2.text.pos - entry1.text.pos;
247 }
248
249
250 // An iterator on RowList that goes top-down, left-right
251 //
252 // We assume that the end of RowList does not change, which makes things simpler
253 //
254 // Records a pair of iterators on the RowEntryList (row_it_, row_end_) and a
255 // pair of iterators on the current row (it_, it_end_).
256 //
257 // it_ always points to a valid position unless row_it_ == row_end_.
258 //
259 // We could turn this into a proper bidirectional iterator, but we don't need as
260 // much.
261 //
262 class TexRow::RowListIterator
263 {
264 public:
265         RowListIterator(RowList::const_iterator r,
266                         RowList::const_iterator r_end)
267                 : row_it_(r), row_end_(r_end),
268                   it_(r == r_end ? RowEntryList::const_iterator() : r->begin()),
269                   it_end_(r == r_end ? RowEntryList::const_iterator() : r->end())
270         {
271                 normalize();
272         }
273
274
275         RowListIterator() :
276                 row_it_(RowList::const_iterator()),
277                 row_end_(RowList::const_iterator()),
278                 it_(RowEntryList::const_iterator()),
279                 it_end_(RowEntryList::const_iterator()) { }
280
281
282         RowEntry const & operator*()
283         {
284                 return *it_;
285         }
286
287
288         RowListIterator & operator++()
289         {
290                 ++it_;
291                 normalize();
292                 return *this;
293         }
294
295
296         bool atEnd() const
297         {
298                 return row_it_ == row_end_;
299         }
300
301
302         bool operator==(RowListIterator const & a) const
303         {
304                 return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
305         }
306
307
308         bool operator!=(RowListIterator const & a) const { return !operator==(a); }
309
310
311         // Current row.
312         RowList::const_iterator const & row() const
313         {
314                 return row_it_;
315         }
316 private:
317         // ensures that it_ points to a valid value unless row_it_ == row_end_
318         void normalize()
319         {
320                 if (row_it_ == row_end_)
321                         return;
322                 while (it_ == it_end_) {
323                         ++row_it_;
324                         if (row_it_ != row_end_) {
325                                 it_ = row_it_->begin();
326                                 it_end_ = row_it_->end();
327                         } else
328                                 return;
329                 }
330         }
331         //
332         RowList::const_iterator row_it_;
333         //
334         RowList::const_iterator row_end_;
335         //
336         RowEntryList::const_iterator it_;
337         //
338         RowEntryList::const_iterator it_end_;
339 };
340
341
342 TexRow::RowListIterator TexRow::begin() const
343 {
344         return RowListIterator(rowlist_.begin(), rowlist_.end());
345 }
346
347
348 TexRow::RowListIterator TexRow::end() const
349 {
350         return RowListIterator(rowlist_.end(), rowlist_.end());
351 }
352
353
354 pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
355 {
356         bool beg_found = false;
357         bool end_is_next = true;
358         int end_offset = 1;
359         size_t best_slice = 0;
360         RowEntry best_entry = row_none;
361         size_t const n = dit.depth();
362         // this loop finds a pair (best_beg_row,best_end_row) where best_beg_row is
363         // the first row of the topmost possible CursorSlice, and best_end_row is
364         // the one just before the first row matching the next CursorSlice.
365         RowListIterator const begin = this->begin();//necessary disambiguation
366         RowListIterator const end = this->end();
367         RowListIterator best_beg_entry;
368         //best last entry with same pos as the beg_entry, or first entry with pos
369         //immediately following the beg_entry
370         RowListIterator best_end_entry;
371         RowListIterator it = begin;
372         for (; it != end; ++it) {
373                 // Compute the best end row.
374                 if (beg_found
375                         && (!sameParOrInsetMath(*it, *best_end_entry)
376                                 || comparePos(*it, *best_end_entry) <= 0)
377                         && sameParOrInsetMath(*it, best_entry)) {
378                     switch (comparePos(*it, best_entry)) {
379                         case 0:
380                                 // Either it is the last one that matches pos...
381                                 best_end_entry = it;
382                                 end_is_next = false;
383                                 end_offset = 1;
384                                 break;
385                         case -1: {
386                                 // ...or it is the row preceding the first that matches pos+1
387                                 if (!end_is_next) {
388                                         end_is_next = true;
389                                         if (it.row() != best_end_entry.row())
390                                                 end_offset = 0;
391                                         best_end_entry = it;
392                                 }
393                                 break;
394                         }
395                         }
396                 }
397                 // Compute the best begin row. It is better than the previous one if it
398                 // matches either at a deeper level, or at the same level but not
399                 // before.
400                 for (size_t i = best_slice; i < n; ++i) {
401                         RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
402                         if (sameParOrInsetMath(*it, entry_i)) {
403                                 if (comparePos(*it, entry_i) >= 0
404                                         && (i > best_slice
405                                                 || !beg_found
406                                                 || !sameParOrInsetMath(*it, *best_beg_entry)
407                                                 || (comparePos(*it, *best_beg_entry) <= 0
408                                                         && comparePos(entry_i, *best_beg_entry) != 0)
409                                                 )
410                                         ) {
411                                         beg_found = true;
412                                         end_is_next = false;
413                                         end_offset = 1;
414                                         best_slice = i;
415                                         best_entry = entry_i;
416                                         best_beg_entry = best_end_entry = it;
417                                 }
418                                 //found CursorSlice
419                                 break;
420                         }
421                 }
422         }
423         if (!beg_found)
424                 return make_pair(-1,-1);
425         int const best_beg_row = distance(rowlist_.begin(),
426                                                                           best_beg_entry.row()) + 1;
427         int const best_end_row = distance(rowlist_.begin(),
428                                                                           best_end_entry.row()) + end_offset;
429         return make_pair(best_beg_row, best_end_row);
430 }
431
432
433 pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
434 {
435         DocIterator beg = cur.selectionBegin();
436         pair<int,int> beg_rows = rowFromDocIterator(beg);
437         if (cur.selection()) {
438                 DocIterator end = cur.selectionEnd();
439                 if (!cur.selIsMultiCell()
440                         // backwardPos asserts without the following test, IMO it's not my
441                         // duty to check this.
442                         && (end.top().pit() != 0
443                                 || end.top().idx() != 0
444                                 || end.top().pos() != 0))
445                         end.top().backwardPos();
446                 pair<int,int> end_rows = rowFromDocIterator(end);
447                 return make_pair(min(beg_rows.first, end_rows.first),
448                                  max(beg_rows.second, end_rows.second));
449         } else
450                 return make_pair(beg_rows.first, beg_rows.second);
451 }
452
453
454 int TexRow::rows() const
455 {
456         return rowlist_.size();
457 }
458
459
460 // debugging functions
461
462 ///
463 docstring TexRow::asString(RowEntry entry)
464 {
465         odocstringstream os;
466         if (entry.is_math)
467                 os << "(1," << entry.math.id << "," << entry.math.cell << ")";
468         else
469                 os << "(0," << entry.text.id << "," << entry.text.pos << ")";
470         return os.str();
471 }
472
473
474 ///prepends the texrow to the source given by tex, for debugging purpose
475 void TexRow::prepend(docstring_list & tex) const
476 {
477         size_type const prefix_length = 25;
478         if (tex.size() < rowlist_.size())
479                 tex.resize(rowlist_.size());
480         auto it = rowlist_.cbegin();
481         auto const beg = rowlist_.cbegin();
482         auto const end = rowlist_.cend();
483         for (; it < end; ++it) {
484                 docstring entry;
485                 for (RowEntry const & e : *it)
486                         entry += asString(e);
487                 if (entry.length() < prefix_length)
488                         entry = entry + docstring(prefix_length - entry.length(), ' ');
489                 ptrdiff_t i = it - beg;
490                 tex[i] = entry + "  " + tex[i];
491         }
492 }
493
494
495
496 LyXErr & operator<<(LyXErr & l, TexRow const & texrow)
497 {
498         if (l.enabled()) {
499                 for (int i = 0; i < texrow.rows(); i++) {
500                         int id,pos;
501                         if (texrow.getIdFromRow(i+1,id,pos) && id>0)
502                                 l << i+1 << ":" << id << ":" << pos << "\n";
503                 }
504         }
505         return l;
506 }
507
508
509
510 } // namespace lyx