2002-05-30 03:37:24 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file TexRow.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2002-05-30 03:37:24 +00:00
|
|
|
* \author Matthias Ettrich
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author John Levon
|
2015-10-13 23:17:05 +00:00
|
|
|
* \author Guillaume Munch
|
2003-08-23 00:17:00 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-05-30 03:37:24 +00:00
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2016-09-03 22:54:05 +00:00
|
|
|
#include "Buffer.h"
|
2015-10-13 19:26:21 +00:00
|
|
|
#include "Cursor.h"
|
2016-09-05 02:23:24 +00:00
|
|
|
#include "FuncRequest.h"
|
2015-09-15 04:56:01 +00:00
|
|
|
#include "Paragraph.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "TexRow.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
#include "mathed/InsetMath.h"
|
|
|
|
|
2016-09-05 02:23:24 +00:00
|
|
|
#include "support/convert.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2015-10-13 23:17:05 +00:00
|
|
|
#include "support/docstring_list.h"
|
2016-06-20 02:47:40 +00:00
|
|
|
#include "support/lassert.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2003-09-05 22:17:02 +00:00
|
|
|
#include <algorithm>
|
2016-09-23 22:50:03 +00:00
|
|
|
#include <iterator>
|
2015-10-13 23:17:05 +00:00
|
|
|
#include <sstream>
|
2003-05-07 21:27:29 +00:00
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
using namespace std;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
namespace lyx {
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
|
2016-09-25 10:38:53 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-04 02:21:19 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
bool TexRow::RowEntryList::addEntry(RowEntry entry)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2016-10-11 12:14:48 +00:00
|
|
|
switch (entry.type) {
|
|
|
|
case text_entry:
|
2016-09-04 02:21:19 +00:00
|
|
|
if (isNone(text_entry_))
|
2016-06-20 02:47:40 +00:00
|
|
|
text_entry_ = entry.text;
|
2016-09-04 02:21:19 +00:00
|
|
|
else if (!v_.empty() && TexRow::sameParOrInsetMath(v_.back(), entry))
|
|
|
|
return false;
|
2016-10-11 12:14:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
2015-10-13 22:51:50 +00:00
|
|
|
forceAddEntry(entry);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
void TexRow::RowEntryList::forceAddEntry(RowEntry entry)
|
2015-10-13 22:51:50 +00:00
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
if (v_.empty() || !(v_.back() == entry))
|
|
|
|
v_.push_back(entry);
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 10:09:38 +00:00
|
|
|
TexRow::TextEntry TexRow::RowEntryList::getTextEntry() const
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
if (!isNone(text_entry_))
|
|
|
|
return text_entry_;
|
2015-10-13 23:17:05 +00:00
|
|
|
return TexRow::text_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
void TexRow::RowEntryList::append(RowEntryList row)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
if (isNone(text_entry_))
|
|
|
|
text_entry_ = row.text_entry_;
|
|
|
|
move(row.begin(), row.end(), back_inserter(v_));
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-04 02:02:47 +00:00
|
|
|
TexRow::TexRow()
|
2015-10-13 22:51:50 +00:00
|
|
|
{
|
2016-09-04 02:02:47 +00:00
|
|
|
reset();
|
2015-10-13 22:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 10:09:38 +00:00
|
|
|
TexRow::TextEntry const TexRow::text_none = { -1, 0 };
|
2016-10-11 12:14:48 +00:00
|
|
|
TexRow::RowEntry const TexRow::row_none = TexRow::textEntry(-1, 0);
|
2015-10-13 23:17:05 +00:00
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
//static
|
|
|
|
bool TexRow::isNone(TextEntry t)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
|
|
|
return t.id < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
//static
|
|
|
|
bool TexRow::isNone(RowEntry r)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2016-10-11 12:14:48 +00:00
|
|
|
return r.type == text_entry && isNone(r.text);
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-04 02:02:47 +00:00
|
|
|
void TexRow::reset()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2015-10-13 23:17:05 +00:00
|
|
|
rowlist_.clear();
|
2016-06-20 02:47:40 +00:00
|
|
|
newline();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:19:48 +00:00
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
TexRow::RowEntryList & TexRow::currentRow()
|
|
|
|
{
|
|
|
|
return rowlist_.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//static
|
2016-10-11 10:09:38 +00:00
|
|
|
TexRow::RowEntry TexRow::textEntry(int id, pos_type pos)
|
1999-11-22 16:19:48 +00:00
|
|
|
{
|
2015-10-13 23:17:05 +00:00
|
|
|
RowEntry entry;
|
2016-10-11 12:14:48 +00:00
|
|
|
entry.type = text_entry;
|
2015-10-13 23:17:05 +00:00
|
|
|
entry.text.pos = pos;
|
|
|
|
entry.text.id = id;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
//static
|
2016-10-11 10:09:38 +00:00
|
|
|
TexRow::RowEntry TexRow::mathEntry(uid_type id, idx_type cell)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
|
|
|
RowEntry entry;
|
2016-10-11 12:14:48 +00:00
|
|
|
entry.type = math_entry;
|
2015-10-13 23:17:05 +00:00
|
|
|
entry.math.cell = cell;
|
|
|
|
entry.math.id = id;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 12:14:48 +00:00
|
|
|
//static
|
|
|
|
TexRow::RowEntry TexRow::beginDocument()
|
|
|
|
{
|
|
|
|
RowEntry entry;
|
|
|
|
entry.type = begin_document;
|
|
|
|
entry.begindocument = {};
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 10:09:38 +00:00
|
|
|
bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2016-10-11 12:14:48 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TexRow::start(RowEntry entry)
|
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
return currentRow().addEntry(entry);
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-03 22:52:55 +00:00
|
|
|
bool TexRow::start(int id, pos_type pos)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
|
|
|
return start(textEntry(id,pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-03 22:52:55 +00:00
|
|
|
void TexRow::forceStart(int id, pos_type pos)
|
2015-10-13 22:51:50 +00:00
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
return currentRow().forceAddEntry(textEntry(id,pos));
|
2015-10-13 22:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TexRow::startMath(uid_type id, idx_type cell)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2015-10-13 22:51:50 +00:00
|
|
|
start(mathEntry(id,cell));
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:19:48 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
void TexRow::newline()
|
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
rowlist_.push_back(RowEntryList());
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
void TexRow::newlines(size_t num_lines)
|
2015-09-15 04:56:01 +00:00
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
while (num_lines--)
|
|
|
|
newline();
|
2015-09-15 04:56:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
void TexRow::append(TexRow other)
|
2015-10-13 22:51:50 +00:00
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
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_));
|
2015-10-13 22:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 10:09:38 +00:00
|
|
|
pair<TexRow::TextEntry, TexRow::TextEntry>
|
|
|
|
TexRow::getEntriesFromRow(int const row) const
|
2016-09-03 22:54:05 +00:00
|
|
|
{
|
2016-10-11 12:52:10 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2016-09-03 22:54:05 +00:00
|
|
|
LYXERR(Debug::LATEX, "getEntriesFromRow: row " << row << " requested");
|
2016-10-11 12:52:10 +00:00
|
|
|
|
2016-09-03 22:54:05 +00:00
|
|
|
// 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};
|
2016-10-11 12:52:10 +00:00
|
|
|
|
2016-09-03 22:54:05 +00:00
|
|
|
// find the start entry
|
2016-10-11 12:52:10 +00:00
|
|
|
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;
|
|
|
|
} ();
|
|
|
|
|
2016-09-03 22:54:05 +00:00
|
|
|
// find the end entry
|
2016-10-11 12:52:10 +00:00
|
|
|
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;
|
|
|
|
} ();
|
|
|
|
|
2016-09-03 22:54:05 +00:00
|
|
|
// 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;
|
2016-10-11 12:52:10 +00:00
|
|
|
|
2016-09-03 22:54:05 +00:00
|
|
|
return {start, end};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-05 02:23:24 +00:00
|
|
|
pair<DocIterator, DocIterator> TexRow::getDocIteratorsFromRow(
|
2016-09-03 22:54:05 +00:00
|
|
|
int const row,
|
|
|
|
Buffer const & buf) const
|
|
|
|
{
|
|
|
|
TextEntry start, end;
|
|
|
|
tie(start,end) = getEntriesFromRow(row);
|
2016-09-05 02:23:24 +00:00
|
|
|
return getDocIteratorsFromEntries(start, end, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//static
|
|
|
|
pair<DocIterator, DocIterator> TexRow::getDocIteratorsFromEntries(
|
|
|
|
TextEntry start,
|
|
|
|
TextEntry end,
|
|
|
|
Buffer const & buf)
|
|
|
|
{
|
2016-10-10 16:07:01 +00:00
|
|
|
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));
|
|
|
|
};
|
2016-09-03 22:54:05 +00:00
|
|
|
// Finding start
|
|
|
|
DocIterator dit_start = buf.getParFromID(start.id);
|
|
|
|
if (dit_start)
|
2016-10-10 16:07:01 +00:00
|
|
|
set_pos(dit_start, start.pos);
|
2016-09-03 22:54:05 +00:00
|
|
|
// Finding end
|
|
|
|
DocIterator dit_end = buf.getParFromID(end.id);
|
|
|
|
if (dit_end) {
|
2016-10-10 16:07:01 +00:00
|
|
|
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()) {
|
2016-09-03 22:54:05 +00:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-05 02:23:24 +00:00
|
|
|
//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)};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 09:22:20 +00:00
|
|
|
FuncRequest TexRow::goToFuncFromRow(int const row) const
|
2016-09-07 00:36:55 +00:00
|
|
|
{
|
2016-10-11 09:22:20 +00:00
|
|
|
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);
|
2016-09-07 00:36:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 02:23:24 +00:00
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
//static
|
2016-10-11 10:09:38 +00:00
|
|
|
TexRow::RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
|
|
|
RowEntry entry;
|
|
|
|
InsetMath * insetMath = slice.asInsetMath();
|
|
|
|
if (insetMath) {
|
2016-10-11 12:14:48 +00:00
|
|
|
entry.type = math_entry;
|
2015-10-13 23:17:05 +00:00
|
|
|
entry.math.id = insetMath->id();
|
|
|
|
entry.math.cell = slice.idx();
|
|
|
|
} else if (slice.text()) {
|
2016-10-11 12:14:48 +00:00
|
|
|
entry.type = text_entry;
|
2015-10-13 23:17:05 +00:00
|
|
|
entry.text.id = slice.paragraph().id();
|
|
|
|
entry.text.pos = slice.pos();
|
2016-06-20 02:47:40 +00:00
|
|
|
} else
|
|
|
|
LASSERT(false, return row_none);
|
2015-10-13 23:17:05 +00:00
|
|
|
return entry;
|
|
|
|
}
|
2007-08-13 14:04:35 +00:00
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
//static
|
|
|
|
bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2016-10-11 12:14:48 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
//static
|
|
|
|
int TexRow::comparePos(RowEntry entry1, RowEntry entry2)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2016-06-20 02:47:40 +00:00
|
|
|
// assume it is sameParOrInsetMath
|
2016-10-11 12:14:48 +00:00
|
|
|
switch (entry1.type /* equal to entry2.type */) {
|
|
|
|
case TexRow::text_entry:
|
2015-10-13 23:17:05 +00:00
|
|
|
return entry2.text.pos - entry1.text.pos;
|
2016-10-11 12:14:48 +00:00
|
|
|
case TexRow::math_entry:
|
|
|
|
return entry2.math.cell - entry1.math.cell;
|
|
|
|
case TexRow::begin_document:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// An iterator on RowList that goes top-down, left-right
|
|
|
|
//
|
|
|
|
// We assume that the end of RowList does not change, which makes things simpler
|
|
|
|
//
|
|
|
|
// Records a pair of iterators on the RowEntryList (row_it_, row_end_) and a
|
|
|
|
// pair of iterators on the current row (it_, it_end_).
|
|
|
|
//
|
|
|
|
// it_ always points to a valid position unless row_it_ == row_end_.
|
|
|
|
//
|
|
|
|
// We could turn this into a proper bidirectional iterator, but we don't need as
|
|
|
|
// much.
|
|
|
|
//
|
|
|
|
class TexRow::RowListIterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RowListIterator(RowList::const_iterator r,
|
2016-06-20 02:47:40 +00:00
|
|
|
RowList::const_iterator r_end)
|
2015-10-13 23:17:05 +00:00
|
|
|
: 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())
|
|
|
|
{
|
|
|
|
normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RowListIterator() :
|
|
|
|
row_it_(RowList::const_iterator()),
|
|
|
|
row_end_(RowList::const_iterator()),
|
|
|
|
it_(RowEntryList::const_iterator()),
|
|
|
|
it_end_(RowEntryList::const_iterator()) { }
|
|
|
|
|
|
|
|
|
|
|
|
RowEntry const & operator*()
|
|
|
|
{
|
|
|
|
return *it_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RowListIterator & operator++()
|
|
|
|
{
|
|
|
|
++it_;
|
|
|
|
normalize();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool atEnd() const
|
|
|
|
{
|
|
|
|
return row_it_ == row_end_;
|
|
|
|
}
|
2015-10-13 22:51:50 +00:00
|
|
|
|
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
bool operator==(RowListIterator const & a) const
|
|
|
|
{
|
|
|
|
return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(RowListIterator const & a) const { return !operator==(a); }
|
|
|
|
|
|
|
|
|
|
|
|
// Current row.
|
|
|
|
RowList::const_iterator const & row() const
|
|
|
|
{
|
|
|
|
return row_it_;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
// ensures that it_ points to a valid value unless row_it_ == row_end_
|
|
|
|
void normalize()
|
|
|
|
{
|
|
|
|
if (row_it_ == row_end_)
|
|
|
|
return;
|
|
|
|
while (it_ == it_end_) {
|
|
|
|
++row_it_;
|
|
|
|
if (row_it_ != row_end_) {
|
|
|
|
it_ = row_it_->begin();
|
|
|
|
it_end_ = row_it_->end();
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
RowList::const_iterator row_it_;
|
|
|
|
//
|
|
|
|
RowList::const_iterator row_end_;
|
|
|
|
//
|
|
|
|
RowEntryList::const_iterator it_;
|
|
|
|
//
|
|
|
|
RowEntryList::const_iterator it_end_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TexRow::RowListIterator TexRow::begin() const
|
|
|
|
{
|
|
|
|
return RowListIterator(rowlist_.begin(), rowlist_.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TexRow::RowListIterator TexRow::end() const
|
|
|
|
{
|
|
|
|
return RowListIterator(rowlist_.end(), rowlist_.end());
|
2000-08-05 05:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2016-10-11 12:52:10 +00:00
|
|
|
// Do not change anything in this algorithm if unsure.
|
2015-10-13 23:17:05 +00:00
|
|
|
bool beg_found = false;
|
|
|
|
bool end_is_next = true;
|
|
|
|
int end_offset = 1;
|
2015-09-15 04:56:01 +00:00
|
|
|
size_t best_slice = 0;
|
2015-10-13 23:17:05 +00:00
|
|
|
RowEntry best_entry = row_none;
|
2015-09-15 04:56:01 +00:00
|
|
|
size_t const n = dit.depth();
|
2015-10-13 23:17:05 +00:00
|
|
|
// this loop finds a pair (best_beg_row,best_end_row) where best_beg_row is
|
|
|
|
// the first row of the topmost possible CursorSlice, and best_end_row is
|
|
|
|
// the one just before the first row matching the next CursorSlice.
|
|
|
|
RowListIterator const begin = this->begin();//necessary disambiguation
|
|
|
|
RowListIterator const end = this->end();
|
|
|
|
RowListIterator best_beg_entry;
|
|
|
|
//best last entry with same pos as the beg_entry, or first entry with pos
|
|
|
|
//immediately following the beg_entry
|
|
|
|
RowListIterator best_end_entry;
|
|
|
|
RowListIterator it = begin;
|
2008-10-18 16:07:04 +00:00
|
|
|
for (; it != end; ++it) {
|
2015-10-13 23:17:05 +00:00
|
|
|
// Compute the best end row.
|
|
|
|
if (beg_found
|
|
|
|
&& (!sameParOrInsetMath(*it, *best_end_entry)
|
|
|
|
|| comparePos(*it, *best_end_entry) <= 0)
|
|
|
|
&& sameParOrInsetMath(*it, best_entry)) {
|
|
|
|
switch (comparePos(*it, best_entry)) {
|
|
|
|
case 0:
|
|
|
|
// Either it is the last one that matches pos...
|
|
|
|
best_end_entry = it;
|
|
|
|
end_is_next = false;
|
|
|
|
end_offset = 1;
|
|
|
|
break;
|
|
|
|
case -1: {
|
2015-10-13 22:51:50 +00:00
|
|
|
// ...or it is the row preceding the first that matches pos+1
|
2015-10-13 23:17:05 +00:00
|
|
|
if (!end_is_next) {
|
|
|
|
end_is_next = true;
|
|
|
|
if (it.row() != best_end_entry.row())
|
|
|
|
end_offset = 0;
|
|
|
|
best_end_entry = it;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 04:56:01 +00:00
|
|
|
}
|
2015-10-13 23:17:05 +00:00
|
|
|
// Compute the best begin row. It is better than the previous one if it
|
|
|
|
// matches either at a deeper level, or at the same level but not
|
|
|
|
// before.
|
|
|
|
for (size_t i = best_slice; i < n; ++i) {
|
2016-06-19 02:39:38 +00:00
|
|
|
RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
|
2015-10-13 23:17:05 +00:00
|
|
|
if (sameParOrInsetMath(*it, entry_i)) {
|
|
|
|
if (comparePos(*it, entry_i) >= 0
|
|
|
|
&& (i > best_slice
|
|
|
|
|| !beg_found
|
|
|
|
|| !sameParOrInsetMath(*it, *best_beg_entry)
|
|
|
|
|| (comparePos(*it, *best_beg_entry) <= 0
|
|
|
|
&& comparePos(entry_i, *best_beg_entry) != 0)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
beg_found = true;
|
|
|
|
end_is_next = false;
|
|
|
|
end_offset = 1;
|
2015-09-15 04:56:01 +00:00
|
|
|
best_slice = i;
|
2015-10-13 23:17:05 +00:00
|
|
|
best_entry = entry_i;
|
|
|
|
best_beg_entry = best_end_entry = it;
|
2015-09-15 04:56:01 +00:00
|
|
|
}
|
|
|
|
//found CursorSlice
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-08-13 14:04:35 +00:00
|
|
|
}
|
2015-10-13 23:17:05 +00:00
|
|
|
if (!beg_found)
|
2016-06-20 02:47:40 +00:00
|
|
|
return make_pair(-1,-1);
|
2015-10-13 23:17:05 +00:00
|
|
|
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;
|
2016-06-20 02:47:40 +00:00
|
|
|
return make_pair(best_beg_row, best_end_row);
|
2015-10-13 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
|
2015-10-13 19:26:21 +00:00
|
|
|
{
|
|
|
|
DocIterator beg = cur.selectionBegin();
|
2016-06-20 02:47:40 +00:00
|
|
|
pair<int,int> beg_rows = rowFromDocIterator(beg);
|
2015-10-13 19:26:21 +00:00
|
|
|
if (cur.selection()) {
|
|
|
|
DocIterator end = cur.selectionEnd();
|
2016-09-03 22:54:05 +00:00
|
|
|
if (!cur.selIsMultiCell() && !end.top().at_cell_begin())
|
2015-10-13 22:51:50 +00:00
|
|
|
end.top().backwardPos();
|
2016-06-20 02:47:40 +00:00
|
|
|
pair<int,int> end_rows = rowFromDocIterator(end);
|
|
|
|
return make_pair(min(beg_rows.first, end_rows.first),
|
|
|
|
max(beg_rows.second, end_rows.second));
|
2015-10-13 19:26:21 +00:00
|
|
|
} else
|
2016-06-20 02:47:40 +00:00
|
|
|
return make_pair(beg_rows.first, beg_rows.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-04 02:21:19 +00:00
|
|
|
size_t TexRow::rows() const
|
2016-06-20 02:47:40 +00:00
|
|
|
{
|
|
|
|
return rowlist_.size();
|
2015-10-13 19:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-04 02:21:19 +00:00
|
|
|
void TexRow::setRows(size_t r)
|
|
|
|
{
|
|
|
|
rowlist_.resize(r, RowEntryList());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
// debugging functions
|
|
|
|
|
|
|
|
///
|
2016-06-20 02:47:40 +00:00
|
|
|
docstring TexRow::asString(RowEntry entry)
|
2015-10-13 23:17:05 +00:00
|
|
|
{
|
2015-10-20 17:19:31 +00:00
|
|
|
odocstringstream os;
|
2016-10-11 12:14:48 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-10-20 17:19:31 +00:00
|
|
|
return os.str();
|
2002-03-21 17:27:08 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
///prepends the texrow to the source given by tex, for debugging purpose
|
|
|
|
void TexRow::prepend(docstring_list & tex) const
|
|
|
|
{
|
2015-11-20 19:48:56 +00:00
|
|
|
size_type const prefix_length = 25;
|
2015-10-13 23:17:05 +00:00
|
|
|
if (tex.size() < rowlist_.size())
|
|
|
|
tex.resize(rowlist_.size());
|
2016-06-20 02:47:40 +00:00
|
|
|
auto it = rowlist_.cbegin();
|
|
|
|
auto const beg = rowlist_.cbegin();
|
|
|
|
auto const end = rowlist_.cend();
|
2015-10-13 23:17:05 +00:00
|
|
|
for (; it < end; ++it) {
|
|
|
|
docstring entry;
|
2016-06-20 02:47:40 +00:00
|
|
|
for (RowEntry const & e : *it)
|
|
|
|
entry += asString(e);
|
2015-10-13 23:17:05 +00:00
|
|
|
if (entry.length() < prefix_length)
|
2016-06-20 02:47:40 +00:00
|
|
|
entry = entry + docstring(prefix_length - entry.length(), ' ');
|
2015-11-20 19:48:56 +00:00
|
|
|
ptrdiff_t i = it - beg;
|
2015-10-13 23:17:05 +00:00
|
|
|
tex[i] = entry + " " + tex[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|