+ // FIXME: Take math entries into account, take table cells into account and
+ // get rid of the ad hoc special text entry for each row.
+ //
+ // FIXME: A yellow note alone on its paragraph makes the reverse-search on
+ // the subsequent line inaccurate. Get rid of text entries that
+ // correspond to no output by delaying their addition, at the level
+ // of otexrowstream, until a character is actually output.
+ //
+ LYXERR(Debug::OUTFILE, "getEntriesFromRow: row " << row << " requested");
+
+ // check bounds for row - 1, our target index
+ if (row <= 0)
+ return {text_none, text_none};
+ size_t const i = static_cast<size_t>(row - 1);
+ if (i >= rowlist_.size())
+ return {text_none, text_none};
+
+ // find the start entry
+ TextEntry const start = [&]() {
+ for (size_t j = i; j > 0; --j) {
+ if (!isNone(rowlist_[j].getTextEntry()))
+ return rowlist_[j].getTextEntry();
+ // Check the absence of begin_document at row j. The begin_document row
+ // entry is used to prevent mixing of body and preamble.
+ for (RowEntry entry : rowlist_[j])
+ if (entry.type == begin_document)
+ return text_none;
+ }
+ return text_none;
+ } ();
+
+ // find the end entry
+ TextEntry end = [&]() {
+ if (isNone(start))
+ return text_none;
+ // select up to the last position of the starting paragraph as a
+ // fallback
+ TextEntry last_pos = {start.id, -1};
+ // find the next occurence of paragraph start.id
+ for (size_t j = i + 1; j < rowlist_.size(); ++j) {
+ for (RowEntry entry : rowlist_[j]) {
+ if (entry.type == begin_document)
+ // what happens in the preamble remains in the preamble
+ return last_pos;
+ if (entry.type == text_entry && entry.text.id == start.id)
+ return entry.text;
+ }
+ }
+ return last_pos;
+ } ();
+
+ // The following occurs for a displayed math inset for instance (for good
+ // reasons involving subtleties of the algorithm in getRowFromDocIterator).
+ // We want this inset selected.
+ if (start.id == end.id && start.pos == end.pos)
+ ++end.pos;
+
+ return {start, end};
+}
+
+
+pair<DocIterator, DocIterator> TexRow::getDocIteratorsFromRow(
+ int const row,
+ Buffer const & buf) const
+{
+ TextEntry start, end;
+ tie(start,end) = getEntriesFromRow(row);
+ return getDocIteratorsFromEntries(start, end, buf);
+}
+
+
+//static
+pair<DocIterator, DocIterator> TexRow::getDocIteratorsFromEntries(
+ TextEntry start,
+ TextEntry end,
+ Buffer const & buf)
+{
+ auto set_pos = [](DocIterator & dit, pos_type pos) {
+ dit.pos() = (pos >= 0) ? min(pos, dit.lastpos())
+ // negative pos values are counted from the end
+ : max(dit.lastpos() + pos + 1, pos_type(0));
+ };
+ // Finding start
+ DocIterator dit_start = buf.getParFromID(start.id);
+ if (dit_start)
+ set_pos(dit_start, start.pos);
+ // Finding end
+ DocIterator dit_end = buf.getParFromID(end.id);
+ if (dit_end) {
+ set_pos(dit_end, end.pos);
+ // Step backwards to prevent selecting the beginning of another
+ // paragraph.
+ if (dit_end.pos() == 0 && !dit_end.top().at_cell_begin()) {
+ CursorSlice end_top = dit_end.top();
+ end_top.backwardPos();
+ if (dit_start && end_top != dit_start.top())
+ dit_end.top() = end_top;
+ }
+ dit_end.boundary(true);
+ }
+ return {dit_start, dit_end};
+}
+
+
+//static
+FuncRequest TexRow::goToFunc(TextEntry start, TextEntry end)
+{
+ return {LFUN_PARAGRAPH_GOTO,
+ convert<string>(start.id) + " " + convert<string>(start.pos) + " " +
+ convert<string>(end.id) + " " + convert<string>(end.pos)};
+}
+
+
+FuncRequest TexRow::goToFuncFromRow(int const row) const
+{
+ TextEntry start, end;
+ tie(start,end) = getEntriesFromRow(row);
+ LYXERR(Debug::OUTFILE,
+ "goToFuncFromRow: for row " << row << ", TexRow has found "
+ "start (id=" << start.id << ",pos=" << start.pos << "), "
+ "end (id=" << end.id << ",pos=" << end.pos << ")");
+ return goToFunc(start, end);
+}
+
+
+//static
+TexRow::RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
+{
+ RowEntry entry;
+ InsetMath * insetMath = slice.asInsetMath();
+ if (insetMath) {
+ entry.type = math_entry;
+ entry.math.id = insetMath->id();
+ entry.math.cell = slice.idx();
+ } else if (slice.text()) {
+ entry.type = text_entry;
+ entry.text.id = slice.paragraph().id();
+ entry.text.pos = slice.pos();
+ } else
+ LASSERT(false, return row_none);
+ return entry;
+}
+
+
+//static
+bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
+{
+ if (entry1.type != entry2.type)
+ return false;
+ switch (entry1.type) {
+ case TexRow::text_entry:
+ return entry1.text.id == entry2.text.id;
+ case TexRow::math_entry:
+ return entry1.math.id == entry2.math.id;
+ case TexRow::begin_document:
+ return true;
+ default: