]> git.lyx.org Git - features.git/blobdiff - src/TexRow.cpp
TexRow: enable new RowEntry types
[features.git] / src / TexRow.cpp
index 8f28f7ffee1a75b142833f072ec3fc97a73b6d2c..24e0f36937d45ff86e4032d6c92ba5f46972a2cb 100644 (file)
@@ -66,11 +66,15 @@ void TexString::validate()
 
 bool TexRow::RowEntryList::addEntry(RowEntry entry)
 {
-       if (!entry.is_math) {
+       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;
@@ -84,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_;
@@ -106,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
@@ -120,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);
 }
 
 
@@ -138,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;
@@ -149,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()
+{
+       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;
+       }
 }
 
 
@@ -217,7 +239,8 @@ void TexRow::append(TexRow other)
 }
 
 
-pair<TextEntry, TextEntry> TexRow::getEntriesFromRow(int const row) const
+pair<TexRow::TextEntry, TexRow::TextEntry>
+TexRow::getEntriesFromRow(int const row) const
 {
        LYXERR(Debug::LATEX, "getEntriesFromRow: row " << row << " requested");
        // check bounds for row - 1, our target index
@@ -299,24 +322,29 @@ FuncRequest TexRow::goToFunc(TextEntry start, TextEntry end)
 }
 
 
-//static
-FuncRequest TexRow::goToFunc(std::pair<TextEntry,TextEntry> entries)
+FuncRequest TexRow::goToFuncFromRow(int const row) const
 {
-       return goToFunc(entries.first, entries.second);
+       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
-RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
+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
@@ -328,10 +356,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;
+       }
 }
 
 
@@ -339,10 +375,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;
+       }
 }
 
 
@@ -563,10 +605,19 @@ void TexRow::setRows(size_t r)
 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();
 }