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