3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Matthias Ettrich
7 * \author Lars Gullik Bjønnes
9 * \author Guillaume Munch
11 * Full author contact details are available in file CREDITS.
17 #include "Paragraph.h"
20 #include "mathed/InsetMath.h"
22 #include "support/debug.h"
23 #include "support/docstring_list.h"
24 #include "support/lassert.h"
35 bool TexRow::RowEntryList::addEntry(RowEntry entry)
38 if (!isNone(text_entry_))
41 text_entry_ = entry.text;
48 void TexRow::RowEntryList::forceAddEntry(RowEntry entry)
50 if (v_.empty() || !(v_.back() == entry))
55 TextEntry TexRow::RowEntryList::getTextEntry() const
57 if (!isNone(text_entry_))
59 return TexRow::text_none;
63 void TexRow::RowEntryList::append(RowEntryList row)
65 if (isNone(text_entry_))
66 text_entry_ = row.text_entry_;
67 move(row.begin(), row.end(), back_inserter(v_));
71 TexRow::TexRow(bool enable)
77 TextEntry const TexRow::text_none = { -1, 0 };
78 RowEntry const TexRow::row_none = { false, { TexRow::text_none } };
82 bool TexRow::isNone(TextEntry t)
89 bool TexRow::isNone(RowEntry r)
91 return !r.is_math && isNone(r.text);
95 void TexRow::reset(bool enable)
103 TexRow::RowEntryList & TexRow::currentRow()
105 return rowlist_.back();
110 RowEntry TexRow::textEntry(int id, int pos)
113 entry.is_math = false;
114 entry.text.pos = pos;
121 RowEntry TexRow::mathEntry(uid_type id, idx_type cell)
124 entry.is_math = true;
125 entry.math.cell = cell;
131 bool operator==(RowEntry entry1, RowEntry entry2)
133 return entry1.is_math == entry2.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));
142 bool TexRow::start(RowEntry entry)
146 return currentRow().addEntry(entry);
150 bool TexRow::start(int id, int pos)
152 return start(textEntry(id,pos));
156 void TexRow::forceStart(int id, int pos)
160 return currentRow().forceAddEntry(textEntry(id,pos));
164 void TexRow::startMath(uid_type id, idx_type cell)
166 start(mathEntry(id,cell));
170 void TexRow::newline()
174 rowlist_.push_back(RowEntryList());
178 void TexRow::newlines(size_t num_lines)
185 void TexRow::append(TexRow other)
187 if (!enabled_ || !other.enabled_)
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_));
197 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
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()))
211 RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
214 InsetMath * insetMath = slice.asInsetMath();
217 entry.math.id = insetMath->id();
218 entry.math.cell = slice.idx();
219 } else if (slice.text()) {
221 entry.text.id = slice.paragraph().id();
222 entry.text.pos = slice.pos();
224 LASSERT(false, return row_none);
230 bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
232 return entry1.is_math == entry2.is_math
234 ? (entry1.math.id == entry2.math.id)
235 : (entry1.text.id == entry2.text.id));
240 int TexRow::comparePos(RowEntry entry1, RowEntry entry2)
242 // assume it is sameParOrInsetMath
244 return entry2.math.cell - entry1.math.cell;
246 return entry2.text.pos - entry1.text.pos;
250 // An iterator on RowList that goes top-down, left-right
252 // We assume that the end of RowList does not change, which makes things simpler
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_).
257 // it_ always points to a valid position unless row_it_ == row_end_.
259 // We could turn this into a proper bidirectional iterator, but we don't need as
262 class TexRow::RowListIterator
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())
276 row_it_(RowList::const_iterator()),
277 row_end_(RowList::const_iterator()),
278 it_(RowEntryList::const_iterator()),
279 it_end_(RowEntryList::const_iterator()) { }
282 RowEntry const & operator*()
288 RowListIterator & operator++()
298 return row_it_ == row_end_;
302 bool operator==(RowListIterator const & a) const
304 return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
308 bool operator!=(RowListIterator const & a) const { return !operator==(a); }
312 RowList::const_iterator const & row() const
317 // ensures that it_ points to a valid value unless row_it_ == row_end_
320 if (row_it_ == row_end_)
322 while (it_ == it_end_) {
324 if (row_it_ != row_end_) {
325 it_ = row_it_->begin();
326 it_end_ = row_it_->end();
332 RowList::const_iterator row_it_;
334 RowList::const_iterator row_end_;
336 RowEntryList::const_iterator it_;
338 RowEntryList::const_iterator it_end_;
342 TexRow::RowListIterator TexRow::begin() const
344 return RowListIterator(rowlist_.begin(), rowlist_.end());
348 TexRow::RowListIterator TexRow::end() const
350 return RowListIterator(rowlist_.end(), rowlist_.end());
354 pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
356 bool beg_found = false;
357 bool end_is_next = true;
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.
375 && (!sameParOrInsetMath(*it, *best_end_entry)
376 || comparePos(*it, *best_end_entry) <= 0)
377 && sameParOrInsetMath(*it, best_entry)) {
378 switch (comparePos(*it, best_entry)) {
380 // Either it is the last one that matches pos...
386 // ...or it is the row preceding the first that matches pos+1
389 if (it.row() != best_end_entry.row())
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
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
406 || !sameParOrInsetMath(*it, *best_beg_entry)
407 || (comparePos(*it, *best_beg_entry) <= 0
408 && comparePos(entry_i, *best_beg_entry) != 0)
415 best_entry = entry_i;
416 best_beg_entry = best_end_entry = it;
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);
433 pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
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));
450 return make_pair(beg_rows.first, beg_rows.second);
454 int TexRow::rows() const
456 return rowlist_.size();
460 // debugging functions
463 docstring TexRow::asString(RowEntry entry)
467 os << "(1," << entry.math.id << "," << entry.math.cell << ")";
469 os << "(0," << entry.text.id << "," << entry.text.pos << ")";
474 ///prepends the texrow to the source given by tex, for debugging purpose
475 void TexRow::prepend(docstring_list & tex) const
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) {
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];
496 LyXErr & operator<<(LyXErr & l, TexRow const & texrow)
499 for (int i = 0; i < texrow.rows(); i++) {
501 if (texrow.getIdFromRow(i+1,id,pos) && id>0)
502 l << i+1 << ":" << id << ":" << pos << "\n";