X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FTexRow.cpp;h=460a141600194adccca48685bab7af85cac7ef2d;hb=28be7d552f62cc02fa86d7f79201d089bfb2d7b5;hp=b4366e07c20219d01978a2a7c81f9a8f0938bfc3;hpb=5fbbf0f281b8543899036419e05242d7feac9a59;p=lyx.git diff --git a/src/TexRow.cpp b/src/TexRow.cpp index b4366e07c2..460a141600 100644 --- a/src/TexRow.cpp +++ b/src/TexRow.cpp @@ -15,16 +15,19 @@ #include "Buffer.h" #include "Cursor.h" +#include "FuncRequest.h" #include "Paragraph.h" #include "TexRow.h" #include "mathed/InsetMath.h" +#include "support/convert.h" #include "support/debug.h" #include "support/docstring_list.h" #include "support/lassert.h" #include +#include #include using namespace std; @@ -33,13 +36,45 @@ using namespace std; namespace lyx { +TexString::TexString(docstring s) + : str(move(s)), texrow(TexRow()) +{ + texrow.setRows(1 + count(str.begin(), str.end(), '\n')); +} + + +TexString::TexString(docstring s, TexRow t) + : str(move(s)), texrow(move(t)) +{ + validate(); +} + + +void TexString::validate() +{ + size_t lines = 1 + count(str.begin(), str.end(), '\n'); + size_t rows = texrow.rows(); + bool valid = lines == rows; + if (!valid) + LYXERR0("TexString has " << lines << " lines but " << rows << " rows." ); + // Assert in devel mode. This is important to catch bugs early, otherwise + // they might be hard to notice and find. Recover gracefully in release + // mode. + LASSERT(valid, texrow.setRows(lines)); +} + + bool TexRow::RowEntryList::addEntry(RowEntry entry) { - if (!entry.is_math) { - if (!isNone(text_entry_)) - return false; - else + switch (entry.type) { + case text_entry: + if (isNone(text_entry_)) text_entry_ = entry.text; + else if (!v_.empty() && TexRow::sameParOrInsetMath(v_.back(), entry)) + return false; + break; + default: + break; } forceAddEntry(entry); return true; @@ -53,7 +88,7 @@ void TexRow::RowEntryList::forceAddEntry(RowEntry entry) } -TextEntry TexRow::RowEntryList::getTextEntry() const +TexRow::TextEntry TexRow::RowEntryList::getTextEntry() const { if (!isNone(text_entry_)) return text_entry_; @@ -75,8 +110,8 @@ TexRow::TexRow() } -TextEntry const TexRow::text_none = { -1, 0 }; -RowEntry const TexRow::row_none = { false, { TexRow::text_none } }; +TexRow::TextEntry const TexRow::text_none = { -1, 0 }; +TexRow::RowEntry const TexRow::row_none = TexRow::textEntry(-1, 0); //static @@ -89,7 +124,7 @@ bool TexRow::isNone(TextEntry t) //static bool TexRow::isNone(RowEntry r) { - return !r.is_math && isNone(r.text); + return r.type == text_entry && isNone(r.text); } @@ -107,10 +142,10 @@ TexRow::RowEntryList & TexRow::currentRow() //static -RowEntry TexRow::textEntry(int id, pos_type pos) +TexRow::RowEntry TexRow::textEntry(int id, pos_type pos) { RowEntry entry; - entry.is_math = false; + entry.type = text_entry; entry.text.pos = pos; entry.text.id = id; return entry; @@ -118,24 +153,42 @@ RowEntry TexRow::textEntry(int id, pos_type pos) //static -RowEntry TexRow::mathEntry(uid_type id, idx_type cell) +TexRow::RowEntry TexRow::mathEntry(uid_type id, idx_type cell) { RowEntry entry; - entry.is_math = true; + entry.type = math_entry; entry.math.cell = cell; entry.math.id = id; return entry; } -bool operator==(RowEntry entry1, RowEntry entry2) +//static +TexRow::RowEntry TexRow::beginDocument() { - return entry1.is_math == entry2.is_math - && (entry1.is_math - ? (entry1.math.id == entry2.math.id - && entry1.math.cell == entry2.math.cell) - : (entry1.text.id == entry2.text.id - && entry1.text.pos == entry2.text.pos)); + RowEntry entry; + entry.type = begin_document; + entry.begindocument = {}; + return entry; +} + + +bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2) +{ + if (entry1.type != entry2.type) + return false; + switch (entry1.type) { + case TexRow::text_entry: + return entry1.text.id == entry2.text.id + && entry1.text.pos == entry2.text.pos; + case TexRow::math_entry: + return entry1.math.id == entry2.math.id + && entry1.math.cell == entry2.math.cell; + case TexRow::begin_document: + return true; + default: + return false; + } } @@ -186,68 +239,102 @@ void TexRow::append(TexRow other) } -bool TexRow::getIdFromRow(int row, int & id, int & pos) const -{ - LYXERR(Debug::LATEX, "getIdFromRow: row " << row << " requested"); - TextEntry t = text_none; - if (row <= int(rowlist_.size())) - while (row > 0 && isNone(t = rowlist_[row - 1].getTextEntry())) - --row; - id = t.id; - pos = t.pos; - return !isNone(t); -} - - -pair TexRow::getEntriesFromRow(int const row) const +pair +TexRow::getEntriesFromRow(int const row) const { + // 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::LATEX, "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(row - 1); if (i >= rowlist_.size()) return {text_none, text_none}; + // find the start entry - size_t j = i; - while (j > 0 && isNone(rowlist_[j].getTextEntry())) - --j; - TextEntry start = rowlist_[j].getTextEntry(); + 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 - j = i + 1; - while (j < rowlist_.size() && isNone(rowlist_[j].getTextEntry())) - ++j; - TextEntry end = - (j < rowlist_.size()) ? rowlist_[j].getTextEntry() : text_none; + 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 TexRow::getDocIteratorFromRow( +pair TexRow::getDocIteratorsFromRow( int const row, Buffer const & buf) const { TextEntry start, end; tie(start,end) = getEntriesFromRow(row); - LYXERR(Debug::LATEX, - "getDocIteratorFromRow: for row " << row << ", TexRow has found " - "start (id=" << start.id << ",pos=" << start.pos << "), " - "end (id=" << end.id << ",pos=" << end.pos << ")"); + return getDocIteratorsFromEntries(start, end, buf); +} + + +//static +pair 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) - dit_start.pos() = min(start.pos, dit_start.lastpos()); + set_pos(dit_start, start.pos); // Finding end DocIterator dit_end = buf.getParFromID(end.id); if (dit_end) { - dit_end.pos() = min(end.pos, dit_end.lastpos()); - // So far dit_end belongs to the next row. Step backwards. - if (!dit_end.top().at_cell_begin()) { + 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()) @@ -260,16 +347,37 @@ pair TexRow::getDocIteratorFromRow( //static -RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice) +FuncRequest TexRow::goToFunc(TextEntry start, TextEntry end) +{ + return {LFUN_PARAGRAPH_GOTO, + convert(start.id) + " " + convert(start.pos) + " " + + convert(end.id) + " " + convert(end.pos)}; +} + + +FuncRequest TexRow::goToFuncFromRow(int const row) const +{ + TextEntry start, end; + tie(start,end) = getEntriesFromRow(row); + LYXERR(Debug::LATEX, + "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.is_math = 1; + entry.type = math_entry; entry.math.id = insetMath->id(); entry.math.cell = slice.idx(); } else if (slice.text()) { - entry.is_math = 0; + entry.type = text_entry; entry.text.id = slice.paragraph().id(); entry.text.pos = slice.pos(); } else @@ -281,10 +389,18 @@ RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice) //static bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2) { - return entry1.is_math == entry2.is_math - && (entry1.is_math - ? (entry1.math.id == entry2.math.id) - : (entry1.text.id == entry2.text.id)); + 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: + return false; + } } @@ -292,10 +408,16 @@ bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2) int TexRow::comparePos(RowEntry entry1, RowEntry entry2) { // assume it is sameParOrInsetMath - if (entry1.is_math) - return entry2.math.cell - entry1.math.cell; - else + switch (entry1.type /* equal to entry2.type */) { + case TexRow::text_entry: return entry2.text.pos - entry1.text.pos; + case TexRow::math_entry: + return entry2.math.cell - entry1.math.cell; + case TexRow::begin_document: + return 0; + default: + return 0; + } } @@ -405,6 +527,7 @@ TexRow::RowListIterator TexRow::end() const pair TexRow::rowFromDocIterator(DocIterator const & dit) const { + // Do not change anything in this algorithm if unsure. bool beg_found = false; bool end_is_next = true; int end_offset = 1; @@ -498,22 +621,37 @@ pair TexRow::rowFromCursor(Cursor const & cur) const } -int TexRow::rows() const +size_t TexRow::rows() const { return rowlist_.size(); } +void TexRow::setRows(size_t r) +{ + rowlist_.resize(r, RowEntryList()); +} + + // debugging functions /// docstring TexRow::asString(RowEntry entry) { odocstringstream os; - if (entry.is_math) - os << "(1," << entry.math.id << "," << entry.math.cell << ")"; - else - os << "(0," << entry.text.id << "," << entry.text.pos << ")"; + switch (entry.type) { + case TexRow::text_entry: + os << "(par " << entry.text.id << "," << entry.text.pos << ")"; + break; + case TexRow::math_entry: + os << "(" << entry.math.id << "," << entry.math.cell << ")"; + break; + case TexRow::begin_document: + os << "(begin_document)"; + break; + default: + break; + } return os.str(); } @@ -539,19 +677,4 @@ void TexRow::prepend(docstring_list & tex) const } - -LyXErr & operator<<(LyXErr & l, TexRow const & texrow) -{ - if (l.enabled()) { - for (int i = 0; i < texrow.rows(); i++) { - int id,pos; - if (texrow.getIdFromRow(i+1,id,pos) && id>0) - l << i+1 << ":" << id << ":" << pos << "\n"; - } - } - return l; -} - - - } // namespace lyx