]> git.lyx.org Git - lyx.git/blobdiff - src/TexRow.cpp
Account for old versions of Pygments
[lyx.git] / src / TexRow.cpp
index 8586a81c41d8fbaac884176d327a02cb8a951d77..460a141600194adccca48685bab7af85cac7ef2d 100644 (file)
 
 #include <config.h>
 
+#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 <algorithm>
+#include <iterator>
 #include <sstream>
 
+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();
+}
+
 
-bool TexRow::RowEntryList::addEntry(RowEntry const & entry)
+void TexString::validate()
 {
-       if (!entry.is_math) {
-               if (text_entry_ < size())
+       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)
+{
+       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;
-               else
-                       text_entry_ = size();
+               break;
+       default:
+               break;
        }
        forceAddEntry(entry);
        return true;
 }
 
 
-void TexRow::RowEntryList::forceAddEntry(RowEntry const & entry)
+void TexRow::RowEntryList::forceAddEntry(RowEntry entry)
 {
-       if (size() == 0 || !(operator[](size() - 1) == entry))
-               push_back(RowEntry(entry));
+       if (v_.empty() || !(v_.back() == entry))
+               v_.push_back(entry);
 }
 
 
 TexRow::TextEntry TexRow::RowEntryList::getTextEntry() const
 {
-       if (text_entry_ < size())
-               return operator[](text_entry_).text;
+       if (!isNone(text_entry_))
+               return text_entry_;
        return TexRow::text_none;
 }
 
 
-TexRow::RowEntry TexRow::RowEntryList::entry() const
+void TexRow::RowEntryList::append(RowEntryList row)
 {
-       if (0 < size())
-               return operator[](0);
-       return TexRow::row_none;
+       if (isNone(text_entry_))
+               text_entry_ = row.text_entry_;
+       move(row.begin(), row.end(), back_inserter(v_));
 }
 
 
-void TexRow::RowEntryList::append(RowEntryList const & row)
+TexRow::TexRow()
 {
-       if (text_entry_ >= size())
-               text_entry_ = row.text_entry_ + size();
-       insert(end(), row.begin(), row.end());
+       reset();
 }
 
 
 TexRow::TextEntry const TexRow::text_none = { -1, 0 };
-TexRow::RowEntry const TexRow::row_none = { false, { TexRow::text_none } };
+TexRow::RowEntry const TexRow::row_none = TexRow::textEntry(-1, 0);
 
 
-bool TexRow::isNone(TextEntry const & t)
+//static
+bool TexRow::isNone(TextEntry t)
 {
        return t.id < 0;
 }
 
 
-bool TexRow::isNone(RowEntry const & r)
+//static
+bool TexRow::isNone(RowEntry r)
 {
-       return !r.is_math && isNone(r.text);
+       return r.type == text_entry && isNone(r.text);
 }
 
 
-void TexRow::reset(bool enable)
+void TexRow::reset()
 {
        rowlist_.clear();
-       current_row_ = RowEntryList();
-       enabled_ = enable;
+       newline();
 }
 
 
-TexRow::RowEntry TexRow::textEntry(int id, int pos)
+TexRow::RowEntryList & TexRow::currentRow()
+{
+       return rowlist_.back();
+}
+
+
+//static
+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;
 }
 
 
+//static
 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==(TexRow::RowEntry const & entry1,
-                               TexRow::RowEntry const & entry2)
+//static
+TexRow::RowEntry TexRow::beginDocument()
+{
+       RowEntry entry;
+       entry.type = begin_document;
+       entry.begindocument = {};
+       return entry;
+}
+
+
+bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2)
 {
-       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));
+       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;
+       }
 }
 
 
 bool TexRow::start(RowEntry entry)
 {
-       if (!enabled_)
-               return false;
-       return current_row_.addEntry(entry);
+       return currentRow().addEntry(entry);
 }
 
 
-bool TexRow::start(int id, int pos)
+bool TexRow::start(int id, pos_type pos)
 {
        return start(textEntry(id,pos));
 }
 
 
-void TexRow::forceStart(int id, int pos)
+void TexRow::forceStart(int id, pos_type pos)
 {
-       if (!enabled_)
-               return;
-       return current_row_.forceAddEntry(textEntry(id,pos));
+       return currentRow().forceAddEntry(textEntry(id,pos));
 }
 
 
@@ -160,97 +218,206 @@ void TexRow::startMath(uid_type id, idx_type cell)
 
 void TexRow::newline()
 {
-       if (!enabled_)
-               return;
-       rowlist_.push_back(current_row_);
-       current_row_ = RowEntryList();
+       rowlist_.push_back(RowEntryList());
 }
 
-void TexRow::newlines(int num_lines)
+
+void TexRow::newlines(size_t num_lines)
 {
-       if (!enabled_)
-               return;
-       for (int i = 0; i < num_lines; ++i) {
+       while (num_lines--)
                newline();
-       }
 }
 
-void TexRow::finalize()
+
+void TexRow::append(TexRow other)
 {
-       if (!enabled_)
-               return;
-       newline();
+       RowList::iterator it = other.rowlist_.begin();
+       RowList::iterator const end = other.rowlist_.end();
+       LASSERT(it != end, return);
+       currentRow().append(move(*it++));
+       move(it, end, back_inserter(rowlist_));
 }
 
 
