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.
18 #include "Paragraph.h"
21 #include "mathed/InsetMath.h"
23 #include "support/debug.h"
24 #include "support/docstring_list.h"
25 #include "support/lassert.h"
37 void TexString::validate()
39 size_t lines = 1 + count(str.begin(), str.end(), '\n');
40 size_t rows = texrow.rows();
41 bool valid = lines == rows;
43 LYXERR0("TexString has " << lines << " lines but " << rows << " rows." );
44 // Assert in devel mode. This is important to catch bugs early, otherwise
45 // they might be hard to notice and find. Recover gracefully in release
47 LASSERT(valid, texrow.setRows(lines));
51 bool TexRow::RowEntryList::addEntry(RowEntry entry)
54 if (isNone(text_entry_))
55 text_entry_ = entry.text;
56 else if (!v_.empty() && TexRow::sameParOrInsetMath(v_.back(), entry))
64 void TexRow::RowEntryList::forceAddEntry(RowEntry entry)
66 if (v_.empty() || !(v_.back() == entry))
71 TextEntry TexRow::RowEntryList::getTextEntry() const
73 if (!isNone(text_entry_))
75 return TexRow::text_none;
79 void TexRow::RowEntryList::append(RowEntryList row)
81 if (isNone(text_entry_))
82 text_entry_ = row.text_entry_;
83 move(row.begin(), row.end(), back_inserter(v_));
93 TextEntry const TexRow::text_none = { -1, 0 };
94 RowEntry const TexRow::row_none = { false, { TexRow::text_none } };
98 bool TexRow::isNone(TextEntry t)
105 bool TexRow::isNone(RowEntry r)
107 return !r.is_math && isNone(r.text);
118 TexRow::RowEntryList & TexRow::currentRow()
120 return rowlist_.back();
125 RowEntry TexRow::textEntry(int id, pos_type pos)
128 entry.is_math = false;
129 entry.text.pos = pos;
136 RowEntry TexRow::mathEntry(uid_type id, idx_type cell)
139 entry.is_math = true;
140 entry.math.cell = cell;
146 bool operator==(RowEntry entry1, RowEntry entry2)
148 return entry1.is_math == entry2.is_math
150 ? (entry1.math.id == entry2.math.id
151 && entry1.math.cell == entry2.math.cell)
152 : (entry1.text.id == entry2.text.id
153 && entry1.text.pos == entry2.text.pos));
157 bool TexRow::start(RowEntry entry)
159 return currentRow().addEntry(entry);
163 bool TexRow::start(int id, pos_type pos)
165 return start(textEntry(id,pos));
169 void TexRow::forceStart(int id, pos_type pos)
171 return currentRow().forceAddEntry(textEntry(id,pos));
175 void TexRow::startMath(uid_type id, idx_type cell)
177 start(mathEntry(id,cell));
181 void TexRow::newline()
183 rowlist_.push_back(RowEntryList());
187 void TexRow::newlines(size_t num_lines)
194 void TexRow::append(TexRow other)
196 RowList::iterator it = other.rowlist_.begin();
197 RowList::iterator const end = other.rowlist_.end();
198 LASSERT(it != end, return);
199 currentRow().append(move(*it++));
200 move(it, end, back_inserter(rowlist_));
204 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
206 LYXERR(Debug::LATEX, "getIdFromRow: row " << row << " requested");
207 TextEntry t = text_none;
208 if (row <= int(rowlist_.size()))
209 while (row > 0 && isNone(t = rowlist_[row - 1].getTextEntry()))
217 pair<TextEntry, TextEntry> TexRow::getEntriesFromRow(int const row) const
219 LYXERR(Debug::LATEX, "getEntriesFromRow: row " << row << " requested");
220 // check bounds for row - 1, our target index
222 return {text_none, text_none};
223 size_t const i = static_cast<size_t>(row - 1);
224 if (i >= rowlist_.size())
225 return {text_none, text_none};
226 // find the start entry
228 while (j > 0 && isNone(rowlist_[j].getTextEntry()))
230 TextEntry start = rowlist_[j].getTextEntry();
231 // find the end entry
233 while (j < rowlist_.size() && isNone(rowlist_[j].getTextEntry()))
236 (j < rowlist_.size()) ? rowlist_[j].getTextEntry() : text_none;
237 // The following occurs for a displayed math inset for instance (for good
238 // reasons involving subtleties of the algorithm in getRowFromDocIterator).
239 // We want this inset selected.
240 if (start.id == end.id && start.pos == end.pos)
246 pair<DocIterator, DocIterator> TexRow::getDocIteratorFromRow(
248 Buffer const & buf) const
250 TextEntry start, end;
251 tie(start,end) = getEntriesFromRow(row);
253 "getDocIteratorFromRow: for row " << row << ", TexRow has found "
254 "start (id=" << start.id << ",pos=" << start.pos << "), "
255 "end (id=" << end.id << ",pos=" << end.pos << ")");
257 DocIterator dit_start = buf.getParFromID(start.id);
259 dit_start.pos() = min(start.pos, dit_start.lastpos());
261 DocIterator dit_end = buf.getParFromID(end.id);
263 dit_end.pos() = min(end.pos, dit_end.lastpos());
264 // So far dit_end belongs to the next row. Step backwards.
265 if (!dit_end.top().at_cell_begin()) {
266 CursorSlice end_top = dit_end.top();
267 end_top.backwardPos();
268 if (dit_start && end_top != dit_start.top())
269 dit_end.top() = end_top;
271 dit_end.boundary(true);
273 return {dit_start, dit_end};
278 RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
281 InsetMath * insetMath = slice.asInsetMath();
284 entry.math.id = insetMath->id();
285 entry.math.cell = slice.idx();
286 } else if (slice.text()) {
288 entry.text.id = slice.paragraph().id();
289 entry.text.pos = slice.pos();
291 LASSERT(false, return row_none);
297 bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
299 return entry1.is_math == entry2.is_math
301 ? (entry1.math.id == entry2.math.id)
302 : (entry1.text.id == entry2.text.id));
307 int TexRow::comparePos(RowEntry entry1, RowEntry entry2)
309 // assume it is sameParOrInsetMath
311 return entry2.math.cell - entry1.math.cell;
313 return entry2.text.pos - entry1.text.pos;
317 // An iterator on RowList that goes top-down, left-right
319 // We assume that the end of RowList does not change, which makes things simpler
321 // Records a pair of iterators on the RowEntryList (row_it_, row_end_) and a
322 // pair of iterators on the current row (it_, it_end_).
324 // it_ always points to a valid position unless row_it_ == row_end_.
326 // We could turn this into a proper bidirectional iterator, but we don't need as
329 class TexRow::RowListIterator
332 RowListIterator(RowList::const_iterator r,
333 RowList::const_iterator r_end)
334 : row_it_(r), row_end_(r_end),
335 it_(r == r_end ? RowEntryList::const_iterator() : r->begin()),
336 it_end_(r == r_end ? RowEntryList::const_iterator() : r->end())
343 row_it_(RowList::const_iterator()),
344 row_end_(RowList::const_iterator()),
345 it_(RowEntryList::const_iterator()),
346 it_end_(RowEntryList::const_iterator()) { }
349 RowEntry const & operator*()
355 RowListIterator & operator++()
365 return row_it_ == row_end_;
369 bool operator==(RowListIterator const & a) const
371 return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
375 bool operator!=(RowListIterator const & a) const { return !operator==(a); }
379 RowList::const_iterator const & row() const
384 // ensures that it_ points to a valid value unless row_it_ == row_end_
387 if (row_it_ == row_end_)
389 while (it_ == it_end_) {
391 if (row_it_ != row_end_) {
392 it_ = row_it_->begin();
393 it_end_ = row_it_->end();
399 RowList::const_iterator row_it_;
401 RowList::const_iterator row_end_;
403 RowEntryList::const_iterator it_;
405 RowEntryList::const_iterator it_end_;
409 TexRow::RowListIterator TexRow::begin() const
411 return RowListIterator(rowlist_.begin(), rowlist_.end());
415 TexRow::RowListIterator TexRow::end() const
417 return RowListIterator(rowlist_.end(), rowlist_.end());
421 pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
423 bool beg_found = false;
424 bool end_is_next = true;
426 size_t best_slice = 0;
427 RowEntry best_entry = row_none;
428 size_t const n = dit.depth();
429 // this loop finds a pair (best_beg_row,best_end_row) where best_beg_row is
430 // the first row of the topmost possible CursorSlice, and best_end_row is
431 // the one just before the first row matching the next CursorSlice.
432 RowListIterator const begin = this->begin();//necessary disambiguation
433 RowListIterator const end = this->end();
434 RowListIterator best_beg_entry;
435 //best last entry with same pos as the beg_entry, or first entry with pos
436 //immediately following the beg_entry
437 RowListIterator best_end_entry;
438 RowListIterator it = begin;
439 for (; it != end; ++it) {
440 // Compute the best end row.
442 && (!sameParOrInsetMath(*it, *best_end_entry)
443 || comparePos(*it, *best_end_entry) <= 0)
444 && sameParOrInsetMath(*it, best_entry)) {
445 switch (comparePos(*it, best_entry)) {
447 // Either it is the last one that matches pos...
453 // ...or it is the row preceding the first that matches pos+1
456 if (it.row() != best_end_entry.row())
464 // Compute the best begin row. It is better than the previous one if it
465 // matches either at a deeper level, or at the same level but not
467 for (size_t i = best_slice; i < n; ++i) {
468 RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
469 if (sameParOrInsetMath(*it, entry_i)) {
470 if (comparePos(*it, entry_i) >= 0
473 || !sameParOrInsetMath(*it, *best_beg_entry)
474 || (comparePos(*it, *best_beg_entry) <= 0
475 && comparePos(entry_i, *best_beg_entry) != 0)
482 best_entry = entry_i;
483 best_beg_entry = best_end_entry = it;
491 return make_pair(-1,-1);
492 int const best_beg_row = distance(rowlist_.begin(),
493 best_beg_entry.row()) + 1;
494 int const best_end_row = distance(rowlist_.begin(),
495 best_end_entry.row()) + end_offset;
496 return make_pair(best_beg_row, best_end_row);
500 pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
502 DocIterator beg = cur.selectionBegin();
503 pair<int,int> beg_rows = rowFromDocIterator(beg);
504 if (cur.selection()) {
505 DocIterator end = cur.selectionEnd();
506 if (!cur.selIsMultiCell() && !end.top().at_cell_begin())
507 end.top().backwardPos();
508 pair<int,int> end_rows = rowFromDocIterator(end);
509 return make_pair(min(beg_rows.first, end_rows.first),
510 max(beg_rows.second, end_rows.second));
512 return make_pair(beg_rows.first, beg_rows.second);
516 size_t TexRow::rows() const
518 return rowlist_.size();
522 void TexRow::setRows(size_t r)
524 rowlist_.resize(r, RowEntryList());
528 // debugging functions
531 docstring TexRow::asString(RowEntry entry)
535 os << "(1," << entry.math.id << "," << entry.math.cell << ")";
537 os << "(0," << entry.text.id << "," << entry.text.pos << ")";
542 ///prepends the texrow to the source given by tex, for debugging purpose
543 void TexRow::prepend(docstring_list & tex) const
545 size_type const prefix_length = 25;
546 if (tex.size() < rowlist_.size())
547 tex.resize(rowlist_.size());
548 auto it = rowlist_.cbegin();
549 auto const beg = rowlist_.cbegin();
550 auto const end = rowlist_.cend();
551 for (; it < end; ++it) {
553 for (RowEntry const & e : *it)
554 entry += asString(e);
555 if (entry.length() < prefix_length)
556 entry = entry + docstring(prefix_length - entry.length(), ' ');
557 ptrdiff_t i = it - beg;
558 tex[i] = entry + " " + tex[i];
564 LyXErr & operator<<(LyXErr & l, TexRow const & texrow)
567 for (size_t i = 0; i < texrow.rows(); i++) {
569 if (texrow.getIdFromRow(i+1,id,pos) && id>0)
570 l << i+1 << ":" << id << ":" << pos << "\n";