-void TexRow::append(TexRow const & texrow)
+pair<TexRow::TextEntry, TexRow::TextEntry>
+TexRow::getEntriesFromRow(int const row) const
 {
-       if (!enabled_ || !texrow.enabled_)
-               return;
-       RowList::const_iterator it = texrow.rowlist_.begin();
-       RowList::const_iterator const end = texrow.rowlist_.end();
-       if (it == end) {
-               current_row_.append(texrow.current_row_);
-       } else {
-               current_row_.append(*it++);
-               rowlist_.push_back(current_row_);
-               rowlist_.insert(rowlist_.end(), it, end);
-               current_row_ = texrow.current_row_;
+       // 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<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)};
+}
+
 
-bool TexRow::getIdFromRow(int row, int & id, int & pos) const
+FuncRequest TexRow::goToFuncFromRow(int const row) const
 {
-       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);
+       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 {
-               // should not happen
-               entry = row_none;
-       }
+       } else
+               LASSERT(false, return row_none);
        return entry;
 }
 
 
-bool TexRow::sameParOrInsetMath(RowEntry const & entry1,
-                                                               RowEntry const & entry2)
+//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;
+       }
 }
 
 
-// assumes it is sameParOrInsetMath
-int TexRow::comparePos(RowEntry const & entry1,
-                                          RowEntry const & entry2)
+//static
+int TexRow::comparePos(RowEntry entry1, RowEntry entry2)
 {
-       if (entry1.is_math)
-               return entry2.math.cell - entry1.math.cell;
-       else
+       // assume it is sameParOrInsetMath
+       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;
+       }
 }
 
 
@@ -270,7 +437,7 @@ class TexRow::RowListIterator
 {
 public:
        RowListIterator(RowList::const_iterator r,
-                                       RowList::const_iterator r_end)
+                       RowList::const_iterator r_end)
                : row_it_(r), row_end_(r_end),
                  it_(r == r_end ? RowEntryList::const_iterator() : r->begin()),
                  it_end_(r == r_end ? RowEntryList::const_iterator() : r->end())
@@ -358,8 +525,9 @@ TexRow::RowListIterator TexRow::end() const
 }
 
 
-std::pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
+pair<int,int> 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;
@@ -405,7 +573,7 @@ std::pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
                // matches either at a deeper level, or at the same level but not
                // before.
                for (size_t i = best_slice; i < n; ++i) {
-                       TexRow::RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
+                       RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
                        if (sameParOrInsetMath(*it, entry_i)) {
                                if (comparePos(*it, entry_i) >= 0
                                        && (i > best_slice
@@ -428,46 +596,62 @@ std::pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
                }
        }
        if (!beg_found)
-               return std::make_pair(-1,-1);
+               return make_pair(-1,-1);
        int const best_beg_row = distance(rowlist_.begin(),
                                                                          best_beg_entry.row()) + 1;
        int const best_end_row = distance(rowlist_.begin(),
                                                                          best_end_entry.row()) + end_offset;
-       return std::make_pair(best_beg_row, best_end_row);
+       return make_pair(best_beg_row, best_end_row);
 }
 
 
-std::pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
+pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
 {
        DocIterator beg = cur.selectionBegin();
-       std::pair<int,int> beg_rows = rowFromDocIterator(beg);
+       pair<int,int> beg_rows = rowFromDocIterator(beg);
        if (cur.selection()) {
                DocIterator end = cur.selectionEnd();
-               if (!cur.selIsMultiCell()
-                       // backwardPos asserts without the following test, IMO it's not my
-                       // duty to check this.
-                       && (end.top().pit() != 0
-                               || end.top().idx() != 0
-                               || end.top().pos() != 0))
+               if (!cur.selIsMultiCell() && !end.top().at_cell_begin())
                        end.top().backwardPos();
-               std::pair<int,int> end_rows = rowFromDocIterator(end);
-               return std::make_pair(std::min(beg_rows.first, end_rows.first),
-                                                         std::max(beg_rows.second, end_rows.second));
+               pair<int,int> end_rows = rowFromDocIterator(end);
+               return make_pair(min(beg_rows.first, end_rows.first),
+                                max(beg_rows.second, end_rows.second));
        } else
-               return std::make_pair(beg_rows.first, beg_rows.second);
+               return make_pair(beg_rows.first, beg_rows.second);
+}
+
+
+size_t TexRow::rows() const
+{
+       return rowlist_.size();
+}
+
+
+void TexRow::setRows(size_t r)
+{
+       rowlist_.resize(r, RowEntryList());
 }
 
 
 // debugging functions
 
 ///
-docstring TexRow::asString(RowEntry const & entry)
+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();
 }
 
@@ -478,36 +662,19 @@ void TexRow::prepend(docstring_list & tex) const
        size_type const prefix_length = 25;
        if (tex.size() < rowlist_.size())
                tex.resize(rowlist_.size());
-       std::vector<RowEntryList>::const_iterator it = rowlist_.begin();
-       std::vector<RowEntryList>::const_iterator const beg = rowlist_.begin();
-       std::vector<RowEntryList>::const_iterator const end = rowlist_.end();
+       auto it = rowlist_.cbegin();
+       auto const beg = rowlist_.cbegin();
+       auto const end = rowlist_.cend();
        for (; it < end; ++it) {
                docstring entry;
-               std::vector<RowEntry>::const_iterator it2 = it->begin();
-               std::vector<RowEntry>::const_iterator const end2 = it->end();
-               for (; it2 != end2; ++it2)
-                       entry += asString(*it2);
+               for (RowEntry const & e : *it)
+                       entry += asString(e);
                if (entry.length() < prefix_length)
-                       entry = entry + docstring(prefix_length - entry.length(), L' ');
+                       entry = entry + docstring(prefix_length - entry.length(), ' ');
                ptrdiff_t i = it - beg;
                tex[i] = entry + "  " + tex[i];
        }
 }
 
 
-
-LyXErr & operator<<(LyXErr & l, TexRow & 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